diff --git a/Normal/Models/Settings+UnblockDurations.swift b/Normal/Models/Settings+UnblockDurations.swift index 69010ce..6556184 100644 --- a/Normal/Models/Settings+UnblockDurations.swift +++ b/Normal/Models/Settings+UnblockDurations.swift @@ -33,6 +33,14 @@ extension Settings { } } + func duration(for request: UnlockDurationRequest) -> TimedUnblockDuration? { + switch request { + case let .fixed(duration): duration + case .useDefault: defaultDuration + case .ask: nil + } + } + func isDefault(_ duration: TimedUnblockDuration) -> Bool { defaultDuration == duration } diff --git a/Normal/Modifiers/WidgetActionResponder.swift b/Normal/Modifiers/WidgetActionResponder.swift index 1130300..2a711c3 100644 --- a/Normal/Modifiers/WidgetActionResponder.swift +++ b/Normal/Modifiers/WidgetActionResponder.swift @@ -17,20 +17,12 @@ private struct WidgetActionResponder: ViewModifier { @State private var keyType: KeyType? @State private var allowBypass = false @State private var requestID: UUID? - @State private var durationGroup: AppGroup? + @State private var durationTarget: UnblockTarget? private var unblockDurations: [TimedUnblockDuration] { allSettings.first?.unblockDurations ?? TimedUnblockDuration.presets } - private var customDomainsEnabled: Bool { - allSettings.first?.enableCustomDomains ?? false - } - - private func customDomains(for group: AppGroup) -> [String] { - customDomainsEnabled ? group.customDomains : [] - } - func body(content: Content) -> some View { content .protectedAction( @@ -40,8 +32,8 @@ private struct WidgetActionResponder: ViewModifier { keyGroupID: actionGroup?.id, requestID: requestID ) - .sheet(item: $durationGroup) { group in - GroupTimedUnblockSheet(group: group) + .sheet(item: $durationTarget) { target in + UnblockDurationSheet(target: target) } .onOpenURL { coordinator.handle(url: $0) } .onChange(of: coordinator.pendingGroupAction) { _, request in resolve(request) } @@ -68,7 +60,7 @@ private struct WidgetActionResponder: ViewModifier { private func resolve(_ request: GroupActionRequest?) { guard let request, - durationGroup == nil, + durationTarget == nil, allSettings.first?.hasCompletedOnboarding == true, let group = groups.first(where: { $0.id == request.groupID }) else { @@ -94,22 +86,16 @@ private struct WidgetActionResponder: ViewModifier { } private func unlock(_ group: AppGroup, duration request: UnlockDurationRequest) { - let duration: TimedUnblockDuration? = switch request { - case let .fixed(duration): duration - case .useDefault: allSettings.first?.defaultDuration - case .ask: nil - } - if let duration { - try? timedUnblockService.startGroup( - duration: duration, - groupId: group.id, - selection: group.selection, - customDomains: customDomains(for: group), - screenTimeService: screenTimeService - ) - } else { - durationGroup = group + guard let settings = allSettings.first, let duration = settings.duration(for: request) else { + durationTarget = .group(group) + return } + try? timedUnblockService.start( + .group(group), + duration: duration, + settings: settings, + screenTimeService: screenTimeService + ) } private func block(_ group: AppGroup) { diff --git a/Normal/Services/UnblockTarget.swift b/Normal/Services/UnblockTarget.swift new file mode 100644 index 0000000..34e827d --- /dev/null +++ b/Normal/Services/UnblockTarget.swift @@ -0,0 +1,67 @@ +import FamilyControls +import Foundation + +enum UnblockTarget: Identifiable { + case all(SelectedApps) + case group(AppGroup) + + var id: String { + switch self { + case .all: "all" + case let .group(group): group.id.uuidString + } + } + + var selection: FamilyActivitySelection { + switch self { + case let .all(mainSelection): mainSelection.selection + case let .group(group): group.selection + } + } + + func customDomains(settings: Settings) -> [String] { + guard settings.enableCustomDomains else { return [] } + return switch self { + case let .all(mainSelection): mainSelection.customDomains + case let .group(group): group.customDomains + } + } +} + +extension TimedUnblockService { + func start( + _ target: UnblockTarget, + duration: TimedUnblockDuration, + settings: Settings, + screenTimeService: any ScreenTimeProviding + ) throws { + switch target { + case let .all(mainSelection): + try startMain( + duration: duration, + mainSelection: mainSelection, + settings: settings, + screenTimeService: screenTimeService + ) + case let .group(group): + try startGroup( + duration: duration, + groupId: group.id, + selection: group.selection, + customDomains: target.customDomains(settings: settings), + screenTimeService: screenTimeService + ) + } + } +} + +extension ScreenTimeProviding { + func unblockPermanently(_ target: UnblockTarget, settings: Settings) { + switch target { + case .all: + unblockAll(blockAllPreventsAppDelete: settings.blockAllPreventsAppDelete) + case .group: + unblock(selection: target.selection, customDomains: target.customDomains(settings: settings)) + } + } +} diff --git a/Normal/Views/BlockUnblockViews/TimedUnlock/GroupTimedUnblockSheet.swift b/Normal/Views/BlockUnblockViews/TimedUnlock/GroupTimedUnblockSheet.swift deleted file mode 100644 index 25f9037..0000000 --- a/Normal/Views/BlockUnblockViews/TimedUnlock/GroupTimedUnblockSheet.swift +++ /dev/null @@ -1,34 +0,0 @@ -import SwiftData -import SwiftUI - -struct GroupTimedUnblockSheet: View { - @Environment(TimedUnblockService.self) private var timedUnblockService - @Environment(ScreenTimeService.self) private var screenTimeService - @Query private var allSettings: [Settings] - - let group: AppGroup - - private var customDomains: [String] { - (allSettings.first?.enableCustomDomains ?? false) ? group.customDomains : [] - } - - var body: some View { - TimedUnblockSheet( - title: "Unblock \(group.name)", - durations: allSettings.first?.unblockDurations ?? TimedUnblockDuration.presets, - initialDuration: allSettings.first?.defaultDuration, - onTimedUnblock: { duration in - try timedUnblockService.startGroup( - duration: duration, - groupId: group.id, - selection: group.selection, - customDomains: customDomains, - screenTimeService: screenTimeService - ) - }, - onPermanentUnblock: { - screenTimeService.unblock(selection: group.selection, customDomains: customDomains) - } - ) - } -} diff --git a/Normal/Views/BlockUnblockViews/TimedUnlock/UnblockDurationSheet.swift b/Normal/Views/BlockUnblockViews/TimedUnlock/UnblockDurationSheet.swift new file mode 100644 index 0000000..2a23836 --- /dev/null +++ b/Normal/Views/BlockUnblockViews/TimedUnlock/UnblockDurationSheet.swift @@ -0,0 +1,43 @@ +import SwiftData +import SwiftUI + +struct UnblockDurationSheet: View { + @Environment(TimedUnblockService.self) private var timedUnblockService + @Environment(ScreenTimeService.self) private var screenTimeService + @Query private var allSettings: [Settings] + + let target: UnblockTarget + var onUnblock: () -> Void = {} + + private var settings: Settings { + allSettings.unwrapped + } + + private var title: String { + switch target { + case .all: "Unblock All" + case let .group(group): "Unblock \(group.name)" + } + } + + var body: some View { + TimedUnblockSheet( + title: title, + durations: settings.unblockDurations, + initialDuration: settings.defaultDuration, + onTimedUnblock: { duration in + try timedUnblockService.start( + target, + duration: duration, + settings: settings, + screenTimeService: screenTimeService + ) + onUnblock() + }, + onPermanentUnblock: { + screenTimeService.unblockPermanently(target, settings: settings) + onUnblock() + } + ) + } +} diff --git a/Normal/Views/Groups/GroupListCardView.swift b/Normal/Views/Groups/GroupListCardView.swift index d9e7306..bc43ce9 100644 --- a/Normal/Views/Groups/GroupListCardView.swift +++ b/Normal/Views/Groups/GroupListCardView.swift @@ -247,11 +247,10 @@ struct GroupListCardView: View { ) { requestAction(allowBypass: false) { if let duration = settings.defaultDuration { - try? timedUnblockService.startGroup( + try? timedUnblockService.start( + .group(appGroup), duration: duration, - groupId: appGroup.id, - selection: appGroup.selection, - customDomains: customDomains, + settings: settings, screenTimeService: screenTimeService ) } else { diff --git a/Normal/Views/Groups/GroupsView.swift b/Normal/Views/Groups/GroupsView.swift index 378c79a..384f602 100644 --- a/Normal/Views/Groups/GroupsView.swift +++ b/Normal/Views/Groups/GroupsView.swift @@ -77,7 +77,7 @@ struct GroupsView: View { keyGroupID: pendingGroupID ) .sheet(item: $pendingDurationGroup) { group in - GroupTimedUnblockSheet(group: group) + UnblockDurationSheet(target: .group(group)) } } } diff --git a/Normal/Views/Home/HomeView.swift b/Normal/Views/Home/HomeView.swift index fa2af8c..fdb601d 100644 --- a/Normal/Views/Home/HomeView.swift +++ b/Normal/Views/Home/HomeView.swift @@ -64,26 +64,7 @@ struct HomeView: View { @ViewBuilder private var timedUnblockSheet: some View { if let mainSelection { - TimedUnblockSheet( - title: "Unblock All", - durations: settings.unblockDurations, - initialDuration: settings.defaultDuration, - onTimedUnblock: { duration in - try timedUnblockService.startMain( - duration: duration, - mainSelection: mainSelection, - settings: settings, - screenTimeService: screenTimeService - ) - recordUnblockForReview() - }, - onPermanentUnblock: { - screenTimeService.unblockAll( - blockAllPreventsAppDelete: settings.blockAllPreventsAppDelete - ) - recordUnblockForReview() - } - ) + UnblockDurationSheet(target: .all(mainSelection), onUnblock: recordUnblockForReview) } } @@ -99,9 +80,9 @@ struct HomeView: View { private func unblockMain(_ selection: SelectedApps) { if let duration = settings.defaultDuration { do { - try timedUnblockService.startMain( + try timedUnblockService.start( + .all(selection), duration: duration, - mainSelection: selection, settings: settings, screenTimeService: screenTimeService ) diff --git a/NormalTests/Models/SettingsUnblockDurationTests.swift b/NormalTests/Models/SettingsUnblockDurationTests.swift index 5664f66..6d21352 100644 --- a/NormalTests/Models/SettingsUnblockDurationTests.swift +++ b/NormalTests/Models/SettingsUnblockDurationTests.swift @@ -132,4 +132,26 @@ struct SettingsUnblockDurationTests { s.customUnblockDurationSeconds = [3600, 3600, 60, 900] #expect(s.unblockDurations == [.fifteenMinutes, .oneHour]) } + + @Test func fixedRequestUsesItsDuration() { + let s = Settings() + s.defaultDuration = .thirtyMinutes + #expect(s.duration(for: .fixed(.fourHours)) == .fourHours) + } + + @Test func defaultRequestUsesDefaultDuration() { + let s = Settings() + s.defaultDuration = .thirtyMinutes + #expect(s.duration(for: .useDefault) == .thirtyMinutes) + } + + @Test func defaultRequestWithoutDefaultHasNoDuration() { + #expect(Settings().duration(for: .useDefault) == nil) + } + + @Test func askRequestHasNoDuration() { + let s = Settings() + s.defaultDuration = .thirtyMinutes + #expect(s.duration(for: .ask) == nil) + } } diff --git a/NormalTests/Services/UnblockTargetTests.swift b/NormalTests/Services/UnblockTargetTests.swift new file mode 100644 index 0000000..9a19a7b --- /dev/null +++ b/NormalTests/Services/UnblockTargetTests.swift @@ -0,0 +1,70 @@ +import FamilyControls +@testable import Normal +import Testing + +@MainActor +struct UnblockTargetTests { + private let activity = FakeDeviceActivityCenter() + private let store = FakeSharedStore() + private let screenTime = FakeScreenTimeService() + private let service: TimedUnblockService + + init() { + service = TimedUnblockService(activityCenter: activity, sharedStore: store, onExpiration: {}) + } + + private func settings(customDomains: Bool) -> Settings { + let settings = Settings() + settings.enableCustomDomains = customDomains + settings.blockAllPreventsAppDelete = false + return settings + } + + @Test func startingAllStartsTheMainUnblock() throws { + let main = SelectedApps(selection: FamilyActivitySelection(), customDomains: ["a.com"]) + + try service.start(.all(main), duration: .oneHour, settings: settings(customDomains: true), screenTimeService: screenTime) + + #expect(service.isMainUnblockActive) + #expect(store.timedUnblocks.first?.customDomains == ["a.com"]) + #expect(store.timedUnblocks.first?.isGroupUnblock == false) + } + + @Test func startingAGroupStartsItsUnblock() throws { + let group = AppGroup(name: "Social", selection: FamilyActivitySelection(), customDomains: ["b.com"]) + + try service.start(.group(group), duration: .oneHour, settings: settings(customDomains: true), screenTimeService: screenTime) + + #expect(service.isGroupUnblockActive(groupId: group.id)) + #expect(store.timedUnblocks.first?.customDomains == ["b.com"]) + #expect(store.timedUnblocks.first?.isGroupUnblock == true) + } + + @Test func customDomainsAreLeftOutWhenTurnedOff() throws { + let group = AppGroup(name: "Social", selection: FamilyActivitySelection(), customDomains: ["b.com"]) + + try service.start(.group(group), duration: .oneHour, settings: settings(customDomains: false), screenTimeService: screenTime) + + #expect(store.timedUnblocks.first?.customDomains == []) + } + + @Test func permanentlyUnblockingAllUnblocksEverything() { + let main = SelectedApps(selection: FamilyActivitySelection()) + + screenTime.unblockPermanently(.all(main), settings: settings(customDomains: true)) + + #expect(screenTime.unblockAllCalled) + #expect(screenTime.unblockAllPreventsAppDelete == false) + #expect(!screenTime.unblockCalled) + } + + @Test func permanentlyUnblockingAGroupUnblocksItsSelection() { + let group = AppGroup(name: "Social", selection: FamilyActivitySelection(), customDomains: ["b.com"]) + + screenTime.unblockPermanently(.group(group), settings: settings(customDomains: true)) + + #expect(screenTime.unblockCalled) + #expect(screenTime.unblockCustomDomains == ["b.com"]) + #expect(!screenTime.unblockAllCalled) + } +}