Bucksley / app · bucksley.ai / Apps / Mac / InvestmentsScreen.swift
| 1 | import SwiftUI |
| 2 | import BucksleyKit |
| 3 | import BucksleyUI |
| 4 | |
| 5 | /// Brokerage, retirement and crypto accounts in one place: the total with its history, each |
| 6 | /// account's change over the chosen range, and positions when the bank sends them. |
| 7 | struct InvestmentsScreen: View { |
| 8 | @Environment(AppModel.self) private var model |
| 9 | @Environment(\.palette) private var palette |
| 10 | @State private var range = 90 |
| 11 | |
| 12 | var body: some View { |
| 13 | let ledger = model.ledger |
| 14 | let accounts = ledger.visibleAccounts.filter { $0.kind.group == .investments } |
| 15 | let total = ledger.total(.investments) |
| 16 | let series = ledger.netWorthSeries(days: range).map { |
| 17 | Ledger.NetWorthPoint(day: $0.day, total: $0.byGroup[.investments] ?? 0, byGroup: [.investments: $0.byGroup[.investments] ?? 0]) |
| 18 | } |
| 19 | // Measured per account from its first reading in the range, so a newly connected account |
| 20 | // never reads as a gain from zero. |
| 21 | let start = accounts.reduce(Decimal(0)) { $0 + (ledger.accountSeries($1.id, days: range).first?.balance ?? $1.balance) } |
| 22 | let change = total - start |
| 23 | let holdings = accounts.flatMap { account in (account.holdings ?? []).map { (account, $0) } }.sorted { $0.1.marketValue > $1.1.marketValue } |
| 24 | |
| 25 | ScreenScaffold("Investments", subtitle: accounts.isEmpty ? "Brokerage, retirement and crypto accounts" : "\(accounts.count) account\(accounts.count == 1 ? "" : "s")") { |
| 26 | if !accounts.isEmpty { PillPicker([(30, "1M"), (90, "3M"), (365, "1Y"), (730, "All")], selection: $range) } |
| 27 | } content: { |
| 28 | VStack(alignment: .leading, spacing: 28) { |
| 29 | if accounts.isEmpty { |
| 30 | empty |
| 31 | } else { |
| 32 | Panel(padding: 22) { |
| 33 | VStack(alignment: .leading, spacing: 14) { |
| 34 | HStack(alignment: .firstTextBaseline, spacing: 12) { |
| 35 | HeroMoney(total) |
| 36 | ChangeChip(change, base: start) |
| 37 | Spacer() |
| 38 | } |
| 39 | Text("Total across your investment accounts").font(.system(size: 12)).foregroundStyle(palette.muted) |
| 40 | if series.count > 1 { NetWorthChart(series).frame(height: 200) } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | VStack(alignment: .leading, spacing: 10) { |
| 45 | SectionHeader("Accounts") { Text("\(rangeTitle) change").font(.system(size: 11, weight: .semibold)).foregroundStyle(palette.muted) } |
| 46 | ForEach(accounts) { account in accountRow(account, ledger: ledger) } |
| 47 | } |
| 48 | |
| 49 | VStack(alignment: .leading, spacing: 10) { |
| 50 | SectionHeader("Holdings") { if !holdings.isEmpty { Text("\(holdings.count) positions").font(.system(size: 11, weight: .semibold)).foregroundStyle(palette.muted) } } |
| 51 | if holdings.isEmpty { |
| 52 | Text("Your bank hasn't sent individual positions yet. SimpleFIN Bridge includes them for most brokerages; they show here after the next sync.") |
| 53 | .font(.system(size: 12)).foregroundStyle(palette.muted).fixedSize(horizontal: false, vertical: true) |
| 54 | } else { |
| 55 | Panel(padding: 6) { |
| 56 | VStack(spacing: 0) { |
| 57 | ForEach(Array(holdings.enumerated()), id: \.offset) { index, pair in |
| 58 | holdingRow(pair.1, account: pair.0, total: total) |
| 59 | if index < holdings.count - 1 { Rectangle().fill(palette.line.opacity(0.6)).frame(height: 0.5).padding(.horizontal, 10) } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | var rangeTitle: String { |
| 71 | switch range { case 30: "1M"; case 90: "3M"; case 365: "1Y"; default: "All" } |
| 72 | } |
| 73 | |
| 74 | var empty: some View { |
| 75 | Panel(padding: 24) { |
| 76 | VStack(alignment: .leading, spacing: 12) { |
| 77 | Text("No investment accounts yet").font(palette.title(18)) |
| 78 | Text("Add your brokerage (Fidelity, Merrill Edge, Schwab, Vanguard and more) in SimpleFIN Bridge and share it with Bucksley, or connect it with Plaid. Accounts that SimpleFIN sends but Bucksley files under another type can be moved here from Accounts → Type → Investment.") |
| 79 | .font(.system(size: 13)).foregroundStyle(palette.muted).fixedSize(horizontal: false, vertical: true) |
| 80 | Button("Connect a brokerage") { model.selection = .settings }.buttonStyle(PrimaryButtonStyle()) |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func accountRow(_ account: Account, ledger: Ledger) -> some View { |
| 86 | let history = ledger.accountSeries(account.id, days: range) |
| 87 | let start = history.first?.balance ?? account.balance |
| 88 | return Button { model.selection = .accounts } label: { |
| 89 | HStack(spacing: 14) { |
| 90 | MerchantBadge(name: account.institution, colorIndex: 0, size: 36, brand: palette.color(for: account)) |
| 91 | VStack(alignment: .leading, spacing: 3) { |
| 92 | Text(account.displayName).font(.system(size: 14, weight: .semibold)).lineLimit(1) |
| 93 | Text("\(account.institution) · as of \(account.balanceDate.formatted(.relative(presentation: .named)))").font(.system(size: 12)).foregroundStyle(palette.muted).lineLimit(1) |
| 94 | } |
| 95 | Spacer(minLength: 12) |
| 96 | if history.count > 4 { |
| 97 | Sparkline(stride(from: 0, to: history.count, by: max(1, history.count / 14)).map { history[$0].balance.double }, colorIndex: account.balance >= start ? 1 : 4) |
| 98 | } |
| 99 | ChangeChip(account.balance - start, base: start) |
| 100 | MoneyText(account.balance, size: 15).frame(minWidth: 110, alignment: .trailing) |
| 101 | } |
| 102 | .padding(.horizontal, palette.ruled ? 4 : 16).padding(.vertical, 14) |
| 103 | .background(palette.ruled ? Color.clear : palette.card, in: RoundedRectangle(cornerRadius: palette.cardRadius, style: .continuous)) |
| 104 | .contentShape(Rectangle()) |
| 105 | } |
| 106 | .buttonStyle(.plain) |
| 107 | } |
| 108 | |
| 109 | func holdingRow(_ holding: Holding, account: Account, total: Decimal) -> some View { |
| 110 | HStack(spacing: 12) { |
| 111 | Text(holding.symbol ?? "—") |
| 112 | .font(palette.figure(12, weight: .semibold)) |
| 113 | .frame(width: 64, alignment: .leading) |
| 114 | VStack(alignment: .leading, spacing: 2) { |
| 115 | Text(holding.name).font(.system(size: 13, weight: .medium)).lineLimit(1) |
| 116 | let crypto = account.kind == .crypto |
| 117 | Text([holding.shares.map { "\(NSDecimalNumber(decimal: $0).doubleValue.formatted(.number.precision(.fractionLength(0...(crypto ? 8 : 4))))) \(crypto ? holding.symbol ?? "" : "shares")" }, account.displayName].compactMap { $0 }.joined(separator: " · ")) |
| 118 | .font(.system(size: 11)).foregroundStyle(palette.muted).lineLimit(1) |
| 119 | } |
| 120 | Spacer(minLength: 12) |
| 121 | if let gain = holding.gain, let cost = holding.costBasis { ChangeChip(gain, base: cost) } |
| 122 | MoneyText(holding.marketValue, size: 13).frame(minWidth: 100, alignment: .trailing) |
| 123 | } |
| 124 | .padding(.horizontal, 12).padding(.vertical, 10) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /// "▲ $1,240 · 2.4%" in gain or loss colors. |
| 129 | struct ChangeChip: View { |
| 130 | @Environment(\.palette) private var palette |
| 131 | let change: Decimal |
| 132 | let base: Decimal |
| 133 | init(_ change: Decimal, base: Decimal) { self.change = change; self.base = base } |
| 134 | |
| 135 | var body: some View { |
| 136 | let up = change >= 0 |
| 137 | let percent = base != 0 ? abs((change / base).double) * 100 : 0 |
| 138 | let tint = change == 0 ? palette.muted : up ? palette.gain : palette.loss |
| 139 | HStack(spacing: 4) { |
| 140 | Image(systemName: up ? "arrow.up.right" : "arrow.down.right").font(.system(size: 9, weight: .bold)) |
| 141 | Text(Money.format(abs(change), cents: false)) |
| 142 | if base != 0 { Text(String(format: "%.1f%%", percent)).opacity(0.8) } |
| 143 | } |
| 144 | .font(palette.figure(11, weight: .semibold)) |
| 145 | .foregroundStyle(tint) |
| 146 | .padding(.horizontal, 8).padding(.vertical, 3) |
| 147 | .background(tint.opacity(palette.dark ? 0.16 : 0.1), in: Capsule()) |
| 148 | .accessibilityLabel("\(up ? "Up" : "Down") \(Money.format(abs(change), cents: false))") |
| 149 | } |
| 150 | } |