Glimmer / app · glimmertoday.ai / native / Glimmer / PersonalTodos.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | struct PersonalTodoList: View { |
| 4 | @Environment(JournalStore.self) private var store |
| 5 | @State private var editing: PersonalTodo? |
| 6 | @State private var tool: TodoTool? |
| 7 | var openPlan: (() -> Void)? = nil |
| 8 | |
| 9 | private var isToday: Bool { openPlan != nil } |
| 10 | private var ordered: [PersonalTodo] { PersonalTodo.ordered(store.journal.personalTodos ?? []) } |
| 11 | private var open: [PersonalTodo] { ordered.filter { !$0.completed } } |
| 12 | private var completed: [PersonalTodo] { ordered.filter(\.completed) } |
| 13 | |
| 14 | var body: some View { |
| 15 | Group { |
| 16 | if isToday { |
| 17 | if !store.duePersonalTodos.isEmpty { |
| 18 | Panel(title: "Personal to-dos") { |
| 19 | rows(Array(store.duePersonalTodos.prefix(4))) |
| 20 | if store.duePersonalTodos.count > 4, let openPlan { |
| 21 | SecondaryAction(title: "All to-dos", systemImage: "chevron.right", action: openPlan) |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | } else { |
| 26 | Panel(title: "Personal to-dos") { |
| 27 | if open.isEmpty { Text("No open to-dos.").font(.callout).foregroundStyle(.secondary) } |
| 28 | if dueMinutes > 0 { |
| 29 | Label("About \(ToolkitFormat.minutes(dueMinutes)) of to-dos due today", systemImage: "clock") |
| 30 | .font(.callout).foregroundStyle(.secondary) |
| 31 | .accessibilityIdentifier("personalTodos.dueTotal") |
| 32 | } |
| 33 | VStack(alignment: .leading, spacing: 0) { |
| 34 | rows(open) |
| 35 | if !open.isEmpty { Divider().padding(.leading, 44) } |
| 36 | Button { editing = PersonalTodo() } label: { |
| 37 | HStack(spacing: 0) { |
| 38 | AddMark() |
| 39 | Text("Add to-do").foregroundStyle(.secondary).padding(.leading, 2) |
| 40 | Spacer(minLength: 0) |
| 41 | } |
| 42 | .frame(maxWidth: .infinity, minHeight: 52, alignment: .leading) |
| 43 | .contentShape(.rect) |
| 44 | } |
| 45 | .buttonStyle(RowPressStyle()) |
| 46 | .padding(.leading, -8) |
| 47 | .accessibilityLabel("Add to-do") |
| 48 | .accessibilityIdentifier("personalTodos.add") |
| 49 | } |
| 50 | FlowLayout(spacing: 8) { |
| 51 | NavigationLink { UnloadView().hidesTabBar() } label: { |
| 52 | Label("Unload", systemImage: "tray.and.arrow.down").fontWeight(.medium) |
| 53 | .frame(minHeight: 24) |
| 54 | } |
| 55 | .glimmerButton().buttonBorderShape(.capsule) |
| 56 | .accessibilityHint("Turn a brain dump into to-dos") |
| 57 | .accessibilityIdentifier("personalTodos.unload") |
| 58 | if !open.isEmpty { |
| 59 | NavigationLink { OneThingView() } label: { |
| 60 | Label("One thing", systemImage: "scope").fontWeight(.medium) |
| 61 | .frame(minHeight: 24) |
| 62 | } |
| 63 | .glimmerButton().buttonBorderShape(.capsule) |
| 64 | .accessibilityHint("Focus on the next step of one to-do") |
| 65 | .accessibilityIdentifier("personalTodos.oneThing") |
| 66 | } |
| 67 | } |
| 68 | if !completed.isEmpty { |
| 69 | Divider() |
| 70 | DisclosureGroup("Completed (\(completed.count))") { rows(completed) } |
| 71 | .accessibilityIdentifier("personalTodos.completed") |
| 72 | } |
| 73 | if store.usesDeviceReminders { reminderStatus } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | .sheet(item: $editing) { PersonalTodoEditor(task: $0, today: store.day) } |
| 78 | .todoToolSheet($tool) |
| 79 | } |
| 80 | |
| 81 | private var dueMinutes: Int { |
| 82 | store.duePersonalTodos.compactMap { store.journal.estimate(for: $0.id)?.minutes }.reduce(0, +) |
| 83 | } |
| 84 | |
| 85 | @ViewBuilder private var reminderStatus: some View { |
| 86 | let reminders = PersonalReminders.shared |
| 87 | if open.contains(where: { $0.reminderAt.map { $0 > .now } == true }) { |
| 88 | if reminders.authorization == .notDetermined { |
| 89 | SecondaryAction(title: "Enable notifications", systemImage: "bell") { |
| 90 | Task { |
| 91 | _ = await reminders.authorize() |
| 92 | reminders.refresh(store.journal.personalTodos ?? []) |
| 93 | } |
| 94 | } |
| 95 | } else if reminders.authorization == .denied { |
| 96 | Text("Turn on Glimmer notifications in system settings to receive reminders here.") |
| 97 | .font(.footnote).foregroundStyle(.secondary) |
| 98 | } else if reminders.schedulingFailed { |
| 99 | Text("Glimmer couldn’t schedule a reminder on this device.").font(.footnote) |
| 100 | .foregroundStyle(.secondary) |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | private func rows(_ tasks: [PersonalTodo]) -> some View { |
| 106 | VStack(alignment: .leading, spacing: 0) { |
| 107 | ForEach(tasks) { task in |
| 108 | if task.id != tasks.first?.id { Divider().padding(.leading, 52) } |
| 109 | PersonalTodoRow(task: task, edit: { editing = task }, use: { tool = $0 }) |
| 110 | } |
| 111 | } |
| 112 | // The tick sits in a 44 point target; pull it out so the mark lines up with the heading. |
| 113 | .padding(.leading, -8) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | private struct PersonalTodoRow: View { |
| 118 | @Environment(JournalStore.self) private var store |
| 119 | let task: PersonalTodo |
| 120 | let edit: () -> Void |
| 121 | let use: (TodoTool) -> Void |
| 122 | |
| 123 | private var steps: [TodoStep] { store.journal.flatSteps(for: task.id) } |
| 124 | private var estimate: Int? { store.journal.estimate(for: task.id)?.minutes } |
| 125 | private var toolkitLabel: String? { |
| 126 | var parts: [String] = [] |
| 127 | if !steps.isEmpty { parts.append("\(steps.filter(\.done).count) of \(steps.count) steps") } |
| 128 | if let estimate, !task.completed { parts.append(ToolkitFormat.chip(estimate)) } |
| 129 | return parts.isEmpty ? nil : parts.joined(separator: " · ") |
| 130 | } |
| 131 | |
| 132 | private var today: String { PracticeDate.key(store.day) } |
| 133 | private var dueLabel: String? { |
| 134 | guard let due = task.dueDay, let date = PracticeDate.parse(due) else { return nil } |
| 135 | if due == today { return "Due today" } |
| 136 | let label = date.formatted(.dateTime.month(.abbreviated).day()) |
| 137 | return due < today && !task.completed ? "Overdue · \(label)" : "Due \(label)" |
| 138 | } |
| 139 | |
| 140 | var body: some View { |
| 141 | HStack(spacing: 0) { |
| 142 | TickButton(done: task.completed, label: "\(task.completed ? "Reopen" : "Complete") \(task.title)") { |
| 143 | store.setPersonalTodoDone(task.id, done: !task.completed) |
| 144 | } |
| 145 | |
| 146 | Button(action: edit) { |
| 147 | HStack(spacing: 10) { |
| 148 | GlyphTile(Glyph.symbol(name: task.title, category: "todo"), tint: Glyph.tint(for: "todo")) |
| 149 | VStack(alignment: .leading, spacing: 3) { |
| 150 | Text(task.title).font(.body).foregroundStyle(task.completed ? Color.secondary : Color.primary) |
| 151 | if dueLabel != nil || toolkitLabel != nil { |
| 152 | HStack(spacing: 6) { |
| 153 | if let dueLabel { |
| 154 | Text(dueLabel) |
| 155 | .foregroundStyle(task.dueDay.map { $0 < today && !task.completed } == true |
| 156 | ? GlimmerColor.ember : Color.secondary) |
| 157 | } |
| 158 | if dueLabel != nil && toolkitLabel != nil { Text("·").foregroundStyle(.secondary) } |
| 159 | if let toolkitLabel { Text(toolkitLabel).foregroundStyle(.secondary) } |
| 160 | } |
| 161 | .font(.footnote) |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | .padding(.leading, 2) |
| 166 | .frame(maxWidth: .infinity, minHeight: 44, alignment: .leading) |
| 167 | .padding(.vertical, 4) |
| 168 | .contentShape(.rect) |
| 169 | .multilineTextAlignment(.leading) |
| 170 | } |
| 171 | .buttonStyle(RowPressStyle()) |
| 172 | .accessibilityHint("Edit to-do") |
| 173 | |
| 174 | if let reminder = task.reminderAt, reminder > .now, !task.completed { |
| 175 | Image(systemName: "bell").font(.footnote).foregroundStyle(.secondary) |
| 176 | .accessibilityLabel("Reminder \(reminder.formatted(date: .abbreviated, time: .shortened))") |
| 177 | } |
| 178 | if !task.completed { |
| 179 | Menu { |
| 180 | Button("Break it down", systemImage: "list.bullet.indent") { use(.breakDown(task, parent: nil)) } |
| 181 | Button("Time check", systemImage: "clock") { use(.timeCheck(task)) } |
| 182 | Button("One thing", systemImage: "scope") { use(.focus(task)) } |
| 183 | } label: { |
| 184 | Image(systemName: "ellipsis").font(.body.weight(.medium)) |
| 185 | .frame(width: 36, height: 44).contentShape(.rect) |
| 186 | } |
| 187 | .menuStyle(.button).buttonStyle(.plain).menuIndicator(.hidden) |
| 188 | .foregroundStyle(.secondary) |
| 189 | .accessibilityLabel("Toolkit for \(task.title)") |
| 190 | .accessibilityIdentifier("personalTodos.tools") |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | struct PersonalTodoEditor: View { |
| 197 | @Environment(JournalStore.self) private var store |
| 198 | @Environment(\.dismiss) private var dismiss |
| 199 | @Environment(\.readingTheme) private var theme |
| 200 | @State private var title: String |
| 201 | @State private var hasDue: Bool |
| 202 | @State private var due: Date |
| 203 | @State private var hasReminder: Bool |
| 204 | @State private var reminder: Date |
| 205 | @State private var showingCalendar = false |
| 206 | @State private var confirmDelete = false |
| 207 | @State private var saving = false |
| 208 | @State private var tool: TodoTool? |
| 209 | let task: PersonalTodo |
| 210 | |
| 211 | init(task: PersonalTodo, today: Date) { |
| 212 | self.task = task |
| 213 | _title = State(initialValue: task.title) |
| 214 | _hasDue = State(initialValue: task.dueDay != nil) |
| 215 | _due = State(initialValue: task.dueDay.flatMap(PracticeDate.parse) ?? today) |
| 216 | _hasReminder = State(initialValue: task.reminderAt != nil) |
| 217 | let nextMinute = Date(timeIntervalSince1970: ceil(Date.now.timeIntervalSince1970 / 60) * 60 + 60) |
| 218 | _reminder = State(initialValue: task.reminderAt ?? nextMinute) |
| 219 | } |
| 220 | |
| 221 | private var exists: Bool { !task.title.isEmpty } |
| 222 | private var cleanTitle: String { title.trimmingCharacters(in: .whitespacesAndNewlines) } |
| 223 | private var validReminder: Bool { !hasReminder || reminder > .now || reminder == task.reminderAt } |
| 224 | |
| 225 | var body: some View { |
| 226 | NavigationStack { |
| 227 | Page(title: exists ? "Edit to-do" : "New to-do", width: 460) { |
| 228 | Panel { |
| 229 | TextField("What do you need to do?", text: $title).glimmerField() |
| 230 | .accessibilityIdentifier("personalTodos.title") |
| 231 | if cleanTitle.count > PersonalTodo.titleLimit { |
| 232 | Text("Use 160 characters or fewer.").font(.footnote).foregroundStyle(.secondary) |
| 233 | } |
| 234 | } |
| 235 | Panel { |
| 236 | Toggle("Due date", isOn: $hasDue).toggleStyle(.switch) |
| 237 | if hasDue { |
| 238 | Button { |
| 239 | showingCalendar = true |
| 240 | } label: { |
| 241 | Label(due.formatted(date: .abbreviated, time: .omitted), systemImage: "calendar") |
| 242 | } |
| 243 | .glimmerButton() |
| 244 | .accessibilityIdentifier("personalTodos.dueDate") |
| 245 | .popover(isPresented: $showingCalendar) { |
| 246 | VStack(alignment: .trailing, spacing: 8) { |
| 247 | GlimmerCalendarGrid(selection: $due) |
| 248 | Button("Done") { showingCalendar = false }.glimmerButton(.prominent) |
| 249 | } |
| 250 | .padding(16) |
| 251 | .frame(idealWidth: 320, maxWidth: 360) |
| 252 | .background(theme.surface) |
| 253 | } |
| 254 | } |
| 255 | Divider() |
| 256 | Toggle("Remind me", isOn: $hasReminder).toggleStyle(.switch) |
| 257 | if hasReminder { |
| 258 | DatePicker("Reminder time", selection: $reminder).tint(theme.selectionInk) |
| 259 | .accessibilityIdentifier("personalTodos.reminderAt") |
| 260 | Text(validReminder ? "Alerts on this device when notifications are enabled." : "Choose a future time.") |
| 261 | .font(.footnote).foregroundStyle(.secondary) |
| 262 | } |
| 263 | } |
| 264 | if exists, let current = store.journal.personalTodos?.first(where: { $0.id == task.id }) { |
| 265 | TodoStepsPanel(todo: current, tool: $tool) |
| 266 | } |
| 267 | } |
| 268 | .actionBar(width: 460) { |
| 269 | SheetCancel { dismiss() } |
| 270 | if exists { |
| 271 | Button("Delete to-do", systemImage: "trash", role: .destructive) { confirmDelete = true } |
| 272 | .labelStyle(.iconOnly).glimmerButton() |
| 273 | } |
| 274 | PrimaryAction(title: "Save", action: save) |
| 275 | .disabled(saving || !(1...PersonalTodo.titleLimit).contains(cleanTitle.count) || !validReminder) |
| 276 | .accessibilityIdentifier("personalTodos.save") |
| 277 | .keyboardShortcut(.defaultAction) |
| 278 | } |
| 279 | .sheetCancel { dismiss() } |
| 280 | } |
| 281 | .confirmationDialog("Delete this personal to-do?", isPresented: $confirmDelete) { |
| 282 | Button("Delete to-do", role: .destructive) { |
| 283 | if store.change({ $0.removePersonalTodo(task.id) }) { dismiss() } |
| 284 | } |
| 285 | } |
| 286 | .todoToolSheet($tool) |
| 287 | .interactiveDismissDisabled(saving) |
| 288 | #if os(macOS) |
| 289 | .frame(minWidth: 460, idealWidth: 500, minHeight: 440) |
| 290 | #endif |
| 291 | } |
| 292 | |
| 293 | private func save() { |
| 294 | var saved = task |
| 295 | saved.title = cleanTitle |
| 296 | saved.dueDay = hasDue ? PracticeDate.key(due) : nil |
| 297 | saved.reminderAt = hasReminder ? reminder : nil |
| 298 | var found = !exists |
| 299 | guard store.change({ journal in |
| 300 | var tasks = journal.personalTodos ?? [] |
| 301 | if let index = tasks.firstIndex(where: { $0.id == task.id }) { |
| 302 | found = true |
| 303 | saved.completed = tasks[index].completed |
| 304 | tasks[index] = saved |
| 305 | } else if !exists { |
| 306 | tasks.append(saved) |
| 307 | } |
| 308 | journal.personalTodos = tasks |
| 309 | }), found else { |
| 310 | if !found { store.notice = "This to-do was removed." } |
| 311 | return |
| 312 | } |
| 313 | guard store.usesDeviceReminders, hasReminder, !saved.completed, reminder > .now else { dismiss(); return } |
| 314 | saving = true |
| 315 | Task { |
| 316 | if await PersonalReminders.shared.authorize() { |
| 317 | PersonalReminders.shared.refresh(store.journal.personalTodos ?? []) |
| 318 | } else { |
| 319 | store.notice = "To-do saved. Turn on Glimmer notifications in system settings for reminders." |
| 320 | } |
| 321 | dismiss() |
| 322 | } |
| 323 | } |
| 324 | } |