codeburn/mac/Sources/CodeBurnMenubar/Data/UsageRefreshCadence.swift
Aditya Vikram Singh 71a8e8e250
menubar: skip unchanged background refreshes, 120s AC idle cadence, utility QoS for tick spawns (#703) (#704)
Co-authored-by: Aditya Vikram Singh <247195684+avs-io@users.noreply.github.com>
2026-07-16 15:11:25 -07:00

43 lines
1.6 KiB
Swift

import Foundation
/// User-configurable cadence for the usage payload refresh loop (#647).
/// `auto` keeps the adaptive default: 30s while active, 120s while closed on AC,
/// backed off further on battery, in Low Power Mode, and while the displays sleep.
/// `manual = 0` never
/// auto-spawns; usage refreshes only on popover open, Refresh Now, and first
/// launch. Stored as raw seconds in UserDefaults (auto = -1), mirroring
/// SubscriptionRefreshCadence.
enum UsageRefreshCadence: Int, CaseIterable, Identifiable {
case auto = -1
case manual = 0
case oneMinute = 60
case fiveMinutes = 300
case fifteenMinutes = 900
var id: Int { rawValue }
var label: String {
switch self {
case .auto: return "Auto (2m, less on battery)"
case .manual: return "Manual"
case .oneMinute: return "1 minute"
case .fiveMinutes: return "5 minutes"
case .fifteenMinutes: return "15 minutes"
}
}
static let defaultsKey = "CodeBurnMenubarRefreshSeconds"
static let `default`: UsageRefreshCadence = .auto
static var current: UsageRefreshCadence {
get {
// integer(forKey:) returns 0 for a missing key, which aliases
// `manual`; probe object(forKey:) to seed the default instead.
if UserDefaults.standard.object(forKey: defaultsKey) == nil {
return .default
}
return UsageRefreshCadence(rawValue: UserDefaults.standard.integer(forKey: defaultsKey)) ?? .default
}
set { UserDefaults.standard.set(newValue.rawValue, forKey: defaultsKey) }
}
}