diff --git a/Bitkit/Extensions/UINavigationController+SwipeBack.swift b/Bitkit/Extensions/UINavigationController+SwipeBack.swift index ee62b34c5..a3a4699b4 100644 --- a/Bitkit/Extensions/UINavigationController+SwipeBack.swift +++ b/Bitkit/Extensions/UINavigationController+SwipeBack.swift @@ -5,6 +5,8 @@ import UIKit enum SwipeBackState { /// When false, the interactive pop gesture is disabled. Set by views that hide the back button. static var allowSwipeBack: Bool = true + /// Send confirm screens a running swipe holds; kept apart so other screens' own setting stays theirs. + static var confirmSwipeHolds = 0 } /// Re-enables the interactive swipe-back gesture when the navigation bar is hidden @@ -20,6 +22,6 @@ extension UINavigationController: @retroactive UIGestureRecognizerDelegate { public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { // Only allow swipe-back when not at root — avoids iOS 17+ freeze when re-pushing after swiping to root guard viewControllers.count > 1 else { return false } - return SwipeBackState.allowSwipeBack + return SwipeBackState.allowSwipeBack && SwipeBackState.confirmSwipeHolds == 0 } } diff --git a/Bitkit/Views/Subscriptions/SubscriptionsView.swift b/Bitkit/Views/Subscriptions/SubscriptionsView.swift index dd5fd3676..a9ffc91d1 100644 --- a/Bitkit/Views/Subscriptions/SubscriptionsView.swift +++ b/Bitkit/Views/Subscriptions/SubscriptionsView.swift @@ -599,6 +599,7 @@ struct SubscriptionSheet: View { @State private var previousRoute: SubscriptionSheetItem.Route? @State private var now = SubscriptionClock.subscriptionNow() @State private var isAccepting = false + @State private var isSendConfirmLocked = false @State private var creationDraft = PaykitSubscriptionDraft.empty @State private var selectedCreationTarget: PaykitPaymentRequestTarget? @@ -649,7 +650,8 @@ struct SubscriptionSheet: View { case let .payment(sendRoute): SendSheet( config: SendSheetItem(presentationID: sheets.activeSheetConfiguration?.presentationID, initialRoute: sendRoute), - isEmbedded: true + isEmbedded: true, + onConfirmLockChange: { isSendConfirmLocked = $0 } ) case .allowanceContact: AllowanceContactView { route = .allowanceSet($0) } @@ -695,7 +697,7 @@ struct SubscriptionSheet: View { } now = SubscriptionClock.subscriptionNow() } - .interactiveDismissDisabled(isAccepting || paymentRequests.isCreatingRequest) + .interactiveDismissDisabled(isAccepting || paymentRequests.isCreatingRequest || isSendConfirmLocked) } private func review(_ subscription: PaykitSubscription) -> some View { diff --git a/Bitkit/Views/Wallets/Send/LnurlPayConfirm.swift b/Bitkit/Views/Wallets/Send/LnurlPayConfirm.swift index e9d4b35f5..c043635a8 100644 --- a/Bitkit/Views/Wallets/Send/LnurlPayConfirm.swift +++ b/Bitkit/Views/Wallets/Send/LnurlPayConfirm.swift @@ -16,6 +16,8 @@ struct LnurlPayConfirm: View { let requestPinCheck: () async -> Bool let prepareIncomingPaymentRequest: () async throws -> Void let routingCacheResetAttempted: Bool + /// Tells the sheet when a running swipe holds this screen, so its drag waits too. + var onLockChange: (Bool) -> Void = { _ in } @State private var showWarningAlert = false @State private var alertContinuation: CheckedContinuation? @@ -27,6 +29,7 @@ struct LnurlPayConfirm: View { @State private var sparkUpdateQuote: MoveToSparkQuote? @State private var isSparkUpdateQuoting = false @State private var screenVisit = ConfirmScreenVisit() + @State private var swipeLock = ConfirmSwipeLock() /// A first subscription payment that needs the update waits for a swipe on its update fee instead of paying by itself. @State private var needsUpdateConfirmation = false @FocusState private var isCommentFocused: Bool @@ -108,6 +111,7 @@ struct LnurlPayConfirm: View { screenVisit = ConfirmScreenVisit() } } + .confirmSwipeLock(swipeLock, isSubmittingPayment: isSubmittingPayment, onLockChange: onLockChange) .onDisappear { screenVisit.leave() } @@ -117,7 +121,7 @@ struct LnurlPayConfirm: View { VStack { SheetHeader( title: reviewTitle, - showBackButton: true, + showBackButton: !swipeLock.isLocked, action: AnyView(SendContactHeaderAvatar()) ) @@ -183,6 +187,8 @@ struct LnurlPayConfirm: View { .focused($isCommentFocused) .dismissKeyboardOnReturn(text: $comment, isFocused: $isCommentFocused) .lineLimit(3 ... 3) + // The comment stays as swiped while the swipe runs + .disabled(swipeLock.isLocked) .onChange(of: comment) { _, newValue in let maxLength = Int(commentAllowed) if newValue.count > maxLength { @@ -207,6 +213,8 @@ struct LnurlPayConfirm: View { isLoading: hasStartedAutomaticPayment || isSubmittingPayment, isSubmitted: hasStartedAutomaticPayment ) { + swipeLock.begin() + defer { swipeLock.end() } try await submitPayment(isAutomatic: false) } } @@ -232,6 +240,8 @@ struct LnurlPayConfirm: View { } hasStartedAutomaticPayment = true do { + swipeLock.begin() + defer { swipeLock.end() } try await submitPayment(isAutomatic: true) } catch is CancellationError { navigationPath.append(.failure(SendFailureContext( diff --git a/Bitkit/Views/Wallets/Send/SendConfirmationView.swift b/Bitkit/Views/Wallets/Send/SendConfirmationView.swift index 53b42dcb8..4534caf5a 100644 --- a/Bitkit/Views/Wallets/Send/SendConfirmationView.swift +++ b/Bitkit/Views/Wallets/Send/SendConfirmationView.swift @@ -29,6 +29,8 @@ struct SendConfirmationView: View { var amountIsRoot = false /// True while the send sheet is still loading the fee rate and choosing the funding source. var isSetupPending = false + /// Tells the sheet when a running swipe holds this screen, so its drag waits too. + var onLockChange: (Bool) -> Void = { _ in } @State private var showDetails = false @State private var routedIncomingRequestId: PaykitPaymentRequest.ID? @@ -91,6 +93,7 @@ struct SendConfirmationView: View { @State private var sparkUpdateQuote: MoveToSparkQuote? @State private var sparkUpdateQuoteRuns = SparkUpdateQuoteRuns() @State private var screenVisit = ConfirmScreenVisit() + @State private var swipeLock = ConfirmSwipeLock() private struct WalletSwitchRun { let id: UUID @@ -407,7 +410,7 @@ struct SendConfirmationView: View { VStack(alignment: .leading, spacing: 0) { SheetHeader( title: reviewTitle, - showBackButton: !navigationPath.isEmpty, + showBackButton: !navigationPath.isEmpty && !swipeLock.isLocked, action: AnyView(SendContactHeaderAvatar(publicKey: preparingRequest?.counterparty)) ) @@ -524,9 +527,13 @@ struct SendConfirmationView: View { isSubmitted: hasStartedAutomaticPayment, swipeProgress: $swipeProgress ) { + swipeLock.begin() + defer { swipeLock.end() } try await submitPayment() } } + // The payment's inputs stay as swiped while the swipe runs + .allowsHitTesting(!swipeLock.isLocked) } private var confirmationPresentation: some View { @@ -621,6 +628,7 @@ struct SendConfirmationView: View { screenVisit = ConfirmScreenVisit() } } + .confirmSwipeLock(swipeLock, isSubmittingPayment: isSubmittingPayment, onLockChange: onLockChange) .onDisappear { screenVisit.leave() startWalletSwitch(context: nil) @@ -681,6 +689,8 @@ struct SendConfirmationView: View { ) else { return } hasStartedAutomaticPayment = true do { + swipeLock.begin() + defer { swipeLock.end() } try await submitPayment(isAutomatic: true) } catch is CancellationError { navigationPath.append(.failure(SendFailureContext( @@ -3164,6 +3174,67 @@ final class ConfirmScreenVisit { } } +/// Holds a confirm screen while a swipe of it, or the payment it started, runs, until it moves on to the next screen or +/// returns to Confirm: Back, swipe back, the sheet's drag and the payment's inputs wait meanwhile. Each change reports at +/// once, not on the next redraw, since a screen covered by the PIN may not redraw. +@MainActor +@Observable +final class ConfirmSwipeLock { + private(set) var runs = 0 + private(set) var isVisible = false + private(set) var isSubmittingPayment = false + @ObservationIgnored var onLockChange: (Bool) -> Void = { _ in } + @ObservationIgnored private var isHolding = false + + var isLocked: Bool { + (runs > 0 || isSubmittingPayment) && isVisible + } + + func begin() { + runs += 1 + report() + } + + func end() { + runs = max(0, runs - 1) + report() + } + + func setVisible(_ isVisible: Bool) { + self.isVisible = isVisible + report() + } + + func setSubmittingPayment(_ isSubmittingPayment: Bool) { + self.isSubmittingPayment = isSubmittingPayment + report() + } + + private func report() { + guard isLocked != isHolding else { return } + isHolding = isLocked + SwipeBackState.confirmSwipeHolds += isLocked ? 1 : -1 + onLockChange(isLocked) + } +} + +extension View { + /// Tracks the screen's stay and payment for `lock`, which tells the sheet of each change. + func confirmSwipeLock( + _ lock: ConfirmSwipeLock, + isSubmittingPayment: Bool, + onLockChange: @escaping (Bool) -> Void + ) -> some View { + onAppear { + lock.onLockChange = onLockChange + lock.setSubmittingPayment(isSubmittingPayment) + lock.setVisible(true) + } + .onDisappear { lock.setVisible(false) } + .onChange(of: isSubmittingPayment) { _, isSubmitting in lock.setSubmittingPayment(isSubmitting) } + } +} + /// Tracks the newest spending balance update quote on Confirm, so an older quote finishing late writes neither its fee nor /// its quote, and only the newest one holds the swipe. struct SparkUpdateQuoteRuns: Equatable { diff --git a/Bitkit/Views/Wallets/Send/SendSheet.swift b/Bitkit/Views/Wallets/Send/SendSheet.swift index 32a1e7084..f383b821b 100644 --- a/Bitkit/Views/Wallets/Send/SendSheet.swift +++ b/Bitkit/Views/Wallets/Send/SendSheet.swift @@ -96,6 +96,12 @@ final class SendPresentationState { var hasValidatedAfterSync = false var incomingPaymentRequest: PaykitPaymentRequest? var isSubmittingPayment = false + /// Confirm screens a running swipe holds; the sheet's drag waits for them too. + var confirmSwipeHolds = 0 + var isConfirmLocked: Bool { + confirmSwipeHolds > 0 + } + var isAmountReviewRequired = false var paymentReview: UUID? var isReviewingUsdtPayment = false @@ -214,6 +220,8 @@ struct SendSheet: View { let config: SendSheetItem let isEmbedded: Bool + /// Tells a sheet embedding this flow when Confirm holds the drag, as its own dismiss rule wins. + var onConfirmLockChange: (Bool) -> Void = { _ in } @State private var flow: SendPresentationState @State private var hwSend: HwSendCoordinator @@ -225,9 +233,10 @@ struct SendSheet: View { return sheets.activeSheetConfiguration?.presentationID == presentationID } - init(config: SendSheetItem, isEmbedded: Bool = false) { + init(config: SendSheetItem, isEmbedded: Bool = false, onConfirmLockChange: @escaping (Bool) -> Void = { _ in }) { self.config = config self.isEmbedded = isEmbedded + self.onConfirmLockChange = onConfirmLockChange _flow = State(initialValue: config.state) let hwSend = config.state.hwSend ?? HwSendCoordinator(walletId: config.hardwareWalletId) config.state.hwSend = hwSend @@ -373,7 +382,8 @@ struct SendSheet: View { } } .animation(.easeInOut(duration: 0.3), value: shouldShowSyncOverlay) - .interactiveDismissDisabled(!hwSend.canLeave) + .interactiveDismissDisabled(!hwSend.canLeave || flow.isConfirmLocked) + .onChange(of: flow.isConfirmLocked) { _, isLocked in onConfirmLockChange(isLocked) } .sheet(isPresented: reconnectPairingBinding) { HardwarePairingSheet(config: HardwarePairingSheetItem()) } @@ -1124,7 +1134,8 @@ struct SendSheet: View { preparingRequest: isPreparingRequest ? config.preparation?.request : nil, isAmountReviewRequired: $flow.isAmountReviewRequired, amountIsRoot: currentRoot == .amount, - isSetupPending: flow.isSetupPending || flow.isFallbackFundingPending + isSetupPending: flow.isSetupPending || flow.isFallbackFundingPending, + onLockChange: { flow.confirmSwipeHolds += $0 ? 1 : -1 } ) case .hardwareSign: let contactContext = app.contactPaymentContext @@ -1258,7 +1269,8 @@ struct SendSheet: View { isSubmittingPayment: $flow.isSubmittingPayment, requestPinCheck: requestPinCheck, prepareIncomingPaymentRequest: prepareIncomingPaymentRequest, - routingCacheResetAttempted: flow.routingCacheResetAttempted + routingCacheResetAttempted: flow.routingCacheResetAttempted, + onLockChange: { flow.confirmSwipeHolds += $0 ? 1 : -1 } ) case .lnurlWithdrawAmount: LnurlWithdrawAmount { diff --git a/BitkitTests/SendConfirmationSwipeTests.swift b/BitkitTests/SendConfirmationSwipeTests.swift index 212198911..ac12f6eb5 100644 --- a/BitkitTests/SendConfirmationSwipeTests.swift +++ b/BitkitTests/SendConfirmationSwipeTests.swift @@ -4,6 +4,55 @@ import SwiftUI import XCTest final class SendConfirmationSwipeTests: XCTestCase { + @MainActor + func testARunningSwipeHoldsConfirmUntilItMovesOnOrReturns() { + let holdsBefore = SwipeBackState.confirmSwipeHolds + let lock = ConfirmSwipeLock() + var reported: [Bool] = [] + lock.onLockChange = { reported.append($0) } + lock.setVisible(true) + XCTAssertFalse(lock.isLocked) + + lock.begin() + XCTAssertTrue(lock.isLocked, "A running swipe holds Back, the sheet's drag and the inputs") + XCTAssertEqual(SwipeBackState.confirmSwipeHolds, holdsBefore + 1) + lock.setVisible(false) + XCTAssertFalse(lock.isLocked, "The screen the swipe moves on to, such as the PIN, is free") + lock.end() + lock.setVisible(true) + XCTAssertFalse(lock.isLocked, "A swipe that stopped while the PIN covered Confirm leaves it free on return") + + lock.begin() + lock.setVisible(false) + lock.setVisible(true) + XCTAssertTrue(lock.isLocked, "Returning to Confirm while the swipe still runs holds it again") + lock.end() + lock.setSubmittingPayment(true) + XCTAssertTrue(lock.isLocked, "A payment still running holds a Confirm rebuilt without the swipe") + lock.setSubmittingPayment(false) + + XCTAssertEqual(reported, [true, false, true, false, true, false, true, false]) + XCTAssertEqual(SwipeBackState.confirmSwipeHolds, holdsBefore, "Every hold is let go") + } + + @MainActor + func testALeavingConfirmKeepsTheNextConfirmsHold() { + let holdsBefore = SwipeBackState.confirmSwipeHolds + let leaving = ConfirmSwipeLock() + let next = ConfirmSwipeLock() + leaving.setVisible(true) + leaving.begin() + next.setVisible(true) + next.setSubmittingPayment(true) + + leaving.setVisible(false) + XCTAssertEqual(SwipeBackState.confirmSwipeHolds, holdsBefore + 1, "The next confirm's payment still holds swipe back") + + leaving.end() + next.setVisible(false) + XCTAssertEqual(SwipeBackState.confirmSwipeHolds, holdsBefore) + } + @MainActor func testRestoredLoadingSwipeKeepsItsSpinnerInTheKnob() async throws { let confirmation = NavigationStack(path: .constant([true])) { diff --git a/changelog.d/next/1418.fixed.md b/changelog.d/next/1418.fixed.md new file mode 100644 index 000000000..2b5122ee8 --- /dev/null +++ b/changelog.d/next/1418.fixed.md @@ -0,0 +1 @@ +Keep the payment confirm screen open while a swipe is paying, so leaving can't undo it silently. diff --git a/journeys/send/update-spending-balance-payment.xml b/journeys/send/update-spending-balance-payment.xml index f08acb76a..13ebba5db 100644 --- a/journeys/send/update-spending-balance-payment.xml +++ b/journeys/send/update-spending-balance-payment.xml @@ -5,7 +5,9 @@ and one swipe first moves the channel's balance to Spark, then pays the invoice from Spark. The move shows in Activity as a "Move to Spark" row, and the emptied channel closes in the background. If the payment fails after the move, the funds stay in Spark and the usual payment error shows. QuickPay never sends such a payment; it always - opens the confirm screen. + opens the confirm screen. From the swipe until it moves on or returns to confirm, the confirm screen is locked: no + Back, no sheet dismiss and no edits. On iOS a confirm screen opened from a link is the sheet's root, with no Back to + press, so only the sheet's drag applies there; a confirm reached from Amount also hides Back and swipe back. Precondition: dev build pointed at a local Spark regtest (spark-sdk regtest/local), QuickPay off, a wallet with an open channel to that stack's SSP Lightning node that can send about 30 000 sats and a Spark balance of about @@ -17,6 +19,7 @@ Verify the Confirm screen (id "SendConfirm") shows 50 000 sats with its details open on their own (Hide Details, id "SendConfirmToggleDetails") Verify the fee row shows the payment's own fee and the "Update fee" row (id "ReviewUpdateFee") shows an amount, without "up to", and that no "Route" row (id "ReviewSwapRoute") shows Swipe to pay (id "GRAB"); the payment is over half of the Spending balance, so verify the "Are You Sure?" dialog warns about sending over 50% of the balance and tap "Yes, Send" (id "DialogConfirm") + While the swipe still shows its loading state, drag the sheet down; verify the Confirm screen stays open Verify the Bitcoin Sent screen (id "SendSuccess") shows 50 000 Go back to Home and verify Activity lists a "Move to Spark" transfer row and the 50 000-sat Lightning payment Open Settings › Advanced › Lightning Connections and verify the channel is closing or closed