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
13 changes: 6 additions & 7 deletions Normal/App/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ struct ContentView: View {
@Query private var schedules: [BlockSchedule]
@Query private var usageLimits: [UsageLimit]

@State private var selectedTab: AppTab = .home
@State private var navigationCoordinator = NavigationCoordinator()

private var settings: Settings? {
Expand All @@ -22,20 +21,20 @@ struct ContentView: View {

var body: some View {
ZStack {
MainTabView(selectedTab: $selectedTab)
MainTabView(selectedTab: $navigationCoordinator.selectedTab)
if onboardingService.isOnboardingActive {
OnboardingOverlayView()
}
}
.environment(\.navigationCoordinator, navigationCoordinator)
.widgetActionResponder(coordinator: navigationCoordinator, selectedTab: $selectedTab)
.widgetActionResponder(coordinator: navigationCoordinator)
.liveActivitySync()
.sheet(isPresented: $navigationCoordinator.isSettingsPresented, onDismiss: navigationCoordinator.settingsDidDismiss) {
SettingsView(initialTab: navigationCoordinator.settingsInitialTab)
}
.environment(\.navigationCoordinator, navigationCoordinator)
.onChange(of: onboardingService.requiredTab) { _, newTab in
if let newTab {
selectedTab = newTab
navigationCoordinator.selectedTab = newTab
}
}
.onChange(of: onboardingService.isOnboardingActive, onOnboardingCompleted)
Expand Down Expand Up @@ -63,7 +62,7 @@ struct ContentView: View {
if settings?.hasCompletedOnboarding == true {
onboardingService.isOnboardingActive = false
onboardingService.currentStep = .complete
selectedTab = settings?.defaultTab ?? .home
navigationCoordinator.selectedTab = settings?.defaultTab ?? .home
}
Task {
await screenTimeService.checkAuthorizationStatus()
Expand Down Expand Up @@ -91,7 +90,7 @@ struct ContentView: View {
private func onOnboardingCompleted(_: Bool, _ isActive: Bool) {
if !isActive, settings?.hasCompletedOnboarding != true {
settings?.hasCompletedOnboarding = true
selectedTab = .appSelect
navigationCoordinator.selectedTab = .appSelect
}
}
}
4 changes: 2 additions & 2 deletions Normal/Intents/AppBlockIntentPerformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class AppBlockIntentPerformer: BlockIntentPerforming {

func blockAll() throws -> BlockOutcome {
logger.info("Block intent: all")
let settings = try intents.validatedSettings(requiringGlobalKey: true)
let settings = try intents.validatedSettings(requiringKey: true)
let mainSelection = try intents.mainSelection(settings: settings)
let wasAlreadyBlocked = intents.screenTimeService.blockStatus(of: .all(mainSelection), settings: settings) == .all
&& intents.timedUnblockService.activeUnblocks.isEmpty
Expand All @@ -48,7 +48,7 @@ final class AppBlockIntentPerformer: BlockIntentPerforming {

func blockGroup(id: UUID) throws -> BlockOutcome {
logger.info("Block intent: group")
let settings = try intents.validatedSettings(requiringGlobalKey: true)
let settings = try intents.validatedSettings(requiringKey: true, groupID: id)
let group = try intents.group(id: id)
let wasAlreadyBlocked = intents.screenTimeService.blockStatus(of: .group(group), settings: settings) == .all
&& !intents.timedUnblockService.isGroupUnblockActive(groupId: id)
Expand Down
2 changes: 1 addition & 1 deletion Normal/Intents/AppUnblockIntentPerformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ final class AppUnblockIntentPerformer: UnblockIntentPerforming {
}

private func readiness(for request: UnblockRequest) throws -> Readiness {
let settings = try intents.validatedSettings(requiringGlobalKey: false)
let settings = try intents.validatedSettings(requiringKey: false)
intents.timedUnblockService.refresh(screenTimeService: intents.screenTimeService)
let target = try intents.target(groupID: request.groupID, settings: settings)
let status = intents.screenTimeService.blockStatus(of: target, settings: settings)
Expand Down
7 changes: 4 additions & 3 deletions Normal/Intents/IntentContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ struct IntentContext {
container.mainContext
}

func validatedSettings(requiringGlobalKey: Bool) throws -> Settings {
func validatedSettings(requiringKey: Bool, groupID: UUID? = nil) throws -> Settings {
let settings = try context.fetch(FetchDescriptor<Settings>()).first
let hasCompletedOnboarding = settings?.hasCompletedOnboarding == true
if requiringGlobalKey {
if requiringKey {
try BlockActions.validate(
isAuthorized: screenTimeService.isAuthorizedNow,
hasCompletedOnboarding: hasCompletedOnboarding,
keys: keys()
keys: keys(),
groupID: groupID
)
} else {
try BlockActions.validateSetup(
Expand Down
10 changes: 3 additions & 7 deletions Normal/Modifiers/WidgetActionResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ private struct WidgetActionResponder: ViewModifier {
@Query private var selectedApps: [SelectedApps]

let coordinator: NavigationCoordinator
@Binding var selectedTab: AppTab

@State private var authAction: ProtectedAction?
@State private var active: ActiveRequest?
Expand Down Expand Up @@ -124,7 +123,7 @@ private struct WidgetActionResponder: ViewModifier {
}

private func present(_ request: ActiveRequest) {
selectedTab = request.target.groupID == nil ? .home : .groups
coordinator.selectedTab = request.target.groupID == nil ? .home : .groups
active = request
}

Expand Down Expand Up @@ -159,10 +158,7 @@ private struct WidgetActionResponder: ViewModifier {
}

extension View {
func widgetActionResponder(
coordinator: NavigationCoordinator,
selectedTab: Binding<AppTab>
) -> some View {
modifier(WidgetActionResponder(coordinator: coordinator, selectedTab: selectedTab))
func widgetActionResponder(coordinator: NavigationCoordinator) -> some View {
modifier(WidgetActionResponder(coordinator: coordinator))
}
}
38 changes: 25 additions & 13 deletions Normal/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,12 @@
"comment" : "A button to add a custom website domain.",
"isCommentAutoGenerated" : true
},
"Add a key in Normal before blocking apps." : {
"comment" : "Error message when a key is not set up.",
"Add a Key First" : {
"comment" : "Title of an alert shown when an action needs a key the user hasn't added.",
"isCommentAutoGenerated" : true
},
"Add a key in the Keys tab before blocking apps." : {
"comment" : "A message displayed in an alert when a user tries to trigger an action but has no keys added.",
"isCommentAutoGenerated" : true
},
"Add a key in the Keys tab before blocking groups." : {
"comment" : "Footer message displayed when the user does not have a global key to block apps.",
"Add a key in Normal before blocking apps." : {
"comment" : "Error message when a key is not set up.",
"isCommentAutoGenerated" : true
},
"Add a key in the Keys tab before creating schedules." : {
Expand Down Expand Up @@ -803,6 +799,10 @@
},
"Go to App Select to choose which apps to manage before creating schedules." : {

},
"Go to Keys" : {
"comment" : "Button that opens the Keys tab.",
"isCommentAutoGenerated" : true
},
"Grant Permission" : {
"comment" : "A button that requests Screen Time permission.",
Expand All @@ -820,6 +820,10 @@
"comment" : "A header label for a section that lists the group keys of a family member.",
"isCommentAutoGenerated" : true
},
"Group keys only unblock their own group. Add a key in the Keys tab to block." : {
"comment" : "Message shown when the user tries to block with only group keys.",
"isCommentAutoGenerated" : true
},
"Group Name" : {
"comment" : "A label for the name of a group.",
"isCommentAutoGenerated" : true
Expand Down Expand Up @@ -1081,10 +1085,6 @@
"comment" : "An alert title when a user has no keys added.",
"isCommentAutoGenerated" : true
},
"No Keys Available" : {
"comment" : "A title for an alert that informs the user that they have no keys registered.",
"isCommentAutoGenerated" : true
},
"No Keys Yet" : {
"comment" : "A title displayed in the header of the \"No Keys Yet\" view.",
"isCommentAutoGenerated" : true
Expand All @@ -1101,7 +1101,7 @@
"isCommentAutoGenerated" : true
},
"None of your registered keys are supported on this device. Add a QR code or barcode key to use on iPad." : {
"comment" : "A message displayed in the \"No Keys Available\" alert when a user has no keys registered on their device.",
"comment" : "Message in the Add a Key First alert when none of the user's keys work on this device.",
"isCommentAutoGenerated" : true
},
"Normal" : {
Expand Down Expand Up @@ -1154,6 +1154,10 @@
},
"Normal works fully offline. No internet connection is required to set up or enforce your screen time limits." : {

},
"Not Now" : {
"comment" : "Button that closes the Add a Key First alert.",
"isCommentAutoGenerated" : true
},
"Now you have a dumb phone with iPhone hardware, the best of both worlds." : {

Expand Down Expand Up @@ -1835,6 +1839,14 @@
"comment" : "A description of the consequences of blocking without a key.",
"isCommentAutoGenerated" : true
},
"You'll need a key to unblock these apps." : {
"comment" : "Message shown when the user tries to unblock without a key.",
"isCommentAutoGenerated" : true
},
"You'll need a key to unblock your apps later." : {
"comment" : "Message shown when the user tries to block without any keys.",
"isCommentAutoGenerated" : true
},
"You're All Set" : {
"comment" : "A title for the onboarding finish view.",
"isCommentAutoGenerated" : true
Expand Down
4 changes: 2 additions & 2 deletions Normal/Services/BlockActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ enum BlockActions {
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
}

static func validate(isAuthorized: Bool, hasCompletedOnboarding: Bool, keys: [Key]) throws {
static func validate(isAuthorized: Bool, hasCompletedOnboarding: Bool, keys: [Key], groupID: UUID? = nil) throws {
try validateSetup(isAuthorized: isAuthorized, hasCompletedOnboarding: hasCompletedOnboarding)
guard Key.hasGlobalKey(in: keys) else { throw BlockIntentError.noGlobalKey }
guard !Key.scoped(keys, toGroup: groupID).isEmpty else { throw BlockIntentError.noGlobalKey }
}

static func validateSetup(isAuthorized: Bool, hasCompletedOnboarding: Bool) throws {
Expand Down
18 changes: 15 additions & 3 deletions Normal/Services/Keys/KeyCheckFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@ final class KeyCheckFlow {
struct Request {
let id = UUID()
let keys: [Key]
let hasAnyGroupKey: Bool
let keyGroupID: UUID?
let availableKeyTypes: [KeyType]
let defaultKeyType: KeyType?
var requestedKeyType: KeyType?
}

enum MissingKey: Equatable {
case noKeys
case onlyGroupKeys
case noKeyToUnblock
case unsupportedOnDevice
}

enum Stage: Equatable {
case idle
case noKeys
case missingKey(MissingKey)
case choosing
case confirmingBypass
case scanningQR
Expand Down Expand Up @@ -57,7 +65,7 @@ final class KeyCheckFlow {
switch stage {
case .choosing, .confirmingBypass, .scanningQR, .checkingLocation: true
case .scanningNFC: nfcOverPicker
case .idle, .noKeys: false
case .idle, .missingKey: false
}
}

Expand All @@ -80,14 +88,18 @@ final class KeyCheckFlow {
self.request = request
self.action = action
allowsBypass = action.allowsBypass
if allowsBypass, request.keys.isEmpty {
stage = .missingKey(request.hasAnyGroupKey ? .onlyGroupKeys : .noKeys)
return
}
switch KeySelectLogic.decide(
availableKeyTypes: request.availableKeyTypes,
allowBypass: allowsBypass,
defaultKeyType: request.defaultKeyType,
requestedKeyType: request.requestedKeyType
) {
case .showNoKeysAlert:
stage = .noKeys
stage = .missingKey(request.keys.isEmpty ? .noKeyToUnblock : .unsupportedOnDevice)
case let .autoSelect(keyType):
begin(keyType, fromPicker: false)
case .showSheet:
Expand Down
6 changes: 6 additions & 0 deletions Normal/Services/NavigationCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ struct ActionRequest: Equatable {
@MainActor
@Observable
final class NavigationCoordinator {
var selectedTab: AppTab = .home
var isSettingsPresented = false
private(set) var settingsInitialTab: SettingsTab = .general

private(set) var pendingAction: ActionRequest?
private var actionAfterSettings: ActionRequest?

func show(_ tab: AppTab) {
isSettingsPresented = false
selectedTab = tab
}

func presentSettings(tab: SettingsTab = .general) {
settingsInitialTab = tab
isSettingsPresented = true
Expand Down
42 changes: 32 additions & 10 deletions Normal/Views/BlockUnblockViews/KeySelect/KeySelectModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import SwiftUI
struct KeySelectModifier: ViewModifier {
@Environment(QRService.self) private var qrService
@Environment(LocationService.self) private var locationService
@Environment(\.navigationCoordinator) private var navigation
@Query private var keys: [Key]
@Query private var allSettings: [Settings]

Expand Down Expand Up @@ -60,14 +61,14 @@ struct KeySelectModifier: ViewModifier {
}
}
}
.alert("No Keys Available", isPresented: noKeysAlert) {
Button("OK", role: .cancel) { flow.cancel() }
} message: {
Text(
flow.request?.keys.isEmpty ?? true
? "Add a key in the Keys tab before blocking apps."
: "None of your registered keys are supported on this device. Add a QR code or barcode key to use on iPad."
)
.alert("Add a Key First", isPresented: missingKeyAlert, presenting: missingKey) { _ in
Button("Not Now", role: .cancel) { flow.cancel() }
Button("Go to Keys") {
flow.cancel()
navigation.show(.keys)
}
} message: { missingKey in
Text(message(for: missingKey))
}
.sheet(item: keySheet, onDismiss: flow.keySheetDismissed) { _ in
keySelectSheet
Expand Down Expand Up @@ -139,13 +140,33 @@ struct KeySelectModifier: ViewModifier {
allowsMinimizedDetent ? [sheetDetent, Self.minimizedDetent] : [sheetDetent]
}

private var noKeysAlert: Binding<Bool> {
private var missingKey: KeyCheckFlow.MissingKey? {
if case let .missingKey(missingKey) = flow.stage {
return missingKey
}
return nil
}

private var missingKeyAlert: Binding<Bool> {
Binding(
get: { flow.stage == .noKeys },
get: { missingKey != nil },
set: { if !$0 { flow.cancel() } }
)
}

private func message(for missingKey: KeyCheckFlow.MissingKey) -> LocalizedStringKey {
switch missingKey {
case .noKeys:
"You'll need a key to unblock your apps later."
case .onlyGroupKeys:
"Group keys only unblock their own group. Add a key in the Keys tab to block."
case .noKeyToUnblock:
"You'll need a key to unblock these apps."
case .unsupportedOnDevice:
"None of your registered keys are supported on this device. Add a QR code or barcode key to use on iPad."
}
}

private var keySheet: Binding<SheetID?> {
Binding(
get: { flow.showsKeySheet ? flow.request.map { SheetID(id: $0.id) } : nil },
Expand All @@ -166,6 +187,7 @@ struct KeySelectModifier: ViewModifier {
flow.start(
KeyCheckFlow.Request(
keys: scopedKeys,
hasAnyGroupKey: keys.contains { $0.groupID != nil },
keyGroupID: keyGroupID,
availableKeyTypes: KeyType.selectable(registered: scopedKeys.map(\.type)),
defaultKeyType: defaultKeyType,
Expand Down
5 changes: 0 additions & 5 deletions Normal/Views/Groups/GroupListCardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ struct GroupListCardView: View {
screenTimeService.activeShieldCount() > 0
}

private var hasGlobalKey: Bool {
Key.hasGlobalKey(in: allKeys)
}

private var blockStatus: BlockStatus {
screenTimeService.blockStatus(selection: appGroup.selection, customDomains: customDomains)
}
Expand Down Expand Up @@ -235,7 +231,6 @@ struct GroupListCardView: View {
requestAction(.tighten(blockGroup))
}
.accessibilityIdentifier("group.blockButton")
.disabled(!hasGlobalKey)
}

private var unblockButton: some View {
Expand Down
Loading
Loading