Skip to content

Commit

Permalink
#11 / [FEAT] Livadata로 SignUp 구현
Browse files Browse the repository at this point in the history
Livadata로 SignUp 구현
  • Loading branch information
Marchbreeze committed May 21, 2023
1 parent b0c5d9a commit 1cc8172
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SignUpViewModel>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySignUpBinding.inflate(layoutInflater)
Expand All @@ -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)
Expand All @@ -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<SignUpResponseDTO> {
override fun onResponse(
call: Call<SignUpResponseDTO>,
response: Response<SignUpResponseDTO>
) {
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<SignUpResponseDTO>, 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<SignUpResponseDTO> = MutableLiveData()
val signUpResult: LiveData<SignUpResponseDTO> = _signUpResult

private val _errorResult: MutableLiveData<String> = MutableLiveData()
val errorResult: LiveData<String> = _errorResult

fun signUp(id: String, password: String, name: String, skill: String) {
AuthServicePool.authService.signUp(
SignUpRequestDTO(
id,
password,
name,
skill
)
).enqueue(object : Callback<SignUpResponseDTO> {
override fun onResponse(
call: Call<SignUpResponseDTO>,
response: Response<SignUpResponseDTO>
) {
if (response.isSuccessful) {
_signUpResult.value = response.body()
} else {
_errorResult.value = response.message()
}
}

override fun onFailure(call: Call<SignUpResponseDTO>, t: Throwable) {
_errorResult.value = t.toString()
}
})
}
}

0 comments on commit 1cc8172

Please sign in to comment.