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
17 changes: 10 additions & 7 deletions Normal/Intents/AppUnblockIntentPerformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ final class AppUnblockIntentPerformer: UnblockIntentPerforming {
func unblock(_ request: UnblockRequest) async throws -> UnblockOutcome {
logger.info("Unblock intent: \(request.groupID == nil ? "all" : "group", privacy: .public)")
let settings: Settings
let target: UnblockTarget
switch try readiness(for: request) {
case let .settled(outcome):
return outcome
case let .ready(readySettings, _):
case let .ready(readySettings, readyTarget):
settings = readySettings
target = readyTarget
}

let keys = try Key.scoped(intents.keys(), toGroup: request.groupID)
guard keyType(for: request, keys: keys, settings: settings) == .location,
settings.unblockLength(for: request.duration) != nil
guard keyType(for: request, keys: keys, settings: settings, group: target.group) == .location,
settings.unblockLength(for: request.duration, group: target.group) != nil
else { return .continueInApp(.checkKey) }
let radiusKind = Key.existingLocationKind(in: keys) ?? .unblock

Expand All @@ -75,7 +77,7 @@ final class AppUnblockIntentPerformer: UnblockIntentPerforming {
case let .settled(outcome):
return outcome
case let .ready(settings, target):
guard let length = settings.unblockLength(for: request.duration),
guard let length = settings.unblockLength(for: request.duration, group: target.group),
let grant = UnblockGrant(target, proof: proof)
else { return .continueInApp(.checkKey) }
return try start(grant, for: length, settings: settings)
Expand All @@ -89,7 +91,8 @@ final class AppUnblockIntentPerformer: UnblockIntentPerforming {
keyType: keyType(
for: request,
keys: Key.scoped((try? intents.keys()) ?? [], toGroup: request.groupID),
settings: intents.settings()
settings: intents.settings(),
group: request.groupID.flatMap { try? intents.group(id: $0) }
)
))
}
Expand Down Expand Up @@ -129,10 +132,10 @@ final class AppUnblockIntentPerformer: UnblockIntentPerforming {
return .unblocked(name: grant.target.name, length: length)
}

private func keyType(for request: UnblockRequest, keys: [Key], settings: Settings?) -> KeyType? {
private func keyType(for request: UnblockRequest, keys: [Key], settings: Settings?, group: AppGroup?) -> KeyType? {
KeySelectLogic.shortcutKeyType(
requested: request.keyType,
defaultKeyType: settings?.defaultKeyType,
defaultKeyType: settings?.defaultKeyType(for: group),
availableKeyTypes: KeyType.selectable(registered: keys.map(\.type))
)
}
Expand Down
36 changes: 36 additions & 0 deletions Normal/Models/AppGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ final class AppGroup: Identifiable {
var lastUpdated: Date
var sortIndex: Int = 0
var customDomains: [String] = []
var overridesDefaultDuration: Bool = false
var defaultUnblockSeconds: Int?
var overridesDefaultKey: Bool = false
var defaultKeyType: KeyType?

init(id: UUID = UUID(), name: String, selection: FamilyActivitySelection, sortIndex: Int = 0, customDomains: [String] = []) {
self.id = id
Expand All @@ -20,6 +24,38 @@ final class AppGroup: Identifiable {
self.customDomains = customDomains
}

var durationOverride: DefaultOverride<TimedUnblockDuration> {
get {
guard overridesDefaultDuration else { return .appDefault }
return defaultUnblockSeconds
.flatMap(TimedUnblockDuration.init(validating:))
.map(DefaultOverride.value) ?? .none
}
set {
overridesDefaultDuration = newValue != .appDefault
if case let .value(duration) = newValue {
defaultUnblockSeconds = duration.seconds
} else {
defaultUnblockSeconds = nil
}
}
}

var keyOverride: DefaultOverride<KeyType> {
get {
guard overridesDefaultKey else { return .appDefault }
return defaultKeyType.map(DefaultOverride.value) ?? .none
}
set {
overridesDefaultKey = newValue != .appDefault
if case let .value(keyType) = newValue {
defaultKeyType = keyType
} else {
defaultKeyType = nil
}
}
}

func deleteCascading(keys: [Key], from context: ModelContext) {
for key in keys where key.groupID == id {
context.delete(key)
Expand Down
5 changes: 5 additions & 0 deletions Normal/Models/DefaultOverride.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum DefaultOverride<Value: Hashable>: Hashable {
case appDefault
case none
case value(Value)
}
17 changes: 17 additions & 0 deletions Normal/Models/Settings+GroupDefaults.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
extension Settings {
func defaultDuration(for group: AppGroup?) -> TimedUnblockDuration? {
switch group?.durationOverride ?? .appDefault {
case .appDefault: defaultDuration
case .none: nil
case let .value(duration): unblockDurations.contains(duration) ? duration : nil
}
}

func defaultKeyType(for group: AppGroup?) -> KeyType? {
switch group?.keyOverride ?? .appDefault {
case .appDefault: defaultKeyType
case .none: nil
case let .value(keyType): keyType
}
}
}
4 changes: 2 additions & 2 deletions Normal/Models/Settings+UnblockDurations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ extension Settings {
}
}

func unblockLength(for request: UnlockDurationRequest) -> UnblockLength? {
func unblockLength(for request: UnlockDurationRequest, group: AppGroup?) -> UnblockLength? {
switch request {
case let .fixed(duration): .timed(duration)
case .permanent: .permanent
case .useDefault: defaultDuration.map(UnblockLength.timed)
case .useDefault: defaultDuration(for: group).map(UnblockLength.timed)
case .ask: nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion Normal/Modifiers/WidgetActionResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private struct WidgetActionResponder: ViewModifier {

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 {
guard let settings = allSettings.first, let length = settings.unblockLength(for: request, group: target.group) else {
durationGrant = grant
return
}
Expand Down
8 changes: 8 additions & 0 deletions Normal/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,14 @@
"comment" : "Feature description for a feature row in the \"QR code or barcode examples\" card.",
"isCommentAutoGenerated" : true
},
"App Default" : {
"comment" : "The value shown in a group's Default Duration or Default Key row when it follows the default set in Settings.",
"isCommentAutoGenerated" : true
},
"App Default (%@)" : {
"comment" : "The group picker option that follows the default set in Settings. The argument is that default, such as a duration or key type, or None.",
"isCommentAutoGenerated" : true
},
"App Groups" : {
"comment" : "The title of a view that lists app groups.",
"isCommentAutoGenerated" : true
Expand Down
7 changes: 7 additions & 0 deletions Normal/Services/UnblockTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ enum UnblockTarget: Identifiable {
}
}

var group: AppGroup? {
if case let .group(group) = self {
return group
}
return nil
}

var groupID: UUID? {
if case let .group(group) = self {
return group.id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct UnblockDurationSheet: View {
TimedUnblockSheet(
title: title,
durations: settings.unblockDurations,
initialDuration: settings.defaultDuration,
initialDuration: settings.defaultDuration(for: grant.target.group),
onConfirm: { length in
let timing = try BlockActions.unblock(
grant,
Expand Down
120 changes: 118 additions & 2 deletions Normal/Views/Groups/GroupFormSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ struct GroupFormSheet: View {
@State private var name: String
@State private var selection: FamilyActivitySelection
@State private var customDomains: [String]
@State private var durationOverride: DefaultOverride<TimedUnblockDuration>
@State private var keyOverride: DefaultOverride<KeyType>
@State private var isShowingAppSelectSheet = false
@State private var showAddKey = false
@State private var showDeleteConfirmation = false
Expand All @@ -28,6 +30,50 @@ struct GroupFormSheet: View {
allKeys.filter { $0.groupID == groupID }
}

private var durations: [TimedUnblockDuration] {
allSettings.first?.unblockDurations ?? TimedUnblockDuration.presets
}

private var groupKeyTypes: [KeyType] {
let keys = Key.scoped(allKeys, toGroup: groupID)
return KeyType.allCases.filter { type in keys.contains { $0.type == type } }
}

private var appDefaultDurationLabel: String {
allSettings.first?.defaultDuration?.label ?? String(localized: "None")
}

private var appDefaultKeyLabel: String {
guard let keyType = allSettings.first?.defaultKeyType, groupKeyTypes.contains(keyType) else {
return String(localized: "None")
}
return keyType.label
}

private var durationSelection: Binding<DefaultOverride<TimedUnblockDuration>> {
Binding(
get: {
if case let .value(duration) = durationOverride, !durations.contains(duration) {
return .none
}
return durationOverride
},
set: { durationOverride = $0 }
)
}

private var keySelection: Binding<DefaultOverride<KeyType>> {
Binding(
get: {
if case let .value(keyType) = keyOverride, !groupKeyTypes.contains(keyType) {
return .none
}
return keyOverride
},
set: { keyOverride = $0 }
)
}

private var isNew: Bool {
existing == nil
}
Expand Down Expand Up @@ -63,6 +109,8 @@ struct GroupFormSheet: View {
_name = State(initialValue: existing?.name ?? "")
_selection = State(initialValue: existing?.selection ?? FamilyActivitySelection())
_customDomains = State(initialValue: existing?.customDomains ?? [])
_durationOverride = State(initialValue: existing?.durationOverride ?? .appDefault)
_keyOverride = State(initialValue: existing?.keyOverride ?? .appDefault)
}

var body: some View {
Expand Down Expand Up @@ -96,6 +144,8 @@ struct GroupFormSheet: View {
}
}

unblockingSection

if !groupKeys.isEmpty || !isReadOnly {
groupKeysSection
}
Expand Down Expand Up @@ -165,6 +215,67 @@ struct GroupFormSheet: View {
}
}

private var unblockingSection: some View {
Section("Unblocking") {
Group {
if #available(iOS 18.0, *) {
Picker("Default Duration", selection: durationSelection) {
durationOptions(appDefault: String(localized: "App Default (\(appDefaultDurationLabel))"))
} currentValueLabel: {
Text(rowLabel(for: durationSelection.wrappedValue) { $0.label })
}
} else {
Picker("Default Duration", selection: durationSelection) {
durationOptions(appDefault: String(localized: "App Default"))
}
}
}
.accessibilityIdentifier("groupForm.defaultDuration")

Group {
if #available(iOS 18.0, *) {
Picker("Default Key", selection: keySelection) {
keyOptions(appDefault: String(localized: "App Default (\(appDefaultKeyLabel))"))
} currentValueLabel: {
Text(rowLabel(for: keySelection.wrappedValue) { $0.label })
}
} else {
Picker("Default Key", selection: keySelection) {
keyOptions(appDefault: String(localized: "App Default"))
}
}
}
.accessibilityIdentifier("groupForm.defaultKey")
}
.disabled(isReadOnly)
}

@ViewBuilder
private func durationOptions(appDefault: String) -> some View {
Text(appDefault).tag(DefaultOverride<TimedUnblockDuration>.appDefault)
Text("None").tag(DefaultOverride<TimedUnblockDuration>.none)
ForEach(durations) { duration in
Text(duration.label).tag(DefaultOverride.value(duration))
}
}

@ViewBuilder
private func keyOptions(appDefault: String) -> some View {
Text(appDefault).tag(DefaultOverride<KeyType>.appDefault)
Text("None").tag(DefaultOverride<KeyType>.none)
ForEach(groupKeyTypes) { type in
Label(type.label, systemImage: type.icon).tag(DefaultOverride.value(type))
}
}

private func rowLabel<Value>(for choice: DefaultOverride<Value>, value: (Value) -> String) -> String {
switch choice {
case .appDefault: String(localized: "App Default")
case .none: String(localized: "None")
case let .value(chosen): value(chosen)
}
}

private var groupKeysSection: some View {
Section {
ForEach(groupKeys) { key in
Expand Down Expand Up @@ -210,6 +321,8 @@ struct GroupFormSheet: View {
existing.name = trimmed
existing.selection = selection
existing.customDomains = storedCustomDomains
existing.durationOverride = durationOverride
existing.keyOverride = keyOverride
existing.lastUpdated = .now
timedUnblockService.updateGroupSelection(
groupId: existing.id,
Expand All @@ -218,13 +331,16 @@ struct GroupFormSheet: View {
)
} else {
let nextIndex = SortIndexing.nextIndex(after: allGroups, sortIndex: \.sortIndex)
modelContext.insert(AppGroup(
let group = AppGroup(
id: groupID,
name: trimmed,
selection: selection,
sortIndex: nextIndex,
customDomains: storedCustomDomains
))
)
group.durationOverride = durationOverride
group.keyOverride = keyOverride
modelContext.insert(group)
}
dismiss()
}
Expand Down
2 changes: 1 addition & 1 deletion Normal/Views/Groups/GroupListCardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ struct GroupListCardView: View {

private func unblockGroup(_ proof: KeyProof) {
guard let grant = UnblockGrant(.group(appGroup), proof: proof) else { return }
guard let duration = settings.defaultDuration else {
guard let duration = settings.defaultDuration(for: appGroup) else {
durationGrant = grant
return
}
Expand Down
4 changes: 3 additions & 1 deletion Normal/Views/Groups/GroupsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ struct GroupsView: View {
// triggered it, instead of on the card row.
.protectedAction(
$authAction,
defaultKeyType: allSettings.unwrapped.defaultKeyType,
defaultKeyType: allSettings.unwrapped.defaultKeyType(
for: appGroups.first { $0.id == pendingGroupID }
),
keyGroupID: pendingGroupID
)
.sheet(item: $durationGrant) { grant in
Expand Down
Loading
Loading