|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// A proposed change to the usage limits, classified by direction. |
| 4 | +/// |
| 5 | +/// Only *loosening* changes are rate limited — tightening is always immediate, |
| 6 | +/// so the commitment device never stands between you and more discipline. |
| 7 | +nonisolated enum UsageLimitEdit: Equatable, Sendable { |
| 8 | + case create(minutes: Int) |
| 9 | + /// `dropsCoverage` is true when apps were removed from what the limit |
| 10 | + /// meters — less watched is less restrictive, so it counts as loosening |
| 11 | + /// even if the allowance itself shrank. |
| 12 | + case adjust(from: Int, to: Int, dropsCoverage: Bool) |
| 13 | + case delete(minutes: Int) |
| 14 | + |
| 15 | + var isLoosening: Bool { |
| 16 | + switch self { |
| 17 | + case .create: false |
| 18 | + case let .adjust(from, to, dropsCoverage): to > from || dropsCoverage |
| 19 | + case .delete: true |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/// Why a change was let through. Only `cooldownElapsed` starts a new week. |
| 25 | +nonisolated enum UsageLimitEditAllowance: Equatable, Sendable { |
| 26 | + case tightening |
| 27 | + case grace |
| 28 | + case cooldownElapsed |
| 29 | +} |
| 30 | + |
| 31 | +nonisolated enum UsageLimitEditDecision: Equatable, Sendable { |
| 32 | + case allowed(UsageLimitEditAllowance) |
| 33 | + case locked(until: Date) |
| 34 | + |
| 35 | + var isAllowed: Bool { |
| 36 | + if case .allowed = self { return true } |
| 37 | + return false |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/// What the Usage screen shows before any change is proposed. |
| 42 | +nonisolated enum UsageLimitLockState: Equatable, Sendable { |
| 43 | + case unlocked |
| 44 | + case grace(until: Date) |
| 45 | + case locked(until: Date) |
| 46 | + |
| 47 | + var lockedUntil: Date? { |
| 48 | + if case let .locked(until) = self { return until } |
| 49 | + return nil |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/// Pure rules for when limits may be loosened. |
| 54 | +/// |
| 55 | +/// The anchor is the moment of the last loosening and never moves during the |
| 56 | +/// grace window — otherwise repeated edits inside grace would walk the clock |
| 57 | +/// forward indefinitely and the weekly cooldown would never bite. |
| 58 | +nonisolated enum UsageLimitEditPolicy { |
| 59 | + static let graceInterval: TimeInterval = .minutes(10) |
| 60 | + static let cooldownInterval: TimeInterval = .days(7) |
| 61 | + |
| 62 | + static func lockState(lastLoosenedAt: Date?, now: Date = .now) -> UsageLimitLockState { |
| 63 | + guard let anchor = lastLoosenedAt else { return .unlocked } |
| 64 | + let graceEnds = anchor + graceInterval |
| 65 | + if now < graceEnds { return .grace(until: graceEnds) } |
| 66 | + let unlock = anchor + cooldownInterval |
| 67 | + return now < unlock ? .locked(until: unlock) : .unlocked |
| 68 | + } |
| 69 | + |
| 70 | + static func decide( |
| 71 | + _ edit: UsageLimitEdit, |
| 72 | + lastLoosenedAt: Date?, |
| 73 | + now: Date = .now |
| 74 | + ) -> UsageLimitEditDecision { |
| 75 | + guard edit.isLoosening else { return .allowed(.tightening) } |
| 76 | + switch lockState(lastLoosenedAt: lastLoosenedAt, now: now) { |
| 77 | + case .unlocked: return .allowed(.cooldownElapsed) |
| 78 | + case .grace: return .allowed(.grace) |
| 79 | + case let .locked(until): return .locked(until: until) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /// The anchor to persist after a change was applied. |
| 84 | + static func anchor( |
| 85 | + after allowance: UsageLimitEditAllowance, |
| 86 | + current: Date?, |
| 87 | + now: Date = .now |
| 88 | + ) -> Date? { |
| 89 | + allowance == .cooldownElapsed ? now : current |
| 90 | + } |
| 91 | +} |
0 commit comments