Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Fix #8357: Disable button to create wallet when wallet is already being created #8358

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
soner-yuksel marked this conversation as resolved.
Show resolved Hide resolved
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
Loading