Skip to content

Commit c1ff89e

Browse files
committed
fix: keep the key picker on screen while NFC and location keys check
Cancelling a key prompt showed the picker only after a delay: it was presented from scratch once Apple's NFC sheet had closed, or once the location sheet had finished sliding away. - NFC: when the picker has other choices, it stays up under Apple's sheet, so Cancel uncovers it. - Location: the check is now a screen inside the key sheet, like the QR scanner, so closing it goes straight back to the picker. This removes the hand-off between the two sheets.
1 parent 3efa703 commit c1ff89e

5 files changed

Lines changed: 84 additions & 113 deletions

File tree

‎Normal/Services/Keys/KeyCheckFlow.swift‎

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,19 @@ final class KeyCheckFlow {
2626
case confirmingBypass
2727
case scanningQR
2828
case scanningNFC
29-
case openingLocation
3029
case checkingLocation
31-
case closingLocation
32-
33-
var showsKeySheet: Bool {
34-
switch self {
35-
case .choosing, .confirmingBypass, .scanningQR: true
36-
default: false
37-
}
38-
}
3930
}
4031

4132
enum SheetScreen: Equatable {
4233
case picker
4334
case bypassConfirm
4435
case qrScanner
36+
case location
37+
}
38+
39+
struct LocationCheck {
40+
let requestID: UUID
41+
let keys: [Key]
4542
}
4643

