Skip to content

Commit 5bb14ea

Browse files
committed
feat: add Unblock All and Unblock Group shortcuts
Adds Shortcuts actions, App Shortcut tiles and Siri phrases for unblocking. The key is still checked in Normal: - The action opens Normal and starts the check for the chosen key type. - With no Key Type, it uses Default Key, or the only key type you have. Opening Normal: - iOS 26+ continues in the foreground (supportedModes and continueInForeground). - iOS 18.2–18.x opens Normal through a hidden intent. - If Normal can't open, the request waits two minutes for you to open it. - Requests are picked up by the active scene once Screen Time access is confirmed, and dropped if the target is already unblocked. Also: - Unblocking no longer requires a global key. - If a timed unblock is already running, the action says so rather than reporting the apps as already unblocked. - The unblock-visibility rules move into BlockStatus. - The duration options read "Default Duration" and "Choose in Normal". - A FAQ entry explains the NFC setup.
1 parent 6428e3a commit 5bb14ea

28 files changed

Lines changed: 913 additions & 141 deletions

‎Normal.xcodeproj/project.pbxproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,11 @@
148148
BlockIntents.swift,
149149
DismissUnblockActivityIntent.swift,
150150
GroupEntity.swift,
151+
KeyTypeEntity.swift,
151152
SharedConstants.swift,
152153
TimedUnblockActivityAttributes.swift,
153154
TimedUnblockDuration.swift,
155+
UnblockDurationEntity.swift,
154156
WidgetSharedData.swift,
155157
);
156158
target = 2FDA0000000000000000A003 /* NormalWidget */;

