@@ -50,6 +50,7 @@ import kotlinx.coroutines.delay
5050import kotlinx.coroutines.flow.MutableStateFlow
5151import kotlinx.coroutines.flow.StateFlow
5252import kotlinx.coroutines.flow.asStateFlow
53+ import kotlinx.coroutines.flow.combine
5354import kotlinx.coroutines.flow.onEach
5455import kotlinx.coroutines.isActive
5556import kotlinx.coroutines.launch
@@ -129,7 +130,7 @@ class JadeQRViewModel(
129130 private val _urPart : MutableStateFlow <String ?> = MutableStateFlow (null )
130131 override val urPart: StateFlow <String ?> = _urPart .asStateFlow()
131132
132- private var _step = 0
133+ private var _step = MutableStateFlow ( 0 )
133134
134135 private var _scenario = MutableStateFlow (scenarionForOperation())
135136 override val scenario = _scenario
@@ -161,7 +162,7 @@ class JadeQRViewModel(
161162 }
162163 }.launchIn(this )
163164
164- scenario.onEach { scenario ->
165+ combine( scenario, _step ) { scenario, step ->
165166 _navData .value = NavData (
166167 title = getString(if (deviceBrand.isJade) (if (scenario.isPinUnlock) Res .string.id_qr_pin_unlock else Res .string.id_scan_qr_with_jade) else Res .string.id_scan_qr_with_device),
167168 actions = listOfNotNull(NavAction (
@@ -171,7 +172,7 @@ class JadeQRViewModel(
171172 onClick = {
172173 restart()
173174 }
174- ).takeIf { scenario.allowReset })
175+ ).takeIf { scenario.allowReset && step > 0 })
175176 )
176177 }.launchIn(this )
177178
@@ -237,7 +238,7 @@ class JadeQRViewModel(
237238 }
238239
239240 private fun restart () {
240- _step = 0
241+ _step .value = 0
241242 _stepInfo .value = scenario.value.steps.first()
242243
243244 when (operation) {
@@ -267,10 +268,10 @@ class JadeQRViewModel(
267268 }
268269
269270 private fun nextStep () {
270- _step ++
271+ _step .value ++
271272
272- if (_step < scenario.value.steps.size) {
273- _stepInfo .value = scenario.value.steps[_step ]
273+ if (_step .value < scenario.value.steps.size) {
274+ _stepInfo .value = scenario.value.steps[_step .value ]
274275 } else {
275276 postSideEffect(SideEffects .Success (true ))
276277 postSideEffect(SideEffects .NavigateBack ())
@@ -338,6 +339,12 @@ class JadeQRViewModel(
338339 throw Exception (httpResponse.jsonObject[" error" ]?.jsonPrimitive?.content)
339340 }
340341 } ? : run {
342+ // Delay resetScanner to prevent error dialog flooding
343+ viewModelScope.launch {
344+ delay(3000L )
345+ resetScanner()
346+ }
347+
341348 throw Exception (" QR code is not related to PIN Unlock" )
342349 }
343350
0 commit comments