From 4d34115a37c0e4cc4a5d0463dfd4992e132787c6 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Tue, 25 Aug 2026 17:37:57 +0200 Subject: [PATCH] feat(wallet): show the lifecycle overlay from the first moment of Add Wallet MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Wallets -> Add Wallet blocked the MainActor for ~5s (wallet-creation FFI on the current network, then the other network's SDK build + creation) before the post-add switch finally raised the overlay - the app looked frozen and was dead to touch with no indicator. - new phase .addingWallet(isImport:) with a Creating/Importing wallet… card; admitted from idle, and the composite continuation (.addingWallet -> .switchingWallet) lets the existing gatedSwitchWallet advance the window Creating -> Switching without dropping through idle (the pair is not owner-scoped on purpose - the blocking overlay window covers every other interactive entry point for the phase's lifetime) - WalletsViewModel.addWallet begins the phase before provisioning and sleeps 100 ms so UIKit commits the overlay window before the blocking work starts (same trick as the Obj-C wipe's dispatch_after); every exit pairs the begin: provisioning error and .alreadyExists finish(), .added hands the phase to the gated switch, and a rejected post-add admission defensively finishes only if still .addingWallet - WALLETOP add begin/done/FAILED telemetry with duration - admission-matrix table test gains the .addingWallet rows and the composite-continuation case This is the cosmetic stage: the MainActor still blocks under the overlay. The real fix (async off-main createWallet in the Swift SDK + lifecycle-queue serialization) follows separately. Co-Authored-By: Claude Fable 5 --- .../WalletLifecycleTransitionState.swift | 13 +++++++ .../UI/Main/WalletLifecycleOverlay.swift | 10 ++++- .../Security/Wallets/WalletsViewModel.swift | 38 +++++++++++++++++++ .../WalletLifecycleTransitionStateTests.swift | 19 ++++++++-- 4 files changed, 75 insertions(+), 5 deletions(-) diff --git a/DashWallet/Sources/Infrastructure/SwiftDashSDK/WalletLifecycleTransitionState.swift b/DashWallet/Sources/Infrastructure/SwiftDashSDK/WalletLifecycleTransitionState.swift index bb64c9c86a..8625f830d5 100644 --- a/DashWallet/Sources/Infrastructure/SwiftDashSDK/WalletLifecycleTransitionState.swift +++ b/DashWallet/Sources/Infrastructure/SwiftDashSDK/WalletLifecycleTransitionState.swift @@ -37,6 +37,11 @@ final class WalletLifecycleTransitionState: ObservableObject { case switchingWallet(targetName: String?) /// Per-wallet removal in flight (`deleteLogicalWallet`). case removingWallet + /// Add-wallet provisioning in flight (`SwiftDashSDKHost.addWallet` — + /// blocking wallet creation on the current network plus the other + /// network's mirror). `isImport` picks the card copy + /// (Creating/Importing wallet…). + case addingWallet(isImport: Bool) /// The wallet switch failed. The previous wallet is NOT guaranteed /// active afterwards (the registry is repointed before the rebuild /// and the host may have bound a fallback wallet), so the card @@ -64,6 +69,7 @@ final class WalletLifecycleTransitionState: ObservableObject { case .failedNetworkSwitch(_, let target, _): return "failedNetworkSwitch(\(target))" case .switchingWallet: return "switchingWallet" case .removingWallet: return "removingWallet" + case .addingWallet: return "addingWallet" case .failedWalletSwitch: return "failedWalletSwitch" case .failedWalletRemoval: return "failedWalletRemoval" case .wiping: return "wiping" @@ -91,9 +97,16 @@ final class WalletLifecycleTransitionState: ObservableObject { case (.idle, .switchingNetwork), (.idle, .switchingWallet), (.idle, .removingWallet), + (.idle, .addingWallet), (.idle, .wiping), (.failedNetworkSwitch, .switchingNetwork), (.failedWalletSwitch, .switchingWallet), + // Composite add → switch: the add flow's own continuation into + // its post-add switch, without dropping the window through idle. + // Deliberately not owner-scoped — in practice only the add flow + // can reach it, because the blocking overlay window covers every + // other interactive entry point for `.addingWallet`'s lifetime. + (.addingWallet, .switchingWallet), // The wipe/reset route stays reachable from EVERY failure card: // without this, a persistently failing switch (destination // network down) would block the whole app behind the overlay diff --git a/DashWallet/Sources/UI/Main/WalletLifecycleOverlay.swift b/DashWallet/Sources/UI/Main/WalletLifecycleOverlay.swift index 7f65582dc8..c59d093d98 100644 --- a/DashWallet/Sources/UI/Main/WalletLifecycleOverlay.swift +++ b/DashWallet/Sources/UI/Main/WalletLifecycleOverlay.swift @@ -56,7 +56,7 @@ final class WalletLifecycleOverlayPresenter { overlayWindow?.isHidden = true overlayWindow = nil case .switchingNetwork, .failedNetworkSwitch, - .switchingWallet, .removingWallet, + .switchingWallet, .removingWallet, .addingWallet, .failedWalletSwitch, .failedWalletRemoval, .wiping: presentIfNeeded() @@ -189,6 +189,14 @@ struct WalletLifecycleOverlayView: View { progressCard( title: NSLocalizedString("Removing wallet…", comment: "Wallets"), subtitle: nil) + case let .addingWallet(isImport): + progressCard( + title: isImport + ? NSLocalizedString("Importing wallet…", comment: "Wallets") + : NSLocalizedString("Creating wallet…", comment: "Wallets"), + subtitle: NSLocalizedString( + "Preparing your wallet. This may take a few seconds.", + comment: "Wallets")) case let .wiping(title): // Falls back to the Delete All copy — the same key the root // controller's HUD used, so translations carry over. diff --git a/DashWallet/Sources/UI/Menu/Security/Wallets/WalletsViewModel.swift b/DashWallet/Sources/UI/Menu/Security/Wallets/WalletsViewModel.swift index d7fb82a38c..3f604c5387 100644 --- a/DashWallet/Sources/UI/Menu/Security/Wallets/WalletsViewModel.swift +++ b/DashWallet/Sources/UI/Menu/Security/Wallets/WalletsViewModel.swift @@ -265,28 +265,66 @@ final class WalletsViewModel: ObservableObject { addInProgress = true defer { addInProgress = false } + // Blocking overlay from the FIRST moment: provisioning is seconds of + // blocking MainActor work (wallet creation FFI on both networks, plus + // the other network's SDK build), and without this phase the app + // looked frozen until the post-add switch finally raised the window. + WalletLifecycleOverlayPresenter.shared.ensureActive() + let state = WalletLifecycleTransitionState.shared + guard state.tryBegin(.addingWallet(isImport: isImported)) else { + errorMessage = SwiftDashSDKWalletRuntime.SwitchError.switchInProgress.localizedDescription + return nil + } + // Give UIKit one runloop turn to commit the overlay window before the + // blocking provisioning starts (the Obj-C wipe's 0.1 s dispatch_after + // trick): the phase-apply Task was already enqueued by the sink, and + // both it and the CoreAnimation commit run while this sleeps. + try? await Task.sleep(for: .milliseconds(100)) + + let opID = String(UUID().uuidString.prefix(8)) + let started = CFAbsoluteTimeGetCurrent() + DWLogger.log("🔁 WALLETOP [\(opID)] add begin import=\(isImported)") + let result: SwiftDashSDKHost.AddWalletResult do { result = try await SwiftDashSDKHost.shared.addWallet( mnemonic: normalized, isImported: isImported) } catch { + // The ONE exit where a forgotten finish() would strand the + // blocking overlay with no owner. + state.finish() + let ms = Int((CFAbsoluteTimeGetCurrent() - started) * 1000) + DWLogger.log("🔁 WALLETOP [\(opID)] add FAILED after \(ms)ms: \(error.localizedDescription)") Self.logger.error("addWallet failed: \(String(describing: error), privacy: .public)") errorMessage = error.localizedDescription return nil } + let ms = Int((CFAbsoluteTimeGetCurrent() - started) * 1000) + DWLogger.log("🔁 WALLETOP [\(opID)] add done in \(ms)ms") switch result { case .alreadyExists(let walletId): + state.finish() return .alreadyOnDevice(walletId: walletId) case .added(let walletId): switchInProgress = true do { + // Admitted from `.addingWallet` by the composite rule — the + // window advances Creating → Switching without dropping + // through idle; from here the helper owns finish/fail. try await Self.gatedSwitchWallet( targetId: walletId, targetName: Self.displayName(for: walletId)) } catch { switchInProgress = false + // Defense in depth: a rejected admission (throws BEFORE any + // fail-phase is set) must not strand `.addingWallet`; a real + // switch failure has already moved to `.failedWalletSwitch`, + // which this deliberately leaves alone. + if case .addingWallet = state.phase { + state.finish() + } Self.logger.error("post-add switch failed: \(String(describing: error), privacy: .public)") // The wallet was added but the switch failed; leave it on // device — the overlay's blocking card owns recovery (Retry / diff --git a/DashWalletTests/WalletLifecycleTransitionStateTests.swift b/DashWalletTests/WalletLifecycleTransitionStateTests.swift index 5ec58bf9f1..aa95b0ad5d 100644 --- a/DashWalletTests/WalletLifecycleTransitionStateTests.swift +++ b/DashWalletTests/WalletLifecycleTransitionStateTests.swift @@ -23,6 +23,7 @@ final class WalletLifecycleTransitionStateTests: XCTestCase { ("switchingNetwork", .switchingNetwork(from: .mainnet, to: .testnet)), ("switchingWallet", .switchingWallet(targetName: "A")), ("removingWallet", .removingWallet), + ("addingWallet", .addingWallet(isImport: false)), ("wiping", .wiping(title: nil)), ] @@ -56,18 +57,28 @@ final class WalletLifecycleTransitionStateTests: XCTestCase { } } - /// No begin is admitted while any operation is busy. + /// No begin is admitted while any operation is busy — except the add + /// flow's own composite continuation into its post-add switch. func testBusyPhasesRejectEveryBegin() { for (busyLabel, busy) in Self.begins { for (nextLabel, next) in Self.begins { let state = makeState(in: busy) - XCTAssertFalse( - state.tryBegin(next), - "\(nextLabel) must be rejected while \(busyLabel) is in flight") + let expected = busyLabel == "addingWallet" && nextLabel == "switchingWallet" + XCTAssertEqual( + state.tryBegin(next), expected, + "\(busyLabel) → \(nextLabel): expected admitted=\(expected)") } } } + /// The add flow's window advances Creating → Switching without dropping + /// through idle. + func testAddFlowCompositeAdvancesToSwitch() { + let state = makeState(in: .addingWallet(isImport: false)) + XCTAssertTrue(state.tryBegin(.switchingWallet(targetName: "A"))) + XCTAssertEqual(state.phase, .switchingWallet(targetName: "A")) + } + /// Failure phases admit exactly their own retry — and a wipe. func testFailurePhaseAdmissions() { let retryOf: [String: String] = [