Branchwell

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

1import SwiftUI
2
3/// Weigh it: pros, cons and unknowns for a decision, ending in a small 7-day experiment.
4struct WeighItView: View {
5 @Environment(JournalStore.self) private var store
6 @SceneStorage("glimmer.toolkit.weigh") private var decision = ""
7 @State private var weighing: Weighing?
8 @State private var source: (source: ToolkitSource, note: String?)?
9 @State private var working: Task<Void, Never>?
10 @State private var weighed = ""
11
12 private var cleanDecision: String { decision.trimmingCharacters(in: .whitespacesAndNewlines) }
13
14 var body: some View {
15 Page(title: "Weigh it", subtitle: "Lay a decision out where you can see it. Glimmer won’t decide for you.", width: 620) {
16 Panel(title: "What are you deciding?") {
17 ToolkitTextEditor(text: $decision, prompt: "Should I…", minHeight: 76)
18 .accessibilityIdentifier("weigh.decision")
19 }
20 if working != nil { Panel { ToolkitThinking(text: "Weighing it up…") } }
21 if weighing != nil { result }
22 }
23 .actionBar(width: 620) {
24 if weighing == nil {
25 PrimaryAction(title: "Weigh it", systemImage: "scalemass", action: weigh)
26 .disabled(working != nil || cleanDecision.isEmpty)
27 .accessibilityIdentifier("weigh.run")
28 } else {
29 Button("Start over", systemImage: "arrow.uturn.backward") {
30 weighing = nil
31 source = nil
32 }
33 .glimmerButton().controlSize(.large)
34 PrimaryAction(title: "Try it for 7 days", systemImage: "calendar", action: startExperiment)
35 .disabled((weighing?.experiment ?? "").trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
36 .accessibilityIdentifier("weigh.experiment")
37 }
38 }
39 .onChange(of: decision) {
40 if weighing != nil && cleanDecision != weighed { weighing = nil; source = nil }
41 }
42 .onDisappear { working?.cancel() }
43 #if DEBUG
44 .onAppear {
45 guard decision.isEmpty, let sample = ToolkitSample.text else { return }
46 decision = sample
47 ToolkitSample.run { weigh() }
48 }
49 #endif
50 }
51
52 @ViewBuilder private var result: some View {
53 if let binding = Binding($weighing) {
54 WeighList(title: "For", symbol: "plus", items: binding.pros)
55 WeighList(title: "Against", symbol: "minus", items: binding.cons)
56 WeighList(title: "Still unknown", symbol: "questionmark", items: binding.unknowns)
57 Panel(title: "A small experiment") {
58 ToolkitTextEditor(text: binding.experiment, prompt: "Something small you could try for a week", minHeight: 76)
59 .accessibilityIdentifier("weigh.experimentText")
60 Text("Seven days of a small, reversible version often tells you more than thinking it over.")
61 .font(.footnote).foregroundStyle(.secondary)
62 .fixedSize(horizontal: false, vertical: true)
63 if let source { ToolkitSourceLabel(source: source.source, note: source.note) }
64 }
65 }
66 }
67
68 private func weigh() {
69 let question = cleanDecision
70 guard !question.isEmpty, working == nil else { return }
71 weighed = question
72 working = Task {
73 let result = await ToolkitAI.weigh(question)
74 guard !Task.isCancelled else { return }
75 if let result {
76 weighing = result.value.sentenceCased
77 source = (result.source, result.note)
78 } else {
79 weighing = OfflineToolkit.weighing(question)
80 source = (.guide, "Add your own reasons below. Apple Intelligence or a connected writing agent can suggest some.")
81 }
82 working = nil
83 }
84 }
85
86 private func startExperiment() {
87 guard let experiment = weighing?.experiment.trimmingCharacters(in: .whitespacesAndNewlines), !experiment.isEmpty else { return }
88 let title = String(experiment.prefix(140))
89 store.startOrQueue(Suggestion(type: "Decision", title: title, sub: String(cleanDecision.prefix(160))))
90 }
91}
92
93/// An editable list of reasons. People can add their own or remove suggested ones.
94private struct WeighList: View {
95 let title: String
96 let symbol: String
97 @Binding var items: [String]
98 @State private var draft = ""
99 @State private var adding = false
100 @FocusState private var fieldFocused: Bool
101
102 var body: some View {
103 Panel(title: title) {
104 VStack(alignment: .leading, spacing: 8) {
105 ForEach(Array(items.enumerated()), id: \.offset) { index, item in
106 HStack(alignment: .firstTextBaseline, spacing: 10) {
107 Image(systemName: symbol).font(.footnote.weight(.semibold)).foregroundStyle(.tertiary)
108 .frame(width: 14).accessibilityHidden(true)
109 Text(item).frame(maxWidth: .infinity, alignment: .leading)
110 .fixedSize(horizontal: false, vertical: true)
111 Button("Remove", systemImage: "xmark") { items.remove(at: index) }
112 .labelStyle(.iconOnly).buttonStyle(.borderless).foregroundStyle(.secondary)
113 .accessibilityLabel("Remove \(item)")
114 }
115 }
116 }
117 if adding {
118 HStack(spacing: 8) {
119 TextField("Add your own", text: $draft).glimmerField().focused($fieldFocused).onSubmit(add)
120 Button("Add", systemImage: "plus", action: add)
121 .glimmerButton(.standard).besideField()
122 .disabled(draft.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
123 .accessibilityLabel("Add to \(title)")
124 }
125 } else if items.count < 12 {
126 Button {
127 adding = true
128 fieldFocused = true
129 } label: {
130 HStack(spacing: 10) {
131 Image(systemName: "plus").font(.footnote.weight(.semibold)).frame(width: 14)
132 Text(items.isEmpty ? "Add a reason" : "Add your own")
133 Spacer(minLength: 0)
134 }
135 .foregroundStyle(.secondary)
136 .frame(maxWidth: .infinity, minHeight: 36, alignment: .leading).contentShape(.rect)
137 }
138 .buttonStyle(RowPressStyle())
139 .accessibilityLabel("Add your own to \(title)")
140 }
141 }
142 }
143
144 private func add() {
145 let clean = draft.trimmingCharacters(in: .whitespacesAndNewlines)
146 guard !clean.isEmpty, items.count < 12 else { return }
147 items.append(String(clean.prefix(160)))
148 draft = ""
149 adding = false
150 }
151}