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
9 changes: 9 additions & 0 deletions Normal/App/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ struct ContentView: View {
@Environment(ScreenTimeService.self) private var screenTimeService
@Environment(OnboardingService.self) private var onboardingService
@Environment(TimedUnblockService.self) private var timedUnblockService
@Environment(ScheduleService.self) private var scheduleService
@Environment(EmergencyUnblockService.self) private var emergencyUnblockService
@Environment(\.scenePhase) private var scenePhase
@Query private var allSettings: [Settings]
@Query private var schedules: [BlockSchedule]

@State private var selectedTab: AppTab = .home
@State private var navigationCoordinator = NavigationCoordinator()
Expand All @@ -34,6 +36,7 @@ struct ContentView: View {
Task { await screenTimeService.checkAuthorizationStatus() }
screenTimeService.notifyUpdate()
timedUnblockService.refresh()
reregisterSchedules()
}
}
.onAppear(perform: onAppear)
Expand All @@ -52,10 +55,16 @@ struct ContentView: View {
await screenTimeService.checkAuthorizationStatus()
if settings?.hasCompletedOnboarding == true {
_ = await screenTimeService.ensureAuthorized()
reregisterSchedules()
}
}
}

private func reregisterSchedules() {
guard settings?.hasCompletedOnboarding == true else { return }
scheduleService.registerAll(schedules, screenTimeService: screenTimeService)
}

