Subscriptions: «Оплачено?» → paid status → «repeat?»; edit and create everything
- ops: paying marks the current charge paid (paid_for, persisted) instead of jumping to the next date; repeat_sub moves to the next period or closes the subscription. Paid-and-undecided subscriptions leave reminders and show in agenda['paid']. update_sub can set paid; tasks reopen when their status goes back; delete for subs and routines (BroV only, /api/delete); /api/tasks?all=1 and /api/subs?all=1. - Bot: «₽ Оплачено?» on reminders, then «🔁 Да, <date> / Нет, закрыть»; after paying by text it asks the same with buttons. - BroV: «Оплачено?» everywhere; «Оплачено — повторить?» section under the notch; «Весь план»: forms to edit or create tasks (title, description, status, due, company), subscriptions (amount, currency, period, date, reminder, company, paid, active) and routines, delete with confirmation, «+» per list, done tasks on demand. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,279 @@
|
||||
import SwiftUI
|
||||
|
||||
// MARK: - Edit / create forms for «Весь план» (tasks, subscriptions, routines)
|
||||
//
|
||||
// Everything goes through the service's /api/action (the same checked operations Claude
|
||||
// uses); deletion through /api/delete, only by an explicit click and a confirmation.
|
||||
|
||||
enum OpsEdit: Identifiable {
|
||||
case task(OpsTask?), sub(OpsSub?), routine(OpsRoutine?)
|
||||
var id: String {
|
||||
switch self {
|
||||
case .task(let t): return "task-\(t?.id ?? 0)"
|
||||
case .sub(let s): return "sub-\(s?.id ?? 0)"
|
||||
case .routine(let r): return "routine-\(r?.id ?? 0)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct OpsEditSheet: View {
|
||||
let edit: OpsEdit
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
var body: some View {
|
||||
Group {
|
||||
switch edit {
|
||||
case .task(let t): TaskForm(task: t, close: { dismiss() })
|
||||
case .sub(let s): SubForm(sub: s, close: { dismiss() })
|
||||
case .routine(let r): RoutineForm(routine: r, close: { dismiss() })
|
||||
}
|
||||
}
|
||||
.frame(width: 440)
|
||||
.padding(18)
|
||||
.preferredColorScheme(.dark)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Shared bits
|
||||
|
||||
enum OpsDate {
|
||||
private static let f: DateFormatter = {
|
||||
let f = DateFormatter(); f.calendar = Calendar(identifier: .gregorian); f.dateFormat = "yyyy-MM-dd"; return f
|
||||
}()
|
||||
static func date(_ iso: String?) -> Date? { iso.flatMap { f.date(from: String($0.prefix(10))) } }
|
||||
static func iso(_ d: Date) -> String { f.string(from: d) }
|
||||
}
|
||||
|
||||
/// Company: none, the three known ones, or any other name.
|
||||
struct ClientPicker: View {
|
||||
@Binding var client: String
|
||||
private let known = ["", "saga", "planet9", "home"]
|
||||
@State private var custom = false
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
Picker("Компания", selection: Binding(
|
||||
get: { custom || !known.contains(client) ? "other" : client },
|
||||
set: { v in
|
||||
if v == "other" { custom = true; if known.contains(client) { client = "" } }
|
||||
else { custom = false; client = v }
|
||||
})) {
|
||||
Text("—").tag("")
|
||||
Text("Сага").tag("saga")
|
||||
Text("Planet9").tag("planet9")
|
||||
Text("Дом").tag("home")
|
||||
Text("Другая…").tag("other")
|
||||
}
|
||||
if custom || !known.contains(client) {
|
||||
TextField("название", text: $client).textFieldStyle(.roundedBorder).frame(width: 140)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct FormButtons: View {
|
||||
let isNew: Bool
|
||||
let canSave: Bool
|
||||
let save: () -> Void
|
||||
let delete: () -> Void
|
||||
let close: () -> Void
|
||||
@State private var confirm = false
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
if !isNew {
|
||||
Button("Удалить", role: .destructive) { confirm = true }
|
||||
.confirmationDialog("Удалить без возможности вернуть?", isPresented: $confirm) {
|
||||
Button("Удалить", role: .destructive) { delete() }
|
||||
Button("Отмена", role: .cancel) {}
|
||||
}
|
||||
}
|
||||
Spacer()
|
||||
Button("Отмена", action: close).keyboardShortcut(.cancelAction)
|
||||
Button(isNew ? "Создать" : "Сохранить", action: save)
|
||||
.keyboardShortcut(.defaultAction)
|
||||
.buttonStyle(.borderedProminent)
|
||||
.disabled(!canSave)
|
||||
}
|
||||
.padding(.top, 6)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Task
|
||||
|
||||
struct TaskForm: View {
|
||||
let task: OpsTask?
|
||||
let close: () -> Void
|
||||
@ObservedObject private var ops = OpsClient.shared
|
||||
@State private var title = ""
|
||||
@State private var notes = ""
|
||||
@State private var status = "todo"
|
||||
@State private var hasDue = false
|
||||
@State private var due = Date()
|
||||
@State private var client = ""
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 10) {
|
||||
Text(task == nil ? "Новая задача" : "Задача #\(task!.id)").font(.system(size: 16, weight: .semibold))
|
||||
TextField("Что сделать", text: $title).textFieldStyle(.roundedBorder)
|
||||
Text("Описание").font(.system(size: 11)).foregroundColor(.secondary)
|
||||
TextEditor(text: $notes).font(.system(size: 12.5)).frame(height: 90)
|
||||
.overlay(RoundedRectangle(cornerRadius: 6).stroke(Color.white.opacity(0.12)))
|
||||
Picker("Статус", selection: $status) {
|
||||
Text("Не начата").tag("todo"); Text("В работе").tag("doing"); Text("Сделана").tag("done")
|
||||
}
|
||||
.pickerStyle(.segmented)
|
||||
HStack {
|
||||
Toggle("Срок", isOn: $hasDue)
|
||||
if hasDue { DatePicker("", selection: $due, displayedComponents: .date).labelsHidden() }
|
||||
Spacer()
|
||||
}
|
||||
ClientPicker(client: $client)
|
||||
FormButtons(isNew: task == nil, canSave: !title.trimmingCharacters(in: .whitespaces).isEmpty,
|
||||
save: save, delete: { Task { await ops.delete("task", task!.id); close() } }, close: close)
|
||||
}
|
||||
.onAppear {
|
||||
guard let t = task else { return }
|
||||
title = t.title; notes = t.notes; status = t.status; client = t.client
|
||||
if let d = OpsDate.date(t.due) { hasDue = true; due = d }
|
||||
}
|
||||
}
|
||||
|
||||
private func save() {
|
||||
var body: [String: Any] = ["title": title, "notes": notes, "client": client.lowercased(),
|
||||
"due": hasDue ? OpsDate.iso(due) : ""]
|
||||
Task {
|
||||
if let t = task {
|
||||
body["op"] = "update_task"; body["id"] = t.id; body["status"] = status
|
||||
await ops.act(body)
|
||||
} else {
|
||||
body["op"] = "add_task"
|
||||
if !hasDue { body.removeValue(forKey: "due") }
|
||||
await ops.act(body)
|
||||
if status != "todo", let new = ops.tasks.flatMap({ [$0] + $0.children }).max(by: { $0.id < $1.id }) {
|
||||
await ops.act(["op": "update_task", "id": new.id, "status": status])
|
||||
}
|
||||
}
|
||||
close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Subscription
|
||||
|
||||
struct SubForm: View {
|
||||
let sub: OpsSub?
|
||||
let close: () -> Void
|
||||
@ObservedObject private var ops = OpsClient.shared
|
||||
@State private var name = ""
|
||||
@State private var amount = ""
|
||||
@State private var currency = "RUB"
|
||||
@State private var period = "month"
|
||||
@State private var every = 1
|
||||
@State private var next = Date()
|
||||
@State private var remind = 3
|
||||
@State private var client = ""
|
||||
@State private var paid = false
|
||||
@State private var active = true
|
||||
@State private var notes = ""
|
||||
|
||||
private var amountValue: Double? { Double(amount.replacingOccurrences(of: ",", with: ".").replacingOccurrences(of: " ", with: "")) }
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 10) {
|
||||
Text(sub == nil ? "Новая подписка" : "Подписка #\(sub!.id)").font(.system(size: 16, weight: .semibold))
|
||||
TextField("Название (Claude Code, домен sanjeev.ru…)", text: $name).textFieldStyle(.roundedBorder)
|
||||
HStack {
|
||||
TextField("Сумма", text: $amount).textFieldStyle(.roundedBorder).frame(width: 110)
|
||||
Picker("", selection: $currency) { Text("₽").tag("RUB"); Text("$").tag("USD"); Text("€").tag("EUR") }
|
||||
.labelsHidden().frame(width: 70)
|
||||
Text("каждые").foregroundColor(.secondary)
|
||||
Stepper("\(every)", value: $every, in: 1...36).frame(width: 70)
|
||||
Picker("", selection: $period) {
|
||||
Text("день").tag("day"); Text("нед.").tag("week"); Text("мес.").tag("month"); Text("год").tag("year")
|
||||
}
|
||||
.labelsHidden().frame(width: 80)
|
||||
}
|
||||
HStack {
|
||||
Text("Следующее списание").foregroundColor(.secondary)
|
||||
DatePicker("", selection: $next, displayedComponents: .date).labelsHidden()
|
||||
Spacer()
|
||||
}
|
||||
Stepper("Напомнить за \(remind) дн.", value: $remind, in: 0...60)
|
||||
ClientPicker(client: $client)
|
||||
HStack(spacing: 18) {
|
||||
Toggle("Оплачено (текущее списание)", isOn: $paid)
|
||||
Toggle("Активна", isOn: $active)
|
||||
}
|
||||
TextField("Заметка", text: $notes).textFieldStyle(.roundedBorder)
|
||||
FormButtons(isNew: sub == nil, canSave: !name.trimmingCharacters(in: .whitespaces).isEmpty && amountValue != nil,
|
||||
save: save, delete: { Task { await ops.delete("sub", sub!.id); close() } }, close: close)
|
||||
}
|
||||
.onAppear {
|
||||
guard let s = sub else { return }
|
||||
name = s.name; amount = OpsClient.amount(s.amount).replacingOccurrences(of: " ", with: ""); currency = s.currency
|
||||
period = s.period; every = s.every; next = OpsDate.date(s.next) ?? Date(); remind = s.remindDays
|
||||
client = s.client; paid = s.paid; active = s.active; notes = s.notes
|
||||
}
|
||||
}
|
||||
|
||||
private func save() {
|
||||
var body: [String: Any] = ["name": name, "amount": amountValue ?? 0, "currency": currency, "period": period,
|
||||
"every": every, "next_date": OpsDate.iso(next), "remind_days": remind,
|
||||
"client": client.lowercased(), "notes": notes]
|
||||
Task {
|
||||
if let s = sub {
|
||||
body["op"] = "update_sub"; body["id"] = s.id; body["paid"] = paid; body["active"] = active ? 1 : 0
|
||||
await ops.act(body)
|
||||
} else {
|
||||
body["op"] = "add_sub"
|
||||
await ops.act(body)
|
||||
if paid, let new = ops.subs.max(by: { $0.id < $1.id }) {
|
||||
await ops.act(["op": "update_sub", "id": new.id, "paid": true])
|
||||
}
|
||||
}
|
||||
close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Routine
|
||||
|
||||
struct RoutineForm: View {
|
||||
let routine: OpsRoutine?
|
||||
let close: () -> Void
|
||||
@ObservedObject private var ops = OpsClient.shared
|
||||
@State private var title = ""
|
||||
@State private var everyDays = 7
|
||||
@State private var next = Date()
|
||||
@State private var notes = ""
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 10) {
|
||||
Text(routine == nil ? "Новое регламентное дело" : "Регламент #\(routine!.id)").font(.system(size: 16, weight: .semibold))
|
||||
TextField("Что делать (бэкап NAS, обновления…)", text: $title).textFieldStyle(.roundedBorder)
|
||||
Stepper("Раз в \(everyDays) дн.", value: $everyDays, in: 1...365)
|
||||
HStack {
|
||||
Text("Следующий раз").foregroundColor(.secondary)
|
||||
DatePicker("", selection: $next, displayedComponents: .date).labelsHidden()
|
||||
Spacer()
|
||||
}
|
||||
TextField("Заметка", text: $notes).textFieldStyle(.roundedBorder)
|
||||
FormButtons(isNew: routine == nil, canSave: !title.trimmingCharacters(in: .whitespaces).isEmpty,
|
||||
save: save, delete: { Task { await ops.delete("routine", routine!.id); close() } }, close: close)
|
||||
}
|
||||
.onAppear {
|
||||
guard let r = routine else { return }
|
||||
title = r.title; everyDays = r.everyDays; next = OpsDate.date(r.next) ?? Date(); notes = r.notes
|
||||
}
|
||||
}
|
||||
|
||||
private func save() {
|
||||
var body: [String: Any] = ["title": title, "every_days": everyDays, "next_date": OpsDate.iso(next), "notes": notes]
|
||||
Task {
|
||||
if let r = routine { body["op"] = "update_routine"; body["id"] = r.id } else { body["op"] = "add_routine" }
|
||||
await ops.act(body)
|
||||
close()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user