Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit bd1f5a4

Browse files
committed
Monitor magic reset key
1 parent 396fb3e commit bd1f5a4

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Aware/macOS/ActivityMonitor.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ struct ActivityMonitor {
6666
}
6767
}()
6868

69+
async let userDefaultsTask: () = { @MainActor in
70+
let store = UserDefaults.standard
71+
for await value in store.updates(forKeyPath: "reset", type: Bool.self, initial: true) {
72+
logger.log("Received UserDefaults \"reset\" change")
73+
if value == true {
74+
state.deactivate()
75+
state.activate()
76+
}
77+
if value != nil {
78+
logger.debug("Cleaning up \"reset\" key")
79+
store.removeObject(forKey: "reset")
80+
}
81+
}
82+
}()
83+
6984
while !Task.isCancelled {
7085
let lastUserEvent = secondsSinceLastUserEvent()
7186
let idleRemaining = configuration.userIdle - lastUserEvent
@@ -89,6 +104,7 @@ struct ActivityMonitor {
89104
}
90105

91106
await notificationsTask
107+
await userDefaultsTask
92108

93109
assert(Task.isCancelled)
94110
try Task.checkCancellation()

Aware/visionOS/ActivityMonitor.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ struct ActivityMonitor {
8080
}
8181
}()
8282

83+
async let userDefaultsTask: () = { @MainActor in
84+
let store = UserDefaults.standard
85+
for await value in store.updates(forKeyPath: "reset", type: Bool.self, initial: true) {
86+
logger.log("Received UserDefaults \"reset\" change")
87+
if value == true {
88+
state.deactivate()
89+
state.activate()
90+
}
91+
if value != nil {
92+
logger.debug("Cleaning up \"reset\" key")
93+
store.removeObject(forKey: "reset")
94+
}
95+
}
96+
}()
97+
8398
let notificationNames = [
8499
UIApplication.didEnterBackgroundNotification,
85100
UIApplication.willEnterForegroundNotification,
@@ -143,6 +158,7 @@ struct ActivityMonitor {
143158
}
144159

145160
try await driftTask
161+
await userDefaultsTask
146162

147163
assert(Task.isCancelled)
148164
try Task.checkCancellation()

0 commit comments

Comments
 (0)