Skip to content

Commit

Permalink
[CHORE] 부연 설명이 필요한 부분 주석 추가
Browse files Browse the repository at this point in the history
ref: #10
  • Loading branch information
leeeha committed Jun 2, 2023
1 parent 592554e commit 5a84a35
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/org/android/go/sopt/GoSoptApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -53,8 +55,6 @@ class GoSoptApplication : Application() {
}
}

// 클래스가 처음 로딩될 때 초기화 되는 companion object
// 프로그램과 생명주기를 같이 하며, 한번만 생성된다. (싱글톤)
companion object {
lateinit var prefs: SharedPreferences
var mockJsonString: String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,18 @@ class SignUpActivity : BindingActivity<ActivitySignUpBinding>(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
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,23 @@ import retrofit2.HttpException
import timber.log.Timber

class SignUpViewModel : ViewModel() {
private val preferenceManager = PreferenceManager()

private val _signUpState = MutableLiveData<RemoteUiState>()
val signUpState: LiveData<RemoteUiState>
get() = _signUpState

// 양방향 데이터 바인딩을 위해 LiveData를 public으로 설정
// EditText에 입력된 값을 LiveData로 가져오는 양방향 데이터 바인딩을 위해
// LiveData를 public으로 설정했다.
val _id = MutableLiveData("")
private val id: String get() = _id.value?.trim() ?: ""

val _pw = MutableLiveData("")
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
Expand All @@ -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)
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
Expand Down

0 comments on commit 5a84a35

Please sign in to comment.