4744
private struct Scan {
@@ -54,11 +51,20 @@ final class KeyCheckFlow {
5451
private(set) var allowsBypass = false
5552
private(set) var sheetScreen: SheetScreen = .picker
5653
private(set) var finishedRequestID: UUID?
54+
private(set) var locationCheck: LocationCheck?
5755

5856
var canReturnToPicker: Bool {
5957
allowsBypass || (request?.availableKeyTypes.count ?? 0) > 1
6058
}
6159

60+
var showsKeySheet: Bool {
61+
switch stage {
62+
case .choosing, .confirmingBypass, .scanningQR, .checkingLocation: true
63+
case .scanningNFC: canReturnToPicker
64+
case .idle, .noKeys: false
65+
}
66+
}
67+
6268
private let nfc: any KeyScanning
6369
private let qr: any QRScanning
6470
@ObservationIgnored private var scan: Scan?
@@ -121,8 +127,8 @@ final class KeyCheckFlow {
121127
check(QRKeyMethod(qrService: qr, keys: request.keys), keyType: .qr)
122128
}
123129

124-
func qrScannerCancelled() {
125-
guard stage == .scanningQR else { return }
130+
func promptCancelled() {
131+
guard stage == .scanningQR || stage == .checkingLocation else { return }
126132
promptEnded()
127133
}
128134

@@ -132,40 +138,20 @@ final class KeyCheckFlow {
132138
}
133139

134140
func keySheetClosed() {
135-
guard stage.showsKeySheet else { return }
141+
guard showsKeySheet else { return }
136142
cancel()
137143
}
138144

139145
func keySheetDismissed() {
140-
switch stage {
141-
case .openingLocation:
142-
stage = .checkingLocation
143-
case let current where current.showsKeySheet:
144-
break
145-
default:
146-
if qr.isScanning {
147-
qr.cancel()
148-
}
149-
if scan?.keyType == .qr {
150-
scan = nil
151-
}
146+
guard !showsKeySheet else { return }
147+
if qr.isScanning {
148+
qr.cancel()
152149
}
153-
}
154-
155-
func locationSheetClosed() {
156-
guard stage == .checkingLocation else { return }
157-
if canReturnToPicker {
158-
stage = .closingLocation
159-
} else {
160-
cancel()
150+
if scan?.keyType == .qr {
151+
scan = nil
161152
}
162153
}
163154

164-
func locationSheetDismissed() {
165-
guard stage == .closingLocation else { return }
166-
showPicker()
167-
}
168-
169155
func cancel() {
170156
stopScan()
171157
finish()
@@ -184,13 +170,16 @@ final class KeyCheckFlow {
184170
guard let request else { return }
185171
switch keyType {
186172
case .nfc:
173+
sheetScreen = .picker
187174
stage = .scanningNFC
188175
check(NFCKeyMethod(nfcService: nfc, keys: request.keys), keyType: .nfc)
189176
case .qr:
190177
sheetScreen = .qrScanner
191178
stage = .scanningQR
192179
case .location:
193-
stage = stage.showsKeySheet ? .openingLocation : .checkingLocation
180+
locationCheck = LocationCheck(requestID: request.id, keys: request.keys)
181+
sheetScreen = .location
182+
stage = .checkingLocation
194183
}
195184
}
196185

‎Normal/Views/BlockUnblockViews/KeySelect/KeySelectModifier.swift‎

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ struct KeySelectModifier: ViewModifier {
1919
let id: UUID
2020
}
2121

22-
private struct LocationCheck: Identifiable {
23-
let id: UUID
24-
let keys: [Key]
25-
}
26-
2722
private struct Trigger: Equatable {
2823
let hasAction: Bool
2924
let requestID: UUID?
@@ -55,13 +50,6 @@ struct KeySelectModifier: ViewModifier {
5550
.sheet(item: keySheet, onDismiss: flow.keySheetDismissed) { _ in
5651
keySelectSheet
5752
}
58-
.sheet(item: locationSheet, onDismiss: flow.locationSheetDismissed) { check in
59-
LocationUnlockSheet(
60-
keys: check.keys,
61-
provider: locationService,
62-
onVerified: { flow.locationVerified(requestID: check.id) }
63-
)
64-
}
6553
}
6654

6755
private var keySelectSheet: some View {
@@ -85,18 +73,29 @@ struct KeySelectModifier: ViewModifier {
8573
.navigationBarBackButtonHidden()
8674
.toolbar {
8775
ToolbarItem(placement: .cancellationAction) {
88-
Button("Cancel", action: flow.qrScannerCancelled)
76+
Button("Cancel", action: flow.promptCancelled)
8977
}
9078
}
9179
.onAppear(perform: flow.scannerAppeared)
9280
}
81+
.navigationDestination(isPresented: screen(.location, onBack: {})) {
82+
if let check = flow.locationCheck {
83+
LocationUnlockView(
84+
keys: check.keys,
85+
provider: locationService,
86+
onVerified: { flow.locationVerified(requestID: check.requestID) },
87+
onClose: flow.promptCancelled
88+
)
89+
.id(check.requestID)
90+
}
91+
}
9392
.toolbar {
9493
ToolbarItem(placement: .cancellationAction) {
9594
Button("Cancel", action: flow.cancel)
9695
}
9796
}
9897
}
99-
.presentationDetents(flow.sheetScreen == .qrScanner ? [.large] : [.medium])
98+
.presentationDetents([.qrScanner, .location].contains(flow.sheetScreen) ? [.large] : [.medium])
10099
.presentationDragIndicator(flow.sheetScreen == .bypassConfirm ? .visible : .hidden)
101100
}
102101

@@ -109,21 +108,11 @@ struct KeySelectModifier: ViewModifier {
109108

110109
private var keySheet: Binding<SheetID?> {
111110
Binding(
112-
get: { flow.stage.showsKeySheet ? flow.request.map { SheetID(id: $0.id) } : nil },
111+
get: { flow.showsKeySheet ? flow.request.map { SheetID(id: $0.id) } : nil },
113112
set: { if $0 == nil { flow.keySheetClosed() } }
114113
)
115114
}
116115

117-
private var locationSheet: Binding<LocationCheck?> {
118-
Binding(
119-
get: {
120-
guard flow.stage == .checkingLocation, let request = flow.request else { return nil }
121-
return LocationCheck(id: request.id, keys: request.keys)
122-
},
123-
set: { if $0 == nil { flow.locationSheetClosed() } }
124-
)
125-
}
126-
127116
private func screen(_ screen: KeyCheckFlow.SheetScreen, onBack: @escaping () -> Void) -> Binding<Bool> {
128117
Binding(
129118
get: { flow.sheetScreen == screen },

Normal/Views/BlockUnblockViews/KeySelect/LocationUnlockSheet.swift renamed to Normal/Views/BlockUnblockViews/KeySelect/LocationUnlockView.swift

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,55 @@
11
import MapKit
22
import SwiftUI
33

4-
struct LocationUnlockSheet: View {
5-
@Environment(\.dismiss) private var dismiss
4+
struct LocationUnlockView: View {
65
@State private var model: LocationUnlockModel
76
@State private var cameraPosition: MapCameraPosition = .automatic
87
@Namespace private var mapScope
98
private let onVerified: () -> Void
9+
private let onClose: () -> Void
1010

11-
init(keys: [Key], provider: any LocationProviding, onVerified: @escaping () -> Void) {
11+
init(
12+
keys: [Key],
13+
provider: any LocationProviding,
14+
onVerified: @escaping () -> Void,
15+
onClose: @escaping () -> Void
16+
) {
1217
_model = State(initialValue: LocationUnlockModel(keys: keys, provider: provider))
1318
self.onVerified = onVerified
19+
self.onClose = onClose
1420
}
1521

1622
private var kind: LocationRadiusKind {
1723
model.kind
1824
}
1925

2026
var body: some View {
21-
NavigationStack {
22-
VStack(spacing: 0) {
23-
map
24-
statusBar
25-
}
26-
.navigationTitle("Location Key")
27-
.navigationBarTitleDisplayMode(.inline)
28-
.toolbarBackground(.visible, for: .navigationBar)
29-
.toolbar {
30-
ToolbarItem(placement: .confirmationAction) {
31-
Button { dismiss() } label: {
32-
Image(systemName: "xmark.circle.fill")
33-
.font(.title2)
34-
.symbolRenderingMode(.hierarchical)
35-
.foregroundStyle(.secondary)
36-
}
37-
.accessibilityLabel("Close")
38-
.accessibilityIdentifier("locationUnlock.close")
27+
VStack(spacing: 0) {
28+
map
29+
statusBar
30+
}
31+
.navigationTitle("Location Key")
32+
.navigationBarTitleDisplayMode(.inline)
33+
.navigationBarBackButtonHidden()
34+
.toolbarBackground(.visible, for: .navigationBar)
35+
.toolbar {
36+
ToolbarItem(placement: .confirmationAction) {
37+
Button(action: onClose) {
38+
Image(systemName: "xmark.circle.fill")
39+
.font(.title2)
40+
.symbolRenderingMode(.hierarchical)
41+
.foregroundStyle(.secondary)
3942
}
43+
.accessibilityLabel("Close")
44+
.accessibilityIdentifier("locationUnlock.close")
4045
}
4146
}
42-
.presentationDetents([.large])
4347
.task {
4448
await model.run()
4549
guard model.phase == .verified else { return }
4650
try? await Task.sleep(for: .milliseconds(350))
4751
guard !Task.isCancelled else { return }
4852
onVerified()
49-
dismiss()
5053
}
5154
}
5255

‎NormalTests/Services/KeyCheckFlowTests.swift‎

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ struct KeyCheckFlowTests {
9797

9898
@Test func rejectedTagEndsWithoutTheAction() async {
9999
start(request([key(.nfc, "tag")], available: [.nfc]))
100+
#expect(!flow.showsKeySheet)
100101
await waitUntil { nfc.isScanning }
101102

102103
nfc.reject()
@@ -108,6 +109,8 @@ struct KeyCheckFlowTests {
108109
@Test func cancelledDefaultNFCScanShowsThePicker() async {
109110
let request = request([key(.nfc, "tag"), key(.qr, "code")], available: [.nfc, .qr], defaultKeyType: .nfc)
110111
start(request)
112+
#expect(flow.showsKeySheet)
113+
#expect(flow.sheetScreen == .picker)
111114
await waitUntil { nfc.isScanning }
112115

113116
nfc.cancel()
@@ -217,7 +220,7 @@ struct KeyCheckFlowTests {
217220
flow.scannerAppeared()
218221
await waitUntil { qr.isScanning }
219222

220-
flow.qrScannerCancelled()
223+
flow.promptCancelled()
221224

222225
#expect(!qr.isScanning)
223226
#expect(flow.stage == .choosing)
@@ -238,7 +241,7 @@ struct KeyCheckFlowTests {
238241
flow.scannerAppeared()
239242
await waitUntil { qr.isScanning }
240243

241-
flow.qrScannerCancelled()
244+
flow.promptCancelled()
242245

243246
#expect(!qr.isScanning)
244247
#expect(flow.stage == .idle)
@@ -324,14 +327,15 @@ struct KeyCheckFlowTests {
324327
#expect(flow.stage == .choosing)
325328
}
326329

327-
@Test func locationOpensAfterThePickerCloses() {
328-
start(request([key(.qr, "code")], available: [.qr, .location]))
330+
@Test func choosingLocationShowsItInTheKeySheet() {
331+
let request = request([key(.qr, "code")], available: [.qr, .location])
332+
start(request)
329333
flow.choose(.location)
330-
#expect(flow.stage == .openingLocation)
331-
332-
flow.keySheetDismissed()
333334

334335
#expect(flow.stage == .checkingLocation)
336+
#expect(flow.sheetScreen == .location)
337+
#expect(flow.showsKeySheet)
338+
#expect(flow.locationCheck?.requestID == request.id)
335339
}
336340

337341
@Test func locationVerificationOnlyCountsForTheCurrentRequest() throws {
@@ -348,44 +352,30 @@ struct KeyCheckFlowTests {
348352
#expect(flow.stage == .idle)
349353
}
350354

351-
@Test func closingTheLocationSheetCancels() {
355+
@Test func closingTheOnlyLocationKeyEndsTheFlow() {
352356
let request = request([], available: [.location], defaultKeyType: .location)
353357
start(request)
354358

355-
flow.locationSheetClosed()
359+
flow.promptCancelled()
356360

357361
#expect(flow.stage == .idle)
358362
#expect(flow.finishedRequestID == request.id)
359363
#expect(counter.runs == 0)
360364
}
361365

362-
@Test func closingTheDefaultLocationSheetShowsThePickerOnceItCloses() {
366+
@Test func closingTheDefaultLocationKeyShowsThePicker() {
363367
let request = request([key(.qr, "code")], available: [.qr, .location], defaultKeyType: .location)
364368
start(request)
365-
#expect(flow.stage == .checkingLocation)
369+
#expect(flow.sheetScreen == .location)
366370

367-
flow.locationSheetClosed()
368-
#expect(flow.stage == .closingLocation)
371+
flow.promptCancelled()
369372

370-
flow.locationSheetDismissed()
371373
#expect(flow.stage == .choosing)
372374
#expect(flow.sheetScreen == .picker)
373375
#expect(flow.finishedRequestID == nil)
374376
#expect(counter.runs == 0)
375377
}
376378

377-
@Test func verifiedLocationDoesNotReopenThePicker() {
378-
let request = request([key(.qr, "code")], available: [.qr, .location], defaultKeyType: .location)
379-
start(request)
380-
381-
flow.locationVerified(requestID: request.id)
382-
flow.locationSheetClosed()
383-
flow.locationSheetDismissed()
384-
385-
#expect(counter.runs == 1)
386-
#expect(flow.stage == .idle)
387-
}
388-
389379
@Test func closingTheSheetCancelsTheScan() async {
390380
start(qrAndNFC)
391381
flow.choose(.qr)

0 commit comments

Comments
 (0)