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
2 changes: 0 additions & 2 deletions Normal/App/AppContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ struct AppContainer: View {
@State private var nfcService = NFCService.shared
@State private var qrService = QRService.shared
@State private var locationService = LocationService.shared
@State private var keyManager = KeyManager()
@State private var scheduleService: ScheduleService
@State private var onboardingService = OnboardingService()
@State private var appReviewService: AppReviewService
Expand All @@ -31,7 +30,6 @@ struct AppContainer: View {
.environment(nfcService)
.environment(qrService)
.environment(locationService)
.environment(keyManager)
.environment(timedUnblockService)
.environment(scheduleService)
.environment(onboardingService)
Expand Down
9 changes: 9 additions & 0 deletions Normal/App/NormalApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ struct NormalApp: App {
context.insert(group)
context.insert(Key(name: "Group Key", type: .qr, rawValue: "GROUP-VALUE", groupID: group.id))
}
if UITestSupport.seedLocationKey {
context.insert(Key(
name: "Test Place",
latitude: 37.33182,
longitude: -122.03118,
radiusMeters: 400,
radiusKind: .unblock
))
}
if UITestSupport.seedSchedule {
context.insert(BlockSchedule(
name: "Test Schedule",
Expand Down
2 changes: 2 additions & 0 deletions Normal/App/UITestSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ enum UITestSupport {

static let seedGroupKey = arguments.contains("-uiTestSeedGroupKey")

static let seedLocationKey = arguments.contains("-uiTestSeedLocationKey")

static let seedGroupID = UUID(uuidString: "6E0A1C9B-2D4F-4B7A-9C3E-5F8D1A2B3C4D")!

static let skipBypassConfirm = arguments.contains("-uiTestSkipBypassConfirm")
Expand Down
52 changes: 29 additions & 23 deletions Normal/Modifiers/WidgetActionResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ private struct WidgetActionResponder: ViewModifier {
@Binding var selectedTab: AppTab

@State private var authAction: (@MainActor () -> Void)?
@State private var actionGroup: AppGroup?
@State private var keyType: KeyType?
@State private var allowBypass = false
@State private var requestID: UUID?
@State private var active: ActiveRequest?
@State private var heldRequest: GroupActionRequest?
@State private var durationTarget: UnblockTarget?

private struct ActiveRequest {
let token: UUID
let group: AppGroup
let keyType: KeyType?
let allowBypass: Bool
}

private var unblockDurations: [TimedUnblockDuration] {
allSettings.first?.unblockDurations ?? TimedUnblockDuration.presets
}
Expand All @@ -27,12 +32,12 @@ private struct WidgetActionResponder: ViewModifier {
content
.protectedAction(
$authAction,
allowBypass: allowBypass,
defaultKeyType: keyType,
keyGroupID: actionGroup?.id,
requestID: requestID
allowBypass: active?.allowBypass ?? false,
defaultKeyType: active?.keyType,
keyGroupID: active?.group.id,
requestID: active?.token
)
.sheet(item: $durationTarget) { target in
.sheet(item: $durationTarget, onDismiss: replayHeldRequest) { target in
UnblockDurationSheet(target: target)
}
.onOpenURL { coordinator.handle(url: $0) }
Expand All @@ -59,30 +64,31 @@ private struct WidgetActionResponder: ViewModifier {
}

private func resolve(_ request: GroupActionRequest?) {
guard let request,
durationTarget == nil,
allSettings.first?.hasCompletedOnboarding == true,
let group = groups.first(where: { $0.id == request.groupID })
else {
coordinator.clearPendingGroupAction()
guard let request else { return }
coordinator.clearPendingGroupAction()
guard durationTarget == nil else {
heldRequest = request
return
}
guard allSettings.first?.hasCompletedOnboarding == true,
let group = groups.first(where: { $0.id == request.groupID })
else { return }
selectedTab = .groups
actionGroup = group
requestID = request.token

switch request.action {
case let .unlock(duration, requestedKeyType):
keyType = requestedKeyType
allowBypass = false
case let .unlock(duration, keyType):
active = ActiveRequest(token: request.token, group: group, keyType: keyType, allowBypass: false)
authAction = { unlock(group, duration: duration) }
case .block:
keyType = nil
allowBypass = true
active = ActiveRequest(token: request.token, group: group, keyType: nil, allowBypass: true)
authAction = { block(group) }
}
}

coordinator.clearPendingGroupAction()
private func replayHeldRequest() {
guard let held = heldRequest else { return }
heldRequest = nil
resolve(held)
}

private func unlock(_ group: AppGroup, duration request: UnlockDurationRequest) {
Expand Down
204 changes: 204 additions & 0 deletions Normal/Services/Keys/KeyCheckFlow.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
import Foundation
import Observation

protocol QRScanning: KeyScanning {
var isScanning: Bool { get }
func cancel()
}

extension QRService: QRScanning {}

@Observable
final class KeyCheckFlow {
struct Request {
let id = UUID()
let keys: [Key]
let availableKeyTypes: [KeyType]
let allowBypass: Bool
let defaultKeyType: KeyType?
}

enum Stage: Equatable {
case idle
case noKeys
case choosing
case confirmingBypass
case scanningQR
case scanningNFC
case openingLocation
case checkingLocation

var showsKeySheet: Bool {
switch self {
case .choosing, .confirmingBypass, .scanningQR: true
default: false
}
}
}

enum SheetScreen: Equatable {
case picker
case bypassConfirm
case qrScanner
}

private(set) var stage: Stage = .idle
private(set) var request: Request?
private(set) var sheetScreen: SheetScreen = .picker
private(set) var finishedRequestID: UUID?

private let nfc: any KeyScanning
private let qr: any QRScanning
@ObservationIgnored private var qrScanRequestID: UUID?
@ObservationIgnored private var action: (@MainActor () -> Void)?

init(nfc: any KeyScanning, qr: any QRScanning) {
self.nfc = nfc
self.qr = qr
}

func start(_ request: Request, action: (@MainActor () -> Void)?) {
stopQRScan()
self.request = request
self.action = action
switch KeySelectLogic.decide(
availableKeyTypes: request.availableKeyTypes,
allowBypass: request.allowBypass,
defaultKeyType: request.defaultKeyType
) {
case .showNoKeysAlert:
stage = .noKeys
case let .autoSelect(keyType):
begin(keyType)
case .showSheet:
sheetScreen = .picker
stage = .choosing
}
}

func choose(_ keyType: KeyType) {
guard stage == .choosing else { return }
begin(keyType)
}

func requestBypass(skippingConfirmation: Bool) {
guard stage == .choosing, request?.allowBypass == true else { return }
if skippingConfirmation {
complete()
} else {
sheetScreen = .bypassConfirm
stage = .confirmingBypass
}
}

func confirmBypass() {
guard stage == .confirmingBypass, request?.allowBypass == true else { return }
complete()
}

func returnFromBypassConfirm() {
guard stage == .confirmingBypass else { return }
sheetScreen = .picker
stage = .choosing
}

func scannerAppeared() {
guard stage == .scanningQR, qrScanRequestID == nil, let request else { return }
qrScanRequestID = request.id
check(QRKeyMethod(qrService: qr, keys: request.keys), for: request)
}

func locationVerified(requestID: UUID) {
guard stage == .checkingLocation, request?.id == requestID else { return }
complete()
}

func keySheetClosed() {
guard stage.showsKeySheet else { return }
cancel()
}

func keySheetDismissed() {
switch stage {
case .openingLocation:
stage = .checkingLocation
case let current where current.showsKeySheet:
break
default:
if qr.isScanning {
qr.cancel()
}
qrScanRequestID = nil
}
}

func locationSheetClosed() {
guard stage == .checkingLocation else { return }
cancel()
}

func cancel() {
stopQRScan()
finish()
}

func hostCleared() {
guard stage != .idle else { return }
stopQRScan()
stage = .idle
request = nil
action = nil
}

private func begin(_ keyType: KeyType) {
guard let request else { return }
switch keyType {
case .nfc:
stage = .scanningNFC
check(NFCKeyMethod(nfcService: nfc, keys: request.keys), for: request)
case .qr:
sheetScreen = .qrScanner
stage = .scanningQR
case .location:
stage = stage.showsKeySheet ? .openingLocation : .checkingLocation
}
}

private func check(_ method: some KeyMethod, for request: Request) {
let requestID = request.id
Task {
let result = await method.checkKey()
scanFinished(result, requestID: requestID)
}
}

private func scanFinished(_ result: KeyResult, requestID: UUID) {
if qrScanRequestID == requestID {
qrScanRequestID = nil
}
guard request?.id == requestID else { return }
if result == .success {
complete()
} else {
finish()
}
}

private func complete() {
action?()
finish()
}

private func finish() {
finishedRequestID = request?.id
stage = .idle
request = nil
action = nil
}

private func stopQRScan() {
guard qrScanRequestID != nil else { return }
qrScanRequestID = nil
qr.cancel()
}
}
20 changes: 0 additions & 20 deletions Normal/Services/Keys/KeyManager.swift

This file was deleted.

Loading
Loading