I'm building an app that allows you to configure time limits for specific apps just like the native Screen Time app, but I'm having trouble replicating Screen Time's "Customize Days" feature. I can quite easily create an event that triggers once a particular usage is reached within an activity, but I'm having issues getting different days of the week to have different limits.
I'm trying to achieve this by creating different events for each day of the week and assigning them all to a single activity that runs every day:
// DeviceActivity receiving event: 00:00 -> 23:59, every day
// limits: [UsageLimit(hours: Int, minutes: Int)]
let calendar = Calendar(identifier: .gregorian)
for (weekday, limit) in limits.days.enumerated() {
let name = DeviceActivityEvent.Name(baseName+"|time-limit-\(weekday)")
events[name] = .init(
applications: selection.applications,
categories: selection.categories,
webDomains: selection.webDomains,
threshold: .init(
calendar: calendar,
hour: limit.hours,
second: limit.minutes,
weekday: weekday + 1
)
)
}
The problem I'm having is that weekday
is not respected in this context. Unless weekday is nil or 0, the event never triggers.
I suppose one way to make this work would be to instead have 7 separate activities that run on different days of the week and contain their respective usage limit event, but I was trying to avoid that as the limit on how many activities you can run at the same time is quite low. What is the intended way to achieve this weekday granularity for events?