Glimmer / app / native / Glimmer / Lessons.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | struct LessonsView: View { |
| 4 | @Environment(JournalStore.self) private var store |
| 5 | let catalog: Catalog |
| 6 | |
| 7 | var body: some View { |
| 8 | let open = catalog.lessons.filter { store.journal.lessonIsOpen($0.number, on: store.day) } |
| 9 | let plan = catalog.lessons |
| 10 | let completed = plan.filter { store.journal.lessons[$0.id]?.completed == true }.count |
| 11 | Page(title: "Lessons", subtitle: "One lesson unlocks each day, and they deepen as you go. After day 90, the Deep Path begins. Come back to any you’ve reached.", width: 900) { |
| 12 | let featured = open.first(where: { store.journal.lessons[$0.id]?.completed != true }) ?? open.last |
| 13 | if let featured { |
| 14 | Panel(padding: 16) { |
| 15 | ViewThatFits(in: .horizontal) { |
| 16 | HStack(alignment: .top, spacing: 18) { |
| 17 | Artwork(slot: featured.number, height: 220).frame(maxWidth: 380) |
| 18 | featuredText(featured, completed: completed, total: plan.count) |
| 19 | } |
| 20 | VStack(alignment: .leading, spacing: 14) { |
| 21 | Artwork(slot: featured.number, height: 190) |
| 22 | featuredText(featured, completed: completed, total: plan.count) |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | let remaining = open.filter { $0.id != featured?.id } |
| 28 | let arcs = remaining.reduce(into: [String]()) { if !$0.contains($1.arc) { $0.append($1.arc) } } |
| 29 | ForEach(arcs, id: \.self) { arc in |
| 30 | Panel(title: arc, plain: true) { |
| 31 | VStack(alignment: .leading, spacing: 0) { |
| 32 | ForEach(remaining.filter { $0.arc == arc }) { lesson in |
| 33 | NavigationLink { LessonReader(lesson: lesson) } label: { |
| 34 | HStack(spacing: 14) { |
| 35 | Artwork(slot: lesson.number, height: 44, corner: 7).frame(width: 66).accessibilityHidden(true) |
| 36 | VStack(alignment: .leading) { |
| 37 | Text(lesson.title).font(.headline) |
| 38 | Text("Day \(lesson.number) · \(lesson.minutes) min").font(.caption).foregroundStyle(.secondary) |
| 39 | } |
| 40 | if store.journal.lessons[lesson.id]?.completed == true { Image(systemName: "checkmark").accessibilityLabel("Completed") } |
| 41 | }.padding(.vertical, 6) |
| 42 | }.buttonStyle(.plain) |
| 43 | Divider() |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | if open.count < catalog.lessons.count { |
| 49 | Label("\(catalog.lessons.count - open.count) more lessons unlock one day at a time.", systemImage: "lock.open") |
| 50 | .font(.callout).foregroundStyle(.secondary) |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | private func featuredText(_ lesson: Lesson, completed: Int, total: Int) -> some View { |
| 56 | VStack(alignment: .leading, spacing: 10) { |
| 57 | Eyebrow("Your reading plan · \(completed) of \(total) complete", color: GlimmerColor.dusk) |
| 58 | Meter(value: Double(completed) / Double(max(1, total)), tint: GlimmerColor.dusk) |
| 59 | Text(lesson.title).font(GlimmerFont.display(.title2, weight: .bold)) |
| 60 | Text(lesson.tagline).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true) |
| 61 | NavigationLink(store.journal.lessons[lesson.id]?.completed == true ? "Read again" : "Read lesson") { LessonReader(lesson: lesson) } |
| 62 | .glimmerButton(.prominent) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | } |
| 67 | |
| 68 | struct LessonReader: View { |
| 69 | @Environment(JournalStore.self) private var store |
| 70 | @Environment(\.dismiss) private var dismiss |
| 71 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 72 | let lesson: Lesson |
| 73 | @State private var forward = true |
| 74 | @State private var index = 0 |
| 75 | |
| 76 | var body: some View { |
| 77 | if !lesson.pages.isEmpty && store.journal.lessonIsOpen(lesson.number, on: store.day) { |
| 78 | let page = lesson.pages[index] |
| 79 | let last = index == lesson.pages.count - 1 |
| 80 | Page(title: page.title, kicker: "Day \(lesson.number) · \(lesson.title)", width: 680) { |
| 81 | StepDots(current: index, total: lesson.pages.count) |
| 82 | if index == 0 { Artwork(slot: lesson.number, height: 240) } |
| 83 | if page.body != nil || page.tryToday != nil { |
| 84 | VStack(alignment: .leading, spacing: 16) { |
| 85 | GlyphTile(Glyph.symbol(icon: page.emoji, name: page.title), tint: GlimmerColor.dusk, size: 44) |
| 86 | if let body = page.body { |
| 87 | ForEach(Array(body.components(separatedBy: "\n\n").enumerated()), id: \.offset) { _, paragraph in |
| 88 | Text(markdown(paragraph)).font(GlimmerFont.reading(.title3)).lineSpacing(5) |
| 89 | .fixedSize(horizontal: false, vertical: true) |
| 90 | } |
| 91 | } |
| 92 | if let tryToday = page.tryToday, page.body?.contains(tryToday) != true { |
| 93 | Label(tryToday, systemImage: "lightbulb").font(.callout.weight(.medium)) |
| 94 | .padding(14).frame(maxWidth: .infinity, alignment: .leading) |
| 95 | .background(GlimmerColor.honey.opacity(0.12), in: .rect(cornerRadius: 14)) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | if let question = page.question { |
| 100 | Panel(title: "Reflect", accent: GlimmerColor.dusk) { |
| 101 | if page.body == nil { GlyphTile(Glyph.symbol(icon: page.emoji, name: page.title), tint: GlimmerColor.dusk, size: 48) } |
| 102 | Text(question).font(GlimmerFont.display(.title3, weight: .bold)) |
| 103 | VStack(spacing: 8) { |
| 104 | ForEach(page.options ?? [], id: \.self) { option in |
| 105 | SelectRow(label: option, selected: currentChoice == option, tint: GlimmerColor.dusk) { |
| 106 | store.change { $0.lessons[lesson.id, default: LessonRecord()].choices[String(index)] = option } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | .glassGroup(spacing: 8) |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | .id(index) |
| 115 | .transition(.identity) |
| 116 | .modifier(ReadingPageTurn(forward: forward)) |
| 117 | .actionBar(width: 680) { |
| 118 | if index > 0 { |
| 119 | GlassIconButton(label: "Previous page", systemImage: "chevron.left") { move(index - 1) } |
| 120 | } |
| 121 | PrimaryAction(title: last ? "Complete lesson" : "Next page", systemImage: last ? "checkmark" : nil) { |
| 122 | if last { |
| 123 | let first = store.journal.lessons[lesson.id]?.completed != true |
| 124 | let previous = store.catalog.map { ReaderProgress(journal: store.journal, catalog: $0).rank } |
| 125 | if store.change({ $0.completeLesson(lesson, on: store.day) }) { |
| 126 | if !first { store.notice = "Lesson complete." } |
| 127 | if let catalog = store.catalog { |
| 128 | let rank = ReaderProgress(journal: store.journal, catalog: catalog).rank |
| 129 | if first && rank != previous { |
| 130 | let text = "\(rank.rawValue) unlocked" |
| 131 | store.notice = text |
| 132 | AccessibilityNotification.Announcement(text).post() |
| 133 | } |
| 134 | } |
| 135 | dismiss() |
| 136 | } |
| 137 | } else { move(index + 1) } |
| 138 | } |
| 139 | .disabled(page.kind == "stop" && currentChoice == nil) |
| 140 | } |
| 141 | .hidesTabBar() |
| 142 | .onAppear { |
| 143 | let progress = store.journal.lessons[lesson.id] |
| 144 | if progress?.completed != true { index = min(max(0, progress?.page ?? 0), lesson.pages.count - 1) } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | private var currentChoice: String? { store.journal.lessons[lesson.id]?.choices[String(index)] } |
| 150 | |
| 151 | private func move(_ page: Int) { |
| 152 | if store.change({ $0.lessons[lesson.id, default: LessonRecord()].page = page }) { |
| 153 | forward = page > index |
| 154 | index = page |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | private func markdown(_ paragraph: String) -> AttributedString { |
| 159 | (try? AttributedString(markdown: paragraph, |
| 160 | options: .init(interpretedSyntax: .inlineOnlyPreservingWhitespace))) ?? AttributedString(paragraph) |
| 161 | } |
| 162 | } |