diff --git a/app/src/main/java/org/android/go/sopt/presentation/auth/LoginViewModel.kt b/app/src/main/java/org/android/go/sopt/presentation/auth/LoginViewModel.kt index 841b542..7e7ed89 100644 --- a/app/src/main/java/org/android/go/sopt/presentation/auth/LoginViewModel.kt +++ b/app/src/main/java/org/android/go/sopt/presentation/auth/LoginViewModel.kt @@ -5,7 +5,6 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import org.android.go.sopt.data.remote.LoginRequestDTO import org.android.go.sopt.data.remote.LoginResponseDTO -import org.android.go.sopt.databinding.ActivityLoginBinding import org.android.go.sopt.module.AuthServicePool.authService import retrofit2.Call import retrofit2.Callback diff --git a/app/src/main/java/org/android/go/sopt/presentation/auth/SignUpActivity.kt b/app/src/main/java/org/android/go/sopt/presentation/auth/SignUpActivity.kt index a07d3d0..ada65e8 100644 --- a/app/src/main/java/org/android/go/sopt/presentation/auth/SignUpActivity.kt +++ b/app/src/main/java/org/android/go/sopt/presentation/auth/SignUpActivity.kt @@ -6,21 +6,20 @@ import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.inputmethod.InputMethodManager +import androidx.activity.viewModels import androidx.core.widget.doAfterTextChanged import org.android.go.sopt.R -import org.android.go.sopt.data.remote.SignUpRequestDTO -import org.android.go.sopt.data.remote.SignUpResponseDTO import org.android.go.sopt.databinding.ActivitySignUpBinding -import org.android.go.sopt.module.AuthServicePool.authService import org.android.go.sopt.util.KeyboardVisibilityUtils import org.android.go.sopt.util.extension.makeSnackBar -import retrofit2.Call -import retrofit2.Response +import timber.log.Timber class SignUpActivity : AppCompatActivity() { private lateinit var binding: ActivitySignUpBinding private lateinit var keyboardVisibilityUtils: KeyboardVisibilityUtils + private val viewModel by viewModels() + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivitySignUpBinding.inflate(layoutInflater) @@ -45,6 +44,20 @@ class SignUpActivity : AppCompatActivity() { signUpWithServer() } + // 뷰모델 observer 설정 + viewModel.signUpResult.observe(this) {signUpResult -> + binding.root.makeSnackBar(getString(R.string.snackbar_signup_success)) + if (!isFinishing) { + val intent = Intent(binding.root.context, LoginActivity::class.java) + startActivity(intent) + finish() + } + } + viewModel.errorResult.observe(this) { errorResult -> + Timber.d("서버 통신 실패 : $errorResult") + binding.root.makeSnackBar(getString(R.string.snackbar_signup_failure)) + } + // 화면 터치로 키보드 내리기 binding.root.setOnClickListener { hideKeyboard(this) @@ -59,43 +72,20 @@ class SignUpActivity : AppCompatActivity() { }) } + private fun signUpWithServer() { + viewModel.signUp( + binding.etSignUpId.text.toString(), + binding.etSignUpPw.text.toString(), + binding.etSignUpName.text.toString(), + binding.etSignUpSkill.text.toString() + ) + } + private fun hideKeyboard(activity: Activity) { val keyboard = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager keyboard.hideSoftInputFromWindow(activity.window.decorView.applicationWindowToken, 0) } - private fun signUpWithServer() { - authService.signUp( - with(binding) { - SignUpRequestDTO( - etSignUpId.text.toString(), - etSignUpPw.text.toString(), - etSignUpName.text.toString(), - etSignUpSkill.text.toString() - ) - } - ).enqueue(object : retrofit2.Callback { - override fun onResponse( - call: Call, - response: Response - ) { - if (response.isSuccessful) { - binding.root.makeSnackBar(getString(R.string.snackbar_signup_success)) - if (!isFinishing) { - val intent = Intent(binding.root.context, LoginActivity::class.java) - startActivity(intent) - finish() - } - } else { - binding.root.makeSnackBar(getString(R.string.snackbar_signup_failure)) - } - } - override fun onFailure(call: Call, t: Throwable) { - binding.root.makeSnackBar(getString(R.string.snackbar_signup_failure)) - } - }) - } - private fun checkUserSignIn(): Boolean { return binding.etSignUpId.text.length in 6..10 && binding.etSignUpPw.text.length in 8..12 diff --git a/app/src/main/java/org/android/go/sopt/presentation/auth/SignUpViewModel.kt b/app/src/main/java/org/android/go/sopt/presentation/auth/SignUpViewModel.kt new file mode 100644 index 0000000..cb00ca6 --- /dev/null +++ b/app/src/main/java/org/android/go/sopt/presentation/auth/SignUpViewModel.kt @@ -0,0 +1,45 @@ +package org.android.go.sopt.presentation.auth + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import org.android.go.sopt.data.remote.SignUpRequestDTO +import org.android.go.sopt.data.remote.SignUpResponseDTO +import org.android.go.sopt.module.AuthServicePool +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response + +class SignUpViewModel : ViewModel() { + private val _signUpResult: MutableLiveData = MutableLiveData() + val signUpResult: LiveData = _signUpResult + + private val _errorResult: MutableLiveData = MutableLiveData() + val errorResult: LiveData = _errorResult + + fun signUp(id: String, password: String, name: String, skill: String) { + AuthServicePool.authService.signUp( + SignUpRequestDTO( + id, + password, + name, + skill + ) + ).enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + if (response.isSuccessful) { + _signUpResult.value = response.body() + } else { + _errorResult.value = response.message() + } + } + + override fun onFailure(call: Call, t: Throwable) { + _errorResult.value = t.toString() + } + }) + } +} \ No newline at end of file