Branchwell

Glimmer / app · glimmertoday.ai / native / Glimmer / ReaderRanks.swift

1import SwiftUI
2
3enum ReaderRank: String, CaseIterable, Identifiable {
4 case fledgling = "Fledgling", seeker = "Seeker", scholar = "Scholar", sage = "Sage", guru = "Guru"
5 var id: Self { self }
6 var bird: String {
7 switch self {
8 case .fledgling: "Chick"
9 case .seeker: "Swallow"
10 case .scholar: "Heron"
11 case .sage: "Crane"
12 case .guru: "Owl"
13 }
14 }
15 var requiredDays: Int {
16 switch self {
17 case .fledgling: 0
18 case .seeker: 14
19 case .scholar: 30
20 case .sage: 60
21 case .guru: 90
22 }
23 }
24
25 /// What this rank opens. Content deepens with practice days, never with pressure.
26 var opens: String {
27 switch self {
28 case .fledgling: "Tiny wins, the first practices and the reading plan"
29 case .seeker: "Steady steps join your daily suggestions: practices and challenges that ask a little more"
30 case .scholar: "Deeper water joins your daily suggestions: patterns, values and what you avoid"
31 case .sage: "Wide circles join your daily suggestions: relationships, repair and community"
32 case .guru: "Quiet mastery joins your daily suggestions: stillness, conscience and a practice of your own"
33 }
34 }
35
36 var requirement: String {
37 switch self {
38 case .fledgling: "Yours from day one"
39 case .guru: "90 active days and 90 lessons"
40 default: "\(requiredDays) active days"
41 }
42 }
43}
44
45/// Glimmer's coaching content opens in five stages that follow the reader ranks'
46/// active-day thresholds. A stage is about practice days, not lessons or coins.
47enum JourneyStage {
48 static let count = ReaderRank.allCases.count
49
50 static func level(days: Int) -> Int {
51 max(1, ReaderRank.allCases.filter { days >= $0.requiredDays }.count)
52 }
53
54 static func rank(for level: Int) -> ReaderRank {
55 ReaderRank.allCases[min(max(level, 1), count) - 1]
56 }
57
58 /// The short name of each stage's content, used for section headings.
59 static func name(_ level: Int) -> String {
60 switch min(max(level, 1), count) {
61 case 1: "Tiny wins"
62 case 2: "Steady steps"
63 case 3: "Deeper water"
64 case 4: "Wide circles"
65 default: "Quiet mastery"
66 }
67 }
68
69 /// What a stage's practices and challenges ask of a person. Everything stays open:
70 /// ranks only set what Glimmer recommends, so these notes describe, never gate.
71 static func asks(_ level: Int) -> String {
72 switch min(max(level, 1), count) {
73 case 1: "Asks for a few minutes"
74 case 2: "Asks for a little steadiness"
75 case 3: "Asks for honest reflection"
76 case 4: "Asks for a real conversation or a kind boundary"
77 default: "Asks for quiet and your own direction"
78 }
79 }
80
81 /// A gentle label for content deeper than Glimmer's current recommendations.
82 static func aheadNote(_ level: Int) -> String { "\(name(level)) · \(asks(level).lowercased())" }
83}
84
85extension Journal {
86 func journeyLevel(asOf date: Date) -> Int {
87 JourneyStage.level(days: activeReaderDays(asOf: date).count)
88 }
89
90 /// The level for today's recommendations. Growth isn't a straight line, so a low or
91 /// empty energy check today brings the daily suggestions back to gentle ground.
92 func guidanceLevel(asOf date: Date) -> Int {
93 let level = journeyLevel(asOf: date)
94 guard let check = energy[CalendarDay.key(date)], ["empty", "low"].contains(check.level) else { return level }
95 return min(level, 2)
96 }
97}
98
99struct ReaderProgress {
100 static let curriculumLength = 90
101 let completed: Int
102 let days: Int
103 var total: Int { Self.curriculumLength }
104 var rank: ReaderRank { ReaderRank.allCases.last(where: hasEarned) ?? .fledgling }
105 var next: ReaderRank? { ReaderRank.allCases.first { !hasEarned($0) } }
106
107 init(journal: Journal, catalog: Catalog, asOf date: Date = .now) {
108 days = journal.activeReaderDays(asOf: date).count
109 let course = catalog.lessons.filter { (1...Self.curriculumLength).contains($0.number) }
110 completed = Set(course.filter { journal.lessons[$0.id]?.completed == true }.map(\.number)).count
111 }
112
113 func hasEarned(_ rank: ReaderRank) -> Bool {
114 days >= rank.requiredDays && (rank != .guru || completed == total)
115 }
116
117 func remaining(until rank: ReaderRank) -> String {
118 var steps = [String]()
119 let daysLeft = rank.requiredDays - days
120 if daysLeft > 0 { steps.append("\(daysLeft) more active \(daysLeft == 1 ? "day" : "days")") }
121 let lessonsLeft = total - completed
122 if rank == .guru && lessonsLeft > 0 {
123 steps.append("\(lessonsLeft) more \(lessonsLeft == 1 ? "lesson" : "lessons")")
124 }
125 return steps.joined(separator: " and ") + " to unlock " + rank.rawValue + "."
126 }
127
128 var summary: String { "\(days) active \(days == 1 ? "day" : "days") · \(completed) of \(total) lessons" }
129}
130
131struct ReaderBird: View {
132 let rank: ReaderRank
133 var unlocked = true
134 var size: CGFloat = 48
135 @Environment(\.readingTheme) private var theme
136 @Environment(\.colorSchemeContrast) private var contrast
137
138 private var opticalOffset: CGSize {
139 switch rank {
140 case .fledgling:
141 // Align the rendered SVG's silhouette with the center of the badge.
142 CGSize(width: -size * 0.015, height: size * 0.002)
143 case .sage: CGSize(width: size * 0.03, height: size * 0.02)
144 default: .zero
145 }
146 }
147
148 var body: some View {
149 let moss = Color(light: "42624D", dark: "A8C5A8")
150 let isGuru = rank == .guru
151 let field = isGuru ? Color(light: "304B40", dark: "C0CFA9") : moss
152 let fineLine = contrast == .increased ? 1.5 : 1.0
153 ZStack {
154 Circle().fill(unlocked ? field : .clear)
155 if !unlocked {
156 Circle().strokeBorder(moss.opacity(contrast == .increased ? 0.8 : 0.46), lineWidth: fineLine)
157 }
158 if isGuru && size > 24 {
159 Circle().inset(by: size * 0.065)
160 .strokeBorder(unlocked ? theme.surface.opacity(0.7) : moss.opacity(0.55),
161 lineWidth: max(fineLine, size * 0.021))
162 }
163 Image(isGuru && (size <= 24 || !unlocked) ? "RankGuruCompact" : "Rank" + rank.rawValue)
164 .resizable().scaledToFit()
165 .foregroundStyle(unlocked ? theme.surface : moss.opacity(contrast == .increased ? 0.85 : 0.65))
166 .padding(size * (isGuru ? (size <= 24 ? 0.165 : 0.21) : 0.19))
167 .offset(opticalOffset)
168 }
169 .frame(width: size, height: size).accessibilityHidden(true)
170 }
171}
172
173struct ReaderRankBar: View {
174 let progress: ReaderProgress
175 let coins: Int
176 var body: some View {
177 NavigationLink {
178 ReaderRanksView(progress: progress, coins: coins)
179 } label: {
180 HStack(spacing: 12) {
181 ReaderBird(rank: progress.rank, size: 40)
182 VStack(alignment: .leading, spacing: 2) {
183 Text(progress.rank.rawValue).font(.headline)
184 Text(progress.summary).font(.caption).foregroundStyle(.secondary)
185 }
186 Spacer()
187 CoinCount(count: coins)
188 Text("coins").font(.callout)
189 Image(systemName: "chevron.right").font(.caption).foregroundStyle(.secondary)
190 }
191 .padding(.vertical, 4).contentShape(.rect)
192 }
193 .buttonStyle(.plain)
194 .accessibilityElement(children: .ignore)
195 .accessibilityLabel("Reader ranks. \(progress.rank.rawValue), \(progress.summary), \(coins) coins")
196 .accessibilityIdentifier("readerRanks")
197 }
198}
199
200struct ReaderRanksView: View {
201 let progress: ReaderProgress
202 let coins: Int
203 var body: some View {
204 Page(title: "Your reader ranks", subtitle: "Your practice adds up. Days away simply pause your progress.", width: 600) {
205 Panel {
206 HStack(spacing: 16) {
207 ReaderBird(rank: progress.rank, size: 68)
208 VStack(alignment: .leading, spacing: 4) {
209 Text(progress.rank.rawValue).font(GlimmerFont.reading(.title, weight: .medium))
210 Text(progress.summary).font(.callout)
211 }
212 }
213 if let next = progress.next {
214 Text(progress.remaining(until: next))
215 .foregroundStyle(.secondary)
216 } else {
217 Text("You’ve earned all five birds.").foregroundStyle(.secondary)
218 }
219 }
220 Text("Each rank brings deeper practices and challenges into what Glimmer suggests. Everything stays open to explore anytime, and low-energy days bring gentler ideas. Completing a lesson or task, checking in, logging a moment, or trying a habit, challenge, cleaning list or experiment counts. Each calendar day counts once.")
221 .font(.callout).foregroundStyle(.secondary)
222 ForEach(ReaderRank.allCases) { rank in
223 let earned = progress.hasEarned(rank)
224 HStack(spacing: 14) {
225 ReaderBird(rank: rank, unlocked: earned)
226 VStack(alignment: .leading, spacing: 3) {
227 Text(rank.rawValue).font(.headline)
228 Text("\(rank.bird) · \(rank.requirement)").font(.callout).foregroundStyle(.secondary)
229 Text(rank.opens).font(.callout).fixedSize(horizontal: false, vertical: true)
230 }
231 Spacer()
232 Image(systemName: earned ? "checkmark.circle.fill" : "lock")
233 .foregroundStyle(earned ? GlimmerColor.sage : .secondary).accessibilityLabel(earned ? "Unlocked" : "Locked")
234 }.accessibilityElement(children: .combine)
235 if rank != .guru { Divider() }
236 }
237 }
238 }
239}