Glimmer / app · glimmertoday.ai / native / Glimmer / Unload.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | /// Unload: a brain dump becomes a reviewed list of personal to-dos. |
| 4 | struct UnloadView: View { |
| 5 | @Environment(JournalStore.self) private var store |
| 6 | @Environment(\.dismiss) private var dismiss |
| 7 | @Environment(\.readingTheme) private var theme |
| 8 | @AppStorage(StepSize.preference) private var stepSize = StepSize.balanced.rawValue |
| 9 | @SceneStorage("glimmer.toolkit.unload") private var text = "" |
| 10 | @State private var items: [UnloadItem] = [] |
| 11 | @State private var result: (source: ToolkitSource, note: String?)? |
| 12 | @State private var breakEachDown = false |
| 13 | @State private var working: Task<Void, Never>? |
| 14 | @State private var progress: String? |
| 15 | |
| 16 | private var chosen: [UnloadItem] { |
| 17 | items.filter { $0.included && !$0.title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty } |
| 18 | } |
| 19 | |
| 20 | var body: some View { |
| 21 | Page(title: "Unload", subtitle: "Get it all out of your head. Glimmer sorts it into to-dos you can check before anything is added.", width: 620) { |
| 22 | if items.isEmpty { |
| 23 | Panel { |
| 24 | ToolkitTextEditor(text: $text, prompt: "Everything on your mind, in any order…", minHeight: 220) |
| 25 | .accessibilityIdentifier("unload.text") |
| 26 | Text("Lists, half-sentences and “oh and also” are all fine.") |
| 27 | .font(.footnote).foregroundStyle(.secondary) |
| 28 | } |
| 29 | if let progress { Panel { ToolkitThinking(text: progress) } } |
| 30 | } else { |
| 31 | review |
| 32 | } |
| 33 | } |
| 34 | .actionBar(width: 620) { |
| 35 | if items.isEmpty { |
| 36 | PrimaryAction(title: "Sort it out", systemImage: "tray.and.arrow.down", action: sort) |
| 37 | .disabled(working != nil || text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) |
| 38 | .accessibilityIdentifier("unload.sort") |
| 39 | } else { |
| 40 | Button("Start over", systemImage: "arrow.uturn.backward") { |
| 41 | items = [] |
| 42 | result = nil |
| 43 | } |
| 44 | .glimmerButton().controlSize(.large).disabled(working != nil) |
| 45 | PrimaryAction(title: chosen.count == 1 ? "Add 1 to-do" : "Add \(chosen.count) to-dos", action: add) |
| 46 | .disabled(working != nil || chosen.isEmpty) |
| 47 | .accessibilityIdentifier("unload.add") |
| 48 | } |
| 49 | } |
| 50 | .onDisappear { working?.cancel() } |
| 51 | #if DEBUG |
| 52 | .onAppear { |
| 53 | if let sample = ProcessInfo.processInfo.environment["GLIMMER_UNLOAD_TEXT"], items.isEmpty { |
| 54 | text = sample.replacingOccurrences(of: "|", with: "\n") |
| 55 | if ProcessInfo.processInfo.environment["GLIMMER_TOOLKIT_AUTORUN"] == "1" { sort() } |
| 56 | } |
| 57 | } |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | private var review: some View { |
| 62 | Group { |
| 63 | Panel(title: "To-dos") { |
| 64 | VStack(spacing: 0) { |
| 65 | ForEach($items) { $item in |
| 66 | HStack(spacing: 2) { |
| 67 | TickButton(done: item.included, label: item.included ? "Include \(item.title)" : "Skip \(item.title)", |
| 68 | tint: theme.selectionInk, onTint: theme.selectionOnInk, completes: false) { |
| 69 | item.included.toggle() |
| 70 | } |
| 71 | .accessibilityValue(item.included ? "Included" : "Skipped") |
| 72 | TextField("To-do", text: $item.title, axis: .vertical) |
| 73 | .textFieldStyle(.plain) |
| 74 | .foregroundStyle(item.included ? .primary : .secondary) |
| 75 | .frame(maxWidth: .infinity, minHeight: 44, alignment: .leading) |
| 76 | if let due = item.dueDay, let date = PracticeDate.parse(due) { |
| 77 | Menu { |
| 78 | Button("Remove due date", systemImage: "calendar.badge.minus") { item.dueDay = nil } |
| 79 | } label: { |
| 80 | Text(dueLabel(due, date)).font(.footnote.weight(.medium)) |
| 81 | .padding(.horizontal, 9).padding(.vertical, 4) |
| 82 | .background(Color.primary.opacity(0.07), in: .capsule) |
| 83 | } |
| 84 | .menuStyle(.button).buttonStyle(.plain).menuIndicator(.hidden) |
| 85 | .accessibilityLabel("Due \(dueLabel(due, date))") |
| 86 | } |
| 87 | } |
| 88 | Divider().opacity(0.6).padding(.leading, 46) |
| 89 | } |
| 90 | Button { items.append(UnloadItem(title: "")) } label: { |
| 91 | HStack(spacing: 2) { |
| 92 | AddMark() |
| 93 | Text("Add another").foregroundStyle(.secondary) |
| 94 | Spacer(minLength: 0) |
| 95 | } |
| 96 | .frame(maxWidth: .infinity, minHeight: 44, alignment: .leading).contentShape(.rect) |
| 97 | } |
| 98 | .buttonStyle(RowPressStyle()) |
| 99 | .accessibilityLabel("Add another") |
| 100 | } |
| 101 | .padding(.leading, -8) |
| 102 | if let result { ToolkitSourceLabel(source: result.source, note: result.note) } |
| 103 | } |
| 104 | Panel { |
| 105 | Toggle("Break each one down too", isOn: $breakEachDown).toggleStyle(.switch) |
| 106 | .accessibilityIdentifier("unload.breakDown") |
| 107 | if breakEachDown { |
| 108 | Text("Uses your step size: \((StepSize(rawValue: stepSize) ?? .balanced).title.lowercased()).") |
| 109 | .font(.footnote).foregroundStyle(.secondary) |
| 110 | } |
| 111 | if let progress { ToolkitThinking(text: progress) } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | private func dueLabel(_ day: String, _ date: Date) -> String { |
| 117 | if day == PracticeDate.key(store.day) { return "Today" } |
| 118 | if let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: store.day), day == PracticeDate.key(tomorrow) { |
| 119 | return "Tomorrow" |
| 120 | } |
| 121 | return date.formatted(.dateTime.weekday(.abbreviated).day()) |
| 122 | } |
| 123 | |
| 124 | private func sort() { |
| 125 | let dump = text |
| 126 | let today = store.day |
| 127 | progress = ToolkitAI.agentChoice.map { "Asking \($0.agent.title)…" } ?? "Sorting…" |
| 128 | working = Task { |
| 129 | let answer = await ToolkitAI.unload(dump, today: today) |
| 130 | guard !Task.isCancelled else { return } |
| 131 | items = answer.value |
| 132 | result = (answer.source, answer.note) |
| 133 | progress = nil |
| 134 | working = nil |
| 135 | if items.isEmpty { store.notice = "Couldn’t find any to-dos in that. Try one per line." } |
| 136 | AccessibilityNotification.Announcement("\(items.count) to-dos found").post() |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | private func add() { |
| 141 | let todos = chosen.map { |
| 142 | PersonalTodo(title: String($0.title.trimmingCharacters(in: .whitespacesAndNewlines).prefix(PersonalTodo.titleLimit)), |
| 143 | dueDay: $0.dueDay) |
| 144 | } |
| 145 | guard store.change({ $0.personalTodos = ($0.personalTodos ?? []) + todos }) else { return } |
| 146 | let size = StepSize(rawValue: stepSize) ?? .balanced |
| 147 | guard breakEachDown else { finish(todos.count) ; return } |
| 148 | working = Task { |
| 149 | for (index, todo) in todos.enumerated() { |
| 150 | progress = "Breaking down \(index + 1) of \(todos.count)…" |
| 151 | let steps = await ToolkitAI.breakDown(todo.title, size: size) |
| 152 | if Task.isCancelled { break } |
| 153 | store.change { $0.replaceSteps(for: todo.id, with: steps.value) } |
| 154 | } |
| 155 | progress = nil |
| 156 | working = nil |
| 157 | finish(todos.count) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | private func finish(_ count: Int) { |
| 162 | items = [] |
| 163 | result = nil |
| 164 | text = "" |
| 165 | store.notice = count == 1 ? "1 to-do added to your plan." : "\(count) to-dos added to your plan." |
| 166 | } |
| 167 | } |