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
8 changes: 8 additions & 0 deletions Normal/App/UITestDeviceActivityCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ final class UITestDeviceActivityCenter: DeviceActivityProviding {
during schedule: DeviceActivitySchedule,
events _: [DeviceActivityEvent.Name: DeviceActivityEvent]
) throws {
if activityName.rawValue.hasPrefix("timedUnblock_") {
if UITestSupport.timedUnblockFails {
throw DeviceActivityCenter.MonitoringError.unauthorized
}
if UITestSupport.timerLimitReached {
throw DeviceActivityCenter.MonitoringError.excessiveActivities
}
}
schedules[activityName.rawValue] = schedule
}

Expand Down
6 changes: 6 additions & 0 deletions Normal/App/UITestSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ enum UITestSupport {

static let websitesOnly = arguments.contains("-uiTestWebsitesOnly")

static let timedUnblockFails = arguments.contains("-uiTestTimedUnblockFails")

static let timerLimitReached = arguments.contains("-uiTestTimerLimitReached")

static let slowScan = arguments.contains("-uiTestSlowScan")

static let seedGroupKey = arguments.contains("-uiTestSeedGroupKey")

static let seedLocationKey = arguments.contains("-uiTestSeedLocationKey")
Expand Down
44 changes: 44 additions & 0 deletions Normal/Extensions/View+UnblockAlert.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import SwiftUI

enum UnblockAlert {
case failed(any Error)
case mayEndLate

init?(_ timing: UnblockTiming) {
guard timing == .mayEndLate else { return nil }
self = .mayEndLate
}

var title: LocalizedStringKey {
switch self {
case .failed: "Couldn't Unblock"
case .mayEndLate: "Unblocked"
}
}

var message: Text {
switch self {
case let .failed(error):
Text(error.localizedDescription)
case .mayEndLate:
Text("iOS only tracks 20 schedules and timers at once, so this unblock may end up to 30 minutes late.")
}
}
}

extension View {
func unblockAlert(_ alert: Binding<UnblockAlert?>) -> some View {
self.alert(
alert.wrappedValue?.title ?? "",
isPresented: Binding(
get: { alert.wrappedValue != nil },
set: { if !$0 { alert.wrappedValue = nil } }
),
presenting: alert.wrappedValue
) { _ in
Button("OK", role: .cancel) {}
} message: { alert in
alert.message
}
}
}
23 changes: 15 additions & 8 deletions Normal/Modifiers/WidgetActionResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ private struct WidgetActionResponder: ViewModifier {
@State private var active: ActiveRequest?
@State private var heldRequest: ActionRequest?
@State private var durationGrant: UnblockGrant?
@State private var unblockAlert: UnblockAlert?

private struct ActiveRequest {
let token: UUID
Expand All @@ -39,8 +40,9 @@ private struct WidgetActionResponder: ViewModifier {
requestID: active?.token
)
.sheet(item: $durationGrant, onDismiss: durationSheetDismissed) { grant in
UnblockDurationSheet(grant: grant)
UnblockDurationSheet(grant: grant) { unblockAlert = UnblockAlert($0) }
}
.unblockAlert($unblockAlert)
.onOpenURL { coordinator.handle(url: $0) }
.onChange(of: coordinator.pendingAction) { _, request in resolve(request) }
.onChange(of: unlockInbox.pending, initial: true) { _, _ in takeInboxRequest() }
Expand Down Expand Up @@ -132,13 +134,18 @@ private struct WidgetActionResponder: ViewModifier {
durationGrant = grant
return
}
try? BlockActions.unblock(
grant,
for: length,
settings: settings,
screenTimeService: screenTimeService,
timedUnblockService: timedUnblockService
)
do {
let timing = try BlockActions.unblock(
grant,
for: length,
settings: settings,
screenTimeService: screenTimeService,
timedUnblockService: timedUnblockService
)
unblockAlert = UnblockAlert(timing)
} catch {
unblockAlert = .failed(error)
}
}

private func block(_ group: AppGroup) {
Expand Down
12 changes: 12 additions & 0 deletions Normal/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,10 @@
"comment" : "Error when unblocking an app fails.",
"isCommentAutoGenerated" : true
},
"Couldn't Unblock" : {
"comment" : "Title of an alert shown when an unblock with the default duration fails.",
"isCommentAutoGenerated" : true
},
"Create a schedule to automatically block or unblock apps at set times." : {
"comment" : "A footer message in the Schedules view that instructs the user on how to create schedules.",
"isCommentAutoGenerated" : true
Expand Down Expand Up @@ -903,6 +907,10 @@
},
"Invalid Key" : {

},
"iOS only tracks 20 schedules and timers at once, so this unblock may end up to 30 minutes late." : {
"comment" : "Message shown after a timed unblock starts without its own iOS timer because too many schedules and timers are registered.",
"isCommentAutoGenerated" : true
},
"It provides both a dummy Apple ID and a random passcode. This can be made near impossible to recover." : {
"comment" : "A description of a service that can be used to generate a passcode and Apple ID for the Screen Time bypass.",
Expand Down Expand Up @@ -1661,6 +1669,10 @@
"comment" : "A heading for the NFC tag unblocking process.",
"isCommentAutoGenerated" : true
},
"Unblocked" : {
"comment" : "Title of an alert shown after a timed unblock starts without its own iOS timer.",
"isCommentAutoGenerated" : true
},
"Unblocking" : {
"comment" : "A section header that lists unblocking-related settings.",
"isCommentAutoGenerated" : true
Expand Down
6 changes: 4 additions & 2 deletions Normal/Services/BlockActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,25 @@ enum BlockActions {
screenTimeService.block(selection: group.selection, customDomains: customDomains)
}

@discardableResult
static func unblock(
_ grant: UnblockGrant,
for length: UnblockLength,
settings: Settings,
screenTimeService: any ScreenTimeProviding,
timedUnblockService: TimedUnblockService
) throws {
) throws -> UnblockTiming {
switch length {
case let .timed(duration):
try timedUnblockService.start(
return try timedUnblockService.start(
grant,
duration: duration,
settings: settings,
screenTimeService: screenTimeService
)
case .permanent:
screenTimeService.unblockPermanently(grant, settings: settings)
return .exact
}
}

Expand Down
3 changes: 3 additions & 0 deletions Normal/Services/QRService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ final class QRService {

func scan(validate: ((String) -> Bool)?) async throws -> String {
if UITestSupport.isActive {
if UITestSupport.slowScan {
try? await Task.sleep(for: .seconds(1))
}
if let validate, !validate(UITestSupport.stubScanValue) {
throw ScanError.invalidKey
}
Expand Down
39 changes: 28 additions & 11 deletions Normal/Services/TimedUnblockService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,20 @@ final class TimedUnblockService {
activeUnblocks[groupId.uuidString]
}

@discardableResult
func startMain(
duration: TimedUnblockDuration,
selection: FamilyActivitySelection,
customDomains: [String] = [],
screenTimeService: any ScreenTimeProviding,
blockAllPreventsAppDelete: Bool = false
) throws {
) throws -> UnblockTiming {
let activityName = SharedConstants.mainTimedUnblockActivityName
cancelMonitoring(activityName: activityName)

let start = clock()
let endDate = Self.endDate(of: duration, from: start)
try scheduleActivity(name: activityName, start: start, endDate: endDate)
let timing = try scheduleActivity(name: activityName, start: start, endDate: endDate)

try persist(
id: Self.mainID,
Expand All @@ -81,22 +82,24 @@ final class TimedUnblockService {
cancelAllGroupUnblocks()
syncHeartbeat()
screenTimeService.reconcile()
return timing
}

@discardableResult
func startGroup(
duration: TimedUnblockDuration,
groupId: UUID,
selection: FamilyActivitySelection,
customDomains: [String] = [],
screenTimeService: any ScreenTimeProviding
) throws {
) throws -> UnblockTiming {
let id = groupId.uuidString
let activityName = SharedConstants.groupTimedUnblockActivityName(for: groupId)
cancelMonitoring(activityName: activityName)

let start = clock()
let endDate = Self.endDate(of: duration, from: start)
try scheduleActivity(name: activityName, start: start, endDate: endDate)
let timing = try scheduleActivity(name: activityName, start: start, endDate: endDate)

try persist(
id: id,
Expand All @@ -110,6 +113,7 @@ final class TimedUnblockService {

syncHeartbeat()
screenTimeService.reconcile()
return timing
}

func cancelMain(
Expand Down Expand Up @@ -285,13 +289,20 @@ final class TimedUnblockService {
return Date(timeIntervalSinceReferenceDate: end.rounded(.down))
}

private func scheduleActivity(name: String, start: Date, endDate: Date) throws {
private func scheduleActivity(name: String, start: Date, endDate: Date) throws -> UnblockTiming {
let schedule = DeviceActivityScheduleFactory.window(from: start, to: endDate)
try activityCenter.startMonitoring(
DeviceActivityName(name),
during: schedule,
events: [:]
)
do {
try activityCenter.startMonitoring(
DeviceActivityName(name),
during: schedule,
events: [:]
)
return .exact
} catch DeviceActivityCenter.MonitoringError.excessiveActivities
where activityCenter.monitoredActivityNames.contains(SharedConstants.reblockHeartbeatActivityName)
{
return .mayEndLate
}
}

private func syncHeartbeat() {
Expand Down Expand Up @@ -329,12 +340,13 @@ final class TimedUnblockService {
}

extension TimedUnblockService {
@discardableResult
func startMain(
duration: TimedUnblockDuration,
mainSelection: SelectedApps,
settings: Settings,
screenTimeService: any ScreenTimeProviding
) throws {
) throws -> UnblockTiming {
try startMain(
duration: duration,
selection: mainSelection.selection,
Expand All @@ -344,3 +356,8 @@ extension TimedUnblockService {
)
}
}

enum UnblockTiming: Equatable {
case exact
case mayEndLate
}
7 changes: 4 additions & 3 deletions Normal/Services/UnblockTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,24 @@ enum UnblockTarget: Identifiable {
}

extension TimedUnblockService {
@discardableResult
func start(
_ grant: UnblockGrant,
duration: TimedUnblockDuration,
settings: Settings,
screenTimeService: any ScreenTimeProviding
) throws {
) throws -> UnblockTiming {
let target = grant.target
switch target {
case let .all(mainSelection):
try startMain(
return try startMain(
duration: duration,
mainSelection: mainSelection,
settings: settings,
screenTimeService: screenTimeService
)
case let .group(group):
try startGroup(
return try startGroup(
duration: duration,
groupId: group.id,
selection: group.selection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct UnblockDurationSheet: View {
@Query private var allSettings: [Settings]

let grant: UnblockGrant
var onUnblock: () -> Void = {}
var onUnblock: (UnblockTiming) -> Void = { _ in }

private var settings: Settings {
allSettings.unwrapped
Expand All @@ -26,14 +26,14 @@ struct UnblockDurationSheet: View {
durations: settings.unblockDurations,
initialDuration: settings.defaultDuration,
onConfirm: { length in
try BlockActions.unblock(
let timing = try BlockActions.unblock(
grant,
for: length,
settings: settings,
screenTimeService: screenTimeService,
timedUnblockService: timedUnblockService
)
onUnblock()
onUnblock(timing)
}
)
}
Expand Down
20 changes: 13 additions & 7 deletions Normal/Views/Groups/GroupListCardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct GroupListCardView: View {
@Binding var authAction: ProtectedAction?
@Binding var pendingGroupID: UUID?
@Binding var durationGrant: UnblockGrant?
@Binding var unblockAlert: UnblockAlert?

@State private var isEditing = false
@State private var isReselecting = false
Expand Down Expand Up @@ -254,12 +255,17 @@ struct GroupListCardView: View {
durationGrant = grant
return
}
try? BlockActions.unblock(
grant,
for: .timed(duration),
settings: settings,
screenTimeService: screenTimeService,
timedUnblockService: timedUnblockService
)
do {
let timing = try BlockActions.unblock(
grant,
for: .timed(duration),
settings: settings,
screenTimeService: screenTimeService,
timedUnblockService: timedUnblockService
)
unblockAlert = UnblockAlert(timing)
} catch {
unblockAlert = .failed(error)
}
}
}
Loading
Loading