diff --git a/app/src/main/java/com/sopt/now/test/data/BaseState.kt b/app/src/main/java/com/sopt/now/test/data/BaseState.kt new file mode 100644 index 0000000..9ddd75b --- /dev/null +++ b/app/src/main/java/com/sopt/now/test/data/BaseState.kt @@ -0,0 +1,6 @@ +package com.sopt.now.test.data + +data class BaseState( + val isSuccess: Boolean, + val message: String +) \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/test/presentation/SignUpActivity.kt b/app/src/main/java/com/sopt/now/test/presentation/SignUpActivity.kt index ce74a72..5c978fc 100644 --- a/app/src/main/java/com/sopt/now/test/presentation/SignUpActivity.kt +++ b/app/src/main/java/com/sopt/now/test/presentation/SignUpActivity.kt @@ -3,60 +3,56 @@ package com.sopt.now.test.presentation import android.content.Intent import android.os.Bundle import android.widget.Toast +import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity import com.sopt.now.databinding.ActivitySignUpBinding -import com.sopt.now.test.data.UserData +import com.sopt.now.test.data.dto.request.RequestSignUpDto class SignUpActivity : AppCompatActivity() { - private lateinit var binding: ActivitySignUpBinding + private val binding by lazy { ActivitySignUpBinding.inflate(layoutInflater) } + private val viewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - binding = ActivitySignUpBinding.inflate(layoutInflater) setContentView(binding.root) - setupSignUpButton() + initViews() + initObserver() } - // 회원 가입 - private fun setupSignUpButton() { + private fun initViews() { binding.btnSignUp.setOnClickListener { - with(binding) { - val userId = etSignUpId.text.toString() - val userPw = etSignUpPw.text.toString() - val userName = etSignUpName.text.toString() - val selfDescription = etSignUpDescription.text.toString() - - val userData = UserData(userId, userPw, userName, selfDescription) - checkSignUp(userData) - } + viewModel.signUp(getSignUpRequestDto()) } } - // 회원 가입 가능 여부 체크 - private fun checkSignUp(userData: UserData) { - val isValidId = userData.userId.length in MIN_ID_LENGTH..MAX_ID_LENGTH - val isValidPw = userData.userPw.length in MIN_PW_LENGTH..MAX_PW_LENGTH - val isValidName = userData.userName.isBlank() // 공백으로만 이루어진 경우 판단 - - val message = when { - !isValidId -> "ID는 6~10 글자여야 합니다." - !isValidPw -> "Password는 8~12 글자여야 합니다." - isValidName -> "공백으로만 이루어진 닉네임은 불가합니다." - else -> { - moveToLogin(userData) - "회원가입 성공!" + private fun initObserver() { + viewModel.liveData.observe(this) { response -> + if (response.isSuccess) { + moveToLogin() } + showToast(response.message) } - showToast(message) + } + + private fun getSignUpRequestDto(): RequestSignUpDto { + val id = binding.etSignUpId.text.toString() + val password = binding.etSignUpPw.text.toString() + val nickname = binding.etSignUpName.text.toString() + val phoneNumber = binding.etSignUpPhone.text.toString() + return RequestSignUpDto( + authenticationId = id, + password = password, + nickname = nickname, + phone = phoneNumber + ) } // 로그인 페이지로 이동 - private fun moveToLogin(userData: UserData) { + private fun moveToLogin() { Intent(this, LoginActivity::class.java).apply { - putExtra("userData", userData) - setResult(RESULT_OK, this) + startActivity(this) finish() } } @@ -64,12 +60,4 @@ class SignUpActivity : AppCompatActivity() { private fun showToast(message: String) { Toast.makeText(this, message, Toast.LENGTH_SHORT).show() } - - // 글자 수 제한 - companion object { - private const val MIN_ID_LENGTH = 6 - private const val MAX_ID_LENGTH = 10 - private const val MIN_PW_LENGTH = 8 - private const val MAX_PW_LENGTH = 12 - } } \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/test/presentation/SignUpViewModel.kt b/app/src/main/java/com/sopt/now/test/presentation/SignUpViewModel.kt new file mode 100644 index 0000000..57ed9ec --- /dev/null +++ b/app/src/main/java/com/sopt/now/test/presentation/SignUpViewModel.kt @@ -0,0 +1,59 @@ +package com.sopt.now.test.presentation + +import android.util.Log +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import com.google.gson.Gson +import com.sopt.now.test.data.ServicePool +import com.sopt.now.test.data.BaseState +import com.sopt.now.test.data.dto.request.RequestSignUpDto +import com.sopt.now.test.data.dto.response.ResponseAuthDto +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response + +class SignUpViewModel : ViewModel() { + private val authService by lazy { ServicePool.authService } + val liveData = MutableLiveData() + + fun signUp(request: RequestSignUpDto) { + authService.signUp(request).enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response, + ) { + if (response.isSuccessful) { + val data: ResponseAuthDto? = response.body() + val userId = response.headers()["location"] + liveData.value = BaseState( + isSuccess = true, + message = "회원가입 성공 유저의 ID는 $userId 입니다." + ) + Log.d("SignUp", "data: $data, userId: $userId") + } else { + val error = response.errorBody()?.string() + val gson = Gson() + try { + val errorResponse = gson.fromJson(error, ResponseAuthDto::class.java) + liveData.value = BaseState( + isSuccess = false, + message = "회원가입 실패: ${errorResponse.message}" // 에러 메시지 사용 + ) + } catch (e: Exception) { + liveData.value = BaseState( + isSuccess = false, + message = "회원가입 실패: 에러 메시지 파싱 실패" + ) + } + } + } + + override fun onFailure(call: Call, t: Throwable) { + liveData.value = BaseState( + isSuccess = false, + message = "서버 에러" + ) + } + }) + } +} \ No newline at end of file