Skip to content

Commit

Permalink
Fix the update in the background thread issue
Browse files Browse the repository at this point in the history
  • Loading branch information
igordanilcenko committed Feb 28, 2024
1 parent c22caf8 commit b1a0a24
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import com.daasuu.ei.Ease
import com.daasuu.ei.EasingInterpolator
Expand All @@ -49,7 +50,7 @@ import com.tari.android.wallet.application.WalletState
import com.tari.android.wallet.databinding.FragmentLocalAuthBinding
import com.tari.android.wallet.event.EventBus
import com.tari.android.wallet.extension.addTo
import com.tari.android.wallet.extension.observe
import com.tari.android.wallet.extension.launchAndRepeatOnLifecycle
import com.tari.android.wallet.infrastructure.security.biometric.BiometricAuthenticationException
import com.tari.android.wallet.ui.common.CommonFragment
import com.tari.android.wallet.ui.extension.doOnGlobalLayout
Expand Down Expand Up @@ -78,11 +79,15 @@ class LocalAuthFragment : CommonFragment<FragmentLocalAuthBinding, LocalAuthView
}

private fun observeUi() = with(viewModel) {
observe(secureState) {
ui.continueBtn.setVisible(it.pinCodeSecured)
ui.authTypeBiometrics.setVisible(it.biometricsAvailable)
ui.secureWithPasscode.setVisible(!it.pinCodeSecured)
ui.secureWithBiometrics.setVisible(it.biometricsAvailable && !it.biometricsSecured)
viewLifecycleOwner.launchAndRepeatOnLifecycle(Lifecycle.State.STARTED) {
launch {
secureState.collect { state ->
ui.continueBtn.setVisible(state.pinCodeSecured)
ui.authTypeBiometrics.setVisible(state.biometricsAvailable)
ui.secureWithPasscode.setVisible(!state.pinCodeSecured)
ui.secureWithBiometrics.setVisible(state.biometricsAvailable && !state.biometricsSecured)
}
}
}
}

Expand Down Expand Up @@ -122,10 +127,11 @@ class LocalAuthFragment : CommonFragment<FragmentLocalAuthBinding, LocalAuthView
lifecycleScope.launch {
try {
val subtitle = string(onboarding_auth_biometric_prompt)
if (viewModel.authService.authenticate(this@LocalAuthFragment, string(onboarding_auth_title), subtitle))
if (viewModel.authService.authenticate(this@LocalAuthFragment, string(onboarding_auth_title), subtitle)) {
viewModel.securedWithBiometrics()
}
} catch (exception: BiometricAuthenticationException) {
viewModel.logger.i( exception.message + "Biometric authentication failed")
viewModel.logger.i(exception.message + "Biometric authentication failed")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.tari.android.wallet.ui.fragment.onboarding.localAuth

import androidx.lifecycle.MutableLiveData
import com.tari.android.wallet.extension.addTo
import com.tari.android.wallet.infrastructure.backup.BackupManager
import com.tari.android.wallet.infrastructure.security.biometric.BiometricAuthenticationService
import com.tari.android.wallet.ui.common.CommonViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import javax.inject.Inject

class LocalAuthViewModel : CommonViewModel() {
Expand All @@ -15,8 +17,8 @@ class LocalAuthViewModel : CommonViewModel() {
@Inject
lateinit var backupManager: BackupManager

val secureState = MutableLiveData(SecureState(true, false, false))

private val _secureState = MutableStateFlow(SecureState())
val secureState = _secureState.asStateFlow()

init {
component.inject(this)
Expand All @@ -28,16 +30,18 @@ class LocalAuthViewModel : CommonViewModel() {
}.addTo(compositeDisposable)
}

fun updateState() {
secureState.value = SecureState(
authService.isBiometricAuthAvailable,
securityPrefRepository.pinCode != null,
securityPrefRepository.biometricsAuth == true
)
private fun updateState() {
_secureState.update {
SecureState(
biometricsAvailable = authService.isBiometricAuthAvailable,
pinCodeSecured = securityPrefRepository.pinCode != null,
biometricsSecured = securityPrefRepository.biometricsAuth == true,
)
}
}

fun securedWithBiometrics() {
securityPrefRepository.biometricsAuth = true
secureState.value = secureState.value!!.copy(biometricsSecured = true)
_secureState.update { it.copy(biometricsSecured = true) }
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package com.tari.android.wallet.ui.fragment.onboarding.localAuth

data class SecureState(val biometricsAvailable: Boolean, val pinCodeSecured: Boolean, val biometricsSecured: Boolean)
data class SecureState(
val biometricsAvailable: Boolean = true,
val pinCodeSecured: Boolean = false,
val biometricsSecured: Boolean = false,
)

0 comments on commit b1a0a24

Please sign in to comment.