Glimmer / app · glimmertoday.ai / native / Glimmer / Wellness.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | struct ChallengesView: View { |
| 4 | @Environment(\.readingTheme) private var theme |
| 5 | @Environment(JournalStore.self) private var store |
| 6 | let catalog: Catalog |
| 7 | |
| 8 | var body: some View { |
| 9 | let level = store.journal.journeyLevel(asOf: store.day) |
| 10 | let stages = Array(Set(catalog.challenges.map(\.stage))).sorted() |
| 11 | Page(title: "Challenges", subtitle: "Guided challenges you can actually finish. They deepen as your practice grows, and every one is yours to try anytime.", width: 940) { |
| 12 | ForEach(stages, id: \.self) { stage in |
| 13 | Panel(title: JourneyStage.name(stage), plain: true) { |
| 14 | Label(stage > level ? "\(JourneyStage.asks(stage)). Glimmer suggests these after \(JourneyStage.rank(for: stage).rawValue)." : JourneyStage.asks(stage) + ".", |
| 15 | systemImage: stage > level ? "leaf" : "sparkles") |
| 16 | .font(.callout).foregroundStyle(.secondary) |
| 17 | LazyVGrid(columns: [GridItem(.adaptive(minimum: 300), spacing: 16)], spacing: 16) { |
| 18 | ForEach(catalog.challenges.filter { $0.stage == stage }) { challenge in |
| 19 | let index = catalog.challenges.firstIndex { $0.id == challenge.id } ?? 0 |
| 20 | card(challenge, slot: 51 + index) |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | let kept = store.journal.history.filter { $0.review == .keep } |
| 26 | if !kept.isEmpty { |
| 27 | Panel(title: "Experiments you kept", accent: GlimmerColor.sage, plain: true) { |
| 28 | ForEach(kept) { Label($0.suggestion.title, systemImage: "checkmark.seal").foregroundStyle(.primary) } |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | private func card(_ challenge: Challenge, slot: Int) -> some View { |
| 35 | let unlocked = challenge.id != "coffee" || store.journal.coffeeUnlocked |
| 36 | let done = store.journal.challenges[challenge.id] != nil |
| 37 | return Panel(padding: 14) { |
| 38 | ZStack(alignment: .topLeading) { |
| 39 | Artwork(slot: slot, height: 150, corner: 14) |
| 40 | .saturation(unlocked ? 1 : 0).opacity(unlocked ? 1 : 0.5) |
| 41 | Text(done ? "Completed" : unlocked ? "\(challenge.mins) min · +\(challenge.coins)" : "Locked") |
| 42 | .font(.caption.weight(.bold)) |
| 43 | .padding(.horizontal, 10).padding(.vertical, 5) |
| 44 | .background(theme.surface.opacity(0.92), in: .capsule) |
| 45 | .padding(10) |
| 46 | } |
| 47 | Text(challenge.title).font(GlimmerFont.display(.title3, weight: .bold)) |
| 48 | Text(challenge.sub).font(.subheadline).foregroundStyle(.secondary) |
| 49 | .fixedSize(horizontal: false, vertical: true) |
| 50 | Spacer(minLength: 0) |
| 51 | if unlocked { |
| 52 | LinkRow(title: done ? "Do it again" : "Start", icon: done ? "arrow.counterclockwise" : "play.fill", |
| 53 | tint: GlimmerColor.honey) { ChallengeView(challenge: challenge) } |
| 54 | .accessibilityLabel(challenge.title) |
| 55 | } else { |
| 56 | Label("Unlocks at 80 coins · \(max(0, 80 - store.journal.coins)) to go", systemImage: "lock") |
| 57 | .font(.callout.weight(.semibold)).foregroundStyle(.secondary) |
| 58 | } |
| 59 | } |
| 60 | .frame(maxHeight: .infinity, alignment: .top) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | struct ChallengeView: View { |
| 65 | @Environment(JournalStore.self) private var store |
| 66 | @Environment(\.dismiss) private var dismiss |
| 67 | let challenge: Challenge |
| 68 | @State private var step = 0 |
| 69 | @State private var choice = "" |
| 70 | @State private var deadline: Date? |
| 71 | |
| 72 | private var gallerySlot: Int { 51 + (store.catalog?.challenges.firstIndex { $0.id == challenge.id } ?? 0) } |
| 73 | private var actionStep: Int { challenge.teach.count + 2 } |
| 74 | private var total: Int { actionStep + 2 } |
| 75 | |
| 76 | var body: some View { |
| 77 | Page(title: heading, kicker: "Tiny win · \(challenge.title)", width: 640) { |
| 78 | StepDots(current: min(step, total - 1), total: total) |
| 79 | switch step { |
| 80 | case 0: |
| 81 | Artwork(slot: gallerySlot, height: 230) |
| 82 | Panel { |
| 83 | Text(challenge.sub).font(GlimmerFont.display(.title3)) |
| 84 | Label("\(challenge.mins) minutes · +\(challenge.coins) coins", systemImage: "timer") |
| 85 | .foregroundStyle(.secondary) |
| 86 | } |
| 87 | case 1...challenge.teach.count: |
| 88 | challengePage(challenge.teach[step - 1]) |
| 89 | case actionStep - 1: |
| 90 | VStack(spacing: 8) { |
| 91 | ForEach(challenge.opts) { option in |
| 92 | ActionRow(title: option.l, emoji: option.e, tint: GlimmerColor.honey, |
| 93 | selected: choice == option.l, tick: choice == option.l) { choice = option.l } |
| 94 | } |
| 95 | } |
| 96 | .glassGroup(spacing: 8) |
| 97 | case actionStep: |
| 98 | challengePage(challenge.action) |
| 99 | timer |
| 100 | default: |
| 101 | Panel(padding: 22) { |
| 102 | HStack(spacing: 14) { |
| 103 | Text("+\(challenge.coins) coins").font(GlimmerFont.display(.title2, weight: .bold)) |
| 104 | .foregroundStyle(GlimmerColor.honey) |
| 105 | } |
| 106 | } |
| 107 | challengePage(challenge.done) |
| 108 | Panel(title: "How did it feel?", accent: GlimmerColor.honey) { |
| 109 | FlowLayout { |
| 110 | ForEach(["Lighter", "About the same", "Harder than expected"], id: \.self) { answer in |
| 111 | Chip(label: answer, selected: store.journal.challenges[challenge.id]?.feedback == answer, |
| 112 | tint: GlimmerColor.honey) { |
| 113 | store.change { $0.challenges[challenge.id]?.feedback = answer } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | .glassGroup(spacing: 8) |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | .actionBar(width: 640) { |
| 122 | if step > 0 && step <= actionStep { |
| 123 | GlassIconButton(label: "Back", systemImage: "chevron.left") { deadline = nil; step -= 1 } |
| 124 | } |
| 125 | switch step { |
| 126 | case 0: next("Get started") |
| 127 | case 1...challenge.teach.count: next("Continue") |
| 128 | case actionStep - 1: next("Continue").disabled(choice.isEmpty) |
| 129 | case actionStep: PrimaryAction(title: "I’m done", systemImage: "checkmark", action: finish) |
| 130 | default: PrimaryAction(title: "Back to challenges") { dismiss() } |
| 131 | } |
| 132 | } |
| 133 | .navigationTitle("Tiny win") |
| 134 | .hidesTabBar() |
| 135 | } |
| 136 | |
| 137 | @ViewBuilder private var timer: some View { |
| 138 | Panel(title: "Timer", accent: GlimmerColor.honey) { |
| 139 | if let deadline { |
| 140 | TimelineView(.periodic(from: .now, by: 1)) { tick in |
| 141 | let seconds = max(0, Int(ceil(deadline.timeIntervalSince(tick.date)))) |
| 142 | let total = Double(challenge.mins * 60) |
| 143 | VStack(alignment: .leading, spacing: 10) { |
| 144 | Text(String(format: "%d:%02d", seconds / 60, seconds % 60)) |
| 145 | .font(.system(size: 54, weight: .bold, design: .rounded).monospacedDigit()) |
| 146 | .contentTransition(.numericText(countsDown: true)) |
| 147 | .accessibilityLabel("\(seconds) seconds left") |
| 148 | Meter(value: 1 - Double(seconds) / max(1, total), tint: GlimmerColor.honey) |
| 149 | } |
| 150 | .onChange(of: seconds) { _, value in if value == 0 { finish() } } |
| 151 | } |
| 152 | SecondaryAction(title: "Stop timer", systemImage: "stop.fill") { self.deadline = nil } |
| 153 | } else { |
| 154 | Text("Optional. Run a short timer so the time stays visible.").foregroundStyle(.secondary) |
| 155 | SecondaryAction(title: "Start \(challenge.mins)-minute timer", systemImage: "timer") { |
| 156 | deadline = .now.addingTimeInterval(Double(challenge.mins) * 60) |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | private var heading: String { |
| 163 | if step == 0 { return challenge.title } |
| 164 | if step <= challenge.teach.count { return challenge.teach[step - 1].title } |
| 165 | if step == actionStep - 1 { return challenge.q } |
| 166 | return step == actionStep ? challenge.action.title : challenge.done.title |
| 167 | } |
| 168 | |
| 169 | private func challengePage(_ page: Challenge.Page) -> some View { |
| 170 | VStack(alignment: .leading, spacing: 14) { |
| 171 | if page.hero != nil { Artwork(slot: gallerySlot + step, height: 210) } |
| 172 | Panel { |
| 173 | ForEach(Array(page.lines.enumerated()), id: \.offset) { _, line in |
| 174 | Text(line).fixedSize(horizontal: false, vertical: true) |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | private func next(_ title: String) -> PrimaryAction { PrimaryAction(title: title) { step += 1 } } |
| 181 | |
| 182 | private func finish() { |
| 183 | guard step == actionStep else { return } |
| 184 | if store.change({ $0.completeChallenge(challenge, choice: choice, on: store.day) }) { |
| 185 | deadline = nil |
| 186 | step += 1 |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | struct HabitsView: View { |
| 192 | @Environment(JournalStore.self) private var store |
| 193 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 194 | @Environment(\.horizontalSizeClass) private var sizeClass |
| 195 | let catalog: Catalog |
| 196 | @State private var monthly = false |
| 197 | @State private var offset = 0 |
| 198 | private let calendar = Calendar.current |
| 199 | |
| 200 | private var month: Date { calendar.date(byAdding: .month, value: offset, to: store.day) ?? store.day } |
| 201 | private var start: Date { calendar.dateInterval(of: .month, for: month)?.start ?? month } |
| 202 | private var dates: [Date] { |
| 203 | (calendar.range(of: .day, in: .month, for: start) ?? 1..<2).compactMap { |
| 204 | calendar.date(byAdding: .day, value: $0 - 1, to: start) |
| 205 | } |
| 206 | } |
| 207 | private var leading: Int { (calendar.component(.weekday, from: start) - calendar.firstWeekday + 7) % 7 } |
| 208 | private var doneToday: Int { catalog.habits.filter { contains($0, store.day) }.count } |
| 209 | private var compact: Bool { |
| 210 | #if os(macOS) |
| 211 | false |
| 212 | #else |
| 213 | sizeClass == .compact |
| 214 | #endif |
| 215 | } |
| 216 | |
| 217 | var body: some View { |
| 218 | Page(title: "Habits", subtitle: "Notice what you’re making room for.", width: 860) { |
| 219 | GlassSegments(label: "View", options: [GlassSegment(value: false, title: "Today"), GlassSegment(value: true, title: "Month")], |
| 220 | selection: $monthly) |
| 221 | .frame(maxWidth: 320) |
| 222 | |
| 223 | if monthly { monthView } else { todayView } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | private var todayView: some View { |
| 228 | ViewThatFits(in: .horizontal) { |
| 229 | HStack(alignment: .top, spacing: 28) { |
| 230 | habitStones.frame(minWidth: 380) |
| 231 | moodSky.frame(width: 300) |
| 232 | } |
| 233 | // On a phone the habits come first and fit on one screen, with the sky just below. |
| 234 | VStack(alignment: .leading, spacing: 22) { habitStones; moodSky } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | private var habitStones: some View { |
| 239 | VStack(alignment: .leading, spacing: 16) { |
| 240 | HStack(alignment: .firstTextBaseline) { |
| 241 | Eyebrow("Today’s habits") |
| 242 | Spacer() |
| 243 | Text("\(doneToday) of \(catalog.habits.count)") |
| 244 | .font(.subheadline.monospacedDigit()).foregroundStyle(.secondary) |
| 245 | .contentTransition(reduceMotion ? .opacity : .numericText(value: Double(doneToday))) |
| 246 | .animation(.easeOut(duration: reduceMotion ? 0.18 : 0.32), value: doneToday) |
| 247 | .accessibilityLabel("\(doneToday) of \(catalog.habits.count) habits done today") |
| 248 | } |
| 249 | LazyVGrid(columns: [GridItem(.adaptive(minimum: compact ? 80 : 100), spacing: compact ? 8 : 10, alignment: .top)], |
| 250 | alignment: .leading, spacing: 20) { |
| 251 | ForEach(catalog.habits) { habit in |
| 252 | HabitStone(icon: habit.icon, name: habit.name, week: week(habit), size: compact ? 70 : 78) { |
| 253 | toggle(habit, store.day) |
| 254 | } |
| 255 | .accessibilityIdentifier("habit." + habit.id) |
| 256 | } |
| 257 | } |
| 258 | Text("Each ring is your last seven days.").font(.footnote).foregroundStyle(.secondary) |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | private var moodSky: some View { |
| 263 | MoodSky(mood: store.journal.moods[store.todayKey]) { value in |
| 264 | store.change { $0.moods[store.todayKey] = value } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | private var monthView: some View { |
| 269 | VStack(spacing: 16) { |
| 270 | HStack { |
| 271 | GlassIconButton(label: "Previous month", systemImage: "chevron.left") { offset -= 1 } |
| 272 | Spacer() |
| 273 | Text(month.formatted(.dateTime.month(.wide).year())).font(GlimmerFont.display(.title3, weight: .bold)) |
| 274 | Spacer() |
| 275 | GlassIconButton(label: "Next month", systemImage: "chevron.right") { offset += 1 }.disabled(offset >= 0) |
| 276 | } |
| 277 | .glassGroup() |
| 278 | LazyVGrid(columns: [GridItem(.adaptive(minimum: 330), spacing: 16)], spacing: 16) { |
| 279 | ForEach(catalog.habits) { habit in |
| 280 | Panel { |
| 281 | let logged = dates.filter { contains(habit, $0) }.count |
| 282 | HStack { |
| 283 | Label { |
| 284 | Text(habit.name).font(.headline) |
| 285 | } icon: { |
| 286 | GlyphTile(Glyph.symbol(icon: habit.icon, name: habit.name, category: "habit"), tint: Glyph.tint(for: "habit")) |
| 287 | } |
| 288 | Spacer() |
| 289 | Text("\(logged) days").font(.subheadline.monospacedDigit()).foregroundStyle(.secondary) |
| 290 | .contentTransition(reduceMotion ? .opacity : .numericText(value: Double(logged))) |
| 291 | .animation(.easeOut(duration: reduceMotion ? 0.18 : 0.32), value: logged) |
| 292 | } |
| 293 | LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 2), count: 7), spacing: 2) { |
| 294 | ForEach(0..<7, id: \.self) { index in |
| 295 | Text(calendar.veryShortWeekdaySymbols[(index + calendar.firstWeekday - 1) % 7]) |
| 296 | .font(.caption2.weight(.semibold)).foregroundStyle(.secondary) |
| 297 | .accessibilityHidden(true) |
| 298 | } |
| 299 | ForEach(0..<leading, id: \.self) { _ in Color.clear.frame(height: 38) } |
| 300 | ForEach(dates, id: \.self) { day($0, habit) } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /// A day in the month: a plain number until it is done, then a sage stone. |
| 309 | private func day(_ date: Date, _ habit: Habit) -> some View { |
| 310 | let on = contains(habit, date) |
| 311 | let today = calendar.isDate(date, inSameDayAs: store.day) |
| 312 | let future = CalendarDay.distance(date, store.day) < 0 |
| 313 | return Button { toggle(habit, date) } label: { |
| 314 | Text(date.formatted(.dateTime.day())).font(.footnote.monospacedDigit().weight(on || today ? .semibold : .regular)) |
| 315 | .dynamicTypeSize(...DynamicTypeSize.xxLarge) |
| 316 | .foregroundStyle(on ? Color(light: "ffffff", dark: "1c1c1b") : .primary) |
| 317 | .frame(width: 32, height: 32) |
| 318 | .background { |
| 319 | Circle().fill(GlimmerColor.sage).scaleEffect(on ? 1 : 0.4).opacity(on ? 1 : 0) |
| 320 | .animation(on && !reduceMotion ? .spring(duration: 0.38, bounce: 0.45) : .easeOut(duration: 0.14), value: on) |
| 321 | if today && !on { Circle().strokeBorder(Color.primary.opacity(0.42), lineWidth: 1.5) } |
| 322 | } |
| 323 | .frame(maxWidth: .infinity, minHeight: 38) |
| 324 | .contentShape(.rect) |
| 325 | } |
| 326 | .buttonStyle(PressStyle(scale: 0.85)) |
| 327 | .tickFeedback(on) |
| 328 | .disabled(future) |
| 329 | .opacity(future ? 0.3 : 1) |
| 330 | .accessibilityLabel(date.formatted(date: .complete, time: .omitted)) |
| 331 | .accessibilityAddTraits(on ? .isSelected : []) |
| 332 | } |
| 333 | |
| 334 | private func contains(_ habit: Habit, _ date: Date) -> Bool { |
| 335 | store.journal.habits[habit.id]?.contains(CalendarDay.key(date)) == true |
| 336 | } |
| 337 | |
| 338 | /// The last seven days for a habit, oldest first, ending today. |
| 339 | private func week(_ habit: Habit) -> [Bool] { |
| 340 | (0..<7).map { back in |
| 341 | calendar.date(byAdding: .day, value: back - 6, to: store.day).map { contains(habit, $0) } ?? false |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | private func toggle(_ habit: Habit, _ date: Date) { |
| 346 | store.change { $0.toggleHabit(habit.id, on: date) } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | struct CleaningView: View { |
| 351 | @Environment(JournalStore.self) private var store |
| 352 | let catalog: Catalog |
| 353 | |
| 354 | @State private var duration: Int? |
| 355 | private let icons = ["room-kitchen": "frying.pan.fill", "room-bath": "bathtub.fill", "room-bed": "bed.double.fill", |
| 356 | "room-living": "sofa.fill", "room-office": "lamp.desk.fill", "room-garage": "door.garage.closed"] |
| 357 | |
| 358 | private var lists: [CleaningList] { catalog.cleaning.rooms + [catalog.cleaning.daily] + catalog.cleaning.speed } |
| 359 | |
| 360 | private var favorites: [HomeChore] { |
| 361 | let saved = store.journal.favoriteChores ?? [] |
| 362 | return chores.filter { saved.contains($0.id) } |
| 363 | } |
| 364 | |
| 365 | private var chores: [HomeChore] { |
| 366 | lists.flatMap { list in list.items.indices.map { HomeChore.cleaning(list, $0) } } |
| 367 | + store.maintenanceChores.map(HomeChore.maintenance) |
| 368 | } |
| 369 | |
| 370 | private var recent: [CompletedChore] { |
| 371 | let first = Calendar.current.date(byAdding: .day, value: -29, to: store.day) ?? store.day |
| 372 | let days = CalendarDay.key(first)...store.todayKey |
| 373 | let choreRecords = chores.flatMap { chore in |
| 374 | (store.journal.recentChores?[chore.id] ?? []).filter { days.contains($0) }.map { |
| 375 | CompletedChore(id: chore.id + ":" + $0, title: chore.title, source: source(of: chore), day: $0) |
| 376 | } |
| 377 | } |
| 378 | let finishedLists = lists.compactMap { list -> CompletedChore? in |
| 379 | guard let day = store.journal.cleaningLast[list.id], days.contains(day) else { return nil } |
| 380 | return CompletedChore(id: "finished:" + list.id, title: "Finished the \(list.name) checklist", source: nil, day: day) |
| 381 | } |
| 382 | return (choreRecords + finishedLists).sorted { |
| 383 | if $0.day != $1.day { return $0.day > $1.day } |
| 384 | if $0.title != $1.title { return $0.title < $1.title } |
| 385 | return $0.id < $1.id |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | var body: some View { |
| 390 | Page(title: "Cleaning & maintenance", width: 800) { |
| 391 | let rooms = catalog.cleaning.rooms |
| 392 | if let room = rooms.first(where: { !(store.journal.cleaningChecks[$0.id] ?? []).isEmpty }) ?? |
| 393 | rooms.min(by: { (store.journal.cleaningLast[$0.id] ?? "") < (store.journal.cleaningLast[$1.id] ?? "") }) { |
| 394 | Panel(padding: 20) { |
| 395 | HStack(spacing: 18) { |
| 396 | Artwork(slot: 72, height: 105, corner: 12).frame(width: 112) |
| 397 | VStack(alignment: .leading, spacing: 7) { |
| 398 | Eyebrow("Suggested next") |
| 399 | Text(room.name).font(GlimmerFont.reading(.title)) |
| 400 | if let step = room.items.first { Text(step).foregroundStyle(.secondary) } |
| 401 | NavigationLink("Open checklist") { CleaningSession(list: room) }.glimmerButton() |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | Panel(title: "Favorites", plain: true) { |
| 407 | let saved = favorites |
| 408 | if saved.isEmpty { |
| 409 | Text("Tap the star on any chore to keep it here.").foregroundStyle(.secondary) |
| 410 | } else { |
| 411 | ForEach(saved.prefix(5)) { chore in choreRow(chore) } |
| 412 | if saved.count > 5 { |
| 413 | NavigationLink("All favorites") { FavoriteChores(chores: saved, source: source) } |
| 414 | .glimmerButton() |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | Panel(title: "How much time do you have?", plain: true) { |
| 419 | FlowLayout(spacing: 6) { |
| 420 | ForEach(catalog.cleaning.speed) { item in |
| 421 | if let minutes = item.mins { |
| 422 | Button("\(minutes) min") { duration = minutes } |
| 423 | .glimmerButton(minutes == (duration ?? catalog.cleaning.speed.first?.mins) ? .selected : .standard, tint: GlimmerColor.sage) |
| 424 | .accessibilityAddTraits(minutes == (duration ?? catalog.cleaning.speed.first?.mins) ? .isSelected : []) |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | if let item = catalog.cleaning.speed.first(where: { $0.mins == duration }) ?? catalog.cleaning.speed.first { |
| 429 | NavigationLink(item.name) { CleaningSession(list: item) }.buttonStyle(.borderless) |
| 430 | } |
| 431 | link(catalog.cleaning.daily, icon: "repeat") |
| 432 | } |
| 433 | Panel(title: "By room", plain: true) { |
| 434 | LazyVGrid(columns: [GridItem(.adaptive(minimum: 180), spacing: 14)], alignment: .leading, spacing: 14) { |
| 435 | ForEach(rooms) { room in |
| 436 | NavigationLink { CleaningSession(list: room) } label: { |
| 437 | HStack(spacing: 12) { |
| 438 | GlyphTile(icons[room.id] ?? "bubbles.and.sparkles.fill", tint: GlimmerColor.accent, size: 34) |
| 439 | Text(room.name).font(.headline) |
| 440 | }.frame(minHeight: 44) |
| 441 | }.buttonStyle(.plain) |
| 442 | } |
| 443 | } |
| 444 | NavigationLink { |
| 445 | Page(title: "House systems") { |
| 446 | MaintenanceChores(chores: store.maintenanceChores.filter { $0.room == "room-house" }) |
| 447 | } |
| 448 | } label: { |
| 449 | Label("House systems", systemImage: "wrench.and.screwdriver") |
| 450 | } |
| 451 | .glimmerButton() |
| 452 | } |
| 453 | Panel(title: "Recently completed", plain: true) { |
| 454 | let records = recent |
| 455 | if records.isEmpty { |
| 456 | Text("Chores you check off will appear here with the day you did them.").foregroundStyle(.secondary) |
| 457 | } else { |
| 458 | completedRows(records.prefix(5)) |
| 459 | if records.count > 5 { |
| 460 | NavigationLink("See all") { |
| 461 | Page(title: "Recently completed", subtitle: "The last 30 days") { |
| 462 | completedRows(recent[...]) |
| 463 | } |
| 464 | } |
| 465 | .glimmerButton() |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | private func source(of chore: HomeChore) -> String { |
| 473 | switch chore { |
| 474 | case .cleaning(let list, _): list.name |
| 475 | case .maintenance(let item): |
| 476 | catalog.cleaning.rooms.first(where: { $0.id == item.room })?.name ?? "House systems" |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | private func choreRow(_ chore: HomeChore) -> some View { |
| 481 | HomeChoreRow(chore: chore, detail: source(of: chore)) |
| 482 | } |
| 483 | |
| 484 | private func completedRows(_ records: ArraySlice<CompletedChore>) -> some View { |
| 485 | ForEach(records) { record in |
| 486 | VStack(alignment: .leading, spacing: 4) { |
| 487 | Text(record.title).font(.callout.weight(.semibold)) |
| 488 | Text([record.source, choreDate(record.day)].compactMap { $0 }.joined(separator: " · ")) |
| 489 | .font(.caption).foregroundStyle(.secondary) |
| 490 | Divider().accessibilityHidden(true) |
| 491 | } |
| 492 | .accessibilityElement(children: .combine) |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | private func link(_ list: CleaningList, icon: String) -> some View { |
| 497 | let checks = store.journal.cleaningChecks[list.id]?.count ?? 0 |
| 498 | let detail: String? = if let date = store.journal.cleaningLast[list.id] { "Last finished \(choreDate(date))" } |
| 499 | else if checks > 0 { "\(checks) of \(list.items.count) done" } |
| 500 | else { list.sub } |
| 501 | return LinkRow(title: list.name, detail: detail, icon: icon, tint: GlimmerColor.accent) { |
| 502 | CleaningSession(list: list) |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | private struct CompletedChore: Identifiable { |
| 508 | let id: String |
| 509 | let title: String |
| 510 | let source: String? |
| 511 | let day: String |
| 512 | } |
| 513 | |
| 514 | private enum HomeChore: Identifiable { |
| 515 | case cleaning(CleaningList, Int) |
| 516 | case maintenance(MaintenanceChore) |
| 517 | |
| 518 | var id: String { |
| 519 | switch self { |
| 520 | case .cleaning(let list, let index): list.choreID(at: index) |
| 521 | case .maintenance(let chore): "maintenance:" + chore.id |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | var title: String { |
| 526 | switch self { |
| 527 | case .cleaning(let list, let index): list.items[index] |
| 528 | case .maintenance(let chore): chore.title |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | private func choreDate(_ day: String) -> String { |
| 534 | guard let date = PracticeDate.parse(day) else { return day } |
| 535 | let calendar = Calendar.current |
| 536 | if calendar.isDateInToday(date) { return "Today" } |
| 537 | if calendar.isDateInYesterday(date) { return "Yesterday" } |
| 538 | return date.formatted(.dateTime.month(.abbreviated).day()) |
| 539 | } |
| 540 | |
| 541 | private struct FavoriteChores: View { |
| 542 | @Environment(JournalStore.self) private var store |
| 543 | let chores: [HomeChore] |
| 544 | let source: (HomeChore) -> String |
| 545 | @State private var visible: [HomeChore] = [] |
| 546 | |
| 547 | var body: some View { |
| 548 | Page(title: "Favorites") { |
| 549 | ForEach(visible) { chore in HomeChoreRow(chore: chore, detail: source(chore)) } |
| 550 | if visible.isEmpty { |
| 551 | Text("Tap the star on any chore to keep it here.").foregroundStyle(.secondary) |
| 552 | } |
| 553 | } |
| 554 | .onAppear { visible = chores.filter { store.journal.favoriteChores?.contains($0.id) == true } } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | private struct HomeChoreRow: View { |
| 559 | @Environment(JournalStore.self) private var store |
| 560 | let chore: HomeChore |
| 561 | var detail: String? = nil |
| 562 | var compactMaintenance: MaintenanceChore? = nil |
| 563 | |
| 564 | private var checked: Bool { |
| 565 | switch chore { |
| 566 | case .cleaning(let list, let index): store.journal.cleaningChecks[list.id]?.contains(index) == true |
| 567 | case .maintenance: store.journal.recentChores?[chore.id]?.contains(store.todayKey) == true |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | var body: some View { |
| 572 | let favorite = store.journal.favoriteChores?.contains(chore.id) == true |
| 573 | HStack(alignment: .center, spacing: 0) { |
| 574 | Button { |
| 575 | store.change { journal in |
| 576 | switch chore { |
| 577 | case .cleaning(let list, let index): journal.toggleCleaningChore(list, at: index, on: store.day) |
| 578 | case .maintenance: journal.toggleMaintenanceChore(chore.id, on: store.day) |
| 579 | } |
| 580 | } |
| 581 | } label: { |
| 582 | if compactMaintenance != nil { |
| 583 | Tick(done: checked, size: 24) |
| 584 | .frame(width: 44, height: 44) |
| 585 | .contentShape(.rect) |
| 586 | .accessibilityLabel((checked ? "Undo " : "Complete ") + chore.title) |
| 587 | } else { |
| 588 | HStack(spacing: 2) { |
| 589 | Tick(done: checked, size: 24).frame(width: 44, height: 44) |
| 590 | GlyphTile(Glyph.symbol(name: chore.title, category: "cleaning"), tint: Glyph.tint(for: "cleaning")) |
| 591 | .padding(.trailing, 8) |
| 592 | VStack(alignment: .leading, spacing: 2) { |
| 593 | Text(chore.title).font(.body) |
| 594 | .foregroundStyle(checked ? Color.secondary : Color.primary) |
| 595 | if let detail { |
| 596 | Text(detail).font(.subheadline).foregroundStyle(.secondary) |
| 597 | } |
| 598 | } |
| 599 | .multilineTextAlignment(.leading) |
| 600 | Spacer(minLength: 4) |
| 601 | } |
| 602 | .padding(.vertical, detail == nil ? 0 : 4) |
| 603 | .frame(maxWidth: .infinity, minHeight: 44, alignment: .leading) |
| 604 | .contentShape(.rect) |
| 605 | } |
| 606 | } |
| 607 | .buttonStyle(RowPressStyle()) |
| 608 | .tickFeedback(checked) |
| 609 | .accessibilityAddTraits(checked ? .isSelected : []) |
| 610 | .accessibilityIdentifier(chore.id) |
| 611 | |
| 612 | if let item = compactMaintenance { |
| 613 | NavigationLink { |
| 614 | Page(title: item.title, width: 660) { |
| 615 | Text(item.tip) |
| 616 | .font(GlimmerFont.reading(.title3)) |
| 617 | .fixedSize(horizontal: false, vertical: true) |
| 618 | } |
| 619 | } label: { |
| 620 | HStack(spacing: 5) { |
| 621 | VStack(alignment: .leading, spacing: 3) { |
| 622 | Text(item.title).font(.callout.weight(.semibold)) |
| 623 | Text(detail ?? item.cadence).font(.caption).foregroundStyle(GlimmerColor.dusk) |
| 624 | } |
| 625 | .fixedSize(horizontal: false, vertical: true) |
| 626 | Spacer(minLength: 0) |
| 627 | Image(systemName: "chevron.right") |
| 628 | .font(.caption2.weight(.semibold)) |
| 629 | .foregroundStyle(.tertiary) |
| 630 | .accessibilityHidden(true) |
| 631 | } |
| 632 | .frame(maxWidth: .infinity, minHeight: 44, alignment: .leading) |
| 633 | .contentShape(.rect) |
| 634 | } |
| 635 | .buttonStyle(.plain) |
| 636 | .accessibilityHint("Open maintenance tip") |
| 637 | } |
| 638 | |
| 639 | Button { |
| 640 | store.change { $0.toggleFavoriteChore(chore.id) } |
| 641 | } label: { |
| 642 | Image(systemName: favorite ? "star.fill" : "star") |
| 643 | .font(.callout.weight(.medium)) |
| 644 | .foregroundStyle(favorite ? GlimmerColor.honey : .secondary) |
| 645 | .frame(width: 44, height: 44) |
| 646 | .contentShape(.rect) |
| 647 | } |
| 648 | .buttonStyle(.plain) |
| 649 | .accessibilityLabel((favorite ? "Remove from favorites" : "Add to favorites") + ", " + chore.title) |
| 650 | .accessibilityIdentifier("favorite:" + chore.id) |
| 651 | .help(favorite ? "Remove from favorites" : "Add to favorites") |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | struct MaintenanceChores: View { |
| 657 | @Environment(JournalStore.self) private var store |
| 658 | let chores: [MaintenanceChore] |
| 659 | var compact = false |
| 660 | |
| 661 | var body: some View { |
| 662 | VStack(alignment: .leading, spacing: compact ? 3 : 10) { |
| 663 | ForEach(chores) { item in |
| 664 | let chore = HomeChore.maintenance(item) |
| 665 | let last = store.journal.recentChores?[chore.id]?.max() |
| 666 | let status = last.map { $0 == store.todayKey ? "Done today" : "Last done \(choreDate($0))" } |
| 667 | let summary = [item.cadence, status].compactMap { $0 }.joined(separator: " · ") |
| 668 | HomeChoreRow(chore: chore, detail: compact ? summary : summary + "\n" + item.tip, |
| 669 | compactMaintenance: compact ? item : nil) |
| 670 | if compact, item.id != chores.last?.id { Divider().accessibilityHidden(true) } |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | struct CleaningSession: View { |
| 677 | @Environment(JournalStore.self) private var store |
| 678 | let list: CleaningList |
| 679 | @State private var section: Section = .cleaning |
| 680 | |
| 681 | private enum Section: String, CaseIterable { case cleaning = "Cleaning", maintenance = "Maintenance" } |
| 682 | |
| 683 | var body: some View { |
| 684 | let maintenance = store.maintenanceChores.filter { $0.room == list.id } |
| 685 | Page(title: list.name, width: maintenance.isEmpty ? 640 : 880) { |
| 686 | if maintenance.isEmpty { |
| 687 | checklist |
| 688 | } else { |
| 689 | ViewThatFits(in: .horizontal) { |
| 690 | HStack(alignment: .top, spacing: 24) { |
| 691 | Panel(title: "Cleaning", plain: true) { checklist }.frame(minWidth: 320) |
| 692 | Panel(title: "Maintenance", plain: true) { MaintenanceChores(chores: maintenance) }.frame(minWidth: 300) |
| 693 | } |
| 694 | VStack(alignment: .leading, spacing: 14) { |
| 695 | GlassSegments(label: "Chore type", options: Section.allCases.map { GlassSegment(value: $0, title: $0.rawValue) }, |
| 696 | selection: $section) |
| 697 | if section == .cleaning { checklist } |
| 698 | else { MaintenanceChores(chores: maintenance) } |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | private var checklist: some View { |
| 706 | let checks = store.journal.cleaningChecks[list.id] ?? [] |
| 707 | return VStack(alignment: .leading, spacing: 10) { |
| 708 | Text("\(checks.count) of \(list.items.count) done").foregroundStyle(.secondary) |
| 709 | Meter(value: Double(checks.count) / Double(max(1, list.items.count)), tint: GlimmerColor.softBlue) |
| 710 | VStack(spacing: 0) { |
| 711 | ForEach(list.items.indices, id: \.self) { index in |
| 712 | HomeChoreRow(chore: .cleaning(list, index)) |
| 713 | if index < list.items.count - 1 { Divider().opacity(0.6).padding(.leading, 44) } |
| 714 | } |
| 715 | } |
| 716 | .padding(.leading, -8) |
| 717 | if !checks.isEmpty { |
| 718 | SecondaryAction(title: "Start a fresh checklist", systemImage: "arrow.counterclockwise") { |
| 719 | store.change { $0.cleaningChecks[list.id] = [] } |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | } |