import SwiftUI import Charts import BucksleyKit import BucksleyUI /// Money in against money out, month by month, with last month told as a short story. struct CashFlowScreen: View { @Environment(AppModel.self) private var model @Environment(\.palette) private var palette @State private var months = 12 var body: some View { let ledger = model.ledger // Months before the first transaction would draw as a flat line at zero. let flow = Array(ledger.cashFlow(months: months).drop { $0.income == 0 && $0.spend == 0 }) let avg = ledger.cashFlowAverages(flow) ScreenScaffold("Cash Flow", subtitle: "What came in, what went out, and what you kept") { PillPicker([(6, "6M"), (12, "1Y"), (24, "2Y")], selection: $months) } content: { if ledger.entries.isEmpty { ConnectFirst() } else { VStack(alignment: .leading, spacing: 18) { HStack(spacing: 12) { stat("Average income", Money.format(avg.income, cents: false), palette.gain) stat("Average spend", Money.format(avg.spend, cents: false), palette.text) stat("Average kept", Money.format(avg.net, cents: false), avg.net >= 0 ? palette.gain : palette.loss) stat("Savings rate", avg.savingsRate.map { "\(Int(($0 * 100).rounded()))%" } ?? "—", palette.text) } Panel { VStack(alignment: .leading, spacing: 10) { HStack(spacing: 14) { legend("Income", palette.gain) legend("Spend", palette.accent) legend("Kept", palette.text, line: true) Spacer() } CashFlowChart(flow: flow).frame(height: 240) } } if let recap = ledger.recap() { RecapPanel(recap: recap) } Panel(padding: 0) { VStack(spacing: 0) { row(["Month", "Income", "Spend", "Kept", "Saved"], header: true) ForEach(flow.reversed().filter { $0.income != 0 || $0.spend != 0 }) { m in Rectangle().fill(palette.line).frame(height: 0.5) row([MonthKey.title(m.month), Money.format(m.income, cents: false), Money.format(m.spend, cents: false), Money.format(m.net, cents: false), m.savingsRate.map { "\(Int(($0 * 100).rounded()))%" } ?? "—"], netColor: m.net >= 0 ? palette.gain : palette.loss) } } } Text("Transfers between your accounts and card payments aren't income or spending. Refunds lower the category they came back to.") .font(.system(size: 11)).foregroundStyle(palette.muted) } } } } func stat(_ label: String, _ value: String, _ color: Color) -> some View { Panel { VStack(alignment: .leading, spacing: 4) { Text(label).font(.system(size: 11, weight: .semibold)).foregroundStyle(palette.muted) Text(value).font(palette.figure(20)).tracking(palette.figureTracking).foregroundStyle(color) } .frame(maxWidth: .infinity, alignment: .leading) } } func legend(_ label: String, _ color: Color, line: Bool = false) -> some View { HStack(spacing: 6) { if line { Capsule().fill(color).frame(width: 14, height: 2) } else { RoundedRectangle(cornerRadius: 2).fill(color).frame(width: 9, height: 9) } Text(label).font(.system(size: 11)).foregroundStyle(palette.muted) } } func row(_ cells: [String], header: Bool = false, netColor: Color? = nil) -> some View { HStack { Text(cells[0]).frame(maxWidth: .infinity, alignment: .leading) ForEach(1.. String { let title = MonthKey.title(month) let short = String(title.prefix(3)) return flow.count > 12 ? short + " " + String(month.prefix(4).suffix(2)) : short } } /// Last month as a few plain sentences. Apple Intelligence may reword it on the device; if its /// wording quotes a figure that isn't in the numbers, Bucksley shows its own sentences instead. struct RecapPanel: View { @Environment(\.palette) private var palette let recap: Ledger.Recap @State private var text: String? @State private var reworded = false var body: some View { Panel { VStack(alignment: .leading, spacing: 8) { HStack { Label("\(MonthKey.title(recap.month)) in a nutshell", systemImage: "text.quote") .font(.system(size: 12, weight: .bold)).foregroundStyle(palette.accent) Spacer() if reworded { Text("Worded by Apple Intelligence").font(.system(size: 10)).foregroundStyle(palette.muted) } } Text(text ?? recap.facts) .font(palette.theme == .almanac ? .system(size: 15, design: .serif) : .system(size: 14)) .fixedSize(horizontal: false, vertical: true) .textSelection(.enabled) } .frame(maxWidth: .infinity, alignment: .leading) } .task(id: recap) { let worded = await AskBucksley.wordRecap(recap) text = worded.text; reworded = worded.reworded } .accessibilityIdentifier("recap") } }