Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#92 [hotfix] 닉네임 10글자 제한 #114

Merged
merged 6 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,53 @@ class BearNamingViewModel : ViewModel() {
private val _isSpecialCharacterEntered = MutableLiveData<Boolean>()
val isSpecialCharacterEntered: LiveData<Boolean> = _isSpecialCharacterEntered

private val _isLengthExceed = MutableLiveData<Boolean>()
val isLengthExceed: LiveData<Boolean> = _isLengthExceed

private val _isWarning = MutableLiveData<Boolean>(false)
val isWarning: LiveData<Boolean> = _isWarning

val nickname: MutableLiveData<String> = 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", ""))
}
}
10 changes: 6 additions & 4 deletions app/src/main/res/layout/fragment_onboarding_bear_naming.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_bear_selection"
style="?android:attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="57dp"
android:layout_marginHorizontal="20dp"
Expand All @@ -98,7 +101,6 @@
android:clickable="@{viewModel.isNickNameValid ? true : false}"
android:enabled="@{viewModel.isNickNameValid ? true : false}"
android:text="@string/bear_naming_button"
style="?android:attr/borderlessButtonStyle"
android:textAppearance="@style/body1"
android:textColor="@color/gray000"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@
<string name="bear_naming_subtitle">한 번 지은 이름은 평생 가요.</string>
<string name="bear_naming_hint">소프티</string>
<string name="bear_naming_button">이 이름이 좋겠어</string>
<string name="bear_naming_warning">*특수문자는 사용할 수 없어요</string>
<string name="bear_naming_special_character_warning">*특수문자는 사용할 수 없어요</string>
<string name="bear_naming_length_warning">*10글자 이내로 작성해 주세요.</string>

<!-- daily routine edit -->
<string name="daily_routine_edit_name">데일리 루틴</string>
Expand Down