Skip to content

Commit

Permalink
Improve delete wallet app state reset (#1438)
Browse files Browse the repository at this point in the history
  • Loading branch information
HonzaR committed May 6, 2024
1 parent c3cf711 commit 6c33077
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package co.electriccoin.zcash.ui.common.viewmodel

import android.app.Activity
import android.app.Application
import android.content.Intent
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import cash.z.ecc.android.bip39.Mnemonics
Expand All @@ -25,6 +27,7 @@ import cash.z.ecc.android.sdk.tool.DerivationTool
import cash.z.ecc.sdk.type.fromResources
import co.electriccoin.zcash.global.getInstance
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.compose.BalanceState
import co.electriccoin.zcash.ui.common.extension.throttle
Expand Down Expand Up @@ -430,7 +433,7 @@ class WalletViewModel(application: Application) : AndroidViewModel(application)
}
}

fun deleteWalletFlow(): Flow<Boolean> =
fun deleteWalletFlow(activity: Activity): Flow<Boolean> =
callbackFlow {
Twig.info { "Delete wallet: Requested" }

Expand All @@ -447,11 +450,15 @@ class WalletViewModel(application: Application) : AndroidViewModel(application)
}

clearAppStateFlow().collect { isAppErased ->
Twig.info { "Delete wallet: Erase SDK result: $isAppErased" }
Twig.info { "Delete wallet: Erase App result: $isAppErased" }
if (!isAppErased) {
trySend(false)
} else {
trySend(true)
activity.run {
finish()
startActivity(Intent(this, MainActivity::class.java))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package co.electriccoin.zcash.ui.screen.deletewallet

import android.content.Context
import android.app.Activity
import androidx.activity.compose.BackHandler
import androidx.activity.viewModels
import androidx.compose.material3.SnackbarHostState
Expand All @@ -23,7 +23,7 @@ internal fun MainActivity.WrapDeleteWallet(goBack: () -> Unit) {
val walletRestoringState = walletViewModel.walletRestoringState.collectAsStateWithLifecycle().value

WrapDeleteWallet(
this,
activity = this,
goBack = goBack,
walletRestoringState = walletRestoringState,
walletViewModel = walletViewModel,
Expand All @@ -32,7 +32,7 @@ internal fun MainActivity.WrapDeleteWallet(goBack: () -> Unit) {

@Composable
internal fun WrapDeleteWallet(
context: Context,
activity: Activity,
goBack: () -> Unit,
walletRestoringState: WalletRestoringState,
walletViewModel: WalletViewModel,
Expand All @@ -50,14 +50,14 @@ internal fun WrapDeleteWallet(
onBack = goBack,
onConfirm = {
scope.launch {
walletViewModel.deleteWalletFlow().collect { isWalletDeleted ->
walletViewModel.deleteWalletFlow(activity).collect { isWalletDeleted ->
if (isWalletDeleted) {
Twig.info { "Wallet deleted successfully" }
// The app flows move to the Onboarding screens reactively
} else {
Twig.error { "Wallet deletion failed" }
snackbarHostState.showSnackbar(
message = context.getString(R.string.delete_wallet_failed)
message = activity.getString(R.string.delete_wallet_failed)
)
}
}
Expand Down

0 comments on commit 6c33077

Please sign in to comment.