diff --git a/app/src/main/java/com/sopetit/softie/ui/onboarding/bearnaming/BearNamingFragment.kt b/app/src/main/java/com/sopetit/softie/ui/onboarding/bearnaming/BearNamingFragment.kt index 325f0fe6..3557ef0b 100644 --- a/app/src/main/java/com/sopetit/softie/ui/onboarding/bearnaming/BearNamingFragment.kt +++ b/app/src/main/java/com/sopetit/softie/ui/onboarding/bearnaming/BearNamingFragment.kt @@ -1,6 +1,8 @@ package com.sopetit.softie.ui.onboarding.bearnaming import android.os.Bundle +import android.text.Editable +import android.text.TextWatcher import android.view.View import androidx.fragment.app.viewModels import androidx.lifecycle.ViewModelProvider @@ -47,7 +49,25 @@ class BearNamingFragment : } private fun setEditTextFilter() { - binding.etBearNaming.filters = arrayOf(viewModel.filterSpecialCharacter) + binding.etBearNaming.filters = + arrayOf(viewModel.filterLength, viewModel.filterSpecialCharacter) + changeNicknameLengthWarning() + } + + private fun changeNicknameLengthWarning() { + binding.etBearNaming.addTextChangedListener(object : TextWatcher { + override fun afterTextChanged(s: Editable?) { + } + + override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { + if (s != null && s.length >= 10) { + viewModel.warnNicknameLength() + } + } + + override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { + } + }) } private fun setNicknameObserver() { diff --git a/app/src/main/java/com/sopetit/softie/ui/onboarding/bearnaming/BearNamingViewModel.kt b/app/src/main/java/com/sopetit/softie/ui/onboarding/bearnaming/BearNamingViewModel.kt index 74c74c29..c38ac0e2 100644 --- a/app/src/main/java/com/sopetit/softie/ui/onboarding/bearnaming/BearNamingViewModel.kt +++ b/app/src/main/java/com/sopetit/softie/ui/onboarding/bearnaming/BearNamingViewModel.kt @@ -13,32 +13,53 @@ class BearNamingViewModel : ViewModel() { private val _isSpecialCharacterEntered = MutableLiveData() val isSpecialCharacterEntered: LiveData = _isSpecialCharacterEntered + private val _isLengthExceed = MutableLiveData() + val isLengthExceed: LiveData = _isLengthExceed + + private val _isWarning = MutableLiveData(false) + val isWarning: LiveData = _isWarning + val nickname: MutableLiveData = MutableLiveData("") - var filterSpecialCharacter = InputFilter { source, _, _, _, _, _ -> + val filterSpecialCharacter = InputFilter { source, _, _, _, _, _ -> if (source.isNullOrBlank() || NICKNAME_REGEX.matcher(source).matches()) { _isSpecialCharacterEntered.value = false + _isLengthExceed.value = false + checkWarning() source } else { - _isSpecialCharacterEntered.value = true - if (source.length <= MAXIMUM_LENGTH) { + if ((nickname.value?.length ?: 0) <= MAXIMUM_LENGTH) { + _isSpecialCharacterEntered.value = true + _isLengthExceed.value = false + checkWarning() source.filter { it.isLetterOrDigit() } - } else { - source.substring(0, MAXIMUM_LENGTH) - } + } else source } } + val filterLength = InputFilter.LengthFilter(MAXIMUM_LENGTH) + fun checkIsNicknameValid() { val nicknameLength = nickname.value?.length ?: 0 _isNickNameValid.value = nicknameLength in MINIMUM_LENGTH..MAXIMUM_LENGTH } + fun warnNicknameLength() { + _isSpecialCharacterEntered.value = false + _isLengthExceed.value = true + checkWarning() + } + + private fun checkWarning() { + _isWarning.value = + (_isSpecialCharacterEntered.value == true) || (_isLengthExceed.value == true) + } + companion object { private const val MINIMUM_LENGTH = 1 private const val MAXIMUM_LENGTH = 10 private const val NICKNAME_PATTERN = - "^[ㄱ-ㅣ가-힣a-zA-Z0-9\\u318D\\u119E\\u11A2\\u2022\\u2025a\\u00B7\\uFE55]{1,10}\$" - val NICKNAME_REGEX: Pattern = Pattern.compile(NICKNAME_PATTERN) + "^[ㄱ-ㅣ가-힣a-zA-Z\\u318D\\u119E\\u11A2\\u2022\\u2025\\u00B7\\uFE550-9]{1,10}\$" + val NICKNAME_REGEX: Pattern = Pattern.compile(NICKNAME_PATTERN.replace("\\s", "")) } } diff --git a/app/src/main/res/layout/fragment_onboarding_bear_naming.xml b/app/src/main/res/layout/fragment_onboarding_bear_naming.xml index 533adc8e..fc327785 100644 --- a/app/src/main/res/layout/fragment_onboarding_bear_naming.xml +++ b/app/src/main/res/layout/fragment_onboarding_bear_naming.xml @@ -66,9 +66,11 @@ android:background="@drawable/shape_white_fill_gray200_stroke_radius_99_rect" android:gravity="center" android:hint="@string/bear_naming_hint" + android:importantForAutofill="no" + android:inputType="text" + android:maxLines="1" android:paddingTop="12dp" android:paddingBottom="11dp" - android:inputType="text" android:text="@={viewModel.nickname}" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" @@ -81,15 +83,16 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" - android:text="@string/bear_naming_warning" + android:text="@{viewModel.isLengthExceed?@string/bear_naming_length_warning : @string/bear_naming_special_character_warning}" android:textColor="@color/red" - android:visibility="@{viewModel.isSpecialCharacterEntered ? View.VISIBLE: View.INVISIBLE}" + android:visibility="@{viewModel.isWarning? View.VISIBLE: View.INVISIBLE}" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/et_bear_naming" /> 한 번 지은 이름은 평생 가요. 소프티 이 이름이 좋겠어 - *특수문자는 사용할 수 없어요 + *특수문자는 사용할 수 없어요 + *10글자 이내로 작성해 주세요. 데일리 루틴