From 5a84a355e2dfd2de7e26fc84c83800173db107b3 Mon Sep 17 00:00:00 2001 From: leeeha Date: Fri, 2 Jun 2023 19:24:47 +0900 Subject: [PATCH] =?UTF-8?q?[CHORE]=20=EB=B6=80=EC=97=B0=20=EC=84=A4?= =?UTF-8?q?=EB=AA=85=EC=9D=B4=20=ED=95=84=EC=9A=94=ED=95=9C=20=EB=B6=80?= =?UTF-8?q?=EB=B6=84=20=EC=A3=BC=EC=84=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ref: #10 --- .../org/android/go/sopt/GoSoptApplication.kt | 4 +-- .../presentation/signup/SignUpActivity.kt | 12 +++---- .../presentation/signup/SignUpViewModel.kt | 32 ++++++++++++------- .../android/go/sopt/util/PreferenceManager.kt | 4 +-- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/org/android/go/sopt/GoSoptApplication.kt b/app/src/main/java/org/android/go/sopt/GoSoptApplication.kt index c1894bd..a47b4b5 100644 --- a/app/src/main/java/org/android/go/sopt/GoSoptApplication.kt +++ b/app/src/main/java/org/android/go/sopt/GoSoptApplication.kt @@ -21,6 +21,8 @@ class GoSoptApplication : Application() { }.getOrNull() } + // assets은 context 참조가 필요하기 때문에 + // 뷰모델이 아니라 application 부분에서 함수를 정의했다. private fun loadAsset(fileName: String): String { return assets.open(fileName).use { inputStream -> val size = inputStream.available() @@ -53,8 +55,6 @@ class GoSoptApplication : Application() { } } - // 클래스가 처음 로딩될 때 초기화 되는 companion object - // 프로그램과 생명주기를 같이 하며, 한번만 생성된다. (싱글톤) companion object { lateinit var prefs: SharedPreferences var mockJsonString: String? = null diff --git a/app/src/main/java/org/android/go/sopt/presentation/signup/SignUpActivity.kt b/app/src/main/java/org/android/go/sopt/presentation/signup/SignUpActivity.kt index f946e81..7364340 100644 --- a/app/src/main/java/org/android/go/sopt/presentation/signup/SignUpActivity.kt +++ b/app/src/main/java/org/android/go/sopt/presentation/signup/SignUpActivity.kt @@ -84,18 +84,18 @@ class SignUpActivity : BindingActivity(R.layout.activity_ } binding.etName.addTextChangedListener { - if (viewModel.name.isEmpty()) { - binding.tvNameEmptyError.visibility = View.VISIBLE - } else { + if (viewModel.isNotBlankName()) { binding.tvNameEmptyError.visibility = View.INVISIBLE + } else { + binding.tvNameEmptyError.visibility = View.VISIBLE } } binding.etHobby.addTextChangedListener { - if (viewModel.hobby.isEmpty()) { - binding.tvHobbyEmptyError.visibility = View.VISIBLE - } else { + if (viewModel.isNotBlankHobby()) { binding.tvHobbyEmptyError.visibility = View.INVISIBLE + } else { + binding.tvHobbyEmptyError.visibility = View.VISIBLE } } } diff --git a/app/src/main/java/org/android/go/sopt/presentation/signup/SignUpViewModel.kt b/app/src/main/java/org/android/go/sopt/presentation/signup/SignUpViewModel.kt index 31536f6..c8da7c1 100644 --- a/app/src/main/java/org/android/go/sopt/presentation/signup/SignUpViewModel.kt +++ b/app/src/main/java/org/android/go/sopt/presentation/signup/SignUpViewModel.kt @@ -20,13 +20,12 @@ import retrofit2.HttpException import timber.log.Timber class SignUpViewModel : ViewModel() { - private val preferenceManager = PreferenceManager() - private val _signUpState = MutableLiveData() val signUpState: LiveData get() = _signUpState - // 양방향 데이터 바인딩을 위해 LiveData를 public으로 설정 + // EditText에 입력된 값을 LiveData로 가져오는 양방향 데이터 바인딩을 위해 + // LiveData를 public으로 설정했다. val _id = MutableLiveData("") private val id: String get() = _id.value?.trim() ?: "" @@ -34,14 +33,10 @@ class SignUpViewModel : ViewModel() { private val pw: String get() = _pw.value?.trim() ?: "" val _name = MutableLiveData("") - val name: String get() = _name.value?.trim() ?: "" + private val name: String get() = _name.value?.trim() ?: "" val _hobby = MutableLiveData("") - val hobby: String get() = _hobby.value?.trim() ?: "" - - private fun isValidInput(): Boolean { - return isValidId() && isValidPw() && name.isNotBlank() && hobby.isNotBlank() - } + private val hobby: String get() = _hobby.value?.trim() ?: "" fun isValidId(): Boolean { return id.isNotBlank() && id.length in MIN_ID_LENGTH..MAX_ID_LENGTH @@ -51,6 +46,18 @@ class SignUpViewModel : ViewModel() { return pw.isNotBlank() && pw.length in MIN_PW_LENGTH..MAX_PW_LENGTH } + fun isNotBlankName(): Boolean { + return name.isNotBlank() + } + + fun isNotBlankHobby(): Boolean { + return hobby.isNotBlank() + } + + private fun isValidInput(): Boolean { + return isValidId() && isValidPw() && isNotBlankName() && isNotBlankHobby() + } + fun signUp() { if (!isValidInput()) { _signUpState.value = Failure(CODE_INVALID_INPUT) @@ -74,18 +81,21 @@ class SignUpViewModel : ViewModel() { .onFailure { t -> if (t is HttpException) { when (t.code()) { - CODE_INCORRECT_INPUT -> _signUpState.value = - Failure(CODE_INCORRECT_INPUT) + CODE_INCORRECT_INPUT -> _signUpState.value = Failure(CODE_INCORRECT_INPUT) CODE_DUPLICATED_ID -> _signUpState.value = Failure(CODE_DUPLICATED_ID) else -> _signUpState.value = Error } Timber.e("POST SIGNUP FAIL ${t.code()} : ${t.message()}") + }else { + _signUpState.value = Error + Timber.e("POST SIGNUP FAIL : ${t.message}") } } } } private fun saveSignedUpUserToPrefs() { + val preferenceManager = PreferenceManager() preferenceManager.signedUpUser = User( id = id, pw = pw, diff --git a/app/src/main/java/org/android/go/sopt/util/PreferenceManager.kt b/app/src/main/java/org/android/go/sopt/util/PreferenceManager.kt index 249f85f..5fb2550 100644 --- a/app/src/main/java/org/android/go/sopt/util/PreferenceManager.kt +++ b/app/src/main/java/org/android/go/sopt/util/PreferenceManager.kt @@ -14,8 +14,8 @@ class PreferenceManager { var signedUpUser: User? set(value) = prefs.edit { - val user = Gson().toJson(value) - putString(KEY_SIGNED_UP_USER, user) + val json = Gson().toJson(value) + putString(KEY_SIGNED_UP_USER, json) } get() { val json = prefs.getString(KEY_SIGNED_UP_USER, "")