codeburn/mac/Sources/CodeBurnMenubar/MenubarScope.swift
ozymandiashh 805d2ffa49
Some checks are pending
CI / semgrep (push) Waiting to run
feat(menubar): add Local/Combined usage toggle (#566) (#568)
Mirror the combined multi-device dashboard in the macOS menubar via a
Local/Combined scope toggle (Settings -> Display). Builds on the menubar-json
`--scope combined` contract.

- MenubarScope enum (.local default) persisted in UserDefaults; PayloadCacheKey
  gains a scope dimension so local and combined payloads cache separately.
- DataClient.fetch(scope:): a pure statusSubcommand builder appends
  `--scope combined` only for combined, forces `--provider all` (the CLI rejects
  combined with a provider/project/exclude filter), and coerces multi-day
  selections to local (the CLI rejects combined + --days).
- MenubarPayload decodes the optional `combined` block (per-device + totals);
  nil when absent (back-compat).
- The badge always stays on the local payload; the combined network pull lives
  on a separate cache key and falls back to local with a "Combined unavailable"
  indicator, so a slow/offline peer never blocks the badge.
- Hero shows combined totals + a per-device breakdown in Combined scope; the
  token metric stays input+output (cache excluded) to match local; the
  per-device daily-budget warning is suppressed for a multi-device total;
  selecting Combined resets the provider tab to All.

Tests run via `swift test` (CommandLineTools Testing.framework on the rpath, no
Xcode): scope persistence, argv (local/combined/provider-forcing/multi-day),
cache-key partition, badge-survives-combined-failure, combined decode, token
consistency, provider reset, budget suppression.
2026-06-28 22:01:01 +02:00

39 lines
1,020 B
Swift

import Foundation
private let menubarScopeDefaultsKey = "CodeBurnMenubarScope"
enum MenubarScope: String, CaseIterable, Identifiable, Sendable {
case local = "Local"
case combined = "Combined"
var id: String { rawValue }
var cliArg: String {
switch self {
case .local: "local"
case .combined: "combined"
}
}
var menubarDefaultsValue: String {
switch self {
case .local: "local"
case .combined: "combined"
}
}
init(menubarDefaultsValue: String?) {
switch menubarDefaultsValue {
case "combined": self = .combined
default: self = .local
}
}
static func savedMenubarScope(defaults: UserDefaults = .standard) -> MenubarScope {
MenubarScope(menubarDefaultsValue: defaults.string(forKey: menubarScopeDefaultsKey))
}
func persistAsMenubarDefault(defaults: UserDefaults = .standard) {
defaults.set(menubarDefaultsValue, forKey: menubarScopeDefaultsKey)
}
}