Subscriptions belong to a company; BroV owns its Keychain token

- ops: subscriptions get a client (company) column (migrated in place); the brain fills it
  from «компания сага / для планеты»; /subs and «Весь план» group by company with
  per-company monthly totals; the company shows in agenda rows and the calendar.
- BroV: the API token handed over in ops-token.import is saved by BroV itself, so the
  Keychain item belongs to BroV and macOS stops asking for the password.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
maksarsanjeev
2026-10-10 03:42:41 +03:00
parent 36326ba0b6
commit 18aed2beb9
8 changed files with 161 additions and 55 deletions
+28 -3
View File
@@ -52,6 +52,7 @@ struct OpsSub: Identifiable, Equatable {
var every: Int
var next: String
var remindDays: Int
var client: String
}
struct OpsRoutine: Identifiable, Equatable {
@@ -76,13 +77,30 @@ final class OpsClient: ObservableObject {
@Published var tasks: [OpsTask] = []
@Published var subs: [OpsSub] = []
@Published var routines: [OpsRoutine] = []
/// Subscriptions per month by company ("" = none), then by currency.
@Published var byClient: [String: [String: Double]] = [:]
var fullLoaded = false
var baseURL: String {
(UserDefaults.standard.string(forKey: Self.urlKey) ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
}
/// Read once from the Keychain, updated by Settings.
var token: String = Keychain.load(key: OpsClient.tokenKey) ?? ""
var token: String = OpsClient.loadToken()
/// A token handed over in ~/Library/Application Support/BroV/ops-token.import (by the
/// installer) is saved by BroV itself — so BroV owns the Keychain item and macOS never
/// asks for the password — and the file is deleted.
private static func loadToken() -> String {
let file = FileManager.default.homeDirectoryForCurrentUser
.appendingPathComponent("Library/Application Support/BroV/ops-token.import")
if let t = try? String(contentsOf: file, encoding: .utf8).trimmingCharacters(in: .whitespacesAndNewlines),
!t.isEmpty {
Keychain.save(key: tokenKey, value: t)
try? FileManager.default.removeItem(at: file)
return t
}
return Keychain.load(key: tokenKey) ?? ""
}
var configured: Bool { !baseURL.isEmpty && !token.isEmpty }
private func request(_ path: String, body: [String: Any]? = nil, timeout: TimeInterval = 8) async -> Any? {
@@ -138,7 +156,11 @@ final class OpsClient: ObservableObject {
subs = (j["subs"] as? [[String: Any]] ?? []).map { d in
OpsSub(id: d["id"] as? Int ?? 0, name: d["name"] as? String ?? "", amount: (d["amount"] as? NSNumber)?.doubleValue ?? 0,
currency: d["currency"] as? String ?? "RUB", period: d["period"] as? String ?? "month",
every: d["every"] as? Int ?? 1, next: d["next_date"] as? String ?? "", remindDays: d["remind_days"] as? Int ?? 3)
every: d["every"] as? Int ?? 1, next: d["next_date"] as? String ?? "", remindDays: d["remind_days"] as? Int ?? 3,
client: d["client"] as? String ?? "")
}
byClient = (j["by_client"] as? [String: [String: Any]] ?? [:]).mapValues { m in
m.compactMapValues { ($0 as? NSNumber)?.doubleValue }
}
}
if let arr = await request("/api/routines") as? [[String: Any]] {
@@ -186,6 +208,9 @@ final class OpsClient: ObservableObject {
f.numberStyle = .decimal; f.groupingSeparator = " "; f.maximumFractionDigits = v.rounded() == v ? 0 : 2
return f.string(from: NSNumber(value: v)) ?? "\(v)"
}
static func clientName(_ c: String) -> String {
["saga": "Сага", "planet9": "Planet9", "home": "Дом"][c] ?? c
}
static func symbol(_ c: String) -> String {
["RUB": "₽", "USD": "$", "EUR": "€"][c.uppercased()] ?? c
}
@@ -436,7 +461,7 @@ struct OpsRow: View {
var parts: [String] = []
if overdue, let d = Self.daysLate(item.date) { parts.append("просрочено на \(d) дн.") } else { parts.append(OpsClient.ruDay(item.date)) }
if let a = item.amount { parts.append("\(OpsClient.amount(a)) \(OpsClient.symbol(item.currency ?? ""))") }
if !item.client.isEmpty { parts.append(item.client) }
if !item.client.isEmpty { parts.append(OpsClient.clientName(item.client)) }
return parts.joined(separator: " · ")
}