Glimmer / app · glimmertoday.ai / native / Glimmer / ToolkitView.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | /// The Toolkit: small helpers for starting, sizing, focusing, saying and deciding. |
| 4 | struct ToolkitView: View { |
| 5 | @Environment(JournalStore.self) private var store |
| 6 | @AppStorage(StepSize.preference) private var stepSize = StepSize.balanced.rawValue |
| 7 | |
| 8 | var body: some View { |
| 9 | Page(title: "Toolkit", subtitle: "Small helpers for when your mind is busy. Use one, then get back to your day.", width: 720) { |
| 10 | section("Get started") { |
| 11 | LinkRow(title: "Unload", detail: "Empty your head. Glimmer sorts it into to-dos", |
| 12 | icon: "tray.and.arrow.down.fill", tint: GlimmerColor.sage) { UnloadView().hidesTabBar() } |
| 13 | .accessibilityIdentifier("toolkit.unload") |
| 14 | LinkRow(title: "Break it down", detail: "Turn one heavy to-do into steps you could start now", |
| 15 | icon: "list.bullet.indent", tint: GlimmerColor.sage) { BreakItDownPicker().hidesTabBar() } |
| 16 | .accessibilityIdentifier("toolkit.breakItDown") |
| 17 | } |
| 18 | section("Get it done") { |
| 19 | LinkRow(title: "Time check", detail: "A kind guess at how long things take today", |
| 20 | icon: "clock.fill", tint: GlimmerColor.accent) { TimeCheckView().hidesTabBar() } |
| 21 | .accessibilityIdentifier("toolkit.timeCheck") |
| 22 | LinkRow(title: "One thing", detail: "See only the next step, with a gentle timer", |
| 23 | icon: "scope", tint: GlimmerColor.accent) { OneThingView().hidesTabBar() } |
| 24 | .accessibilityIdentifier("toolkit.oneThing") |
| 25 | } |
| 26 | section("Say it") { |
| 27 | LinkRow(title: "Read the room", detail: "How a message might come across, before you reply", |
| 28 | icon: "eye.fill", tint: GlimmerColor.dusk) { ReadTheRoomView().hidesTabBar() } |
| 29 | .accessibilityIdentifier("toolkit.readTheRoom") |
| 30 | LinkRow(title: "Reword", detail: "The same message, kinder, clearer or shorter", |
| 31 | icon: "wand.and.sparkles", tint: GlimmerColor.dusk) { RewordView().hidesTabBar() } |
| 32 | .accessibilityIdentifier("toolkit.reword") |
| 33 | } |
| 34 | section("Decide") { |
| 35 | LinkRow(title: "Weigh it", detail: "Pros, cons and a small experiment to find out", |
| 36 | icon: "scalemass.fill", tint: GlimmerColor.honey) { WeighItView().hidesTabBar() } |
| 37 | .accessibilityIdentifier("toolkit.weighIt") |
| 38 | } |
| 39 | Panel(title: "Step size") { |
| 40 | StepSizeControl(value: $stepSize) |
| 41 | Text("Sets how small Break it down and Unload make things, and how much Reword changes.") |
| 42 | .font(.footnote).foregroundStyle(.secondary) |
| 43 | .fixedSize(horizontal: false, vertical: true) |
| 44 | } |
| 45 | Label(ToolkitAI.routeSummary, systemImage: ToolkitAI.hasDeviceModel || ToolkitAI.agentChoice != nil ? "lock" : "book.closed") |
| 46 | .font(.footnote).foregroundStyle(.secondary) |
| 47 | .fixedSize(horizontal: false, vertical: true) |
| 48 | .padding(.top, 4) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | private func section<Content: View>(_ title: String, @ViewBuilder _ content: () -> Content) -> some View { |
| 53 | Panel(title: title, plain: true) { |
| 54 | VStack(spacing: 8) { content() }.glassGroup(spacing: 8) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // MARK: - Shared controls |
| 60 | |
| 61 | /// Five stepping stones, large to small. Picking one sets how fine-grained the Toolkit gets. |
| 62 | struct StepSizeControl: View { |
| 63 | @Binding var value: Int |
| 64 | @Environment(\.readingTheme) private var theme |
| 65 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 66 | |
| 67 | private var size: StepSize { StepSize(rawValue: value) ?? .balanced } |
| 68 | |
| 69 | private var chosenTitle: some View { |
| 70 | Text(size.title).font(.callout.weight(.semibold)).foregroundStyle(.primary) |
| 71 | .contentTransition(.opacity) |
| 72 | } |
| 73 | |
| 74 | var body: some View { |
| 75 | VStack(alignment: .leading, spacing: 8) { |
| 76 | HStack(alignment: .center, spacing: 0) { |
| 77 | ForEach(StepSize.allCases) { option in |
| 78 | let diameter = CGFloat(30 - (option.rawValue - 1) * 5) |
| 79 | let chosen = option == size |
| 80 | Button { |
| 81 | withAnimation(reduceMotion ? nil : .spring(duration: 0.3, bounce: 0.2)) { value = option.rawValue } |
| 82 | } label: { |
| 83 | Circle() |
| 84 | .fill(option.rawValue <= size.rawValue ? theme.selectionInk.opacity(chosen ? 1 : 0.35) |
| 85 | : Color.primary.opacity(0.10)) |
| 86 | .overlay { Circle().strokeBorder(Color.primary.opacity(chosen ? 0 : 0.12), lineWidth: 1) } |
| 87 | .frame(width: diameter, height: diameter) |
| 88 | .scaleEffect(chosen && !reduceMotion ? 1.08 : 1) |
| 89 | .frame(maxWidth: .infinity, minHeight: 44) |
| 90 | .contentShape(.rect) |
| 91 | } |
| 92 | .buttonStyle(.plain) |
| 93 | .accessibilityHidden(true) |
| 94 | } |
| 95 | } |
| 96 | ViewThatFits(in: .horizontal) { |
| 97 | HStack { |
| 98 | Text("Big strides") |
| 99 | Spacer() |
| 100 | chosenTitle |
| 101 | Spacer() |
| 102 | Text("Tiny steps") |
| 103 | } |
| 104 | .lineLimit(1) |
| 105 | // Large text: the chosen size on its own line, the scale's ends below it. |
| 106 | VStack(alignment: .leading, spacing: 2) { |
| 107 | chosenTitle |
| 108 | Text("Big strides to tiny steps") |
| 109 | } |
| 110 | } |
| 111 | .font(.caption).foregroundStyle(.secondary) |
| 112 | .accessibilityHidden(true) |
| 113 | } |
| 114 | .frame(maxWidth: 440, alignment: .leading) |
| 115 | .sensoryFeedback(.selection, trigger: value) |
| 116 | .accessibilityElement() |
| 117 | .accessibilityLabel("Step size") |
| 118 | .accessibilityValue(size.title) |
| 119 | .accessibilityAdjustableAction { direction in |
| 120 | switch direction { |
| 121 | case .increment: value = min(5, value + 1) |
| 122 | case .decrement: value = max(1, value - 1) |
| 123 | @unknown default: break |
| 124 | } |
| 125 | } |
| 126 | .accessibilityIdentifier("toolkit.stepSize") |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /// Says where a Toolkit result came from. |
| 131 | struct ToolkitSourceLabel: View { |
| 132 | let source: ToolkitSource |
| 133 | var note: String? = nil |
| 134 | |
| 135 | var body: some View { |
| 136 | VStack(alignment: .leading, spacing: 2) { |
| 137 | Label(source.label, systemImage: source.symbol) |
| 138 | if let note { Text(note) } |
| 139 | } |
| 140 | .font(.footnote).foregroundStyle(.secondary) |
| 141 | .accessibilityElement(children: .combine) |
| 142 | .accessibilityIdentifier("toolkit.source") |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /// A calm placeholder while a suggestion is being made. |
| 147 | struct ToolkitThinking: View { |
| 148 | let text: String |
| 149 | var body: some View { |
| 150 | HStack(spacing: 10) { |
| 151 | ProgressView().controlSize(.small) |
| 152 | Text(text).font(.callout).foregroundStyle(.secondary) |
| 153 | } |
| 154 | .frame(maxWidth: .infinity, minHeight: 44, alignment: .leading) |
| 155 | .accessibilityElement(children: .combine) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /// Today's focus from the energy check-in, if there is one. |
| 160 | extension JournalStore { |
| 161 | var todaysFocus: FocusLevel? { journal.energy[todayKey].map { FocusLevel(energy: $0.value) } } |
| 162 | var openPersonalTodos: [PersonalTodo] { PersonalTodo.ordered((journal.personalTodos ?? []).filter { !$0.completed }) } |
| 163 | } |
| 164 | |
| 165 | // MARK: - Choosing a to-do |
| 166 | |
| 167 | /// Lets a tool start from an open to-do, or from a new one typed here. |
| 168 | struct TodoChooser: View { |
| 169 | @Environment(JournalStore.self) private var store |
| 170 | let prompt: String |
| 171 | let detail: (PersonalTodo) -> String? |
| 172 | let choose: (PersonalTodo) -> Void |
| 173 | @State private var newTitle = "" |
| 174 | |
| 175 | var body: some View { |
| 176 | Panel(title: "New to-do") { |
| 177 | HStack(spacing: 8) { |
| 178 | TextField(prompt, text: $newTitle).glimmerField().onSubmit(add) |
| 179 | .accessibilityIdentifier("toolkit.newTodo") |
| 180 | Button("Add", systemImage: "plus", action: add) |
| 181 | .glimmerButton(.standard).besideField() |
| 182 | .disabled(clean.isEmpty || clean.count > PersonalTodo.titleLimit) |
| 183 | .accessibilityIdentifier("toolkit.addTodo") |
| 184 | } |
| 185 | } |
| 186 | Panel(title: "Your open to-dos", plain: true) { |
| 187 | if store.openPersonalTodos.isEmpty { |
| 188 | Text("No open to-dos yet. Add one above, or use Unload to empty your head first.") |
| 189 | .font(.callout).foregroundStyle(.secondary) |
| 190 | } |
| 191 | VStack(spacing: 8) { |
| 192 | ForEach(store.openPersonalTodos) { todo in |
| 193 | ActionRow(title: todo.title, detail: detail(todo), icon: Glyph.symbol(name: todo.title, category: "todo"), tint: Glyph.tint(for: "todo")) { choose(todo) } |
| 194 | } |
| 195 | } |
| 196 | .glassGroup(spacing: 8) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | private var clean: String { newTitle.trimmingCharacters(in: .whitespacesAndNewlines) } |
| 201 | |
| 202 | private func add() { |
| 203 | guard !clean.isEmpty, clean.count <= PersonalTodo.titleLimit else { return } |
| 204 | let todo = PersonalTodo(title: clean) |
| 205 | if store.change({ $0.personalTodos = ($0.personalTodos ?? []) + [todo] }) { |
| 206 | newTitle = "" |
| 207 | choose(todo) |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /// A roomy multi-line field for pasting or typing longer text. It sits straight on its panel's paper. |
| 213 | struct ToolkitTextEditor: View { |
| 214 | @Binding var text: String |
| 215 | let prompt: String |
| 216 | var minHeight: CGFloat = 160 |
| 217 | |
| 218 | // TextEditor insets its text a little; line the text up with the panel's headings instead. |
| 219 | #if os(iOS) |
| 220 | private static let inset = EdgeInsets(top: 8, leading: 5, bottom: 8, trailing: 5) |
| 221 | #else |
| 222 | private static let inset = EdgeInsets(top: 0, leading: 5, bottom: 0, trailing: 5) |
| 223 | #endif |
| 224 | |
| 225 | var body: some View { |
| 226 | TextEditor(text: $text) |
| 227 | .font(.body) |
| 228 | .scrollContentBackground(.hidden) |
| 229 | .padding(.horizontal, -Self.inset.leading) |
| 230 | .frame(minHeight: minHeight) |
| 231 | .background(alignment: .topLeading) { |
| 232 | if text.isEmpty { |
| 233 | Text(prompt).foregroundStyle(.tertiary) |
| 234 | .padding(.top, Self.inset.top) |
| 235 | .allowsHitTesting(false) |
| 236 | .accessibilityHidden(true) |
| 237 | } |
| 238 | } |
| 239 | .accessibilityLabel(prompt) |
| 240 | } |
| 241 | } |