From a4fe3569500972d98861af93d5b36102be45908f Mon Sep 17 00:00:00 2001 From: StephenHeaps <5314553+StephenHeaps@users.noreply.github.com> Date: Tue, 14 Nov 2023 09:02:07 -0500 Subject: [PATCH] Fix: #8404: Invalid presentation over panel while loading keyring (#8405) Fix invalid presentation over panel while loading. --- .../BraveWallet/Panels/WalletPanelView.swift | 37 +++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/Sources/BraveWallet/Panels/WalletPanelView.swift b/Sources/BraveWallet/Panels/WalletPanelView.swift index 09707428bde..9a7945f99be 100644 --- a/Sources/BraveWallet/Panels/WalletPanelView.swift +++ b/Sources/BraveWallet/Panels/WalletPanelView.swift @@ -146,14 +146,20 @@ public struct WalletPanelContainerView: View { } } .frame(idealWidth: 320, maxWidth: .infinity) - .onChange(of: keyringStore.isWalletCreated) { newValue in - if visibleScreen != .panel, !keyringStore.lockedManually { - presentWalletWithContext?(.panelUnlockOrSetup) - } - } .onChange(of: keyringStore.isWalletLocked) { newValue in - if visibleScreen != .panel, !keyringStore.lockedManually { - presentWalletWithContext?(.panelUnlockOrSetup) + guard keyringStore.isLoaded, newValue, !keyringStore.lockedManually else { return } + // Wallet was auto-locked with panel open + presentWalletWithContext?(.panelUnlockOrSetup) + } + .onChange(of: keyringStore.isLoaded) { newValue in + guard newValue else { return } // KeyringStore loaded + handleKeyringStoreLoaded() + } + .onAppear { + if keyringStore.isLoaded { + // If KeyringStore is loaded prior to view appearing on + // screen onChange won't be executed + handleKeyringStoreLoaded() } } .environment( @@ -163,6 +169,23 @@ public struct WalletPanelContainerView: View { return .handled })) } + + /// Flag to help prevent race condition between panel appearing on screen and KeyringStore `isLoaded`. + @State private var didHandleKeyringLoaded: Bool = false + /// Present unlock if displayed locked state (unless manually locked), or onboarding if displaying + /// onboarding state + private func handleKeyringStoreLoaded() { + guard !didHandleKeyringLoaded else { return } + didHandleKeyringLoaded = true + if visibleScreen == .onboarding { + // automatically open full wallet when displaying onboarding + presentWalletWithContext?(.panelUnlockOrSetup) + } else if visibleScreen == .unlock, !keyringStore.lockedManually { + // automatically open full unlock wallet view when displaying + // locked panel unless user locked manually + presentWalletWithContext?(.panelUnlockOrSetup) + } + } } struct WalletPanelView: View {