Skip to content

Commit

Permalink
[#30] 로그인 기능 추가
Browse files Browse the repository at this point in the history
- 프로필 필드 유효성 검사를 뷰모델에서 실행
- 모두 유효한 경우에만 버튼 활성화
- 필드 V 아이콘 제거
- RoundButton 비활성화 시 텍스트 색상 화이트로 수정
  • Loading branch information
ethan-223 committed Sep 4, 2022
1 parent e7a17a1 commit 2c939ad
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.moyerun.moyeorun_android.common.extension

import android.text.TextUtils
import android.view.View
import android.widget.RadioButton
import android.widget.TextView
import androidx.annotation.DrawableRes

Expand All @@ -27,6 +28,9 @@ fun TextView.setTextIfNew(text: CharSequence?) {
}
}

fun TextView.setDrawableEnd(@DrawableRes resId: Int?) {
setCompoundDrawablesWithIntrinsicBounds(0, 0, resId ?: 0, 0)
fun RadioButton.setCheckIfNew(check: Boolean) {
val oldValue = isChecked
if (oldValue != check) {
isChecked = check
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ class ProfileEditActivity : AppCompatActivity() {
.map { it.name }
.distinctUntilChanged()
.collect {
binding.edittextProfileName.setTextAndCheckIcon(it)
binding.edittextProfileName.setTextIfNew(it)
}
}
launch {
viewModel.profileUiModel
.map { it.nickname }
.distinctUntilChanged()
.collect {
binding.edittextProfileNickname.setTextAndCheckIcon(it)
binding.edittextProfileNickname.setTextIfNew(it)
}
}
launch {
Expand All @@ -126,6 +126,12 @@ class ProfileEditActivity : AppCompatActivity() {
}
}
}
launch {
viewModel.isButtonEnabled
.collect { buttonEnabled ->
binding.buttonProfileConfirm.isEnabled = buttonEnabled
}
}
}

observeEvent(viewModel.profileEvent) {
Expand All @@ -150,29 +156,6 @@ class ProfileEditActivity : AppCompatActivity() {
}
}

private fun isValidText(text: String): Boolean {
// Todo: 유효성 검사 조건 추가 (ex. 정규 표현식, 글자 제한)
return text.isNotEmpty()
}

private fun EditText.setTextAndCheckIcon(text: String) {
val isValid = isValidText(text)
val resId = if (isValid) {
R.drawable.ic_check
} else {
null
}
setDrawableEnd(resId)
setTextIfNew(text)
}

private fun RadioButton.setCheckIfNew(check: Boolean) {
val oldValue = isChecked
if (oldValue != check) {
isChecked = check
}
}

companion object {
private const val EXTRA_PROFILE_UI_MODEL = "profileUiModel"
private const val EXTRA_SIGN_UP_META_DATA = "signUpMetaData"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class ProfileEditViewModel @Inject constructor(
val profileErrorEvent: EventLiveData<ProfileError>
get() = _profileErrorEvent

private val _isButtonEnabled = MutableStateFlow(false)
val isButtonEnabled: StateFlow<Boolean>
get() = _isButtonEnabled

private var signUpMetaData: SignUpMetaData? = null
private var oldProfileUiModel: ProfileUiModel? = null

Expand All @@ -55,16 +59,18 @@ class ProfileEditViewModel @Inject constructor(
}
}

fun onNameChanged(name: String) {
fun onNameChanged(name: String) = updateWithValidate {
_profileUiModel.update {
it.copy(name = name)
}
_profileUiModel.value
}

fun onNicknameChanged(nickname: String) {
fun onNicknameChanged(nickname: String) = updateWithValidate {
_profileUiModel.update {
it.copy(nickname = nickname)
}
_profileUiModel.value
}

fun onImageUrlChanged(imageUri: Uri) {
Expand All @@ -73,10 +79,11 @@ class ProfileEditViewModel @Inject constructor(
}
}

fun onGenderChanged(gender: Gender) {
fun onGenderChanged(gender: Gender) = updateWithValidate {
_profileUiModel.update {
it.copy(gender = gender)
}
_profileUiModel.value
}

fun signUp() {
Expand All @@ -92,4 +99,25 @@ class ProfileEditViewModel @Inject constructor(
_profileEvent.event = ProfileEvent.SUCCESS_SIGN_UP
}
}

private fun updateWithValidate(updateAction: () -> ProfileUiModel) {
val updatedProfileUiModel = updateAction.invoke()
_isButtonEnabled.update {
updatedProfileUiModel.validate()
}
}

private fun ProfileUiModel.validate(): Boolean {
return name.validateName()
&& nickname.validateNickname()
&& gender != Gender.NONE
}

private fun String.validateName(): Boolean {
return isNotEmpty()
}

private fun String.validateNickname(): Boolean {
return isNotEmpty()
}
}
13 changes: 0 additions & 13 deletions app/src/main/res/drawable/ic_check.xml

This file was deleted.

6 changes: 0 additions & 6 deletions app/src/main/res/drawable/selector_round_button_text.xml

This file was deleted.

2 changes: 1 addition & 1 deletion app/src/main/res/values/styles_button.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
<item name="android:background">@drawable/selector_round_button</item>
<item name="android:textSize">21dp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">@drawable/selector_round_button_text</item>
<item name="android:textColor">@color/main_white</item>
</style>
</resources>

0 comments on commit 2c939ad

Please sign in to comment.