«Дела»: today-at-a-glance popover and the «Весь план» window

- Popover (click on the Memoji): counters (overdue, today, subscriptions per month),
  Сейчас / Горит / Сегодня / На неделе (collapsed), ○ to finish, «Оплачено» for charges,
  ⏱ and «завтра» on hover, the text box to the service's Claude.
- «Весь план» button opens a regular window: month calendar (task deadlines, upcoming
  charges and routine dates with repeats projected forward, overdue days outlined) with
  the selected day's list; the task tree with subtasks, clients, minutes and the timer;
  subscriptions with the monthly total; routines with the last time done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
maksarsanjeev
2026-10-10 03:33:43 +03:00
parent 42de561085
commit 36326ba0b6
2 changed files with 724 additions and 75 deletions
+237 -75
View File
@@ -31,6 +31,37 @@ struct OpsAgenda: Equatable {
}
}
struct OpsTask: Identifiable, Equatable {
var id: Int
var parentId: Int?
var title: String
var client: String
var status: String
var due: String?
var minutes: Int
var running: Bool
var children: [OpsTask]
}
struct OpsSub: Identifiable, Equatable {
var id: Int
var name: String
var amount: Double
var currency: String
var period: String
var every: Int
var next: String
var remindDays: Int
}
struct OpsRoutine: Identifiable, Equatable {
var id: Int
var title: String
var everyDays: Int
var next: String
var last: String?
}
@MainActor
final class OpsClient: ObservableObject {
static let shared = OpsClient()
@@ -41,6 +72,11 @@ final class OpsClient: ObservableObject {
@Published var reachable: Bool? = nil // nil = not configured
@Published var busy = false
@Published var lastAnswer: String?
/// Full lists for «Весь план» (loaded when the window is open).
@Published var tasks: [OpsTask] = []
@Published var subs: [OpsSub] = []
@Published var routines: [OpsRoutine] = []
var fullLoaded = false
var baseURL: String {
(UserDefaults.standard.string(forKey: Self.urlKey) ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
@@ -87,13 +123,40 @@ final class OpsClient: ObservableObject {
agenda = a
}
func refreshAll() async {
await refresh()
if let arr = await request("/api/tasks") as? [[String: Any]] {
func task(_ d: [String: Any]) -> OpsTask {
OpsTask(id: d["id"] as? Int ?? 0, parentId: d["parent_id"] as? Int, title: d["title"] as? String ?? "",
client: d["client"] as? String ?? "", status: d["status"] as? String ?? "todo",
due: d["due"] as? String, minutes: d["minutes"] as? Int ?? 0, running: d["running"] as? Bool ?? false,
children: (d["children"] as? [[String: Any]] ?? []).map(task))
}
tasks = arr.map(task)
}
if let j = await request("/api/subs") as? [String: Any] {
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)
}
}
if let arr = await request("/api/routines") as? [[String: Any]] {
routines = arr.map { d in
OpsRoutine(id: d["id"] as? Int ?? 0, title: d["title"] as? String ?? "", everyDays: d["every_days"] as? Int ?? 1,
next: d["next_date"] as? String ?? "", last: d["last_done"] as? String)
}
}
fullLoaded = true
}
/// Free text → the service's Claude brain (same as Telegram).
func say(_ text: String) async {
busy = true
defer { busy = false }
let r = await request("/api/say", body: ["text": text], timeout: 120) as? [String: Any]
lastAnswer = r?["answer"] as? String ?? (reachable == false ? "Сервис дел не отвечает." : nil)
await refresh()
if fullLoaded { await refreshAll() } else { await refresh() }
}
/// A direct action from a button: done / paid / timer / snooze.
@@ -101,7 +164,7 @@ final class OpsClient: ObservableObject {
let r = await request("/api/action", body: body) as? [String: Any]
if let lines = r?["result"] as? [String] { lastAnswer = lines.joined(separator: "\n") }
SoundEngine.shared.play("pop")
await refresh()
if fullLoaded { await refreshAll() } else { await refresh() }
}
func done(_ it: OpsItem) async {
@@ -113,6 +176,25 @@ final class OpsClient: ObservableObject {
}
var attentionCount: Int { agenda.overdue.count + agenda.today.count }
/// «4 200 ₽ · 200 $»
static func money(_ m: [String: Double]) -> String {
m.sorted { $0.key < $1.key }.map { "\(Self.amount($0.value)) \(Self.symbol($0.key))" }.joined(separator: " · ")
}
static func amount(_ v: Double) -> String {
let f = NumberFormatter()
f.numberStyle = .decimal; f.groupingSeparator = " "; f.maximumFractionDigits = v.rounded() == v ? 0 : 2
return f.string(from: NSNumber(value: v)) ?? "\(v)"
}
static func symbol(_ c: String) -> String {
["RUB": "₽", "USD": "$", "EUR": "€"][c.uppercased()] ?? c
}
/// 2026-11-15 → 15.11.2026
static func ruDay(_ iso: String?) -> String {
guard let iso else { return "" }
let p = iso.prefix(10).split(separator: "-")
return p.count == 3 ? "\(p[2]).\(p[1]).\(p[0])" : iso
}
}
// MARK: - Menu on the Memoji: the task manager
@@ -128,66 +210,102 @@ struct BroMenu: View {
}
}
// MARK: - «Дела» panel
// MARK: - «Дела» panel (under the notch): today at a glance
struct OpsPanel: View {
@ObservedObject var state: AppState
@ObservedObject private var ops = OpsClient.shared
@State private var text = ""
@State private var showWeek = false
@FocusState private var focused: Bool
private static let dayTitle: DateFormatter = {
let f = DateFormatter(); f.locale = Locale(identifier: "ru_RU"); f.dateFormat = "EE, dd.MM.yyyy"; return f
}()
var body: some View {
VStack(alignment: .leading, spacing: 8) {
VStack(alignment: .leading, spacing: 9) {
HStack(spacing: 6) {
Image(systemName: "checklist").font(.system(size: 12, weight: .semibold))
.foregroundColor(Color(hex: "#D97757"))
Text("Дела").font(.system(size: 14, weight: .semibold))
Text(Self.dayTitle.string(from: Date())).font(.system(size: 11)).foregroundColor(Color(hex: "#6B7079"))
Spacer()
if ops.busy { ProgressView().controlSize(.mini) }
Button { Task { await ops.refresh() } } label: {
Image(systemName: "arrow.clockwise").font(.system(size: 11))
Button {
state.botMenuOpen = false
OpsWindow.show()
} label: {
HStack(spacing: 4) {
Image(systemName: "calendar").font(.system(size: 11))
Text("Весь план").font(.system(size: 11, weight: .medium))
}
.padding(.horizontal, 8).frame(height: 22)
.background(Capsule().fill(Color.white.opacity(0.08)))
}
.buttonStyle(.plain)
.help("Обновить")
.help("Календарь, все задачи, подписки и регламент")
}
.foregroundColor(Color(hex: "#B0B5BE"))
if !ops.configured {
Text("Сервис дел не подключён. Настройки → Чат → «Дела (хоумлаб)»: адрес и токен из /etc/brov-ops/config.env на ВМ.")
.font(.system(size: 11.5)).foregroundColor(Color(hex: "#B0B5BE"))
.fixedSize(horizontal: false, vertical: true)
note("Сервис дел не подключён: Настройки → Чат → «Дела (хоумлаб)».", "#B0B5BE")
} else if ops.reachable == false {
Text("Сервис дел не отвечает. Ты не дома? Включи сеть «Дом» в 🌐.")
.font(.system(size: 11.5)).foregroundColor(Color(hex: "#FB923C"))
.fixedSize(horizontal: false, vertical: true)
note("Сервис дел не отвечает. Ты не дома? Включи сеть «Дом» в 🌐.", "#FB923C")
}
if let r = ops.agenda.running {
HStack(spacing: 8) {
Image(systemName: "timer").foregroundColor(Color(hex: "#34D399"))
Text(r.title).font(.system(size: 12, weight: .semibold)).lineLimit(1)
Text("\(r.minutes) мин").font(.system(size: 11)).monospacedDigit().foregroundColor(Color(hex: "#8E939C"))
Spacer()
Button("Стоп") { Task { await ops.act(["op": "stop_timer"]) } }
.buttonStyle(.plain).font(.system(size: 11, weight: .medium)).foregroundColor(Color(hex: "#F4505E"))
}
.padding(8)
.background(RoundedRectangle(cornerRadius: 8).fill(Color(hex: "#34D399").opacity(0.10)))
HStack(spacing: 6) {
tile("\(ops.agenda.overdue.count)", "просрочено", "#F4505E", dim: ops.agenda.overdue.isEmpty)
tile("\(ops.agenda.today.count)", "сегодня", "#F5A524", dim: ops.agenda.today.isEmpty)
tile(ops.agenda.monthly.isEmpty ? "—" : OpsClient.money(ops.agenda.monthly), "подписки / мес", "#B0B5BE", dim: true)
}
ScrollView(.vertical, showsIndicators: false) {
VStack(alignment: .leading, spacing: 2) {
section("ПРОСРОЧЕНО", ops.agenda.overdue, "#F4505E")
section("СЕГОДНЯ", ops.agenda.today, "#F5A524")
section("НА НЕДЕЛЕ", ops.agenda.soon, "#8E939C")
if ops.configured && ops.reachable == true && ops.agenda.overdue.isEmpty
&& ops.agenda.today.isEmpty && ops.agenda.soon.isEmpty {
VStack(alignment: .leading, spacing: 1) {
if let r = ops.agenda.running {
header("СЕЙЧАС", "#34D399")
HStack(spacing: 8) {
Image(systemName: "timer").font(.system(size: 11)).foregroundColor(Color(hex: "#34D399")).frame(width: 16)
Text(r.title).font(.system(size: 12, weight: .medium)).lineLimit(1)
Spacer()
Text("\(r.minutes) мин").font(.system(size: 11)).monospacedDigit().foregroundColor(Color(hex: "#34D399"))
Button { Task { await ops.act(["op": "stop_timer"]) } } label: {
Image(systemName: "stop.circle.fill").font(.system(size: 14)).foregroundColor(Color(hex: "#34D399"))
}
.buttonStyle(.plain).help("Остановить таймер")
}
.padding(.horizontal, 6).padding(.vertical, 6)
.background(RoundedRectangle(cornerRadius: 7).fill(Color(hex: "#34D399").opacity(0.10)))
}
if !ops.agenda.overdue.isEmpty {
header("ГОРИТ", "#F4505E")
ForEach(ops.agenda.overdue) { OpsRow(item: $0, overdue: true) }
}
if !ops.agenda.today.isEmpty {
header("СЕГОДНЯ", "#F5A524")
ForEach(ops.agenda.today) { OpsRow(item: $0) }
}
if !ops.agenda.soon.isEmpty {
Button { withAnimation(.easeOut(duration: 0.15)) { showWeek.toggle() } } label: {
HStack(spacing: 4) {
Text("НА НЕДЕЛЕ · \(ops.agenda.soon.count)").font(.system(size: 9.5, weight: .bold))
Image(systemName: showWeek ? "chevron.up" : "chevron.down").font(.system(size: 8, weight: .bold))
}
.foregroundColor(Color(hex: "#8E939C"))
.padding(.top, 8).padding(.bottom, 3).padding(.leading, 6)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
if showWeek { ForEach(ops.agenda.soon) { OpsRow(item: $0) } }
}
if ops.configured && ops.reachable == true && ops.agenda.running == nil
&& ops.agenda.overdue.isEmpty && ops.agenda.today.isEmpty && ops.agenda.soon.isEmpty {
Text("На неделю ничего не горит 👌").font(.system(size: 12)).foregroundColor(Color(hex: "#8E939C"))
.padding(.vertical, 6)
.padding(.vertical, 8).padding(.leading, 6)
}
}
}
.frame(maxHeight: 260)
.frame(maxHeight: 280)
if let a = ops.lastAnswer, !a.isEmpty {
Text(a).font(.system(size: 11)).foregroundColor(Color(hex: "#B0B5BE"))
@@ -198,73 +316,116 @@ struct OpsPanel: View {
.background(RoundedRectangle(cornerRadius: 8).fill(Color.white.opacity(0.05)))
}
HStack(spacing: 6) {
TextField("Записать: «продлить домен до 15.11, 1200 ₽»", text: $text)
.textFieldStyle(.plain)
.font(.system(size: 12.5))
.focused($focused)
.onSubmit(send)
Button(action: send) {
Image(systemName: "arrow.up").font(.system(size: 10, weight: .bold))
.foregroundColor(Color(hex: "#0B0C0E"))
.frame(width: 20, height: 20)
.background(Circle().fill(Color(hex: text.isEmpty ? "#5F646D" : "#D97757")))
}
.buttonStyle(.plain)
.disabled(text.isEmpty || ops.busy || !ops.configured)
}
.padding(.horizontal, 10).padding(.vertical, 7)
.background(RoundedRectangle(cornerRadius: 10).fill(Color.white.opacity(0.07)))
OpsInput(text: $text, focused: $focused)
}
.padding(12)
.frame(width: 380)
.frame(width: 390)
.foregroundColor(Color(hex: "#F5F6F8"))
.onAppear { focused = true }
}
private func note(_ t: String, _ color: String) -> some View {
Text(t).font(.system(size: 11.5)).foregroundColor(Color(hex: color)).fixedSize(horizontal: false, vertical: true)
}
private func header(_ t: String, _ color: String) -> some View {
Text(t).font(.system(size: 9.5, weight: .bold)).foregroundColor(Color(hex: color))
.padding(.top, 8).padding(.bottom, 3).padding(.leading, 6)
}
private func tile(_ value: String, _ label: String, _ color: String, dim: Bool) -> some View {
VStack(alignment: .leading, spacing: 1) {
Text(value).font(.system(size: 15, weight: .semibold)).lineLimit(1).minimumScaleFactor(0.6)
.foregroundColor(Color(hex: dim ? "#F5F6F8" : color))
Text(label).font(.system(size: 10.5)).foregroundColor(Color(hex: dim ? "#6B7079" : color).opacity(dim ? 1 : 0.85))
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.horizontal, 9).padding(.vertical, 7)
.background(RoundedRectangle(cornerRadius: 9).fill(dim ? Color.white.opacity(0.05) : Color(hex: color).opacity(0.13)))
}
}
/// «Записать или спросить…» → the service's Claude.
struct OpsInput: View {
@Binding var text: String
var focused: FocusState<Bool>.Binding
@ObservedObject private var ops = OpsClient.shared
var body: some View {
HStack(spacing: 6) {
TextField("Записать или спросить: «продлить домен до 15.11, 1200 ₽»", text: $text)
.textFieldStyle(.plain)
.font(.system(size: 12.5))
.focused(focused)
.onSubmit(send)
Button(action: send) {
Image(systemName: "arrow.up").font(.system(size: 10, weight: .bold))
.foregroundColor(Color(hex: "#0B0C0E"))
.frame(width: 20, height: 20)
.background(Circle().fill(Color(hex: text.isEmpty ? "#5F646D" : "#D97757")))
}
.buttonStyle(.plain)
.disabled(text.isEmpty || ops.busy || !ops.configured)
}
.padding(.horizontal, 10).padding(.vertical, 7)
.background(RoundedRectangle(cornerRadius: 10).fill(Color.white.opacity(0.07)))
}
private func send() {
let t = text.trimmingCharacters(in: .whitespacesAndNewlines)
guard !t.isEmpty else { return }
text = ""
Task { await ops.say(t) }
}
@ViewBuilder private func section(_ title: String, _ items: [OpsItem], _ color: String) -> some View {
if !items.isEmpty {
Text(title).font(.system(size: 9.5, weight: .bold)).foregroundColor(Color(hex: color))
.padding(.top, 6).padding(.bottom, 2)
ForEach(items) { OpsRow(item: $0) }
}
}
}
/// One agenda line: ○ done on the left (₽ «Оплачено» for subscriptions), ⏱ / ⏰ on hover.
struct OpsRow: View {
let item: OpsItem
var overdue = false
@ObservedObject private var ops = OpsClient.shared
@State private var hovered = false
private var icon: String {
switch item.kind { case "sub": return "creditcard"; case "routine": return "arrow.triangle.2.circlepath"; default: return "pin" }
}
var body: some View {
HStack(spacing: 8) {
Image(systemName: icon).font(.system(size: 11)).foregroundColor(Color(hex: "#8E939C")).frame(width: 14)
if item.kind == "sub" {
Image(systemName: "creditcard").font(.system(size: 11)).foregroundColor(Color(hex: "#60A5FA")).frame(width: 16)
} else {
Button { Task { await ops.done(item) } } label: {
Image(systemName: hovered ? "checkmark.circle" : "circle")
.font(.system(size: 13))
.foregroundColor(Color(hex: overdue ? "#F4505E" : (item.kind == "routine" ? "#34D399" : "#8E939C")))
.frame(width: 16)
}
.buttonStyle(.plain)
.help(item.kind == "routine" ? "Сделано (следующий раз посчитается сам)" : "Сделано")
}
VStack(alignment: .leading, spacing: 1) {
Text(item.title).font(.system(size: 12, weight: .medium)).lineLimit(1)
Text(subtitle).font(.system(size: 10.5)).foregroundColor(Color(hex: "#6B7079")).lineLimit(1)
HStack(spacing: 4) {
if item.kind == "routine" {
Image(systemName: "arrow.triangle.2.circlepath").font(.system(size: 9)).foregroundColor(Color(hex: "#34D399"))
}
Text(item.title).font(.system(size: 12, weight: .medium)).lineLimit(1)
}
Text(subtitle).font(.system(size: 10.5)).foregroundColor(Color(hex: overdue ? "#F4505E" : "#6B7079")).lineLimit(1)
}
Spacer(minLength: 4)
if hovered {
if item.kind == "task" {
small("timer", "Засечь время") { Task { await ops.act(["op": "start_timer", "id": item.ref]) } }
}
small("clock.arrow.circlepath", "Завтра") {
small("moon.zzz", "Перенести на завтра") {
Task { await ops.act(["op": "snooze", "kind": item.kind, "id": item.ref, "days": 1]) }
}
}
small(item.kind == "sub" ? "rublesign.circle" : "checkmark.circle",
item.kind == "sub" ? "Оплачено" : "Сделано") { Task { await ops.done(item) } }
if item.kind == "sub" {
Button { Task { await ops.done(item) } } label: {
Text("Оплачено").font(.system(size: 10.5, weight: .medium)).foregroundColor(Color(hex: "#60A5FA"))
.padding(.horizontal, 7).frame(height: 18)
.background(Capsule().fill(Color(hex: "#60A5FA").opacity(0.14)))
}
.buttonStyle(.plain)
}
}
.padding(.horizontal, 6).padding(.vertical, 5)
.background(RoundedRectangle(cornerRadius: 7).fill(Color.white.opacity(hovered ? 0.06 : 0)))
@@ -272,21 +433,22 @@ struct OpsRow: View {
}
private var subtitle: String {
var parts = [Self.ruDay(item.date)]
if let a = item.amount { parts.append("\(a.formatted()) \(item.currency ?? "")") }
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) }
return parts.joined(separator: " · ")
}
/// 2026-11-15 → 15.11.2026
static func ruDay(_ iso: String) -> String {
let p = iso.prefix(10).split(separator: "-")
return p.count == 3 ? "\(p[2]).\(p[1]).\(p[0])" : iso
static func daysLate(_ iso: String) -> Int? {
let f = DateFormatter(); f.dateFormat = "yyyy-MM-dd"
guard let d = f.date(from: String(iso.prefix(10))) else { return nil }
return Calendar.current.dateComponents([.day], from: d, to: Calendar.current.startOfDay(for: Date())).day
}
private func small(_ symbol: String, _ help: String, _ action: @escaping () -> Void) -> some View {
Button(action: action) {
Image(systemName: symbol).font(.system(size: 13)).foregroundColor(Color(hex: "#B0B5BE"))
Image(systemName: symbol).font(.system(size: 12)).foregroundColor(Color(hex: "#B0B5BE"))
}
.buttonStyle(.plain)
.help(help)