‎Normal/App/AppContainer.swift‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct AppContainer: View {
1414
@State private var emergencyUnblockService: EmergencyUnblockService
1515
@State private var donationService = DonationService()
1616
@State private var usageLimitService: UsageLimitService
17+
@State private var unlockInbox: UnlockRequestInbox
1718

1819
init(services: AppServices) {
1920
_screenTimeService = State(initialValue: services.screenTime)
@@ -22,6 +23,7 @@ struct AppContainer: View {
2223
_appReviewService = State(initialValue: services.appReview)
2324
_emergencyUnblockService = State(initialValue: services.emergencyUnblock)
2425
_usageLimitService = State(initialValue: services.usageLimit)
26+
_unlockInbox = State(initialValue: services.unlockInbox)
2527
}
2628

2729
var body: some View {
@@ -37,6 +39,7 @@ struct AppContainer: View {
3739
.environment(emergencyUnblockService)
3840
.environment(donationService)
3941
.environment(usageLimitService)
42+
.environment(unlockInbox)
4043
.task { mirrorCustomDomainsEnabled() }
4144
.onChange(of: allSettings.first?.enableCustomDomains ?? false) { _, enabled in
4245
scheduleService.mirrorCustomDomainsEnabled(enabled)

‎Normal/App/AppServices.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ final class AppServices {
88
let appReview: AppReviewService
99
let emergencyUnblock: EmergencyUnblockService
1010
let usageLimit: UsageLimitService
11+
let unlockInbox = UnlockRequestInbox()
1112

1213
init() {
1314
if UITestSupport.isActive {

‎Normal/App/NormalApp.swift‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ struct NormalApp: App {
8181
services: services
8282
)
8383
AppDependencyManager.shared.add(dependency: blockPerformer)
84+
85+
let unblockPerformer: any UnblockIntentPerforming = AppUnblockIntentPerformer(
86+
container: container,
87+
services: services
88+
)
89+
AppDependencyManager.shared.add(dependency: unblockPerformer)
8490
}
8591

8692
var body: some Scene {

‎Normal/Intents/AppBlockIntentPerformer.swift‎

Lines changed: 19 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import OSLog
33
import SwiftData
44

55
final class AppBlockIntentPerformer: BlockIntentPerforming {
6-
private let container: ModelContainer
7-
private let screenTimeService: any ScreenTimeProviding
8-
private let timedUnblockService: TimedUnblockService
6+
private let intents: IntentContext
97
private let syncsExternalSurfaces: Bool
108
private let logger = Logger(subsystem: "com.normalengineering.normal", category: "Intents")
119

@@ -15,9 +13,11 @@ final class AppBlockIntentPerformer: BlockIntentPerforming {
1513
timedUnblockService: TimedUnblockService,
1614
syncsExternalSurfaces: Bool = true
1715
) {
18-
self.container = container
19-
self.screenTimeService = screenTimeService
20-
self.timedUnblockService = timedUnblockService
16+
intents = IntentContext(
17+
container: container,
18+
screenTimeService: screenTimeService,
19+
timedUnblockService: timedUnblockService
20+
)
2121
self.syncsExternalSurfaces = syncsExternalSurfaces
2222
}
2323

@@ -29,78 +29,42 @@ final class AppBlockIntentPerformer: BlockIntentPerforming {
2929
)
3030
}
3131

32-
private var context: ModelContext {
33-
container.mainContext
34-
}
35-
3632
func blockAll() throws -> BlockOutcome {
3733
logger.info("Block intent: all")
38-
let settings = try validatedSettings()
39-
guard let mainSelection = try context.fetch(FetchDescriptor<SelectedApps>()).first,
40-
!mainSelection.selection.isEmpty
41-
|| (settings.enableCustomDomains && !mainSelection.customDomains.isEmpty)
42-
else { throw BlockIntentError.noAppsSelected }
43-
44-
let status = screenTimeService.blockStatus(
45-
selection: mainSelection.selection,
46-
customDomains: settings.enableCustomDomains ? mainSelection.customDomains : []
47-
)
48-
let wasAlreadyBlocked = status == .all && timedUnblockService.activeUnblocks.isEmpty
34+
let settings = try intents.validatedSettings(requiringGlobalKey: true)
35+
let mainSelection = try intents.mainSelection(settings: settings)
36+
let wasAlreadyBlocked = intents.screenTimeService.blockStatus(of: .all(mainSelection), settings: settings) == .all
37+
&& intents.timedUnblockService.activeUnblocks.isEmpty
4938

5039
BlockActions.blockAll(
5140
mainSelection: mainSelection,
5241
settings: settings,
53-
screenTimeService: screenTimeService,
54-
timedUnblockService: timedUnblockService
42+
screenTimeService: intents.screenTimeService,
43+
timedUnblockService: intents.timedUnblockService
5544
)
5645
syncExternalSurfaces(settings: settings)
5746
return BlockOutcome(name: String(localized: "All Apps"), wasAlreadyBlocked: wasAlreadyBlocked)
5847
}
5948

6049
func blockGroup(id: UUID) throws -> BlockOutcome {
6150
logger.info("Block intent: group")
62-
let settings = try validatedSettings()
63-
let descriptor = FetchDescriptor<AppGroup>(predicate: #Predicate { $0.id == id })
64-
guard let group = try context.fetch(descriptor).first else {
65-
throw BlockIntentError.groupNotFound
66-
}
67-
68-
let status = screenTimeService.blockStatus(
69-
selection: group.selection,
70-
customDomains: settings.enableCustomDomains ? group.customDomains : []
71-
)
72-
let wasAlreadyBlocked = status == .all && !timedUnblockService.isGroupUnblockActive(groupId: id)
51+
let settings = try intents.validatedSettings(requiringGlobalKey: true)
52+
let group = try intents.group(id: id)
53+
let wasAlreadyBlocked = intents.screenTimeService.blockStatus(of: .group(group), settings: settings) == .all
54+
&& !intents.timedUnblockService.isGroupUnblockActive(groupId: id)
7355

7456
BlockActions.blockGroup(
7557
group,
7658
settings: settings,
77-
screenTimeService: screenTimeService,
78-
timedUnblockService: timedUnblockService
59+
screenTimeService: intents.screenTimeService,
60+
timedUnblockService: intents.timedUnblockService
7961
)
8062
syncExternalSurfaces(settings: settings)
8163
return BlockOutcome(name: group.name, wasAlreadyBlocked: wasAlreadyBlocked)
8264
}
8365

84-
private func validatedSettings() throws -> Settings {
85-
let settings = try context.fetch(FetchDescriptor<Settings>()).first
86-
try BlockActions.validate(
87-
isAuthorized: screenTimeService.isAuthorizedNow,
88-
hasCompletedOnboarding: settings?.hasCompletedOnboarding == true,
89-
keys: context.fetch(FetchDescriptor<Key>())
90-
)
91-
guard let settings else { throw BlockIntentError.setupIncomplete }
92-
return settings
93-
}
94-
9566
private func syncExternalSurfaces(settings: Settings) {
9667
guard syncsExternalSurfaces else { return }
97-
let groups = (try? context.fetch(FetchDescriptor<AppGroup>(sortBy: [SortDescriptor(\.sortIndex)]))) ?? []
98-
let keys = (try? context.fetch(FetchDescriptor<Key>())) ?? []
99-
WidgetSync.sync(groups: groups, keys: keys, settings: settings, screenTimeService: screenTimeService)
100-
LiveActivityManager.reconcile(
101-
timedUnblockService: timedUnblockService,
102-
groups: groups,
103-
isEnabled: settings.showTimedUnblockLiveActivity
104-
)
68+
intents.syncExternalSurfaces(settings: settings)
10569
}
10670
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import Foundation
2+
import OSLog
3+
import SwiftData
4+
5+
final class AppUnblockIntentPerformer: UnblockIntentPerforming {
6+
private let intents: IntentContext
7+
private let unlockInbox: UnlockRequestInbox
8+
private let logger = Logger(subsystem: "com.normalengineering.normal", category: "Intents")
9+
10+
init(
11+
container: ModelContainer,
12+
screenTimeService: any ScreenTimeProviding,
13+
timedUnblockService: TimedUnblockService,
14+
unlockInbox: UnlockRequestInbox
15+
) {
16+
intents = IntentContext(
17+
container: container,
18+
screenTimeService: screenTimeService,
19+
timedUnblockService: timedUnblockService
20+
)
21+
self.unlockInbox = unlockInbox
22+
}
23+
24+
convenience init(container: ModelContainer, services: AppServices) {
25+
self.init(
26+
container: container,
27+
screenTimeService: services.screenTime,
28+
timedUnblockService: services.timedUnblock,
29+
unlockInbox: services.unlockInbox
30+
)
31+
}
32+
33+
func unblock(_ request: UnblockRequest) throws -> UnblockOutcome {
34+
logger.info("Unblock intent: \(request.groupID == nil ? "all" : "group", privacy: .public)")
35+
let settings = try intents.validatedSettings(requiringGlobalKey: false)
36+
intents.timedUnblockService.refresh(screenTimeService: intents.screenTimeService)
37+
let target = try intents.target(groupID: request.groupID, settings: settings)
38+
let status = intents.screenTimeService.blockStatus(of: target, settings: settings)
39+
guard target.canUnblock(status: status, timedUnblockService: intents.timedUnblockService) else {
40+
return status == .none ? .alreadyUnblocked(name: target.name) : .timedUnblockRunning(name: target.name)
41+
}
42+
return .continueInApp
43+
}
44+
45+
func continueInApp(_ request: UnblockRequest) {
46+
unlockInbox.post(UnblockRequest(
47+
groupID: request.groupID,
48+
duration: request.duration,
49+
keyType: keyType(for: request)
50+
))
51+
}
52+
53+
private func keyType(for request: UnblockRequest) -> KeyType? {
54+
let keys = Key.scoped((try? intents.keys()) ?? [], toGroup: request.groupID)
55+
return KeySelectLogic.shortcutKeyType(
56+
requested: request.keyType,
57+
defaultKeyType: intents.settings()?.defaultKeyType,
58+
availableKeyTypes: KeyType.selectable(registered: keys.map(\.type))
59+
)
60+
}
61+
}

‎Normal/Intents/IntentContext.swift‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import Foundation
2+
import SwiftData
3+
4+
struct IntentContext {
5+
let container: ModelContainer
6+
let screenTimeService: any ScreenTimeProviding
7+
let timedUnblockService: TimedUnblockService
8+
9+
private var context: ModelContext {
10+
container.mainContext
11+
}
12+
13+
func validatedSettings(requiringGlobalKey: Bool) throws -> Settings {
14+
let settings = try context.fetch(FetchDescriptor<Settings>()).first
15+
let hasCompletedOnboarding = settings?.hasCompletedOnboarding == true
16+
if requiringGlobalKey {
17+
try BlockActions.validate(
18+
isAuthorized: screenTimeService.isAuthorizedNow,
19+
hasCompletedOnboarding: hasCompletedOnboarding,
20+
keys: keys()
21+
)
22+
} else {
23+
try BlockActions.validateSetup(
24+
isAuthorized: screenTimeService.isAuthorizedNow,
25+
hasCompletedOnboarding: hasCompletedOnboarding
26+
)
27+
}
28+
guard let settings else { throw BlockIntentError.setupIncomplete }
29+
return settings
30+
}
31+
32+
func settings() -> Settings? {
33+
try? context.fetch(FetchDescriptor<Settings>()).first
34+
}
35+
36+
func keys() throws -> [Key] {
37+
try context.fetch(FetchDescriptor<Key>())
38+
}
39+
40+
func target(groupID: UUID?, settings: Settings) throws -> UnblockTarget {
41+
if let groupID {
42+
return try .group(group(id: groupID))
43+
}
44+
return try .all(mainSelection(settings: settings))
45+
}
46+
47+
func mainSelection(settings: Settings) throws -> SelectedApps {
48+
guard let mainSelection = try context.fetch(FetchDescriptor<SelectedApps>()).first,
49+
!mainSelection.selection.isEmpty
50+
|| (settings.enableCustomDomains && !mainSelection.customDomains.isEmpty)
51+
else { throw BlockIntentError.noAppsSelected }
52+
return mainSelection
53+
}
54+
55+
func group(id: UUID) throws -> AppGroup {
56+
let descriptor = FetchDescriptor<AppGroup>(predicate: #Predicate { $0.id == id })
57+
guard let group = try context.fetch(descriptor).first else {
58+
throw BlockIntentError.groupNotFound
59+
}
60+
return group
61+
}
62+
63+
func syncExternalSurfaces(settings: Settings) {
64+
let groups = (try? context.fetch(FetchDescriptor<AppGroup>(sortBy: [SortDescriptor(\.sortIndex)]))) ?? []
65+
let keys = (try? keys()) ?? []
66+
WidgetSync.sync(groups: groups, keys: keys, settings: settings, screenTimeService: screenTimeService)
67+
LiveActivityManager.reconcile(
68+
timedUnblockService: timedUnblockService,
69+
groups: groups,
70+
isEnabled: settings.showTimedUnblockLiveActivity
71+
)
72+
}
73+
}

‎Normal/Intents/NormalShortcuts.swift‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@ struct NormalShortcuts: AppShortcutsProvider {
2222
shortTitle: "Block Group",
2323
systemImageName: "lock.square.stack.fill"
2424
)
25+
AppShortcut(
26+
intent: UnblockAllIntent(),
27+
phrases: [
28+
"Unblock all apps in \(.applicationName)",
29+
"Unblock everything in \(.applicationName)",
30+
"\(.applicationName) unblock all",
31+
],
32+
shortTitle: "Unblock All",
33+
systemImageName: "lock.open.fill"
34+
)
35+
AppShortcut(
36+
intent: UnblockGroupIntent(),
37+
phrases: [
38+
"Unblock \(\.$group) in \(.applicationName)",
39+
"\(.applicationName) unblock \(\.$group)",
40+
"Unblock a group in \(.applicationName)",
41+
],
42+
shortTitle: "Unblock Group",
43+
systemImageName: "lock.open"
44+
)
2545
}
2646

2747
static let shortcutTileColor: ShortcutTileColor = .blue

0 commit comments

Comments
 (0)