Bucksley / app · bucksley.ai / Apps / Mac / SmartBudgetWalkthrough.swift
| 1 | import SwiftUI |
| 2 | import BucksleyKit |
| 3 | import BucksleyUI |
| 4 | |
| 5 | /// Whether the smart budget walkthrough is showing, so Ask Bucksley can open it on the Budgets screen. |
| 6 | @MainActor @Observable |
| 7 | final class SmartBudgetFlow { |
| 8 | static let shared = SmartBudgetFlow() |
| 9 | var presented = false |
| 10 | var step = 0 |
| 11 | func open() { step = 0; presented = true } |
| 12 | } |
| 13 | |
| 14 | /// Four steps: your money at a glance and a savings goal, fixed bills, flexible spending, then review |
| 15 | /// and create. Amounts start at Bucksley's recommendation; every one can be changed or left out. |
| 16 | struct SmartBudgetWalkthrough: View { |
| 17 | @Environment(AppModel.self) private var model |
| 18 | @Environment(\.palette) private var palette |
| 19 | @State private var flow = SmartBudgetFlow.shared |
| 20 | @State private var savingsRate = 20 |
| 21 | @State private var amounts: [String: Decimal] = [:] |
| 22 | @State private var skipped: Set<String> = [] |
| 23 | |
| 24 | static let steps = ["Your money", "Fixed bills", "Flexible spending", "Review"] |
| 25 | |
| 26 | var plan: SmartBudgetPlan { model.ledger.smartBudgetPlan(savingsRate: savingsRate) } |
| 27 | |
| 28 | var body: some View { |
| 29 | let plan = plan |
| 30 | VStack(alignment: .leading, spacing: 18) { |
| 31 | stepper |
| 32 | Group { |
| 33 | switch flow.step { |
| 34 | case 0: overview(plan) |
| 35 | case 1: items(plan.fixedItems, title: "Fixed bills", note: "Spending you can't easily change, like rent, insurance and utilities. Each is budgeted at what you've spent lately and never cut. Wrong category here? Move it to flexible.") |
| 36 | case 2: items(plan.flexible, title: "Flexible spending", note: "Set a little under what you've spent lately, so the budget nudges you down. Anything trimmed further for your savings goal says so. Can't change one? Mark it fixed.") |
| 37 | default: review(plan) |
| 38 | } |
| 39 | } |
| 40 | .frame(maxWidth: 760, alignment: .leading) |
| 41 | navigation(plan) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // MARK: Steps |
| 46 | |
| 47 | var stepper: some View { |
| 48 | HStack(spacing: 8) { |
| 49 | ForEach(Array(Self.steps.enumerated()), id: \.offset) { index, name in |
| 50 | Button { flow.step = index } label: { |
| 51 | HStack(spacing: 6) { |
| 52 | Text("\(index + 1)").font(.system(size: 11, weight: .bold)).frame(width: 20, height: 20) |
| 53 | .foregroundStyle(index <= flow.step ? palette.accentText : palette.muted) |
| 54 | .background(index <= flow.step ? palette.accent : palette.card, in: Circle()) |
| 55 | Text(name).font(.system(size: 12, weight: index == flow.step ? .semibold : .regular)) |
| 56 | .foregroundStyle(index == flow.step ? palette.text : palette.muted) |
| 57 | } |
| 58 | } |
| 59 | .buttonStyle(.plain) |
| 60 | if index < Self.steps.count - 1 { Rectangle().fill(palette.line).frame(width: 24, height: 1) } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func overview(_ plan: SmartBudgetPlan) -> some View { |
| 66 | VStack(alignment: .leading, spacing: 16) { |
| 67 | HStack(spacing: 14) { |
| 68 | Panel { stat("Recent income", plan.income, note: "a month") } |
| 69 | Panel { stat("Recent spending", plan.typicalSpending, note: "a month, \(plan.items.count) categories") } |
| 70 | Panel { stat("Savings goal", plan.savingsGoal, note: "\(plan.savingsRate)% of income") } |
| 71 | } |
| 72 | VStack(alignment: .leading, spacing: 8) { |
| 73 | Text("How much of your income do you want to keep free each month?").font(.system(size: 13, weight: .semibold)) |
| 74 | PillPicker([(10, "10%"), (15, "15%"), (20, "20%"), (25, "25%"), (30, "30%")], selection: $savingsRate) |
| 75 | .fixedSize() |
| 76 | .onChange(of: savingsRate) { amounts = [:] } |
| 77 | } |
| 78 | coach(plan.summary) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func items(_ items: [SmartBudgetPlan.Item], title: String, note: String) -> some View { |
| 83 | VStack(alignment: .leading, spacing: 12) { |
| 84 | Text(note).font(.system(size: 13)).foregroundStyle(palette.muted).fixedSize(horizontal: false, vertical: true) |
| 85 | if items.isEmpty { Text("Nothing here yet. Bucksley budgets categories you spend $10 or more in during a typical month.").font(.system(size: 13)).foregroundStyle(palette.muted) } |
| 86 | ForEach(items) { item in row(item) } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func row(_ item: SmartBudgetPlan.Item) -> some View { |
| 91 | let included = !skipped.contains(item.categoryID) |
| 92 | let category = model.workspace.category(item.categoryID) |
| 93 | return HStack(alignment: .center, spacing: 14) { |
| 94 | Button { |
| 95 | if included { skipped.insert(item.categoryID) } else { skipped.remove(item.categoryID) } |
| 96 | } label: { |
| 97 | Image(systemName: included ? "checkmark.circle.fill" : "circle").font(.system(size: 18)) |
| 98 | .foregroundStyle(included ? palette.accent : palette.muted) |
| 99 | } |
| 100 | .buttonStyle(.plain).accessibilityLabel(included ? "Leave out \(item.name)" : "Include \(item.name)") |
| 101 | VStack(alignment: .leading, spacing: 4) { |
| 102 | Text("\(category?.icon ?? "") \(item.name)").font(.system(size: 14, weight: .semibold)) |
| 103 | Text(item.reason).font(.system(size: 12)).foregroundStyle(palette.muted).fixedSize(horizontal: false, vertical: true) |
| 104 | HStack(spacing: 10) { |
| 105 | Button(item.fixed ? "Move to flexible" : "Mark as fixed") { setFixed(item, !item.fixed) } |
| 106 | .buttonStyle(.plain).font(.system(size: 11, weight: .semibold)).foregroundStyle(palette.accent) |
| 107 | if let current = item.current { Text("Your budget now: \(Money.format(current, cents: false))").font(.system(size: 11)).foregroundStyle(palette.muted) } |
| 108 | } |
| 109 | } |
| 110 | Spacer(minLength: 12) |
| 111 | HStack(spacing: 10) { |
| 112 | Button { nudge(item, by: -1) } label: { Image(systemName: "minus").frame(width: 22, height: 22) } |
| 113 | .buttonStyle(.plain).background(palette.canvas, in: Circle()).accessibilityLabel("Less") |
| 114 | TextField("", value: amountBinding(item), format: .currency(code: "USD").precision(.fractionLength(0))) |
| 115 | .textFieldStyle(.plain).font(palette.figure(15, weight: .semibold)).multilineTextAlignment(.center).frame(width: 84) |
| 116 | Button { nudge(item, by: 1) } label: { Image(systemName: "plus").frame(width: 22, height: 22) } |
| 117 | .buttonStyle(.plain).background(palette.canvas, in: Circle()).accessibilityLabel("More") |
| 118 | } |
| 119 | .opacity(included ? 1 : 0.4).disabled(!included) |
| 120 | } |
| 121 | .padding(14) |
| 122 | .background(palette.card, in: RoundedRectangle(cornerRadius: palette.cardRadius)) |
| 123 | } |
| 124 | |
| 125 | func review(_ plan: SmartBudgetPlan) -> some View { |
| 126 | let chosen = plan.items.filter { !skipped.contains($0.categoryID) } |
| 127 | let total = chosen.reduce(Decimal(0)) { $0 + amount($1) } |
| 128 | let left = plan.income - total |
| 129 | return VStack(alignment: .leading, spacing: 16) { |
| 130 | HStack(spacing: 14) { |
| 131 | Panel { stat("Budgeted", total, note: "\(chosen.count) categories") } |
| 132 | if plan.income > 0 { |
| 133 | Panel { stat("Left each month", left, note: left >= plan.savingsGoal ? "meets your \(plan.savingsRate)% goal" : "\(Money.format(plan.savingsGoal - left, cents: false)) short of your goal") } |
| 134 | } |
| 135 | } |
| 136 | VStack(alignment: .leading, spacing: 0) { |
| 137 | ForEach(chosen) { item in |
| 138 | HStack { |
| 139 | Text("\(model.workspace.category(item.categoryID)?.icon ?? "") \(item.name)").font(.system(size: 13)) |
| 140 | Spacer() |
| 141 | MoneyText(amount(item), size: 13, cents: false) |
| 142 | Text("/mo").font(.system(size: 11)).foregroundStyle(palette.muted) |
| 143 | } |
| 144 | .padding(.vertical, 8) |
| 145 | Divider().opacity(0.5) |
| 146 | } |
| 147 | } |
| 148 | .padding(.horizontal, 14).padding(.vertical, 6) |
| 149 | .background(palette.card, in: RoundedRectangle(cornerRadius: palette.cardRadius)) |
| 150 | Text("Budgets you already have in other categories stay as they are. You can change any of these later on the Budgets screen.") |
| 151 | .font(.system(size: 12)).foregroundStyle(palette.muted) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | func navigation(_ plan: SmartBudgetPlan) -> some View { |
| 156 | let chosen = plan.items.filter { !skipped.contains($0.categoryID) } |
| 157 | return HStack(spacing: 10) { |
| 158 | Button("Cancel") { flow.presented = false }.buttonStyle(SecondaryButtonStyle()) |
| 159 | Spacer() |
| 160 | if flow.step > 0 { Button("Back") { flow.step -= 1 }.buttonStyle(SecondaryButtonStyle()) } |
| 161 | if flow.step == 0 && !plan.items.isEmpty { |
| 162 | Button("Use Bucksley's plan") { create(plan.items, plan) }.buttonStyle(SecondaryButtonStyle()) |
| 163 | .help("Create every recommended budget now") |
| 164 | } |
| 165 | if flow.step < Self.steps.count - 1 { |
| 166 | Button("Next") { flow.step += 1 }.buttonStyle(PrimaryButtonStyle()).disabled(plan.items.isEmpty) |
| 167 | } else { |
| 168 | Button("Create \(chosen.count) budget\(chosen.count == 1 ? "" : "s")") { create(chosen, plan) } |
| 169 | .buttonStyle(PrimaryButtonStyle()).disabled(chosen.isEmpty) |
| 170 | } |
| 171 | } |
| 172 | .frame(maxWidth: 760) |
| 173 | } |
| 174 | |
| 175 | // MARK: Helpers |
| 176 | |
| 177 | func coach(_ text: String) -> some View { |
| 178 | HStack(alignment: .top, spacing: 10) { |
| 179 | Image(systemName: "sparkles").foregroundStyle(palette.accent) |
| 180 | Text(text).font(palette.theme == .almanac ? .system(size: 14, design: .serif) : .system(size: 13)).fixedSize(horizontal: false, vertical: true) |
| 181 | } |
| 182 | .padding(14).frame(maxWidth: .infinity, alignment: .leading) |
| 183 | .background(palette.card, in: RoundedRectangle(cornerRadius: palette.cardRadius)) |
| 184 | } |
| 185 | |
| 186 | func stat(_ label: String, _ value: Decimal, note: String) -> some View { |
| 187 | VStack(alignment: .leading, spacing: 4) { |
| 188 | Text(label).font(.system(size: 12, weight: .medium)).foregroundStyle(palette.muted) |
| 189 | MoneyText(value, size: 22, cents: false) |
| 190 | Text(note).font(.system(size: 11)).foregroundStyle(palette.muted) |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /// Remembers the choice on the category, so the next plan and the budget suggestions follow it. |
| 195 | func setFixed(_ item: SmartBudgetPlan.Item, _ fixed: Bool) { |
| 196 | amounts[item.categoryID] = nil |
| 197 | model.updateWorkspace { ws in |
| 198 | if let i = ws.categories.firstIndex(where: { $0.id == item.categoryID }) { ws.categories[i].fixed = fixed } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | func amount(_ item: SmartBudgetPlan.Item) -> Decimal { amounts[item.categoryID] ?? item.recommended } |
| 203 | |
| 204 | func amountBinding(_ item: SmartBudgetPlan.Item) -> Binding<Decimal> { |
| 205 | Binding(get: { amount(item) }, set: { amounts[item.categoryID] = max($0, 0) }) |
| 206 | } |
| 207 | |
| 208 | /// Steps of $5 under $50, $10 under $200, $25 under $1,000, then $50. |
| 209 | func nudge(_ item: SmartBudgetPlan.Item, by direction: Int) { |
| 210 | let value = amount(item).double |
| 211 | let step: Double = value < 50 ? 5 : value < 200 ? 10 : value < 1000 ? 25 : 50 |
| 212 | amounts[item.categoryID] = Decimal(max(step, value + Double(direction) * step)) |
| 213 | } |
| 214 | |
| 215 | func create(_ items: [SmartBudgetPlan.Item], _ plan: SmartBudgetPlan) { |
| 216 | let chosen = items |
| 217 | let values = Dictionary(uniqueKeysWithValues: chosen.map { ($0.categoryID, amount($0)) }) |
| 218 | model.updateWorkspace { SmartBudgetPlan.apply(chosen, amounts: values, to: &$0) } |
| 219 | SupportPrompt.recordCoreAction() |
| 220 | flow.presented = false |
| 221 | } |
| 222 | } |