@@ -9,17 +9,20 @@ import com.getcode.App
99import com.getcode.R
1010import com.getcode.crypt.MnemonicPhrase
1111import com.getcode.ed25519.Ed25519
12- import com.getcode.manager.*
13- import com.getcode.navigation.screens.AccessKeyScreen
12+ import com.getcode.manager.SessionManager
13+ import com.getcode.manager.TopBarManager
1414import com.getcode.navigation.core.CodeNavigator
15+ import com.getcode.navigation.screens.AccessKeyScreen
1516import com.getcode.navigation.screens.HomeScreen
1617import com.getcode.navigation.screens.PhoneNumberScreen
17- import com.getcode.network.repository.*
18+ import com.getcode.network.repository.IdentityRepository
19+ import com.getcode.network.repository.PhoneRepository
20+ import com.getcode.network.repository.encodeBase64
1821import com.getcode.util.OtpSmsBroadcastReceiver
1922import com.getcode.util.PhoneUtils
2023import com.getcode.util.resources.ResourceHelper
2124import com.getcode.utils.ErrorUtils
22- import com.getcode.view.*
25+ import com.getcode.view.BaseViewModel
2326import dagger.hilt.android.lifecycle.HiltViewModel
2427import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
2528import io.reactivex.rxjava3.annotations.NonNull
@@ -31,7 +34,7 @@ import kotlinx.coroutines.Dispatchers
3134import kotlinx.coroutines.flow.MutableStateFlow
3235import kotlinx.coroutines.flow.update
3336import kotlinx.coroutines.launch
34- import java.util.*
37+ import java.util.Timer
3538import java.util.concurrent.TimeUnit
3639import javax.inject.Inject
3740import kotlin.concurrent.fixedRateTimer
@@ -44,15 +47,15 @@ data class PhoneConfirmUiModel(
4447 val phoneNumber : String? = null ,
4548 val phoneNumberFormatted : String? = null ,
4649 val isLoading : Boolean = false ,
50+ val isResendingCode : Boolean = false ,
4751 val isSuccess : Boolean = false ,
4852 val isAutoSubmitted : Boolean = false ,
4953 val entropyB64 : String? = null ,
5054 val isPhoneLinking : Boolean = false ,
5155 val isNewAccount : Boolean = false ,
5256 val isResendTimerRunning : Boolean = true ,
53- val resetTimerTime : Int = 60 ,
57+ val resetTimerTime : Int = PhoneConfirmViewModel . RESEND_TIMER_MAX ,
5458 val attempts : Int = 0 ,
55- val isCodeResent : Boolean = false
5659)
5760
5861@HiltViewModel
@@ -62,6 +65,11 @@ class PhoneConfirmViewModel @Inject constructor(
6265 private val phoneUtils : PhoneUtils ,
6366 private val resources : ResourceHelper ,
6467) : BaseViewModel(resources) {
68+
69+ companion object {
70+ internal const val RESEND_TIMER_MAX = 60
71+ }
72+
6573 val uiFlow = MutableStateFlow (PhoneConfirmUiModel ())
6674 private var navigator: CodeNavigator ? = null
6775 private var timer: Timer ? = null
@@ -156,9 +164,8 @@ class PhoneConfirmViewModel @Inject constructor(
156164 private fun startTimer () {
157165 uiFlow.update {
158166 it.copy(
159- isCodeResent = false ,
160167 isResendTimerRunning = true ,
161- resetTimerTime = 60
168+ resetTimerTime = RESEND_TIMER_MAX
162169 )
163170 }
164171 timer?.cancel()
@@ -175,25 +182,23 @@ class PhoneConfirmViewModel @Inject constructor(
175182 }
176183
177184 fun resendCode () {
178- startTimer()
179-
180185 val phoneNumber = uiFlow.value.phoneNumber ? : return
181186
182187 CoroutineScope (Dispatchers .IO ).launch {
183188 phoneRepository.sendVerificationCode(phoneNumber)
184189 .firstElement()
190+ .doOnSubscribe { setIsResending(true ) }
191+ .doOnTerminate { setIsResending(false ) }
192+ .doOnComplete { setIsResending(false ) }
185193 .observeOn(AndroidSchedulers .mainThread())
186- .doOnSubscribe { setIsLoading(true ) }
187- .doOnTerminate { setIsLoading(false ) }
188- .doOnComplete { setIsLoading(false ) }
189194 .map { res ->
190195 when (res) {
191196 PhoneVerificationService .SendVerificationCodeResponse .Result .OK -> null
192197 else -> getGenericError()
193198 }?.let { message -> TopBarManager .showMessage(message) }
194199 res == PhoneVerificationService .SendVerificationCodeResponse .Result .OK
195200 }
196- .subscribe({}, ErrorUtils ::handleError)
201+ .subscribe({ startTimer() }, ErrorUtils ::handleError)
197202 }
198203 }
199204
@@ -259,7 +264,6 @@ class PhoneConfirmViewModel @Inject constructor(
259264 val isPhoneLinking = uiFlow.value.isPhoneLinking
260265 val isNewAccount = uiFlow.value.isNewAccount
261266 val attempts = uiFlow.value.attempts
262- val isSeedInput = entropyB64 != null
263267
264268 if (entropyB64 == null && ! isNewAccount) return
265269
@@ -358,6 +362,12 @@ class PhoneConfirmViewModel @Inject constructor(
358362 }
359363 }
360364
365+ private fun setIsResending (resending : Boolean ) {
366+ uiFlow.update {
367+ it.copy(isResendingCode = resending)
368+ }
369+ }
370+
361371 private fun getInvalidCodeError () = TopBarManager .TopBarMessage (
362372 resources.getString(R .string.error_title_invalidVerificationCode),
363373 resources.getString(R .string.error_description_invalidVerificationCode)
0 commit comments