Skip to content
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
4 changes: 3 additions & 1 deletion Bitkit/Extensions/UINavigationController+SwipeBack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
6 changes: 4 additions & 2 deletions Bitkit/Views/Subscriptions/SubscriptionsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down Expand Up @@ -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) }
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion Bitkit/Views/Wallets/Send/LnurlPayConfirm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bool, Error>?
Expand All @@ -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
Expand Down Expand Up @@ -108,6 +111,7 @@ struct LnurlPayConfirm: View {
screenVisit = ConfirmScreenVisit()
}
}
.confirmSwipeLock(swipeLock, isSubmittingPayment: isSubmittingPayment, onLockChange: onLockChange)
.onDisappear {
screenVisit.leave()
}
Expand All @@ -117,7 +121,7 @@ struct LnurlPayConfirm: View {
VStack {
SheetHeader(
title: reviewTitle,
showBackButton: true,
showBackButton: !swipeLock.isLocked,
action: AnyView(SendContactHeaderAvatar())
)

Expand Down Expand Up @@ -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 {
Expand All @@ -207,6 +213,8 @@ struct LnurlPayConfirm: View {
isLoading: hasStartedAutomaticPayment || isSubmittingPayment,
isSubmitted: hasStartedAutomaticPayment
) {
swipeLock.begin()
defer { swipeLock.end() }
try await submitPayment(isAutomatic: false)
}
}
Expand All @@ -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(
Expand Down
73 changes: 72 additions & 1 deletion Bitkit/Views/Wallets/Send/SendConfirmationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
)

Expand Down Expand Up @@ -524,9 +527,13 @@ struct SendConfirmationView: View {
isSubmitted: hasStartedAutomaticPayment,
swipeProgress: $swipeProgress
) {
swipeLock.begin()
defer { swipeLock.end() }
try await submitPayment()
Comment thread
ovitrif marked this conversation as resolved.
Resolved
}
}
// The payment's inputs stay as swiped while the swipe runs
.allowsHitTesting(!swipeLock.isLocked)
}

private var confirmationPresentation: some View {
Expand Down Expand Up @@ -621,6 +628,7 @@ struct SendConfirmationView: View {
screenVisit = ConfirmScreenVisit()
}
}
.confirmSwipeLock(swipeLock, isSubmittingPayment: isSubmittingPayment, onLockChange: onLockChange)
.onDisappear {
screenVisit.leave()
startWalletSwitch(context: nil)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 16 additions & 4 deletions Bitkit/Views/Wallets/Send/SendSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
49 changes: 49 additions & 0 deletions BitkitTests/SendConfirmationSwipeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
1 change: 1 addition & 0 deletions changelog.d/next/1418.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Keep the payment confirm screen open while a swipe is paying, so leaving can't undo it silently.
5 changes: 4 additions & 1 deletion journeys/send/update-spending-balance-payment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,6 +19,7 @@
<action>Verify the Confirm screen (id "SendConfirm") shows 50 000 sats with its details open on their own (Hide Details, id "SendConfirmToggleDetails")</action>
<action>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</action>
<action>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")</action>
<action>While the swipe still shows its loading state, drag the sheet down; verify the Confirm screen stays open</action>
<action>Verify the Bitcoin Sent screen (id "SendSuccess") shows 50 000</action>
<action>Go back to Home and verify Activity lists a "Move to Spark" transfer row and the 50 000-sat Lightning payment</action>
<action>Open Settings › Advanced › Lightning Connections and verify the channel is closing or closed</action>
Expand Down
Loading