codeburn/mac/Sources/CodeBurnMenubar/Data/UsageRefreshCadence.swift
AgentSeal b4c04e7beb feat(menubar): user-selectable usage refresh cadence (Auto / 1m / 5m / 15m / Manual)
Settings gains a Usage Refresh picker mirroring the quota cadence
pattern. Auto keeps the adaptive default (30s active, battery and Low
Power backoff); fixed cadences ignore power state but an open popover
always gets the active 30s; Manual never auto-spawns, refreshing only
on popover open, Refresh Now, and first launch (wake included: manual
wakes run stuck-state cleanup without spawning). Stored in UserDefaults
as CodeBurnMenubarRefreshSeconds (-1 auto, 0 manual, else seconds) and
documented in the README with the defaults-write escape hatch.

Refs #647
2026-07-09 22:48:07 +02:00

42 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, backed off 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 (30s, 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) }
}
}