Glimmer / app · glimmertoday.ai / native / Glimmer / CheckIns.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | /// The three kinds of thing a moment can be about, each shown as a collapsible dropdown. |
| 4 | private enum SourceGroup: String { |
| 5 | case apps = "Apps & sites", activities = "Activities", people = "People" |
| 6 | |
| 7 | /// The glyph kind for this group's rows. |
| 8 | var kind: String { |
| 9 | switch self { |
| 10 | case .apps: "app" |
| 11 | case .activities: "activity" |
| 12 | case .people: "person" |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | var icon: String { |
| 17 | switch self { |
| 18 | case .apps: "square.grid.2x2" |
| 19 | case .activities: "figure.walk" |
| 20 | case .people: "person.2" |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /// The inline control mirrors the date pill and calendar popover in OpenTalon. |
| 26 | private struct MomentDateControl: View { |
| 27 | @Binding var date: Date |
| 28 | @Environment(\.calendar) private var calendar |
| 29 | @Environment(\.locale) private var locale |
| 30 | @Environment(\.readingTheme) private var theme |
| 31 | @State private var pending = Date() |
| 32 | @State private var showingCalendar = false |
| 33 | |
| 34 | private var selectedDay: Binding<Date> { |
| 35 | Binding(get: { calendar.startOfDay(for: pending) }, set: { day in |
| 36 | let time = calendar.dateComponents([.hour, .minute], from: pending) |
| 37 | let combined = calendar.date(bySettingHour: time.hour ?? 0, minute: time.minute ?? 0, second: 0, |
| 38 | of: day, matchingPolicy: .nextTimePreservingSmallerComponents) |
| 39 | pending = min(combined ?? day, .now) |
| 40 | }) |
| 41 | } |
| 42 | |
| 43 | var body: some View { |
| 44 | HStack(spacing: 8) { |
| 45 | Text("When").font(.callout) |
| 46 | Spacer(minLength: 6) |
| 47 | Button { |
| 48 | pending = date |
| 49 | showingCalendar = true |
| 50 | } label: { |
| 51 | Label(date.formatted(.dateTime.month(.abbreviated).day().year().hour().minute()), systemImage: "calendar") |
| 52 | .font(.callout.weight(.medium)) |
| 53 | .frame(minHeight: 24) |
| 54 | } |
| 55 | .glimmerButton() |
| 56 | .accessibilityLabel("Moment date and time") |
| 57 | .accessibilityValue(date.formatted(date: .long, time: .shortened)) |
| 58 | .accessibilityHint("Opens a calendar and time controls") |
| 59 | .accessibilityIdentifier("momentDate") |
| 60 | .popover(isPresented: $showingCalendar, arrowEdge: .bottom) { |
| 61 | #if os(macOS) |
| 62 | calendarContent |
| 63 | .frame(width: 304) |
| 64 | .padding(14) |
| 65 | .background(theme.surface) |
| 66 | #else |
| 67 | ScrollView { |
| 68 | calendarContent.frame(maxWidth: 350) |
| 69 | .frame(maxWidth: .infinity) |
| 70 | .padding(18) |
| 71 | } |
| 72 | .presentationCompactAdaptation(.sheet) |
| 73 | .background(theme.surface) |
| 74 | #endif |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private var calendarContent: some View { |
| 80 | VStack(alignment: .leading, spacing: 12) { |
| 81 | GlimmerCalendarGrid(selection: selectedDay, latestDay: calendar.startOfDay(for: .now)) |
| 82 | HStack { |
| 83 | Text("Time").font(.callout) |
| 84 | Spacer() |
| 85 | timeMenu(.hour) |
| 86 | timeMenu(.minute) |
| 87 | } |
| 88 | HStack { |
| 89 | Spacer(minLength: 0) |
| 90 | Button("Cancel") { showingCalendar = false }.glimmerButton() |
| 91 | Button("Apply") { |
| 92 | date = min(pending, .now) |
| 93 | showingCalendar = false |
| 94 | } |
| 95 | .glimmerButton(.prominent) |
| 96 | .keyboardShortcut(.defaultAction) |
| 97 | .accessibilityIdentifier("applyMomentDate") |
| 98 | } |
| 99 | } |
| 100 | .tint(theme.selectionInk) |
| 101 | } |
| 102 | |
| 103 | private func timeMenu(_ component: Calendar.Component) -> some View { |
| 104 | let isHour = component == .hour |
| 105 | let hour = calendar.component(.hour, from: pending) |
| 106 | let minute = calendar.component(.minute, from: pending) |
| 107 | let selected = isHour ? hour : minute |
| 108 | let title = isHour ? "Hour" : "Minute" |
| 109 | return Menu { |
| 110 | ForEach(0..<(isHour ? 24 : 60), id: \.self) { value in |
| 111 | let option = calendar.date(bySettingHour: isHour ? value : hour, minute: isHour ? minute : value, |
| 112 | second: 0, of: pending, matchingPolicy: .strict) |
| 113 | .flatMap { calendar.isDate($0, inSameDayAs: pending) && $0 <= .now ? $0 : nil } |
| 114 | Button(label(value, isHour: isHour)) { |
| 115 | if let option { pending = option } |
| 116 | } |
| 117 | .disabled(option == nil) |
| 118 | } |
| 119 | } label: { |
| 120 | HStack(spacing: 6) { |
| 121 | Text(label(selected, isHour: isHour)).monospacedDigit() |
| 122 | Image(systemName: "chevron.down").font(.caption2.weight(.semibold)) |
| 123 | } |
| 124 | .font(.callout.weight(.medium)) |
| 125 | .foregroundStyle(theme.selectionInk) |
| 126 | .padding(.horizontal, 9) |
| 127 | .frame(minHeight: 38) |
| 128 | .background(theme.selectionFill, in: .capsule) |
| 129 | } |
| 130 | .menuStyle(.borderlessButton) |
| 131 | .menuIndicator(.hidden) |
| 132 | .fixedSize() |
| 133 | .accessibilityLabel(title) |
| 134 | .accessibilityValue(label(selected, isHour: isHour)) |
| 135 | } |
| 136 | |
| 137 | private func label(_ value: Int, isHour: Bool) -> String { |
| 138 | guard isHour, let time = calendar.date(bySettingHour: value, minute: 0, second: 0, |
| 139 | of: pending, matchingPolicy: .strict), |
| 140 | calendar.isDate(time, inSameDayAs: pending) else { |
| 141 | return value.formatted(.number.precision(.integerLength(2)).locale(locale)) |
| 142 | } |
| 143 | return time.formatted(.dateTime.hour().locale(locale)) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | struct MomentFlow: View { |
| 148 | @Environment(JournalStore.self) private var store |
| 149 | @Environment(\.dismiss) private var dismiss |
| 150 | @Environment(\.readingTheme) private var theme |
| 151 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 152 | @ScaledMetric(relativeTo: .body) private var sourceGlyph = 20.0 |
| 153 | @ScaledMetric(relativeTo: .body) private var sourceCheck = 18.0 |
| 154 | let catalog: Catalog |
| 155 | @State private var step = 0 |
| 156 | @State private var source = "TikTok" |
| 157 | @State private var custom = "" |
| 158 | @State private var date = Date() |
| 159 | @State private var tags: [String] = [] |
| 160 | @State private var value = 0.5 |
| 161 | @State private var decision = Decision.same |
| 162 | @State private var openGroup: SourceGroup? |
| 163 | |
| 164 | private var sources: [String] { catalog.sources + store.journal.customSources } |
| 165 | private var possibleTags: [String] { |
| 166 | if catalog.activities.contains(source) { return ["Solo", "With others", "At home", "Outside", "Intentional", "Unplanned"] } |
| 167 | if catalog.people.contains(source) { return ["In person", "Online", "Supportive", "Social", "Work", "Family"] } |
| 168 | return catalog.tags[source] ?? catalog.defaultTags |
| 169 | } |
| 170 | |
| 171 | var body: some View { |
| 172 | NavigationStack { |
| 173 | Page(title: ["What did you do?", "What kind of content?", "How did it leave you?"][step], |
| 174 | kicker: "Log a moment", subtitle: subtitle, width: 600) { |
| 175 | StepDots(current: step, total: 3) |
| 176 | switch step { |
| 177 | case 0: sourceStep |
| 178 | case 1: tagStep |
| 179 | default: dialStep |
| 180 | } |
| 181 | if let notice = store.notice, notice.hasPrefix("Glimmer couldn’t save") { |
| 182 | Label(notice, systemImage: "exclamationmark.triangle").foregroundStyle(GlimmerColor.ember) |
| 183 | } |
| 184 | } |
| 185 | .id(step) |
| 186 | #if DEBUG |
| 187 | .onAppear { |
| 188 | if let value = ProcessInfo.processInfo.environment["GLIMMER_LOG_STEP"].flatMap(Int.init), step == 0 { |
| 189 | step = min(2, max(0, value)) |
| 190 | } |
| 191 | if let name = ProcessInfo.processInfo.environment["GLIMMER_LOG_OPEN"] { |
| 192 | openGroup = SourceGroup(rawValue: name) |
| 193 | } |
| 194 | } |
| 195 | #endif |
| 196 | .actionBar(width: 600) { |
| 197 | SheetCancel { dismiss() } |
| 198 | if step > 0 { |
| 199 | GlassIconButton(label: "Back", systemImage: "chevron.left") { step -= 1 } |
| 200 | } |
| 201 | switch step { |
| 202 | case 0: PrimaryAction(title: "Continue") { step += 1 }.disabled(source.isEmpty) |
| 203 | case 1: PrimaryAction(title: tags.isEmpty ? "Skip tags" : "Continue") { step += 1 } |
| 204 | default: PrimaryAction(title: "Save log", systemImage: "checkmark", action: save).accessibilityIdentifier("saveMoment") |
| 205 | } |
| 206 | } |
| 207 | .sheetCancel { dismiss() } |
| 208 | } |
| 209 | #if os(macOS) |
| 210 | .frame(minWidth: 580, idealWidth: 620, minHeight: 640, idealHeight: 700) |
| 211 | #endif |
| 212 | } |
| 213 | |
| 214 | private var subtitle: String { |
| 215 | switch step { |
| 216 | case 0: "An app, an activity or time with someone." |
| 217 | case 1: "Choose up to three tags, or continue without them." |
| 218 | default: "Turn the dial to how you feel now, not how much you wanted it." |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | private var sourceStep: some View { |
| 223 | VStack(spacing: 10) { |
| 224 | sourceSection(.apps, items: sources) |
| 225 | sourceSection(.activities, items: catalog.activities) |
| 226 | sourceSection(.people, items: catalog.people) |
| 227 | Panel(padding: 16) { |
| 228 | MomentDateControl(date: $date) |
| 229 | } |
| 230 | .padding(.top, 10) |
| 231 | } |
| 232 | .accessibilityIdentifier("momentSource") |
| 233 | } |
| 234 | |
| 235 | /// One collapsible dropdown: a glass header showing the current choice, and its options when open. |
| 236 | private func sourceSection(_ group: SourceGroup, items: [String]) -> some View { |
| 237 | let chosen = items.contains(source) ? source : nil |
| 238 | let isOpen = openGroup == group |
| 239 | return VStack(alignment: .leading, spacing: 10) { |
| 240 | Button { |
| 241 | openGroup = isOpen ? nil : group |
| 242 | } label: { |
| 243 | HStack(spacing: 12) { |
| 244 | Image(systemName: group.icon) |
| 245 | .font(.system(size: sourceGlyph, weight: .medium)) |
| 246 | .foregroundStyle(chosen == nil ? Color.secondary : theme.selectionInk) |
| 247 | .frame(minWidth: 28) |
| 248 | .accessibilityHidden(true) |
| 249 | VStack(alignment: .leading, spacing: 4) { |
| 250 | Text(group.rawValue).font(.body.weight(.semibold)).foregroundStyle(Color.primary) |
| 251 | Text(chosen ?? "\(items.count) to choose from").font(.subheadline) |
| 252 | .foregroundStyle(chosen == nil ? Color.secondary : Color.primary) |
| 253 | } |
| 254 | .fixedSize(horizontal: false, vertical: true) |
| 255 | Spacer(minLength: 6) |
| 256 | if chosen != nil { |
| 257 | Image(systemName: "checkmark.circle.fill") |
| 258 | .font(.system(size: sourceCheck)) |
| 259 | .foregroundStyle(theme.selectionInk) |
| 260 | .accessibilityHidden(true) |
| 261 | } |
| 262 | Image(systemName: "chevron.down").font(.subheadline.weight(.medium)).foregroundStyle(.secondary) |
| 263 | .frame(minWidth: 20) |
| 264 | .rotationEffect(.degrees(isOpen ? 180 : 0)) |
| 265 | .animation(reduceMotion ? nil : .smooth(duration: 0.22), value: isOpen) |
| 266 | .accessibilityHidden(true) |
| 267 | } |
| 268 | .padding(.horizontal, 12) |
| 269 | .padding(.top, 10) |
| 270 | .padding(.bottom, 13) |
| 271 | .frame(maxWidth: .infinity, minHeight: RowLabel.minHeight + 26, alignment: .leading) |
| 272 | .contentShape(.rect) |
| 273 | } |
| 274 | .glassRow(tint: theme.selectionInk, selected: chosen != nil) |
| 275 | .accessibilityLabel(group.rawValue) |
| 276 | .accessibilityValue(chosen ?? "Nothing chosen") |
| 277 | .accessibilityHint(isOpen ? "Collapses the list" : "Shows the list") |
| 278 | ReadingDisclosure(expanded: isOpen) { visible in |
| 279 | ChoiceList(items: items, selection: chosen, visible: visible, |
| 280 | favorites: store.journal.favoriteSources ?? [], recents: recentSources, |
| 281 | prompt: group == .apps ? "Search or add an app or site" : "Search \(group.rawValue.lowercased())", |
| 282 | alphabetical: group == .apps, |
| 283 | kind: group.kind, |
| 284 | onSelect: choose, onToggleFavorite: toggleFavorite, |
| 285 | onAdd: group == .apps ? { custom = $0; addSource() } : nil) |
| 286 | .padding(.bottom, 8) |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /// Sources from past logs, most recent first. |
| 292 | private var recentSources: [String] { |
| 293 | var seen = Set<String>() |
| 294 | return store.journal.moments.sorted { $0.date > $1.date }.map(\.source).filter { seen.insert($0).inserted } |
| 295 | } |
| 296 | |
| 297 | private func choose(_ name: String) { |
| 298 | if source != name { tags = [] } |
| 299 | source = name |
| 300 | openGroup = nil |
| 301 | } |
| 302 | |
| 303 | private func toggleFavorite(_ name: String) { |
| 304 | store.change { journal in |
| 305 | var favorites = journal.favoriteSources ?? [] |
| 306 | if let index = favorites.firstIndex(of: name) { favorites.remove(at: index) } else { favorites.append(name) } |
| 307 | journal.favoriteSources = favorites |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | private var tagStep: some View { |
| 312 | Panel(title: source) { |
| 313 | FlowLayout { |
| 314 | ForEach(possibleTags, id: \.self) { tag in |
| 315 | Chip(label: tag, selected: tags.contains(tag), tint: theme.selectionInk) { |
| 316 | if let index = tags.firstIndex(of: tag) { tags.remove(at: index) } |
| 317 | else if tags.count < 3 { tags.append(tag) } |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | .glassGroup(spacing: 8) |
| 322 | Text("\(tags.count) of 3 chosen").font(.caption).foregroundStyle(.secondary) |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | private var dialStep: some View { |
| 327 | Panel(padding: 22) { |
| 328 | HStack { |
| 329 | Eyebrow(source) |
| 330 | Spacer() |
| 331 | if !tags.isEmpty { Text(tags.joined(separator: " · ")).font(.caption).foregroundStyle(.secondary) } |
| 332 | } |
| 333 | ReflectionDial(title: "Aftertaste", words: Moment.words, |
| 334 | colors: ["596078", "7c8699", "9db2c4", "64a6e8", "3d8ef0"].map(Color.init(hex:)), value: $value) |
| 335 | Divider() |
| 336 | Text("Want more or less of this?").font(.headline) |
| 337 | GlassSegments(label: "Want more or less of this?", options: [ |
| 338 | GlassSegment(value: Decision.less, title: Decision.less.title, symbol: "arrow.down"), |
| 339 | GlassSegment(value: Decision.same, title: Decision.same.title, symbol: "equal"), |
| 340 | GlassSegment(value: Decision.more, title: Decision.more.title, symbol: "arrow.up"), |
| 341 | ], selection: $decision) |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | private func addSource() { |
| 346 | let name = custom.trimmingCharacters(in: .whitespacesAndNewlines) |
| 347 | guard !name.isEmpty else { return } |
| 348 | if let existing = (sources + catalog.activities + catalog.people).first(where: { $0.caseInsensitiveCompare(name) == .orderedSame }) { |
| 349 | source = existing |
| 350 | } else { |
| 351 | guard store.change({ $0.customSources.append(name) }) else { return } |
| 352 | source = name |
| 353 | } |
| 354 | tags = [] |
| 355 | custom = "" |
| 356 | openGroup = nil |
| 357 | } |
| 358 | |
| 359 | private func save() { |
| 360 | let moment = Moment(source: source, kind: Moment.kind(of: source, catalog: catalog), tags: tags, value: value, decision: decision, |
| 361 | date: date, day: CalendarDay.key(date)) |
| 362 | let previousAward = store.earnedCoins?.id |
| 363 | if store.change({ $0.record(moment) }) { |
| 364 | if store.earnedCoins?.id == previousAward { store.notice = "Logged. Thanks for noticing." } |
| 365 | dismiss() |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | struct EnergyFlow: View { |
| 371 | @Environment(JournalStore.self) private var store |
| 372 | @Environment(\.dismiss) private var dismiss |
| 373 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 374 | let catalog: Catalog |
| 375 | @State private var value = 0.5 |
| 376 | |
| 377 | var body: some View { |
| 378 | NavigationStack { |
| 379 | Page(title: "How’s your energy right now?", kicker: store.day.formatted(date: .complete, time: .omitted), |
| 380 | subtitle: "Ten seconds. It shapes how big today feels.", width: 560) { |
| 381 | // The dial sits straight on the page, and the line under it answers as it turns. |
| 382 | VStack(spacing: 6) { |
| 383 | ReflectionDial(title: "Energy", words: catalog.energy.map(\.label), |
| 384 | colors: catalog.energy.map { Color(hex: $0.color) }, value: $value) |
| 385 | let level = catalog.energy[Moment.step(value)] |
| 386 | Text(catalog.energyMessages[level.id] ?? "") |
| 387 | .font(GlimmerFont.reading(.title3)).foregroundStyle(.secondary) |
| 388 | .multilineTextAlignment(.center) |
| 389 | .fixedSize(horizontal: false, vertical: true) |
| 390 | .frame(maxWidth: 360, minHeight: 56, alignment: .top) |
| 391 | .id(level.id) |
| 392 | .transition(reduceMotion ? AnyTransition.opacity : AnyTransition(.blurReplace)) |
| 393 | .accessibilityIdentifier("energy.message") |
| 394 | } |
| 395 | .frame(maxWidth: .infinity) |
| 396 | .padding(.top, 12) |
| 397 | .animation(reduceMotion ? nil : .smooth(duration: 0.3), value: Moment.step(value)) |
| 398 | } |
| 399 | .actionBar(width: 560) { |
| 400 | SheetCancel { dismiss() } |
| 401 | PrimaryAction(title: "Set today’s energy", systemImage: "bolt.heart") { |
| 402 | let level = catalog.energy[Moment.step(value)] |
| 403 | if store.change({ $0.recordEnergy(.init(level: level.id, value: value), on: store.day) }) { |
| 404 | store.notice = catalog.energyMessages[level.id] |
| 405 | dismiss() |
| 406 | } |
| 407 | } |
| 408 | .keyboardShortcut(.defaultAction) |
| 409 | } |
| 410 | .sheetCancel { dismiss() } |
| 411 | } |
| 412 | .onAppear { value = store.journal.energy[store.todayKey]?.value ?? 0.5 } |
| 413 | #if os(macOS) |
| 414 | .frame(minWidth: 520, idealWidth: 560, minHeight: 600, idealHeight: 640) |
| 415 | #endif |
| 416 | } |
| 417 | } |