Bucksley / app · bucksley.ai / Apps / Mac / CashFlowScreen.swift
| 1 | import SwiftUI |
| 2 | import Charts |
| 3 | import BucksleyKit |
| 4 | import BucksleyUI |
| 5 | |
| 6 | /// Money in against money out, month by month, with last month told as a short story. |
| 7 | struct CashFlowScreen: View { |
| 8 | @Environment(AppModel.self) private var model |
| 9 | @Environment(\.palette) private var palette |
| 10 | @State private var months = 12 |
| 11 | |
| 12 | var body: some View { |
| 13 | let ledger = model.ledger |
| 14 | // Months before the first transaction would draw as a flat line at zero. |
| 15 | let flow = Array(ledger.cashFlow(months: months).drop { $0.income == 0 && $0.spend == 0 }) |
| 16 | let avg = ledger.cashFlowAverages(flow) |
| 17 | ScreenScaffold("Cash Flow", subtitle: "What came in, what went out, and what you kept") { |
| 18 | PillPicker([(6, "6M"), (12, "1Y"), (24, "2Y")], selection: $months) |
| 19 | } content: { |
| 20 | if ledger.entries.isEmpty { |
| 21 | ConnectFirst() |
| 22 | } else { |
| 23 | VStack(alignment: .leading, spacing: 18) { |
| 24 | HStack(spacing: 12) { |
| 25 | stat("Average income", Money.format(avg.income, cents: false), palette.gain) |
| 26 | stat("Average spend", Money.format(avg.spend, cents: false), palette.text) |
| 27 | stat("Average kept", Money.format(avg.net, cents: false), avg.net >= 0 ? palette.gain : palette.loss) |
| 28 | stat("Savings rate", avg.savingsRate.map { "\(Int(($0 * 100).rounded()))%" } ?? "—", palette.text) |
| 29 | } |
| 30 | Panel { |
| 31 | VStack(alignment: .leading, spacing: 10) { |
| 32 | HStack(spacing: 14) { |
| 33 | legend("Income", palette.gain) |
| 34 | legend("Spend", palette.accent) |
| 35 | legend("Kept", palette.text, line: true) |
| 36 | Spacer() |
| 37 | } |
| 38 | CashFlowChart(flow: flow).frame(height: 240) |
| 39 | } |
| 40 | } |
| 41 | if let recap = ledger.recap() { RecapPanel(recap: recap) } |
| 42 | Panel(padding: 0) { |
| 43 | VStack(spacing: 0) { |
| 44 | row(["Month", "Income", "Spend", "Kept", "Saved"], header: true) |
| 45 | ForEach(flow.reversed().filter { $0.income != 0 || $0.spend != 0 }) { m in |
| 46 | Rectangle().fill(palette.line).frame(height: 0.5) |
| 47 | row([MonthKey.title(m.month), Money.format(m.income, cents: false), Money.format(m.spend, cents: false), |
| 48 | Money.format(m.net, cents: false), m.savingsRate.map { "\(Int(($0 * 100).rounded()))%" } ?? "—"], |
| 49 | netColor: m.net >= 0 ? palette.gain : palette.loss) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | Text("Transfers between your accounts and card payments aren't income or spending. Refunds lower the category they came back to.") |
| 54 | .font(.system(size: 11)).foregroundStyle(palette.muted) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func stat(_ label: String, _ value: String, _ color: Color) -> some View { |
| 61 | Panel { |
| 62 | VStack(alignment: .leading, spacing: 4) { |
| 63 | Text(label).font(.system(size: 11, weight: .semibold)).foregroundStyle(palette.muted) |
| 64 | Text(value).font(palette.figure(20)).tracking(palette.figureTracking).foregroundStyle(color) |
| 65 | } |
| 66 | .frame(maxWidth: .infinity, alignment: .leading) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func legend(_ label: String, _ color: Color, line: Bool = false) -> some View { |
| 71 | HStack(spacing: 6) { |
| 72 | if line { Capsule().fill(color).frame(width: 14, height: 2) } else { RoundedRectangle(cornerRadius: 2).fill(color).frame(width: 9, height: 9) } |
| 73 | Text(label).font(.system(size: 11)).foregroundStyle(palette.muted) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func row(_ cells: [String], header: Bool = false, netColor: Color? = nil) -> some View { |
| 78 | HStack { |
| 79 | Text(cells[0]).frame(maxWidth: .infinity, alignment: .leading) |
| 80 | ForEach(1..<cells.count, id: \.self) { i in |
| 81 | Text(cells[i]).frame(width: 110, alignment: .trailing) |
| 82 | .foregroundStyle(header ? palette.muted : (i == 3 ? (netColor ?? palette.text) : palette.text)) |
| 83 | } |
| 84 | } |
| 85 | .font(header ? .system(size: 11, weight: .semibold) : palette.figure(12, weight: .regular)) |
| 86 | .foregroundStyle(header ? palette.muted : palette.text) |
| 87 | .padding(.horizontal, 14).padding(.vertical, header ? 9 : 8) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// Income and spend bars per month, with what was kept drawn as a line across them. |
| 92 | struct CashFlowChart: View { |
| 93 | @Environment(\.palette) private var palette |
| 94 | let flow: [Ledger.CashFlowMonth] |
| 95 | |
| 96 | var body: some View { |
| 97 | Chart { |
| 98 | ForEach(flow) { m in |
| 99 | BarMark(x: .value("Month", label(m.month)), y: .value("Amount", m.income.double), width: .ratio(0.36)) |
| 100 | .foregroundStyle(palette.gain.opacity(palette.theme == .almanac ? 0.75 : 0.85)) |
| 101 | .position(by: .value("Kind", "Income")) |
| 102 | .cornerRadius(palette.theme == .almanac ? 0 : 3) |
| 103 | BarMark(x: .value("Month", label(m.month)), y: .value("Amount", m.spend.double), width: .ratio(0.36)) |
| 104 | .foregroundStyle(palette.accent.opacity(palette.theme == .almanac ? 0.75 : 0.85)) |
| 105 | .position(by: .value("Kind", "Spend")) |
| 106 | .cornerRadius(palette.theme == .almanac ? 0 : 3) |
| 107 | } |
| 108 | ForEach(flow) { m in |
| 109 | LineMark(x: .value("Month", label(m.month)), y: .value("Kept", m.net.double)) |
| 110 | .foregroundStyle(palette.text).lineStyle(StrokeStyle(lineWidth: 1.6, lineCap: .round)) |
| 111 | .interpolationMethod(palette.theme == .almanac ? .linear : .monotone) |
| 112 | PointMark(x: .value("Month", label(m.month)), y: .value("Kept", m.net.double)) |
| 113 | .foregroundStyle(palette.text).symbolSize(16) |
| 114 | } |
| 115 | RuleMark(y: .value("Zero", 0)).foregroundStyle(palette.line) |
| 116 | } |
| 117 | .chartLegend(.hidden) |
| 118 | .chartYAxis { |
| 119 | AxisMarks(position: .trailing, values: .automatic(desiredCount: 4)) { v in |
| 120 | AxisGridLine(stroke: StrokeStyle(lineWidth: 0.5, dash: palette.theme == .almanac ? [2, 3] : [])).foregroundStyle(palette.line) |
| 121 | AxisValueLabel { if let d = v.as(Double.self) { Text(Money.short(Decimal(d))).font(palette.figure(10, weight: .regular)).foregroundStyle(palette.muted) } } |
| 122 | } |
| 123 | } |
| 124 | .chartXAxis { |
| 125 | AxisMarks { _ in AxisValueLabel().font(.system(size: 10)).foregroundStyle(palette.muted) } |
| 126 | } |
| 127 | .accessibilityLabel("Cash flow chart") |
| 128 | } |
| 129 | |
| 130 | func label(_ month: String) -> String { |
| 131 | let title = MonthKey.title(month) |
| 132 | let short = String(title.prefix(3)) |
| 133 | return flow.count > 12 ? short + " " + String(month.prefix(4).suffix(2)) : short |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /// Last month as a few plain sentences. Apple Intelligence may reword it on the device; if its |
| 138 | /// wording quotes a figure that isn't in the numbers, Bucksley shows its own sentences instead. |
| 139 | struct RecapPanel: View { |
| 140 | @Environment(\.palette) private var palette |
| 141 | let recap: Ledger.Recap |
| 142 | @State private var text: String? |
| 143 | @State private var reworded = false |
| 144 | |
| 145 | var body: some View { |
| 146 | Panel { |
| 147 | VStack(alignment: .leading, spacing: 8) { |
| 148 | HStack { |
| 149 | Label("\(MonthKey.title(recap.month)) in a nutshell", systemImage: "text.quote") |
| 150 | .font(.system(size: 12, weight: .bold)).foregroundStyle(palette.accent) |
| 151 | Spacer() |
| 152 | if reworded { Text("Worded by Apple Intelligence").font(.system(size: 10)).foregroundStyle(palette.muted) } |
| 153 | } |
| 154 | Text(text ?? recap.facts) |
| 155 | .font(palette.theme == .almanac ? .system(size: 15, design: .serif) : .system(size: 14)) |
| 156 | .fixedSize(horizontal: false, vertical: true) |
| 157 | .textSelection(.enabled) |
| 158 | } |
| 159 | .frame(maxWidth: .infinity, alignment: .leading) |
| 160 | } |
| 161 | .task(id: recap) { |
| 162 | let worded = await AskBucksley.wordRecap(recap) |
| 163 | text = worded.text; reworded = worded.reworded |
| 164 | } |
| 165 | .accessibilityIdentifier("recap") |
| 166 | } |
| 167 | } |