@@ -3,6 +3,10 @@ package team.aliens.dms.android.feature.resetpassword
33import androidx.lifecycle.viewModelScope
44import dagger.hilt.android.lifecycle.HiltViewModel
55import kotlinx.coroutines.Dispatchers
6+ import kotlinx.coroutines.FlowPreview
7+ import kotlinx.coroutines.flow.debounce
8+ import kotlinx.coroutines.flow.distinctUntilChanged
9+ import kotlinx.coroutines.flow.map
610import kotlinx.coroutines.launch
711import team.aliens.dms.android.core.ui.mvi.BaseMviViewModel
812import team.aliens.dms.android.core.ui.mvi.Intent
@@ -15,6 +19,8 @@ import team.aliens.dms.android.shared.validator.checkIfPasswordValid
1519import java.util.UUID
1620import javax.inject.Inject
1721
22+ const val SEARCH_DEBOUNCE_MILLIS = 1000L
23+
1824@HiltViewModel
1925class ResetPasswordViewModel @Inject constructor(
2026 private val studentRepository : StudentRepository ,
@@ -27,6 +33,10 @@ class ResetPasswordViewModel @Inject constructor(
2733 검사에서 가능이 뜨게 된다면 "이메일 인증번호 보내기 APi"를 사용해서 사용자 이메일에 이메일을 발송합니다.
2834 그리고 이메일 인증번호 확인 Api를 사용하여 인증을 완료하고 Students의 비밀번호 재설정 Api를 사용하여 재설정합니다.*/
2935
36+ init {
37+ debounceName()
38+ }
39+
3040 override fun processIntent (intent : ResetPasswordIntent ) {
3141 when (intent) {
3242 ResetPasswordIntent .SetPassword -> resetPassword()
@@ -43,6 +53,17 @@ class ResetPasswordViewModel @Inject constructor(
4353 }
4454 }
4555
56+ @OptIn(FlowPreview ::class )
57+ private fun debounceName () {
58+ viewModelScope.launch {
59+ stateFlow.map { it.accountId }.distinctUntilChanged().debounce(SEARCH_DEBOUNCE_MILLIS ).collect {
60+ if (it.isNotBlank()) {
61+ checkIdExists()
62+ }
63+ }
64+ }
65+ }
66+
4667 private fun resetPassword () = viewModelScope.launch(Dispatchers .IO ) {
4768 val capturedState = stateFlow.value
4869 if (capturedState.newPassword != capturedState.newPasswordRepeat) {
@@ -105,7 +126,7 @@ class ResetPasswordViewModel @Inject constructor(
105126 }
106127
107128 private fun updateEmailVerificationCode (value : String ) = run {
108- if (value.length > ResetPasswordViewModel . EMAIL_VERIFICATION_CODE_LENGTH ) {
129+ if (value.length > EMAIL_VERIFICATION_CODE_LENGTH ) {
109130 return @run false
110131 }
111132 reduce(newState = stateFlow.value.copy(emailVerificationCode = value))
0 commit comments