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
15 changes: 9 additions & 6 deletions Normal/Intents/AppUnblockIntentPerformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ final class AppUnblockIntentPerformer: UnblockIntentPerforming {

let location = await locationVerifier.check(keys: keys)
try Task.checkCancellation()
let proof: KeyProof
switch location {
case .verified:
break
proof = KeyProof(keyGroupID: request.groupID)
case .outOfRange:
throw UnblockIntentError.notAtLocation(radiusKind)
case .needsAlways:
Expand All @@ -74,8 +75,10 @@ final class AppUnblockIntentPerformer: UnblockIntentPerforming {
case let .settled(outcome):
return outcome
case let .ready(settings, target):
guard let length = settings.unblockLength(for: request.duration) else { return .continueInApp(.checkKey) }
return try start(target, for: length, settings: settings)
guard let length = settings.unblockLength(for: request.duration),
let grant = UnblockGrant(target, proof: proof)
else { return .continueInApp(.checkKey) }
return try start(grant, for: length, settings: settings)
}
}

Expand Down Expand Up @@ -107,10 +110,10 @@ final class AppUnblockIntentPerformer: UnblockIntentPerforming {
return .ready(settings, target)
}

private func start(_ target: UnblockTarget, for length: UnblockLength, settings: Settings) throws -> UnblockOutcome {
private func start(_ grant: UnblockGrant, for length: UnblockLength, settings: Settings) throws -> UnblockOutcome {
do {
try BlockActions.unblock(
target,
grant,
for: length,
settings: settings,
screenTimeService: intents.screenTimeService,
Expand All @@ -123,7 +126,7 @@ final class AppUnblockIntentPerformer: UnblockIntentPerforming {
if syncsExternalSurfaces {
intents.syncExternalSurfaces(settings: settings)
}
return .unblocked(name: target.name, length: length)
return .unblocked(name: grant.target.name, length: length)
}

private func keyType(for request: UnblockRequest, keys: [Key], settings: Settings?) -> KeyType? {
Expand Down
8 changes: 2 additions & 6 deletions Normal/Modifiers/ProtectedActionModifier.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import SwiftUI

struct ProtectedActionModifier: ViewModifier {
@Binding var action: (@MainActor () -> Void)?
var allowBypass: Bool
@Binding var action: ProtectedAction?
var defaultKeyType: KeyType?
var requestedKeyType: KeyType?
var keyGroupID: UUID?
Expand All @@ -13,7 +12,6 @@ struct ProtectedActionModifier: ViewModifier {
.screenTimeGuard(action: $action)
.keySelect(
action: $action,
allowBypass: allowBypass,
defaultKeyType: defaultKeyType,
requestedKeyType: requestedKeyType,
keyGroupID: keyGroupID,
Expand All @@ -24,16 +22,14 @@ struct ProtectedActionModifier: ViewModifier {

extension View {
func protectedAction(
_ action: Binding<(@MainActor () -> Void)?>,
allowBypass: Bool = false,
_ action: Binding<ProtectedAction?>,
defaultKeyType: KeyType? = nil,
requestedKeyType: KeyType? = nil,
keyGroupID: UUID? = nil,
requestID: UUID? = nil
) -> some View {
modifier(ProtectedActionModifier(
action: action,
allowBypass: allowBypass,
defaultKeyType: defaultKeyType,
requestedKeyType: requestedKeyType,
keyGroupID: keyGroupID,
Expand Down
6 changes: 3 additions & 3 deletions Normal/Modifiers/ScreenTimeGuardModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import SwiftUI

struct ScreenTimeGuardModifier: ViewModifier {
@Environment(ScreenTimeService.self) private var screenTimeService
@Binding var action: (@MainActor () -> Void)?
@State private var deferred: (@MainActor () -> Void)?
@Binding var action: ProtectedAction?
@State private var deferred: ProtectedAction?

func body(content: Content) -> some View {
content.onChange(of: action != nil) { _, hasAction in
Expand All @@ -24,7 +24,7 @@ struct ScreenTimeGuardModifier: ViewModifier {
}

extension View {
func screenTimeGuard(action: Binding<(@MainActor () -> Void)?>) -> some View {
func screenTimeGuard(action: Binding<ProtectedAction?>) -> some View {
modifier(ScreenTimeGuardModifier(action: action))
}
}
29 changes: 14 additions & 15 deletions Normal/Modifiers/WidgetActionResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ private struct WidgetActionResponder: ViewModifier {
let coordinator: NavigationCoordinator
@Binding var selectedTab: AppTab

@State private var authAction: (@MainActor () -> Void)?
@State private var authAction: ProtectedAction?
@State private var active: ActiveRequest?
@State private var heldRequest: ActionRequest?
@State private var durationTarget: UnblockTarget?
@State private var durationGrant: UnblockGrant?

private struct ActiveRequest {
let token: UUID
let target: UnblockTarget
let keyType: KeyType?
let allowBypass: Bool
}

private var unblockDurations: [TimedUnblockDuration] {
Expand All @@ -35,13 +34,12 @@ private struct WidgetActionResponder: ViewModifier {
content
.protectedAction(
$authAction,
allowBypass: active?.allowBypass ?? false,
requestedKeyType: active?.keyType,
keyGroupID: active?.target.groupID,
requestID: active?.token
)
.sheet(item: $durationTarget, onDismiss: durationSheetDismissed) { target in
UnblockDurationSheet(target: target)
.sheet(item: $durationGrant, onDismiss: durationSheetDismissed) { grant in
UnblockDurationSheet(grant: grant)
}
.onOpenURL { coordinator.handle(url: $0) }
.onChange(of: coordinator.pendingAction) { _, request in resolve(request) }
Expand Down Expand Up @@ -72,7 +70,7 @@ private struct WidgetActionResponder: ViewModifier {
private func takeInboxRequest() {
guard scenePhase == .active,
screenTimeService.authorizationState == .authorized,
durationTarget == nil,
durationGrant == nil,
let request = unlockInbox.take(),
let settings = allSettings.first,
let target = target(for: request.groupID),
Expand All @@ -92,7 +90,7 @@ private struct WidgetActionResponder: ViewModifier {
private func resolve(_ request: ActionRequest?) {
guard let request else { return }
coordinator.clearPendingAction()
guard durationTarget == nil else {
guard durationGrant == nil else {
heldRequest = request
return
}
Expand All @@ -101,12 +99,12 @@ private struct WidgetActionResponder: ViewModifier {
switch request.action {
case let .unblock(unblock):
guard let target = target(for: unblock.groupID) else { return }
present(ActiveRequest(token: request.token, target: target, keyType: unblock.keyType, allowBypass: false))
authAction = { unlock(target, duration: unblock.duration) }
present(ActiveRequest(token: request.token, target: target, keyType: unblock.keyType))
authAction = .weaken { unlock(target, duration: unblock.duration, proof: $0) }
case let .blockGroup(groupID):
guard let group = groups.first(where: { $0.id == groupID }) else { return }
present(ActiveRequest(token: request.token, target: .group(group), keyType: nil, allowBypass: true))
authAction = { block(group) }
present(ActiveRequest(token: request.token, target: .group(group), keyType: nil))
authAction = .tighten { block(group) }
}
}

Expand All @@ -128,13 +126,14 @@ private struct WidgetActionResponder: ViewModifier {
active = request
}

private func unlock(_ target: UnblockTarget, duration request: UnlockDurationRequest) {
private func unlock(_ target: UnblockTarget, duration request: UnlockDurationRequest, proof: KeyProof) {
guard let grant = UnblockGrant(target, proof: proof) else { return }
guard let settings = allSettings.first, let length = settings.unblockLength(for: request) else {
durationTarget = target
durationGrant = grant
return
}
try? BlockActions.unblock(
target,
grant,
for: length,
settings: settings,
screenTimeService: screenTimeService,
Expand Down
6 changes: 3 additions & 3 deletions Normal/Services/BlockActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ enum BlockActions {
}

static func unblock(
_ target: UnblockTarget,
_ grant: UnblockGrant,
for length: UnblockLength,
settings: Settings,
screenTimeService: any ScreenTimeProviding,
Expand All @@ -38,13 +38,13 @@ enum BlockActions {
switch length {
case let .timed(duration):
try timedUnblockService.start(
target,
grant,
duration: duration,
settings: settings,
screenTimeService: screenTimeService
)
case .permanent:
screenTimeService.unblockPermanently(target, settings: settings)
screenTimeService.unblockPermanently(grant, settings: settings)
}
}

Expand Down
32 changes: 18 additions & 14 deletions Normal/Services/Keys/KeyCheckFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ final class KeyCheckFlow {
struct Request {
let id = UUID()
let keys: [Key]
let keyGroupID: UUID?
let availableKeyTypes: [KeyType]
let allowBypass: Bool
let defaultKeyType: KeyType?
var requestedKeyType: KeyType?
}
Expand Down Expand Up @@ -45,26 +45,28 @@ final class KeyCheckFlow {

private(set) var stage: Stage = .idle
private(set) var request: Request?
private(set) var allowsBypass = false
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)?
@ObservationIgnored private var action: ProtectedAction?

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

func start(_ request: Request, action: (@MainActor () -> Void)?) {
func start(_ request: Request, action: ProtectedAction) {
stopQRScan()
self.request = request
self.action = action
allowsBypass = action.allowsBypass
switch KeySelectLogic.decide(
availableKeyTypes: request.availableKeyTypes,
allowBypass: request.allowBypass,
allowBypass: allowsBypass,
defaultKeyType: request.defaultKeyType,
requestedKeyType: request.requestedKeyType
) {
Expand All @@ -84,18 +86,18 @@ final class KeyCheckFlow {
}

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

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

func returnFromBypassConfirm() {
Expand All @@ -111,8 +113,8 @@ final class KeyCheckFlow {
}

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

func keySheetClosed() {
Expand Down Expand Up @@ -150,6 +152,7 @@ final class KeyCheckFlow {
stage = .idle
request = nil
action = nil
allowsBypass = false
}

private func begin(_ keyType: KeyType) {
Expand Down Expand Up @@ -178,16 +181,16 @@ final class KeyCheckFlow {
if qrScanRequestID == requestID {
qrScanRequestID = nil
}
guard request?.id == requestID else { return }
guard let request, request.id == requestID else { return }
if result == .success {
complete()
complete(with: KeyProof(keyGroupID: request.keyGroupID))
} else {
finish()
}
}

private func complete() {
action?()
private func complete(with proof: KeyProof?) {
action?.run(with: proof)
finish()
}

Expand All @@ -196,6 +199,7 @@ final class KeyCheckFlow {
stage = .idle
request = nil
action = nil
allowsBypass = false
}

private func stopQRScan() {
Expand Down
26 changes: 26 additions & 0 deletions Normal/Services/Keys/KeyProof.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Foundation

struct KeyProof: Equatable {
let keyGroupID: UUID?

var coversAllApps: Bool {
keyGroupID == nil
}

func covers(_ target: UnblockTarget) -> Bool {
coversAllApps || keyGroupID == target.groupID
}
}

struct UnblockGrant: Identifiable {
let target: UnblockTarget

init?(_ target: UnblockTarget, proof: KeyProof) {
guard proof.covers(target) else { return nil }
self.target = target
}

var id: String {
target.id
}
}
24 changes: 24 additions & 0 deletions Normal/Services/Keys/ProtectedAction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Foundation

enum ProtectedAction {
case tighten(@MainActor () -> Void)
case weaken(@MainActor (KeyProof) -> Void)

var allowsBypass: Bool {
if case .tighten = self {
return true
}
return false
}

func run(with proof: KeyProof?) {
switch self {
case let .tighten(action):
action()
case let .weaken(action):
if let proof {
action(proof)
}
}
}
}
2 changes: 1 addition & 1 deletion Normal/Services/ScreenTimeProviding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protocol ScreenTimeProviding: AnyObject {
func requestAuthorization() async
func ensureAuthorized() async -> Bool
func enablePreventAppDelete()
func disablePreventAppDelete()
func disablePreventAppDelete(_ proof: KeyProof)
func blockAll(
selection: FamilyActivitySelection,
customDomains: [String],
Expand Down
Loading
Loading