Glimmer / app · glimmertoday.ai / native / Glimmer / Catalog.swift
| 1 | import Foundation |
| 2 | |
| 3 | struct Catalog: Decodable { |
| 4 | let sources: [String] |
| 5 | // Built-in names are plain words; any emoji in an older or remote edition is dropped (see Glyph). |
| 6 | @PlainNames var activities: [String] |
| 7 | @PlainNames var people: [String] |
| 8 | let tags: [String: [String]] |
| 9 | let defaultTags: [String] |
| 10 | let initialTasks: [TaskTemplate] |
| 11 | let library: [TaskCategory] |
| 12 | let preload: [TaskTemplate] |
| 13 | let onboarding: [OnboardingStep] |
| 14 | let challenges: [Challenge] |
| 15 | let energy: [EnergyLevel] |
| 16 | let energyMessages: [String: String] |
| 17 | let habits: [Habit] |
| 18 | let cleaning: CleaningCatalog |
| 19 | let seasonal: [SeasonalEdition]? |
| 20 | let quotes: [DailyQuote]? |
| 21 | let coach: [Suggestion] |
| 22 | let startingPractices: [String: Suggestion] |
| 23 | let practiceTopics: [PracticeTopic] |
| 24 | let praise: [String] |
| 25 | let lessons: [Lesson] |
| 26 | |
| 27 | static func load(from url: URL) throws -> Self { |
| 28 | try JSONDecoder().decode(Self.self, from: Data(contentsOf: url)) |
| 29 | } |
| 30 | |
| 31 | func startingSuggestions(_ answers: [String: [String]]) -> [Suggestion] { |
| 32 | let distractions = answers["distraction"] ?? [] |
| 33 | let ai = answers["ai_use"] ?? [] |
| 34 | let replacements = answers["replacements"] ?? [] |
| 35 | let topics = (answers["primary_goal"] ?? []).compactMap { goal in practiceTopics.first { $0.id == goal } } |
| 36 | // Give each selected topic a place before offering a second practice from one topic. |
| 37 | var suggestions: [Suggestion] = [] |
| 38 | func add(_ key: String) { |
| 39 | if let suggestion = startingPractices[key] { suggestions.append(suggestion) } |
| 40 | } |
| 41 | for round in 0..<3 { |
| 42 | suggestions += topics.compactMap { topic -> Suggestion? in |
| 43 | let opening = topic.exercises.filter { $0.stage == 1 } |
| 44 | return opening.indices.contains(round) ? opening[round].suggestion : nil |
| 45 | } |
| 46 | } |
| 47 | if !Set(distractions).isDisjoint(with: ["outrage", "feeds"]) { |
| 48 | suggestions.append(contentsOf: coach.prefix(2)) |
| 49 | } |
| 50 | if distractions.contains("short_video") { |
| 51 | add("short_video") |
| 52 | } |
| 53 | if !ai.isEmpty && !ai.contains("rare") { |
| 54 | add("ai_quiz") |
| 55 | } |
| 56 | if replacements.contains("walk") { |
| 57 | add("change_of_scene") |
| 58 | } |
| 59 | if replacements.contains("friend") { |
| 60 | add("contact_someone") |
| 61 | } |
| 62 | add("notice_feed") |
| 63 | add("screen_free_meal") |
| 64 | return Array(suggestions.prefix(4)) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// A list of names decoded without leading or trailing emoji, so "💻 Deep work" reads "Deep work". |
| 69 | @propertyWrapper |
| 70 | struct PlainNames: Decodable { |
| 71 | var wrappedValue: [String] |
| 72 | |
| 73 | init(wrappedValue: [String]) { self.wrappedValue = wrappedValue } |
| 74 | |
| 75 | init(from decoder: Decoder) throws { |
| 76 | wrappedValue = try decoder.singleValueContainer().decode([String].self).map(Glyph.plainName) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | struct DailyQuote: Decodable, Identifiable { |
| 81 | let id: String |
| 82 | let text: String |
| 83 | let author: String |
| 84 | |
| 85 | private static let firstDay = DateComponents(year: 2026, month: 11, day: 23) |
| 86 | |
| 87 | static func quote(on date: Date, from quotes: [DailyQuote]) -> DailyQuote? { |
| 88 | let calendar = PracticeDate.calendar |
| 89 | guard !quotes.isEmpty, let start = calendar.date(from: firstDay) else { return nil } |
| 90 | let offset = CalendarDay.distance(start, date, calendar: calendar) |
| 91 | let index = (offset % quotes.count + quotes.count) % quotes.count |
| 92 | return quotes[index] |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | struct TaskTemplate: Decodable, Identifiable { |
| 97 | let icon: String |
| 98 | let name: String |
| 99 | let freq: String |
| 100 | let cat: String? |
| 101 | var id: String { |
| 102 | "lt_" + name.lowercased() |
| 103 | .replacingOccurrences(of: "[^a-z0-9]+", with: "_", options: .regularExpression) |
| 104 | .trimmingCharacters(in: CharacterSet(charactersIn: "_")) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | struct TaskCategory: Decodable, Identifiable { |
| 109 | let id: String |
| 110 | let icon: String |
| 111 | let name: String |
| 112 | let desc: String |
| 113 | let tasks: [TaskTemplate] |
| 114 | } |
| 115 | |
| 116 | struct OnboardingStep: Decodable { |
| 117 | struct Option: Decodable, Identifiable { |
| 118 | let v: String |
| 119 | let label: String |
| 120 | var id: String { v } |
| 121 | } |
| 122 | |
| 123 | let kind: String |
| 124 | let key: String? |
| 125 | let mode: String? |
| 126 | let max: Int? |
| 127 | let kicker: String? |
| 128 | let title: String? |
| 129 | let sub: String? |
| 130 | let options: [Option]? |
| 131 | |
| 132 | func toggling(_ value: String, in answers: [String]) -> [String] { |
| 133 | if mode == "single" { return [value] } |
| 134 | if answers.contains(value) { return answers.filter { $0 != value } } |
| 135 | if let max, answers.count >= max { return answers } |
| 136 | return answers + [value] |
| 137 | } |
| 138 | |
| 139 | } |
| 140 | |
| 141 | struct PracticeTopic: Decodable, Identifiable { |
| 142 | let id: String |
| 143 | let title: String |
| 144 | let exercises: [Practice] |
| 145 | } |
| 146 | |
| 147 | /// Editorial identifiers remain stable when the displayed copy changes. |
| 148 | struct Practice: Decodable, Identifiable { |
| 149 | let id: String |
| 150 | let title: String |
| 151 | let sub: String |
| 152 | let type: String |
| 153 | /// Journey stage (1–5) at which this practice opens. Missing means stage 1. |
| 154 | let level: Int? |
| 155 | var stage: Int { min(max(level ?? 1, 1), JourneyStage.count) } |
| 156 | var suggestion: Suggestion { .init(type: type, title: title, sub: sub) } |
| 157 | |
| 158 | func taskID(day: String) -> String { "glimmer:practice:\(day):\(id)" } |
| 159 | |
| 160 | func task(on date: Date) -> PlanTask { |
| 161 | var task = PlanTask() |
| 162 | task.id = taskID(day: PracticeDate.key(date)) |
| 163 | task.name = title |
| 164 | task.icon = "leaf.fill" |
| 165 | task.repeats = false |
| 166 | task.anchor = date |
| 167 | return task |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | struct Suggestion: Codable, Identifiable, Hashable { |
| 172 | let type: String |
| 173 | let title: String |
| 174 | let sub: String |
| 175 | var id: String { title } |
| 176 | } |
| 177 | |
| 178 | struct Challenge: Decodable, Identifiable { |
| 179 | struct Page: Decodable { |
| 180 | let title: String |
| 181 | let lines: [String] |
| 182 | let hero: String? |
| 183 | let heroAlt: String? |
| 184 | } |
| 185 | |
| 186 | struct Option: Decodable, Identifiable { |
| 187 | let e: String |
| 188 | let l: String |
| 189 | var id: String { l } |
| 190 | } |
| 191 | |
| 192 | let id: String |
| 193 | let icon: String |
| 194 | let title: String |
| 195 | let sub: String |
| 196 | let coins: Int |
| 197 | let mins: Int |
| 198 | /// Journey stage (1–5) at which this challenge opens. Missing means stage 1. |
| 199 | let level: Int? |
| 200 | var stage: Int { min(max(level ?? 1, 1), JourneyStage.count) } |
| 201 | let hero: String? |
| 202 | let heroAlt: String? |
| 203 | let teach: [Page] |
| 204 | let q: String |
| 205 | let opts: [Option] |
| 206 | let action: Page |
| 207 | let done: Page |
| 208 | } |
| 209 | |
| 210 | struct EnergyLevel: Decodable, Identifiable { |
| 211 | let id: String |
| 212 | let label: String |
| 213 | let color: String |
| 214 | } |
| 215 | |
| 216 | struct Habit: Decodable, Identifiable { |
| 217 | let id: String |
| 218 | let icon: String |
| 219 | let name: String |
| 220 | } |
| 221 | |
| 222 | struct CleaningCatalog: Decodable { |
| 223 | let speed: [CleaningList] |
| 224 | let daily: CleaningList |
| 225 | let rooms: [CleaningList] |
| 226 | let maintenance: [MaintenanceChore]? |
| 227 | } |
| 228 | |
| 229 | struct MaintenanceChore: Decodable, Identifiable { |
| 230 | let id: String |
| 231 | let room: String |
| 232 | let title: String |
| 233 | let cadence: String |
| 234 | let tip: String |
| 235 | } |
| 236 | |
| 237 | struct CleaningList: Decodable, Identifiable { |
| 238 | let id: String |
| 239 | let title: String? |
| 240 | let mins: Int? |
| 241 | let sub: String? |
| 242 | let items: [String] |
| 243 | var name: String { title ?? "\(mins ?? 0)-minute clean" } |
| 244 | |
| 245 | func choreID(at index: Int) -> String { "cleaning:\(id):\(items[index])" } |
| 246 | } |
| 247 | |
| 248 | struct SeasonalEdition: Decodable, Identifiable { |
| 249 | struct Tip: Decodable, Identifiable { |
| 250 | let id: String |
| 251 | let title: String |
| 252 | let summary: String |
| 253 | let body: String |
| 254 | } |
| 255 | |
| 256 | struct Checklist: Decodable, Identifiable { |
| 257 | struct Item: Decodable, Identifiable { |
| 258 | let id: String |
| 259 | let title: String |
| 260 | let detail: String |
| 261 | } |
| 262 | |
| 263 | let id: String |
| 264 | let title: String |
| 265 | let summary: String |
| 266 | let items: [Item] |
| 267 | } |
| 268 | |
| 269 | struct Resource: Decodable, Identifiable { |
| 270 | let title: String |
| 271 | let url: URL |
| 272 | var id: URL { url } |
| 273 | } |
| 274 | |
| 275 | let id: String |
| 276 | let title: String |
| 277 | let summary: String |
| 278 | let start: String |
| 279 | let end: String |
| 280 | let tips: [Tip] |
| 281 | let maintenance: [String] |
| 282 | let checklists: [Checklist] |
| 283 | let resources: [Resource]? |
| 284 | |
| 285 | func isFeatured(on date: Date) -> Bool { |
| 286 | let day = String(PracticeDate.key(date).suffix(5)) |
| 287 | if start <= end { return (start...end).contains(day) } |
| 288 | return day >= start || day <= end |
| 289 | } |
| 290 | |
| 291 | func recommendation(on date: Date) -> (list: Checklist, item: Checklist.Item)? { |
| 292 | guard isFeatured(on: date) else { return nil } |
| 293 | let recommendations = checklists.flatMap { list in list.items.map { (list, $0) } } |
| 294 | guard !recommendations.isEmpty else { return nil } |
| 295 | let calendar = PracticeDate.calendar |
| 296 | let year = calendar.component(.year, from: date) |
| 297 | let seasonYear = start > end && String(PracticeDate.key(date).suffix(5)) < start ? year - 1 : year |
| 298 | guard let first = PracticeDate.parse("\(seasonYear)-" + start) else { return nil } |
| 299 | let offset = calendar.dateComponents([.day], from: calendar.startOfDay(for: first), |
| 300 | to: calendar.startOfDay(for: date)).day ?? 0 |
| 301 | return recommendations[offset % recommendations.count] |
| 302 | } |
| 303 | |
| 304 | func progressKey(for checklist: Checklist, on date: Date) -> String { |
| 305 | let key = PracticeDate.key(date) |
| 306 | let year = Int(key.prefix(4)) ?? PracticeDate.calendar.component(.year, from: date) |
| 307 | let seasonYear = start > end && String(key.suffix(5)) < start ? year - 1 : year |
| 308 | return "\(id)/\(seasonYear)/\(checklist.id)" |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | struct Lesson: Decodable, Identifiable { |
| 313 | struct Page: Decodable { |
| 314 | let kind: String |
| 315 | let emoji: String |
| 316 | let title: String |
| 317 | let body: String? |
| 318 | let question: String? |
| 319 | let options: [String]? |
| 320 | let tryToday: String? |
| 321 | } |
| 322 | |
| 323 | let slug: String |
| 324 | let number: Int |
| 325 | let arc: String |
| 326 | let title: String |
| 327 | let tagline: String |
| 328 | let minutes: Int |
| 329 | let hero: String? |
| 330 | let heroAlt: String? |
| 331 | let pages: [Page] |
| 332 | var id: String { slug } |
| 333 | } |
| 334 | |
| 335 | /// A dashboard key uses the Mac's local Gregorian date, independent of display language. |
| 336 | enum PracticeDate { |
| 337 | static var calendar: Calendar { |
| 338 | var value = Calendar(identifier: .gregorian) |
| 339 | value.timeZone = .current |
| 340 | return value |
| 341 | } |
| 342 | static func key(_ date: Date) -> String { CalendarDay.key(date, calendar: calendar) } |
| 343 | static func parse(_ value: String) -> Date? { |
| 344 | guard value.utf8.count == 10 else { return nil } |
| 345 | let parts = value.split(separator: "-").compactMap { Int($0) } |
| 346 | guard parts.count == 3, let date = calendar.date(from: DateComponents(year: parts[0], month: parts[1], day: parts[2], hour: 12)), |
| 347 | key(date) == value else { return nil } |
| 348 | return date |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | extension Catalog { |
| 353 | static let reflectionTopicID = "everyday_reflection" |
| 354 | |
| 355 | var reflectionTopic: PracticeTopic? { |
| 356 | practiceTopics.first { $0.id == Self.reflectionTopicID } |
| 357 | } |
| 358 | |
| 359 | func dailyReflection(on date: Date, level: Int = JourneyStage.count) -> Practice? { |
| 360 | guard let exercises = reflectionTopic?.exercises.filter({ $0.stage <= level }), !exercises.isEmpty else { return nil } |
| 361 | return Self.stagePick(exercises, stage: \.stage, level: level, day: dayIndex(date)) |
| 362 | } |
| 363 | |
| 364 | /// Picks from content that is open at this level. Two days in three come from the |
| 365 | /// current or previous stage, so the daily guidance deepens as the person does. |
| 366 | static func stagePick<T>(_ open: [T], stage: (T) -> Int, level: Int, day: Int) -> T? { |
| 367 | guard !open.isEmpty else { return nil } |
| 368 | let fresh = open.filter { stage($0) >= max(1, level - 1) } |
| 369 | // Each pool counts only its own days, so both rotate through every item. |
| 370 | if fresh.isEmpty || day % 3 == 2 { return open[(day / 3) % open.count] } |
| 371 | return fresh[((day / 3) * 2 + day % 3) % fresh.count] |
| 372 | } |
| 373 | |
| 374 | private func dayIndex(_ date: Date) -> Int { |
| 375 | PracticeDate.calendar.ordinality(of: .day, in: .era, for: date) ?? 1 |
| 376 | } |
| 377 | |
| 378 | func dailyPractice(on date: Date, level: Int = JourneyStage.count) -> (topic: PracticeTopic, practice: Practice)? { |
| 379 | let choices = practiceTopics.filter { $0.id != Self.reflectionTopicID } |
| 380 | .flatMap { topic in topic.exercises.filter { $0.stage <= level }.map { (topic, $0) } } |
| 381 | return Self.stagePick(choices, stage: { $0.1.stage }, level: level, day: dayIndex(date)) |
| 382 | } |
| 383 | |
| 384 | /// Up to two practices from the person's current stage, from their chosen topics first. |
| 385 | func suggestions(level: Int, goals: [String]) -> [Suggestion] { |
| 386 | guard level > 1 else { return [] } |
| 387 | let ordered = practiceTopics.filter { goals.contains($0.id) } + practiceTopics.filter { !goals.contains($0.id) } |
| 388 | let matching = ordered.flatMap { $0.exercises.filter { [level, level - 1].contains($0.stage) && $0.stage > 1 } } |
| 389 | return Array(matching.prefix(2).map(\.suggestion)) |
| 390 | } |
| 391 | |
| 392 | /// The challenge to offer next: an unfinished one at the person's current stage first, |
| 393 | /// then the earliest unfinished one that is open. |
| 394 | func nextChallenge(for journal: Journal, level: Int) -> Challenge? { |
| 395 | let open = challenges.filter { |
| 396 | journal.challenges[$0.id] == nil && $0.stage <= level && ($0.id != "coffee" || journal.coffeeUnlocked) |
| 397 | } |
| 398 | return open.first { $0.stage == level } ?? open.first |
| 399 | } |
| 400 | |
| 401 | func dailyLesson(for journal: Journal, on date: Date) -> Lesson? { |
| 402 | guard !lessons.isEmpty else { return nil } |
| 403 | return lessons[(journal.planDay(on: date) - 1) % lessons.count] |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | /// Shared native and MCP attribution for Glimmer's editorial guidance. |
| 408 | enum CoachingSource { |
| 409 | static let author = "Brad Dunlap" |
| 410 | static let role = "author and life coach" |
| 411 | static let credit = "\(author), \(role)" |
| 412 | static let sourcesURL = URL(string: "https://glimmertoday.ai/support#coaching-sources")! |
| 413 | static let note = "I created Glimmer to share what I’ve learned from lived experience, my research and writing, and the people I’ve learned from along the way. Some exercises draw on published research. Other exercises and spiritual reflections come from my own perspective. These are ideas you can try and judge for yourself. Choose what fits your life. I’m not a doctor. Glimmer offers general life guidance and doesn’t provide medical advice, diagnosis, or treatment." |
| 414 | } |