Improve delete wallet app state reset (#1438)

This commit is contained in:
Honza Rychnovský 2024-05-06 19:29:50 +02:00 committed by GitHub
parent c3cf711ee6
commit 6c3307748a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 7 deletions

View File

@ -1,6 +1,8 @@
package co.electriccoin.zcash.ui.common.viewmodel package co.electriccoin.zcash.ui.common.viewmodel
import android.app.Activity
import android.app.Application import android.app.Application
import android.content.Intent
import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import cash.z.ecc.android.bip39.Mnemonics import cash.z.ecc.android.bip39.Mnemonics
@ -25,6 +27,7 @@ import cash.z.ecc.android.sdk.tool.DerivationTool
import cash.z.ecc.sdk.type.fromResources import cash.z.ecc.sdk.type.fromResources
import co.electriccoin.zcash.global.getInstance import co.electriccoin.zcash.global.getInstance
import co.electriccoin.zcash.spackle.Twig import co.electriccoin.zcash.spackle.Twig
import co.electriccoin.zcash.ui.MainActivity
import co.electriccoin.zcash.ui.common.ANDROID_STATE_FLOW_TIMEOUT import co.electriccoin.zcash.ui.common.ANDROID_STATE_FLOW_TIMEOUT
import co.electriccoin.zcash.ui.common.compose.BalanceState import co.electriccoin.zcash.ui.common.compose.BalanceState
import co.electriccoin.zcash.ui.common.extension.throttle import co.electriccoin.zcash.ui.common.extension.throttle
@ -430,7 +433,7 @@ class WalletViewModel(application: Application) : AndroidViewModel(application)
} }
} }
fun deleteWalletFlow(): Flow<Boolean> = fun deleteWalletFlow(activity: Activity): Flow<Boolean> =
callbackFlow { callbackFlow {
Twig.info { "Delete wallet: Requested" } Twig.info { "Delete wallet: Requested" }
@ -447,11 +450,15 @@ class WalletViewModel(application: Application) : AndroidViewModel(application)
} }
clearAppStateFlow().collect { isAppErased -> clearAppStateFlow().collect { isAppErased ->
Twig.info { "Delete wallet: Erase SDK result: $isAppErased" } Twig.info { "Delete wallet: Erase App result: $isAppErased" }
if (!isAppErased) { if (!isAppErased) {
trySend(false) trySend(false)
} else { } else {
trySend(true) trySend(true)
activity.run {
finish()
startActivity(Intent(this, MainActivity::class.java))
}
} }
} }
} }

View File

@ -1,6 +1,6 @@
package co.electriccoin.zcash.ui.screen.deletewallet package co.electriccoin.zcash.ui.screen.deletewallet
import android.content.Context import android.app.Activity
import androidx.activity.compose.BackHandler import androidx.activity.compose.BackHandler
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.compose.material3.SnackbarHostState import androidx.compose.material3.SnackbarHostState
@ -23,7 +23,7 @@ internal fun MainActivity.WrapDeleteWallet(goBack: () -> Unit) {
val walletRestoringState = walletViewModel.walletRestoringState.collectAsStateWithLifecycle().value val walletRestoringState = walletViewModel.walletRestoringState.collectAsStateWithLifecycle().value
WrapDeleteWallet( WrapDeleteWallet(
this, activity = this,
goBack = goBack, goBack = goBack,
walletRestoringState = walletRestoringState, walletRestoringState = walletRestoringState,
walletViewModel = walletViewModel, walletViewModel = walletViewModel,
@ -32,7 +32,7 @@ internal fun MainActivity.WrapDeleteWallet(goBack: () -> Unit) {
@Composable @Composable
internal fun WrapDeleteWallet( internal fun WrapDeleteWallet(
context: Context, activity: Activity,
goBack: () -> Unit, goBack: () -> Unit,
walletRestoringState: WalletRestoringState, walletRestoringState: WalletRestoringState,
walletViewModel: WalletViewModel, walletViewModel: WalletViewModel,
@ -50,14 +50,14 @@ internal fun WrapDeleteWallet(
onBack = goBack, onBack = goBack,
onConfirm = { onConfirm = {
scope.launch { scope.launch {
walletViewModel.deleteWalletFlow().collect { isWalletDeleted -> walletViewModel.deleteWalletFlow(activity).collect { isWalletDeleted ->
if (isWalletDeleted) { if (isWalletDeleted) {
Twig.info { "Wallet deleted successfully" } Twig.info { "Wallet deleted successfully" }
// The app flows move to the Onboarding screens reactively // The app flows move to the Onboarding screens reactively
} else { } else {
Twig.error { "Wallet deletion failed" } Twig.error { "Wallet deletion failed" }
snackbarHostState.showSnackbar( snackbarHostState.showSnackbar(
message = context.getString(R.string.delete_wallet_failed) message = activity.getString(R.string.delete_wallet_failed)
) )
} }
} }