Skip to content

Commit

Permalink
Fix brave/brave-ios#8357: Disable button to create wallet when wallet…
Browse files Browse the repository at this point in the history
… is already being created (brave/brave-ios#8358)

Disable `Continue` button when already creating/restoring a wallet during create wallet / restore wallet to prevent double taps.
  • Loading branch information
StephenHeaps authored Nov 7, 2023
1 parent 66d478e commit 535f29a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
9 changes: 7 additions & 2 deletions Sources/BraveWallet/Crypto/Onboarding/CreateWalletView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private struct CreateWalletView: View {
}
} else {
keyringStore.createWallet(password: password) { mnemonic in
if !mnemonic.isEmpty {
if let mnemonic, !mnemonic.isEmpty {
isNewWalletCreated = true
}
}
Expand Down Expand Up @@ -144,6 +144,11 @@ private struct CreateWalletView: View {
)
.hidden(isHidden: error == nil)
}

private var isContinueDisabled: Bool {
validationError != nil || password.isEmpty || repeatedPassword.isEmpty ||
keyringStore.isCreatingWallet || keyringStore.isRestoringWallet
}

var body: some View {
VStack(spacing: 16) {
Expand Down Expand Up @@ -200,7 +205,7 @@ private struct CreateWalletView: View {
.frame(maxWidth: .infinity)
}
.buttonStyle(BraveFilledButtonStyle(size: .large))
.disabled(validationError != nil || password.isEmpty || repeatedPassword.isEmpty)
.disabled(isContinueDisabled)
.padding(.top, 60)
}
.padding(.horizontal, 20)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private struct RestoreWalletView: View {
}

private var isContinueDisabled: Bool {
!recoveryWords.allSatisfy({ !$0.isEmpty })
!recoveryWords.allSatisfy({ !$0.isEmpty }) || keyringStore.isRestoringWallet
}

private var errorLabel: some View {
Expand Down
30 changes: 22 additions & 8 deletions Sources/BraveWallet/Crypto/Stores/KeyringStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,14 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
public var origin: URLOrigin?

/// If this KeyringStore instance is creating a wallet.
/// This flag is used to know when to dismiss onboarding when multiple windows are visible.
private var isCreatingWallet = false
/// Note: Flag is reset prior to onboarding completion step.
@Published var isCreatingWallet = false
/// If this KeyringStore instance is restoring a wallet.
/// Note: Flag is reset prior to onboarding completion step.
@Published var isRestoringWallet = false
/// If this KeyringStore instance is creating a wallet or restoring a wallet.
/// This flag is used to know when to dismiss onboarding when multiple windows are visible.
private var isRestoringWallet = false
private var isOnboarding: Bool = false

private let keyringService: BraveWalletKeyringService
private let walletService: BraveWalletBraveWalletService
Expand Down Expand Up @@ -224,7 +227,7 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
},
_keyringCreated: { [weak self] keyringId in
guard let self else { return }
if self.isOnboardingVisible, !self.isCreatingWallet, keyringId == BraveWallet.KeyringId.default {
if self.isOnboardingVisible, !self.isOnboarding, keyringId == BraveWallet.KeyringId.default {
// Another window has created a wallet. We should dismiss onboarding on this
// window and allow the other window to continue with it's onboarding flow.
self.isOnboardingVisible = false
Expand All @@ -242,7 +245,7 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
},
_walletRestored: { [weak self] in
guard let self else { return }
if self.isOnboardingVisible && !self.isRestoringWallet {
if self.isOnboardingVisible && !self.isOnboarding {
// Another window has restored a wallet. We should dismiss onboarding on this
// window and allow the other window to continue with it's onboarding flow.
self.isOnboardingVisible = false
Expand Down Expand Up @@ -328,8 +331,7 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
}

func markOnboardingCompleted() {
self.isCreatingWallet = false
self.isRestoringWallet = false
self.isOnboarding = false
self.isOnboardingVisible = false
}

Expand Down Expand Up @@ -387,9 +389,15 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
}
}

func createWallet(password: String, completion: ((String) -> Void)? = nil) {
func createWallet(password: String, completion: ((String?) -> Void)? = nil) {
guard !isCreatingWallet else {
completion?(nil)
return
}
isOnboarding = true
isCreatingWallet = true
keyringService.createWallet(password) { [weak self] mnemonic in
self?.isCreatingWallet = false
self?.updateKeyringInfo()
if !mnemonic.isEmpty {
self?.passwordToSaveInBiometric = password
Expand All @@ -413,13 +421,19 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
}

func restoreWallet(phrase: String, password: String, isLegacyBraveWallet: Bool, completion: ((Bool) -> Void)? = nil) {
guard !isRestoringWallet else { // wallet is already being restored.
completion?(false)
return
}
isOnboarding = true
isRestoringWallet = true
keyringService.restoreWallet(
phrase,
password: password,
isLegacyBraveWallet: isLegacyBraveWallet
) { [weak self] isMnemonicValid in
guard let self = self else { return }
self.isRestoringWallet = false
if isMnemonicValid {
// Restoring from wallet means you already have your phrase backed up
self.passwordToSaveInBiometric = password
Expand Down

0 comments on commit 535f29a

Please sign in to comment.