Glimmer / app · glimmertoday.ai / native / Glimmer / Onboarding.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | struct OnboardingView: View { |
| 4 | @Environment(JournalStore.self) private var store |
| 5 | let catalog: Catalog |
| 6 | var onFinishReplay: (() -> Void)? |
| 7 | @State private var replayIndex = 0 |
| 8 | @State private var replayAnswers: [String: [String]] = [:] |
| 9 | @State private var name = "" |
| 10 | @State private var forward = true |
| 11 | private var replaying: Bool { onFinishReplay != nil } |
| 12 | private var index: Int { |
| 13 | min(max(0, replaying ? replayIndex : store.journal.onboardingIndex), catalog.onboarding.count - 1) |
| 14 | } |
| 15 | private var answers: [String: [String]] { replaying ? replayAnswers : store.journal.answers } |
| 16 | private var step: OnboardingStep { catalog.onboarding[index] } |
| 17 | private var isLast: Bool { index == catalog.onboarding.count - 1 } |
| 18 | private var choices: [String] { answers[step.key ?? "", default: []] } |
| 19 | |
| 20 | var body: some View { |
| 21 | VStack(spacing: 0) { |
| 22 | header |
| 23 | Page(title: heading, kicker: step.kicker ?? "Welcome to Glimmer", subtitle: step.sub ?? hint, width: 640) { |
| 24 | content |
| 25 | .modifier(ReadingPageTurn(forward: forward)) |
| 26 | } |
| 27 | .id(index) |
| 28 | } |
| 29 | .actionBar(width: 640) { |
| 30 | if let onFinishReplay { |
| 31 | SecondaryAction(title: "Skip onboarding", systemImage: "xmark", fill: false, action: onFinishReplay) |
| 32 | .accessibilityIdentifier("onboardingSkip") |
| 33 | } |
| 34 | if index > 0 { |
| 35 | SecondaryAction(title: "Back", systemImage: "chevron.left", fill: false) { |
| 36 | forward = false |
| 37 | go(to: max(0, index - 1)) |
| 38 | } |
| 39 | .accessibilityIdentifier("onboardingBack") |
| 40 | .keyboardShortcut(.leftArrow, modifiers: .command) |
| 41 | } |
| 42 | PrimaryAction(title: isLast ? (replaying ? "Done" : "Start my plan") : "Continue", systemImage: isLast ? "sparkles" : nil) { |
| 43 | if isLast { |
| 44 | if let onFinishReplay { onFinishReplay() } |
| 45 | else { store.start(name: name) } |
| 46 | } else { |
| 47 | forward = true |
| 48 | go(to: index + 1) |
| 49 | } |
| 50 | } |
| 51 | .disabled(step.key != nil && choices.isEmpty) |
| 52 | .accessibilityIdentifier("onboardingContinue") |
| 53 | .keyboardShortcut(.defaultAction) |
| 54 | } |
| 55 | .background { PageBackdrop() } |
| 56 | .onAppear { if !replaying { name = store.journal.name } } |
| 57 | } |
| 58 | |
| 59 | private func go(to value: Int) { |
| 60 | if replaying { replayIndex = value } |
| 61 | else { store.change { $0.onboardingIndex = value } } |
| 62 | } |
| 63 | |
| 64 | private func setAnswer(_ choice: [String], for key: String) { |
| 65 | if replaying { replayAnswers[key] = choice } |
| 66 | else { store.change { $0.answers[key] = choice } } |
| 67 | } |
| 68 | |
| 69 | private var header: some View { |
| 70 | HStack(spacing: 14) { |
| 71 | StepDots(current: index, total: catalog.onboarding.count) |
| 72 | Text("\(index + 1) of \(catalog.onboarding.count)").font(.caption.monospacedDigit().weight(.semibold)) |
| 73 | .foregroundStyle(.secondary).accessibilityHidden(true) |
| 74 | } |
| 75 | .frame(maxWidth: 640, minHeight: 44) |
| 76 | .padding(.horizontal, 20).padding(.top, 10) |
| 77 | .frame(maxWidth: .infinity) |
| 78 | } |
| 79 | |
| 80 | private var hint: String? { |
| 81 | guard step.kind == "q" else { return nil } |
| 82 | if step.mode == "single" { return "Choose one." } |
| 83 | return step.max.map { "Choose up to \($0)." } ?? "Choose all that apply." |
| 84 | } |
| 85 | |
| 86 | private var heading: String { |
| 87 | if let title = step.title { return title } |
| 88 | return switch step.kind { |
| 89 | case "welcome": "A quiet place to get unstuck" |
| 90 | case "note": "Notice the aftertaste" |
| 91 | case "exp": "Pick one small thing to try" |
| 92 | case "profile": "What you want to work on" |
| 93 | case "projection": "What to try on a hard day" |
| 94 | default: "Your plan is ready" |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private var columns: [GridItem] { [GridItem(.adaptive(minimum: 270), spacing: 10)] } |
| 99 | |
| 100 | @ViewBuilder private var content: some View { |
| 101 | switch step.kind { |
| 102 | case "welcome": |
| 103 | ZStack(alignment: .bottomLeading) { |
| 104 | Artwork(slot: 0, height: 148) |
| 105 | GlimmerLogo(size: 48) |
| 106 | .shadow(color: .black.opacity(0.18), radius: 6, y: 2) |
| 107 | .padding(12) |
| 108 | .readingArrival(delay: 0.18) |
| 109 | } |
| 110 | .readingArrival() |
| 111 | Panel(padding: 22) { |
| 112 | HStack(alignment: .top, spacing: 16) { |
| 113 | VStack(alignment: .leading, spacing: 10) { |
| 114 | Text("Glimmer is a gentle spiritual life coach for minds too busy thinking. Take a quiet moment to consider what matters in your work, your rest, and the way you treat people.") |
| 115 | .font(GlimmerFont.display(.title3)) |
| 116 | Text("Start by recalling one moment you liked or that drained you, and jot it down if you want. Your journal is just a quiet place to keep what stands out, and experiments are small things you can try.") |
| 117 | .foregroundStyle(.secondary) |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | Panel(title: "What should I call you?") { |
| 122 | TextField("First name (optional)", text: $name) |
| 123 | .glimmerField() |
| 124 | .onChange(of: name) { _, value in |
| 125 | if !replaying { store.change { $0.name = value } } |
| 126 | } |
| 127 | Label("Your journal is saved on this device. No Glimmer account required.", systemImage: "lock") |
| 128 | .font(.callout).foregroundStyle(.secondary) |
| 129 | } |
| 130 | if store.canUseCloud && !replaying { CloudSyncPanel() } |
| 131 | case "q": |
| 132 | if let key = step.key { |
| 133 | LazyVGrid(columns: columns, spacing: 10) { |
| 134 | ForEach(step.options ?? []) { option in |
| 135 | OnboardingChoice(label: option.label, selected: choices.contains(option.v), multiple: step.mode == "multi") { |
| 136 | setAnswer(step.toggling(option.v, in: answers[key, default: []]), for: key) |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | .padding(.vertical, 4) |
| 141 | } |
| 142 | case "note": |
| 143 | Panel(padding: 22) { |
| 144 | HStack(alignment: .top, spacing: 16) { |
| 145 | Image(systemName: "dial.medium").font(.system(size: 34, weight: .semibold)) |
| 146 | .foregroundStyle(GlimmerColor.accent).frame(width: 56) |
| 147 | .accessibilityHidden(true) |
| 148 | VStack(alignment: .leading, spacing: 10) { |
| 149 | Text("Aftertaste is how you feel after the thing you just did.") |
| 150 | .font(GlimmerFont.display(.title3, weight: .bold)) |
| 151 | Text("It can be different from how much you wanted to do it. A quick log helps you spot the difference.") |
| 152 | .foregroundStyle(.secondary) |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | case "exp": |
| 157 | Text("Built from what you just told me. Small, reversible, your call.").foregroundStyle(.secondary) |
| 158 | VStack(spacing: 10) { |
| 159 | ForEach(catalog.startingSuggestions(answers)) { suggestion in |
| 160 | SelectRow(label: suggestion.title, |
| 161 | selected: answers["first_experiment"]?.first == suggestion.id) { |
| 162 | setAnswer([suggestion.id], for: "first_experiment") |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | .glassGroup(spacing: 10) |
| 167 | case "profile": |
| 168 | Panel { |
| 169 | ForEach(["primary_goal", "distraction", "aftertaste", "wants", "coach_style"], id: \.self) { key in |
| 170 | if let question = catalog.onboarding.first(where: { $0.key == key }) { |
| 171 | VStack(alignment: .leading, spacing: 4) { |
| 172 | Eyebrow(question.kicker ?? "") |
| 173 | Text((question.options ?? []).filter { answers[key, default: []].contains($0.v) } |
| 174 | .map(\.label).joined(separator: " · ")) |
| 175 | .font(.body.weight(.medium)) |
| 176 | } |
| 177 | if key != "coach_style" { Divider() } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | case "projection": |
| 182 | VStack(spacing: 12) { |
| 183 | tip("arrow.down.right.circle", "Make the first step smaller", |
| 184 | "If a task feels too big, start by opening the document or putting one cup in the sink.") |
| 185 | tip("timer", "Make time visible", "Try a short timer before an activity that tends to run long.") |
| 186 | tip("dial.medium", "Check in afterward", |
| 187 | "Use the aftertaste dial to notice how you feel. Over time, your logs can show which changes are worth keeping.") |
| 188 | } |
| 189 | default: |
| 190 | Panel(padding: 22) { |
| 191 | HStack(alignment: .top, spacing: 16) { |
| 192 | VStack(alignment: .leading, spacing: 8) { |
| 193 | Eyebrow("Your first experiment", color: GlimmerColor.ember) |
| 194 | if let first = answers["first_experiment"]?.first { |
| 195 | Text(first).font(GlimmerFont.display(.title2, weight: .bold)) |
| 196 | } |
| 197 | Text("Start with one small log. The coach learns from what you choose to notice.") |
| 198 | .foregroundStyle(.secondary) |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | private func tip(_ icon: String, _ title: String, _ text: String) -> some View { |
| 206 | Panel { |
| 207 | HStack(alignment: .top, spacing: 14) { |
| 208 | Image(systemName: icon).font(.title2).foregroundStyle(GlimmerColor.accent).frame(width: 34) |
| 209 | .accessibilityHidden(true) |
| 210 | VStack(alignment: .leading, spacing: 4) { |
| 211 | Text(title).font(.headline) |
| 212 | Text(text).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true) |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | private struct OnboardingChoice: View { |
| 220 | let label: String |
| 221 | let selected: Bool |
| 222 | let multiple: Bool |
| 223 | let action: () -> Void |
| 224 | @Environment(\.readingTheme) private var theme |
| 225 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 226 | |
| 227 | var body: some View { |
| 228 | let outline = RoundedRectangle(cornerRadius: 14, style: .continuous) |
| 229 | Button(action: action) { |
| 230 | HStack(spacing: 14) { |
| 231 | Text(label) |
| 232 | .font(.body.weight(.medium)) |
| 233 | .multilineTextAlignment(.leading) |
| 234 | .fixedSize(horizontal: false, vertical: true) |
| 235 | .frame(maxWidth: .infinity, alignment: .leading) |
| 236 | let mark = RoundedRectangle(cornerRadius: multiple ? 6 : 12, style: .continuous) |
| 237 | ZStack { |
| 238 | mark.fill(selected ? GlimmerColor.accent : .clear) |
| 239 | mark.strokeBorder(selected ? .clear : Color.secondary.opacity(0.55), lineWidth: 1.5) |
| 240 | Image(systemName: "checkmark") |
| 241 | .font(.system(size: 12, weight: .bold)).foregroundStyle(theme.surface) |
| 242 | .opacity(selected ? 1 : 0) |
| 243 | .animation(reduceMotion || selected ? nil : .easeOut(duration: 0.14), value: selected) |
| 244 | } |
| 245 | .frame(width: 24, height: 24) |
| 246 | .accessibilityHidden(true) |
| 247 | } |
| 248 | .padding(.horizontal, 16).padding(.vertical, 12) |
| 249 | .frame(maxWidth: .infinity, minHeight: 56) |
| 250 | .background(theme.surface, in: outline) |
| 251 | .overlay { |
| 252 | outline.fill(GlimmerColor.accent.opacity(selected ? 0.08 : 0)) |
| 253 | .allowsHitTesting(false) |
| 254 | } |
| 255 | .overlay { |
| 256 | outline.strokeBorder(selected ? GlimmerColor.accent.opacity(0.40) : Color.primary.opacity(0.13)) |
| 257 | } |
| 258 | .completionLight(selected, outline: outline) |
| 259 | .contentShape(outline) |
| 260 | } |
| 261 | .buttonStyle(.plain) |
| 262 | .accessibilityAddTraits(selected ? .isSelected : []) |
| 263 | } |
| 264 | } |