private func onOnboardingCompleted(_: Bool, _ isActive: Bool) {
if !isActive, settings?.hasCompletedOnboarding != true {
settings?.hasCompletedOnboarding = true
Expand Down
19 changes: 19 additions & 0 deletions Normal/Models/BlockSchedule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ final class BlockSchedule: Identifiable {
self.sortIndex = sortIndex
}

func isActive(at now: Date, calendar: Calendar = .current) -> Bool {
guard isEnabled else { return false }

let startOfToday = calendar.startOfDay(for: now)
for dayOffset in [0, -1] {
guard let day = calendar.date(byAdding: .day, value: dayOffset, to: startOfToday),
let start = calendar.date(
bySettingHour: startHour, minute: startMinute, second: 0, of: day
)
else { continue }
let end = start + .minutes(durationMinutes)
if weekdays.contains(calendar.component(.weekday, from: start)),
now >= start, now < end {
return true
}
}
return false
}

func toDTO() -> ScheduleDTO? {
guard let data = try? selection.toData() else { return nil }
return ScheduleDTO(
Expand Down
4 changes: 4 additions & 0 deletions Normal/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@
"comment" : "The text for a button that finalizes the selection of apps for a family member.",
"isCommentAutoGenerated" : true
},
"Duration must be at least %lld minutes." : {
"comment" : "An error message displayed in the checkout view when the duration of a schedule is too short.",
"isCommentAutoGenerated" : true
},
"Duration Type" : {
"comment" : "A label displayed above the picker for the duration type of a schedule.",
"isCommentAutoGenerated" : true
Expand Down
22 changes: 22 additions & 0 deletions Normal/Services/DeviceActivityScheduleFactory.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import DeviceActivity
import Foundation

enum DeviceActivityScheduleFactory {
static let minimumInterval: TimeInterval = .minutes(15)

private static let boundaryMargin: TimeInterval = 1

static func window(
from start: Date,
to end: Date,
calendar: Calendar = .current
) -> DeviceActivitySchedule {
let flooredEnd = max(end, start.addingTimeInterval(minimumInterval + boundaryMargin))
let fields: Set<Calendar.Component> = [.year, .month, .day, .hour, .minute, .second]
return DeviceActivitySchedule(
intervalStart: calendar.dateComponents(fields, from: start),
intervalEnd: calendar.dateComponents(fields, from: flooredEnd),
repeats: false
)
}
}
41 changes: 41 additions & 0 deletions Normal/Services/ScheduleService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ final class ScheduleService {

let deviceSchedule = makeDeviceSchedule(for: schedule)
try activityCenter.startMonitoring(activityName, during: deviceSchedule, events: [:])
applyIfActiveNow(schedule, screenTimeService: screenTimeService)
}

func registerAll(
_ schedules: [BlockSchedule],
screenTimeService: any ScreenTimeProviding
) {
syncAllToSharedStore(schedules)
for schedule in schedules where schedule.isEnabled {
try? sync(schedule, screenTimeService: screenTimeService)
}
}

func remove(
Expand All @@ -49,6 +60,23 @@ final class ScheduleService {
try sync(schedule, screenTimeService: screenTimeService)
}

func setScheduleOverride(_ active: Bool) {
sharedStore.setScheduleOverrideActive(active)
}

func disableAll(
_ schedules: [BlockSchedule],
screenTimeService: any ScreenTimeProviding
) {
for schedule in schedules where schedule.isEnabled {
schedule.isEnabled = false
}
for schedule in schedules {
try? sync(schedule, screenTimeService: screenTimeService)
}
syncAllToSharedStore(schedules)
}

func syncAllToSharedStore(_ schedules: [BlockSchedule]) {
sharedStore.saveSchedules(schedules.compactMap { $0.toDTO() })
}
Expand All @@ -66,6 +94,19 @@ final class ScheduleService {
DeviceActivityName(SharedConstants.scheduleActivityName(for: schedule.id))
}

private func applyIfActiveNow(
_ schedule: BlockSchedule,
screenTimeService: any ScreenTimeProviding
) {
guard schedule.isActive(at: .now) else { return }
if schedule.shouldBlock {
guard !sharedStore.isUnblockAllInEffect() else { return }
screenTimeService.addToShields(selection: schedule.selection)
} else {
screenTimeService.removeFromShields(selection: schedule.selection)
}
}

private func liftBlockIfNeeded(
_ schedule: BlockSchedule,
screenTimeService: any ScreenTimeProviding
Expand Down
21 changes: 9 additions & 12 deletions Normal/Services/TimedUnblockService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ final class TimedUnblockService {
cancelMonitoring(activityName: activityName)
cancelAllGroupUnblocks()

let endDate = Date.now.addingTimeInterval(duration.timeInterval)
let start = Date.now
let endDate = start.addingTimeInterval(duration.timeInterval)
screenTimeService.removeShieldOnAll(blockAllPreventsAppDelete: blockAllPreventsAppDelete)
try scheduleActivity(name: activityName, endDate: endDate)
sharedStore.setScheduleOverrideActive(false)
try scheduleActivity(name: activityName, start: start, endDate: endDate)

try persist(
id: Self.mainID,
Expand All @@ -76,9 +78,10 @@ final class TimedUnblockService {
let activityName = SharedConstants.groupTimedUnblockActivityName(for: groupId)
cancelMonitoring(activityName: activityName)

let endDate = Date.now.addingTimeInterval(duration.timeInterval)
let start = Date.now
let endDate = start.addingTimeInterval(duration.timeInterval)
screenTimeService.removeFromShields(selection: selection)
try scheduleActivity(name: activityName, endDate: endDate)
try scheduleActivity(name: activityName, start: start, endDate: endDate)

try persist(
id: id,
Expand Down Expand Up @@ -220,14 +223,8 @@ final class TimedUnblockService {
}
}

private func scheduleActivity(name: String, endDate: Date) throws {
let calendar = Calendar.current
let fields: Set<Calendar.Component> = [.year, .month, .day, .hour, .minute, .second]
let schedule = DeviceActivitySchedule(
intervalStart: calendar.dateComponents(fields, from: .now),
intervalEnd: calendar.dateComponents(fields, from: endDate),
repeats: false
)
private func scheduleActivity(name: String, start: Date, endDate: Date) throws {
let schedule = DeviceActivityScheduleFactory.window(from: start, to: endDate)
try activityCenter.startMonitoring(
DeviceActivityName(name),
during: schedule,
Expand Down
3 changes: 3 additions & 0 deletions Normal/Views/Home/MainBlockButtonView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import SwiftUI
struct MainBlockButtonView: View {
@Environment(ScreenTimeService.self) private var screenTimeService
@Environment(TimedUnblockService.self) private var timedUnblockService
@Environment(ScheduleService.self) private var scheduleService
@Environment(AppReviewService.self) private var appReviewService
@Environment(\.requestReview) private var requestReview
@Query private var allSettings: [Settings]
Expand Down Expand Up @@ -47,6 +48,7 @@ struct MainBlockButtonView: View {
blockAllPreventsAppDelete: settings.blockAllPreventsAppDelete
)
timedUnblockService.clearAll()
scheduleService.setScheduleOverride(false)
}
} label: {
HStack {
Expand Down Expand Up @@ -104,6 +106,7 @@ struct MainBlockButtonView: View {
screenTimeService.removeShieldOnAll(
blockAllPreventsAppDelete: settings.blockAllPreventsAppDelete
)
scheduleService.setScheduleOverride(true)
recordUnblockForReview()
}
)
Expand Down
15 changes: 13 additions & 2 deletions Normal/Views/Schedules/ScheduleFormSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ struct ScheduleFormSheet: View {

private var isNew: Bool { existing == nil }

private static let minimumDurationMinutes = 15

private var isDurationTooShort: Bool {
isTimed && computedDurationMinutes < Self.minimumDurationMinutes
}

private var canSave: Bool {
!name.trimmingCharacters(in: .whitespaces).isEmpty
&& selection.count > 0
&& !selectedWeekdays.isEmpty
&& (!isTimed || computedDurationMinutes > 0)
&& !isDurationTooShort
}

private var computedDurationMinutes: Int {
Expand Down Expand Up @@ -151,7 +157,12 @@ struct ScheduleFormSheet: View {
Text("Time")
} footer: {
if isTimed {
Text("Duration: \(formattedComputedDuration)")
if isDurationTooShort {
Text("Duration must be at least \(Self.minimumDurationMinutes) minutes.")
.foregroundStyle(.red)
} else {
Text("Duration: \(formattedComputedDuration)")
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions Normal/Views/Settings/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import SwiftUI
struct SettingsView: View {
@Environment(\.dismiss) private var dismiss
@Environment(ScreenTimeService.self) private var screenTimeService
@Environment(ScheduleService.self) private var scheduleService
@Environment(EmergencyUnblockService.self) private var emergencyUnblockService
@Query private var allSettings: [Settings]
@Query private var keys: [Key]
@Query private var schedules: [BlockSchedule]

@State private var showConfirmation = false
@State private var showSuccessAlert = false
Expand Down Expand Up @@ -70,6 +72,7 @@ struct SettingsView: View {
private func performEmergencyUnblock() {
emergencyUnblockService.record(into: settings)
screenTimeService.removeShieldOnAll(blockAllPreventsAppDelete: true)
scheduleService.disableAll(schedules, screenTimeService: screenTimeService)
showSuccessAlert = true
}
}
2 changes: 2 additions & 0 deletions NormalMonitor/NormalMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ final class NormalMonitor: DeviceActivityMonitor {
let selection = try? FamilyActivitySelection.fromData(schedule.selectionData)
else { return }

guard sharedStore.resolveScheduleStart() == .apply else { return }

if schedule.shouldBlock {
store.unionShields(with: selection)
} else {
Expand Down
Loading
Loading