Glimmer / app · glimmertoday.ai / native / Glimmer / Plan.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | struct PlanView: View { |
| 4 | @State private var completionNote: String? |
| 5 | @Environment(JournalStore.self) private var store |
| 6 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 7 | let catalog: Catalog |
| 8 | @State private var edit: PlanTask? |
| 9 | @State private var quickName = "" |
| 10 | @State private var showAll = false |
| 11 | |
| 12 | private var tasks: [PlanTask] { showAll ? store.journal.tasks : store.todaysTasks } |
| 13 | private var count: Int { store.todaysTasks.filter { store.todayCompletion.contains($0.id) }.count } |
| 14 | private var fraction: Double { Double(count) / Double(max(1, store.todaysTasks.count)) } |
| 15 | |
| 16 | var body: some View { |
| 17 | Page(title: "One small task", subtitle: "Give today a shape that feels manageable.", width: 820) { |
| 18 | progress |
| 19 | Panel(padding: 10) { |
| 20 | HStack { |
| 21 | Eyebrow(showAll ? "All scheduled tasks" : "Today") |
| 22 | Spacer() |
| 23 | Button(showAll ? "Show today" : "Show all") { |
| 24 | withAnimation(reduceMotion ? nil : .smooth(duration: 0.3)) { showAll.toggle() } |
| 25 | } |
| 26 | .buttonStyle(.plain).font(.subheadline).foregroundStyle(.secondary) |
| 27 | .frame(minHeight: 30).contentShape(.rect) |
| 28 | .accessibilityLabel(showAll ? "Show today’s tasks" : "Show all scheduled tasks") |
| 29 | } |
| 30 | .padding(.horizontal, 8).padding(.top, 4) |
| 31 | VStack(spacing: 0) { |
| 32 | if tasks.isEmpty { |
| 33 | Text("Nothing scheduled yet. Write one small task below.") |
| 34 | .font(.callout).foregroundStyle(.secondary) |
| 35 | .frame(maxWidth: .infinity, minHeight: 44, alignment: .leading).padding(.horizontal, 8) |
| 36 | } |
| 37 | ForEach(tasks) { task in |
| 38 | row(task) |
| 39 | Divider().padding(.leading, 52) |
| 40 | } |
| 41 | quickAdd |
| 42 | } |
| 43 | Button("New scheduled task", systemImage: "calendar.badge.plus") { edit = PlanTask() } |
| 44 | .buttonStyle(.plain).font(.subheadline).foregroundStyle(.secondary) |
| 45 | .frame(minHeight: 36).padding(.horizontal, 8).contentShape(.rect) |
| 46 | } |
| 47 | PersonalTodoList() |
| 48 | Panel(title: "Ideas", plain: true) { |
| 49 | VStack(spacing: 0) { |
| 50 | LinkRow(title: "Toolkit", detail: "Break to-dos down, size them and focus on one thing", |
| 51 | icon: "wrench.and.screwdriver", tint: GlimmerColor.sage) { ToolkitView() } |
| 52 | .accessibilityIdentifier("plan.toolkit") |
| 53 | LinkRow(title: "Plan my day with suggestions", detail: "A few small tasks to choose from", |
| 54 | icon: "sparkles", tint: GlimmerColor.honey) { PreloadView(catalog: catalog) } |
| 55 | LinkRow(title: "Browse the task library", detail: "\(catalog.library.count) categories", |
| 56 | icon: "books.vertical", tint: GlimmerColor.dusk) { TaskLibraryView(catalog: catalog) } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | .sheet(item: $edit) { TaskEditor(task: $0) } |
| 61 | } |
| 62 | |
| 63 | /// Where today stands: one line, the tracker and the coins. No box around it. |
| 64 | private var progress: some View { |
| 65 | let percent = Int((fraction * 100).rounded()) |
| 66 | return VStack(alignment: .leading, spacing: 10) { |
| 67 | HStack(spacing: 16) { |
| 68 | if store.journal.tracker == .ring { |
| 69 | ZStack { |
| 70 | Circle().stroke(Color.primary.opacity(0.08), lineWidth: 8) |
| 71 | Circle().trim(from: 0, to: fraction) |
| 72 | .stroke(GlimmerColor.sage, style: StrokeStyle(lineWidth: 8, lineCap: .round)) |
| 73 | .rotationEffect(.degrees(-90)) |
| 74 | .opacity(fraction == 0 ? 0 : 1) |
| 75 | .animation(reduceMotion ? nil : .smooth(duration: 0.58), value: fraction) |
| 76 | Text("\(percent)%").font(.subheadline.monospacedDigit().weight(.semibold)) |
| 77 | .contentTransition(reduceMotion ? .opacity : .numericText(value: Double(percent))) |
| 78 | .animation(.easeOut(duration: reduceMotion ? 0.18 : 0.32), value: percent) |
| 79 | } |
| 80 | .frame(width: 60, height: 60) |
| 81 | .accessibilityHidden(true) |
| 82 | } |
| 83 | Text("\(count) of \(store.todaysTasks.count) complete").font(GlimmerFont.display(.title3)) |
| 84 | .contentTransition(reduceMotion ? .opacity : .numericText(value: Double(count))) |
| 85 | .animation(.easeOut(duration: reduceMotion ? 0.18 : 0.32), value: count) |
| 86 | Spacer(minLength: 0) |
| 87 | CoinCount(count: store.journal.coins) |
| 88 | } |
| 89 | if store.journal.tracker != .ring { Meter(value: fraction, tint: GlimmerColor.sage) } |
| 90 | // The praise line has its own place, so the list below never moves under a finger. |
| 91 | Text(" ").font(.callout).frame(minHeight: 23).accessibilityHidden(true) |
| 92 | .frame(maxWidth: .infinity, alignment: .leading) |
| 93 | .overlay(alignment: .leading) { |
| 94 | if let completionNote { |
| 95 | HStack(spacing: 7) { |
| 96 | GlimmerLogo(size: 23) |
| 97 | Text(completionNote).font(.callout).lineLimit(2) |
| 98 | } |
| 99 | .accessibilityElement(children: .combine) |
| 100 | .transition(reduceMotion |
| 101 | ? .opacity.animation(.easeOut(duration: 0.18)) |
| 102 | : .opacity.combined(with: .offset(y: -6))) |
| 103 | .task(id: completionNote) { |
| 104 | try? await Task.sleep(for: .seconds(3)) |
| 105 | if !Task.isCancelled { setCompletionNote(nil) } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | .padding(.horizontal, 2) |
| 111 | } |
| 112 | |
| 113 | /// A task: the tick completes it, the rest of the line opens it. |
| 114 | private func row(_ task: PlanTask) -> some View { |
| 115 | let done = store.todayCompletion.contains(task.id) |
| 116 | let today = task.occurs(on: store.day) |
| 117 | return HStack(spacing: 0) { |
| 118 | if today { |
| 119 | TickButton(done: done, label: "\(done ? "Uncheck" : "Complete") \(task.name)") { toggle(task) } |
| 120 | } else { |
| 121 | Color.clear.frame(width: 44, height: 44) |
| 122 | } |
| 123 | Button { edit = task } label: { |
| 124 | HStack(spacing: 10) { |
| 125 | GlyphTile(Glyph.symbol(icon: task.icon, name: task.name, category: task.category), |
| 126 | tint: Glyph.tint(for: task.category)) |
| 127 | VStack(alignment: .leading, spacing: 1) { |
| 128 | Text(task.name).font(.body).multilineTextAlignment(.leading) |
| 129 | if showAll || task.schedule != Cadence.daily.rawValue { |
| 130 | Text(task.schedule).font(.footnote).foregroundStyle(.secondary) |
| 131 | } |
| 132 | } |
| 133 | .foregroundStyle(done ? Color.secondary : Color.primary) |
| 134 | Spacer(minLength: 6) |
| 135 | } |
| 136 | .padding(.leading, 2).padding(.trailing, 8) |
| 137 | .frame(maxWidth: .infinity, minHeight: 52, alignment: .leading) |
| 138 | .contentShape(.rect) |
| 139 | } |
| 140 | .buttonStyle(RowPressStyle()) |
| 141 | .accessibilityElement(children: .combine) |
| 142 | .accessibilityHint("Edit task") |
| 143 | } |
| 144 | .animation(.easeOut(duration: reduceMotion ? 0.15 : 0.25), value: done) |
| 145 | } |
| 146 | |
| 147 | /// Typing a line here adds it to the plan, like writing on the next row of a list. |
| 148 | private var quickAdd: some View { |
| 149 | let empty = quickName.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty |
| 150 | return HStack(spacing: 0) { |
| 151 | AddMark() |
| 152 | TextField("Quick add a task", text: $quickName).textFieldStyle(.plain).font(.body) |
| 153 | .padding(.leading, 2) |
| 154 | .frame(maxWidth: .infinity, minHeight: 52) |
| 155 | .onSubmit(add) |
| 156 | if !empty { |
| 157 | Button("Add", action: add).glimmerButton(.prominent).padding(.trailing, 6) |
| 158 | .transition(.opacity.combined(with: .scale(scale: 0.9))) |
| 159 | } |
| 160 | } |
| 161 | .animation(reduceMotion ? nil : .easeOut(duration: 0.18), value: empty) |
| 162 | } |
| 163 | |
| 164 | private func add() { |
| 165 | let name = quickName.trimmingCharacters(in: .whitespacesAndNewlines) |
| 166 | guard !name.isEmpty else { return } |
| 167 | var task = PlanTask() |
| 168 | task.name = name |
| 169 | if store.change({ $0.tasks.append(task) }) { quickName = ""; store.notice = "Task added to your plan." } |
| 170 | } |
| 171 | |
| 172 | private func toggle(_ task: PlanTask) { |
| 173 | let wasDone = store.todayCompletion.contains(task.id) |
| 174 | guard store.change({ $0.toggleTask(task, on: store.day) }) else { return } |
| 175 | if wasDone { |
| 176 | setCompletionNote(nil) |
| 177 | return |
| 178 | } |
| 179 | let note = store.todaysTasks.allSatisfy { store.todayCompletion.contains($0.id) } |
| 180 | ? "That’s your whole day. Rest easy." |
| 181 | : catalog.praise.randomElement() |
| 182 | setCompletionNote(note) |
| 183 | } |
| 184 | |
| 185 | private func setCompletionNote(_ note: String?) { |
| 186 | withAnimation(reduceMotion ? nil : .smooth(duration: 0.28)) { |
| 187 | completionNote = note |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | struct TaskEditor: View { |
| 193 | @Environment(JournalStore.self) private var store |
| 194 | @Environment(\.dismiss) private var dismiss |
| 195 | @State var task: PlanTask |
| 196 | @State private var delete = false |
| 197 | private let icons = GlyphMap.taskIconPicker |
| 198 | /// The task's current glyph, so a saved emoji icon shows (and stays selected) as its symbol. |
| 199 | private var currentIcon: String { Glyph.symbol(icon: task.icon, name: task.name, category: task.category) } |
| 200 | private var exists: Bool { store.journal.tasks.contains { $0.id == task.id } } |
| 201 | |
| 202 | var body: some View { |
| 203 | NavigationStack { |
| 204 | Page(title: task.name.isEmpty ? "New task" : "Edit task", width: 560) { |
| 205 | Panel(title: "Task") { |
| 206 | TextField("Task name", text: $task.name).glimmerField() |
| 207 | Text("Icon").font(.subheadline.weight(.semibold)).foregroundStyle(.secondary) |
| 208 | FlowLayout(spacing: 6) { |
| 209 | ForEach(icons.contains(currentIcon) ? icons : [currentIcon] + icons, id: \.self) { icon in |
| 210 | Button { task.icon = icon } label: { |
| 211 | Image(systemName: icon).font(.body.weight(.semibold)).symbolRenderingMode(.monochrome) |
| 212 | .frame(width: 30, height: 30) |
| 213 | } |
| 214 | .glimmerButton(currentIcon == icon ? .selected : .standard, circle: true) |
| 215 | .accessibilityLabel("Icon \(icon.replacingOccurrences(of: ".", with: " "))") |
| 216 | .accessibilityAddTraits(currentIcon == icon ? .isSelected : []) |
| 217 | } |
| 218 | } |
| 219 | .glassGroup(spacing: 6) |
| 220 | } |
| 221 | Panel(title: "Schedule", accent: GlimmerColor.sage) { |
| 222 | DatePicker(task.repeats ? "Starts" : "Date", selection: $task.anchor, displayedComponents: .date) |
| 223 | Toggle("Repeat", isOn: $task.repeats).toggleStyle(.switch) |
| 224 | if task.repeats { |
| 225 | GlassSegments(label: "Frequency", options: Cadence.allCases.map { GlassSegment(value: $0, title: $0.rawValue) }, |
| 226 | selection: $task.cadence) |
| 227 | Stepper("Every \(task.every) \(unit)", value: $task.every, in: 1...30) |
| 228 | if task.cadence == .weekly { |
| 229 | FlowLayout(spacing: 6) { |
| 230 | ForEach(1...7, id: \.self) { weekday in |
| 231 | Chip(label: Calendar.current.shortWeekdaySymbols[weekday - 1], |
| 232 | selected: task.weekdays.contains(weekday), tint: GlimmerColor.sage) { |
| 233 | if task.weekdays.remove(weekday) == nil { task.weekdays.insert(weekday) } |
| 234 | } |
| 235 | .accessibilityLabel(Calendar.current.weekdaySymbols[weekday - 1]) |
| 236 | } |
| 237 | } |
| 238 | .glassGroup(spacing: 6) |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | .actionBar(width: 560) { |
| 244 | SheetCancel { dismiss() } |
| 245 | if exists { |
| 246 | Button(role: .destructive) { delete = true } label: { |
| 247 | Label("Delete task", systemImage: "trash").labelStyle(.iconOnly).frame(width: 22, height: 30) |
| 248 | } |
| 249 | .glimmerButton(.standard, circle: true).controlSize(.large) |
| 250 | .accessibilityLabel("Delete task") |
| 251 | } |
| 252 | PrimaryAction(title: "Save task", systemImage: "checkmark", action: save) |
| 253 | .disabled(task.name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) |
| 254 | .keyboardShortcut(.defaultAction) |
| 255 | } |
| 256 | .sheetCancel { dismiss() } |
| 257 | } |
| 258 | .confirmationDialog("Remove this task from your plan?", isPresented: $delete) { |
| 259 | Button("Delete task", role: .destructive) { |
| 260 | if store.change({ $0.tasks.removeAll { $0.id == task.id } }) { dismiss() } |
| 261 | } |
| 262 | } |
| 263 | #if os(macOS) |
| 264 | .frame(minWidth: 540, idealWidth: 580, minHeight: 580, idealHeight: 640) |
| 265 | #endif |
| 266 | } |
| 267 | |
| 268 | private var unit: String { |
| 269 | let name = task.cadence == .daily ? "day" : task.cadence == .weekly ? "week" : "month" |
| 270 | return task.every == 1 ? name : name + "s" |
| 271 | } |
| 272 | |
| 273 | private func save() { |
| 274 | task.name = task.name.trimmingCharacters(in: .whitespacesAndNewlines) |
| 275 | if store.change({ journal in |
| 276 | if let index = journal.tasks.firstIndex(where: { $0.id == task.id }) { journal.tasks[index] = task } |
| 277 | else { journal.tasks.append(task) } |
| 278 | }) { dismiss() } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | struct TaskLibraryView: View { |
| 283 | @Environment(JournalStore.self) private var store |
| 284 | let catalog: Catalog |
| 285 | |
| 286 | var body: some View { |
| 287 | Page(title: "Task library", subtitle: "Pick what fits, skip the rest.", width: 900) { |
| 288 | LazyVGrid(columns: [GridItem(.adaptive(minimum: 280), spacing: 14)], spacing: 14) { |
| 289 | ForEach(catalog.library) { category in |
| 290 | Panel { |
| 291 | GlyphTile(Glyph.symbol(icon: category.icon, name: category.name, category: category.id), |
| 292 | tint: Glyph.tint(for: category.id), size: 44) |
| 293 | Text(category.name).font(GlimmerFont.display(.title3, weight: .bold)) |
| 294 | Text(category.desc).font(.subheadline).foregroundStyle(.secondary) |
| 295 | .fixedSize(horizontal: false, vertical: true) |
| 296 | Spacer(minLength: 0) |
| 297 | LinkRow(title: "Browse \(category.tasks.count) tasks", tint: GlimmerColor.dusk) { |
| 298 | categoryPage(category) |
| 299 | } |
| 300 | .accessibilityLabel(category.name) |
| 301 | } |
| 302 | .frame(maxHeight: .infinity, alignment: .top) |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | .navigationTitle("Task library") |
| 307 | } |
| 308 | |
| 309 | private func categoryPage(_ category: TaskCategory) -> some View { |
| 310 | Page(title: category.name, subtitle: category.desc) { |
| 311 | Panel { |
| 312 | VStack(spacing: 8) { |
| 313 | ForEach(category.tasks) { template in |
| 314 | let added = store.journal.tasks.contains { $0.id == template.id } |
| 315 | ActionRow(title: template.name, detail: added ? "In your plan · \(template.freq)" : template.freq, |
| 316 | emoji: template.icon, tint: GlimmerColor.sage, |
| 317 | trailing: added ? "checkmark.circle.fill" : "plus.circle", selected: added) { |
| 318 | store.change { |
| 319 | if added { $0.tasks.removeAll { $0.id == template.id } } |
| 320 | else { $0.tasks.append(PlanTask(template: template, category: category.id, date: store.day)) } |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | .glassGroup(spacing: 8) |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | struct PreloadView: View { |
| 332 | @Environment(JournalStore.self) private var store |
| 333 | @Environment(\.dismiss) private var dismiss |
| 334 | let catalog: Catalog |
| 335 | @State private var selection: Set<String> = [] |
| 336 | |
| 337 | var body: some View { |
| 338 | Page(title: "Plan my day", kicker: "Start of day", subtitle: "A few suggestions. Pick what’s doable.") { |
| 339 | VStack(spacing: 8) { |
| 340 | ForEach(catalog.preload) { template in |
| 341 | let already = store.journal.tasks.contains { $0.id == template.id } |
| 342 | ActionRow(title: template.name, detail: already ? "Already in your plan" : template.freq, |
| 343 | emoji: template.icon, tint: GlimmerColor.sage, |
| 344 | selected: already || selection.contains(template.id), |
| 345 | tick: already || selection.contains(template.id)) { |
| 346 | if selection.remove(template.id) == nil { selection.insert(template.id) } |
| 347 | } |
| 348 | .disabled(already) |
| 349 | } |
| 350 | } |
| 351 | .glassGroup(spacing: 8) |
| 352 | } |
| 353 | .actionBar { |
| 354 | PrimaryAction(title: selection.isEmpty ? "Done" : "Add \(selection.count) to my plan") { |
| 355 | if selection.isEmpty { dismiss(); return } |
| 356 | if store.change({ journal in |
| 357 | let existing = Set(journal.tasks.map(\.id)) |
| 358 | journal.tasks += catalog.preload.filter { selection.contains($0.id) && !existing.contains($0.id) } |
| 359 | .map { PlanTask(template: $0, date: store.day) } |
| 360 | }) { store.notice = "Your day is set."; dismiss() } |
| 361 | } |
| 362 | } |
| 363 | .onAppear { selection = Set(catalog.preload.map(\.id)).subtracting(store.journal.tasks.map(\.id)) } |
| 364 | } |
| 365 | } |