Bucksley / app / Apps / Mac / ContentView.swift
| 1 | import SwiftUI |
| 2 | import BucksleyKit |
| 3 | import BucksleyUI |
| 4 | |
| 5 | struct ContentView: View { |
| 6 | @Environment(AppModel.self) private var model |
| 7 | @Environment(\.palette) private var palette |
| 8 | @AppStorage(BucksleyTheme.preferenceKey) private var themeRaw = BucksleyTheme.defaultPreference.rawValue |
| 9 | |
| 10 | var body: some View { |
| 11 | HStack(spacing: 0) { |
| 12 | Sidebar() |
| 13 | .frame(width: 248) |
| 14 | .background(palette.sidebar) |
| 15 | Rectangle().fill(palette.line).frame(width: 1) |
| 16 | detail |
| 17 | .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 18 | .background(palette.canvas) |
| 19 | } |
| 20 | .background(palette.canvas) |
| 21 | .onChange(of: themeRaw) { _, raw in AppIcon.apply(BucksleyTheme(preference: raw)) } |
| 22 | .alert("Bucksley", isPresented: Binding(get: { model.message != nil }, set: { if !$0 { model.message = nil } })) { |
| 23 | Button("OK") { model.message = nil } |
| 24 | } message: { Text(model.message ?? "") } |
| 25 | .modifier(SupportPrompt.Presenter()) |
| 26 | } |
| 27 | |
| 28 | @ViewBuilder var detail: some View { |
| 29 | switch model.selection { |
| 30 | case .overview: OverviewScreen() |
| 31 | case .accounts: AccountsScreen() |
| 32 | case .investments: InvestmentsScreen() |
| 33 | case .transactions: TransactionsScreen() |
| 34 | case .cashFlow: CashFlowScreen() |
| 35 | case .categories: CategoriesScreen() |
| 36 | case .budgets: BudgetsScreen() |
| 37 | case .recurring: RecurringScreen() |
| 38 | case .ask: AskScreen() |
| 39 | case .agents: AgentsScreen() |
| 40 | case .settings: SettingsScreen() |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | struct Sidebar: View { |
| 46 | @Environment(AppModel.self) private var model |
| 47 | @Environment(\.palette) private var palette |
| 48 | /// Account groups the user folded away, remembered between launches. |
| 49 | @AppStorage("sidebar.collapsedGroups") private var collapsedRaw = "" |
| 50 | |
| 51 | var body: some View { |
| 52 | VStack(alignment: .leading, spacing: 0) { |
| 53 | VStack(alignment: .leading, spacing: 2) { |
| 54 | ForEach(Screen.main) { screen in |
| 55 | NavRow(screen: screen, badge: screen == .transactions && model.reviewCount > 0 ? "\(model.reviewCount)" : nil) |
| 56 | } |
| 57 | } |
| 58 | .padding(.horizontal, 10).padding(.top, 12) |
| 59 | |
| 60 | // The header stays put; only the accounts under it scroll. |
| 61 | if !model.ledger.visibleAccounts.isEmpty { |
| 62 | Text("MY ACCOUNTS") |
| 63 | .font(.system(size: 10.5, weight: .bold)).tracking(1) |
| 64 | .foregroundStyle(palette.muted) |
| 65 | .accessibilityAddTraits(.isHeader) |
| 66 | .frame(maxWidth: .infinity, alignment: .leading) |
| 67 | .padding(.horizontal, 16).padding(.top, 22).padding(.bottom, 6) |
| 68 | } |
| 69 | ScrollView { |
| 70 | VStack(alignment: .leading, spacing: 14) { |
| 71 | ForEach(AccountGroup.allCases, id: \.self) { group in |
| 72 | let accounts = model.ledger.visibleAccounts.filter { $0.kind.group == group } |
| 73 | if !accounts.isEmpty { accountGroup(group, accounts) } |
| 74 | } |
| 75 | } |
| 76 | .padding(.horizontal, 16).padding(.top, 2).padding(.bottom, 8) |
| 77 | } |
| 78 | .frame(maxHeight: .infinity) |
| 79 | |
| 80 | VStack(alignment: .leading, spacing: 2) { |
| 81 | NavRow(screen: .agents, badge: nil) |
| 82 | NavRow(screen: .settings, badge: nil) |
| 83 | } |
| 84 | .padding(.horizontal, 10) |
| 85 | |
| 86 | HStack(spacing: 6) { |
| 87 | Image(systemName: "lock.shield").font(.system(size: 11)) |
| 88 | Text(syncLine).font(palette.theme == .almanac ? .system(size: 11, design: .serif).italic() : .system(size: 11)).lineLimit(2) |
| 89 | Spacer() |
| 90 | Button { Task { await model.syncBanks() } } label: { |
| 91 | Image(systemName: "arrow.clockwise").rotationEffect(.degrees(model.syncing ? 360 : 0)) |
| 92 | .animation(model.syncing ? .linear(duration: 1).repeatForever(autoreverses: false) : .default, value: model.syncing) |
| 93 | } |
| 94 | .buttonStyle(.plain).help("Sync banks now (⌘R)").accessibilityLabel("Sync banks now") |
| 95 | } |
| 96 | .foregroundStyle(palette.muted) |
| 97 | .padding(16) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | var collapsed: Set<String> { Set(collapsedRaw.split(separator: ",").map(String.init)) } |
| 102 | |
| 103 | func toggle(_ group: AccountGroup) { |
| 104 | var set = collapsed |
| 105 | if set.contains(group.rawValue) { set.remove(group.rawValue) } else { set.insert(group.rawValue) } |
| 106 | collapsedRaw = set.sorted().joined(separator: ",") |
| 107 | } |
| 108 | |
| 109 | func accountGroup(_ group: AccountGroup, _ accounts: [Account]) -> some View { |
| 110 | let open = !collapsed.contains(group.rawValue) |
| 111 | return VStack(alignment: .leading, spacing: 2) { |
| 112 | Button { withAnimation(.snappy(duration: 0.2)) { toggle(group) } } label: { |
| 113 | HStack(spacing: 5) { |
| 114 | Text(group.title) |
| 115 | .font(palette.theme == .almanac ? .system(size: 13, design: .serif).italic() : .system(size: 12, weight: .semibold)) |
| 116 | Image(systemName: "chevron.down") |
| 117 | .font(.system(size: 8.5, weight: .bold)) |
| 118 | .rotationEffect(.degrees(open ? 0 : -90)) |
| 119 | Spacer() |
| 120 | MoneyText(model.ledger.total(group), size: 11, cents: false) |
| 121 | } |
| 122 | .foregroundStyle(palette.muted) |
| 123 | .padding(.vertical, 4) |
| 124 | .contentShape(Rectangle()) |
| 125 | } |
| 126 | .buttonStyle(.plain) |
| 127 | .accessibilityLabel("\(group.title) accounts") |
| 128 | .accessibilityValue(open ? "Expanded" : "Collapsed") |
| 129 | .accessibilityIdentifier("sidebar.group.\(group.rawValue)") |
| 130 | if open { |
| 131 | ForEach(accounts) { account in SidebarAccountRow(account: account) } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | var syncLine: String { |
| 137 | guard let last = model.workspace.settings.lastSyncAt else { return "Read-only. Bucksley never moves money." } |
| 138 | return "Read-only. Synced \(last.formatted(.relative(presentation: .named)))." |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | struct SidebarAccountRow: View { |
| 143 | @Environment(AppModel.self) private var model |
| 144 | @Environment(\.palette) private var palette |
| 145 | @State private var hovering = false |
| 146 | let account: Account |
| 147 | |
| 148 | var body: some View { |
| 149 | Button { model.selection = .accounts } label: { |
| 150 | HStack(spacing: 9) { |
| 151 | Circle().fill(palette.color(for: account)).frame(width: 7, height: 7) |
| 152 | Text(account.displayName).font(.system(size: 12.5)).lineLimit(1) |
| 153 | Spacer(minLength: 6) |
| 154 | MoneyText(account.balance, size: 12, cents: false, weight: .regular) |
| 155 | } |
| 156 | .padding(.horizontal, 8).padding(.vertical, 6) |
| 157 | .background(hovering ? palette.text.opacity(palette.dark ? 0.07 : 0.05) : .clear, in: RoundedRectangle(cornerRadius: 7, style: .continuous)) |
| 158 | .contentShape(Rectangle()) |
| 159 | } |
| 160 | .buttonStyle(.plain) |
| 161 | .padding(.horizontal, -8) |
| 162 | .onHover { hovering = $0 } |
| 163 | .help("\(account.institution) · \(account.kind.title)") |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | struct NavRow: View { |
| 168 | @Environment(AppModel.self) private var model |
| 169 | @Environment(\.palette) private var palette |
| 170 | @State private var hovering = false |
| 171 | let screen: Screen |
| 172 | let badge: String? |
| 173 | |
| 174 | var body: some View { |
| 175 | let on = model.selection == screen |
| 176 | Button { model.selection = screen } label: { |
| 177 | HStack(spacing: 10) { |
| 178 | let answerer = AskBucksley.answerer(AskBucksley.provider(for: model.workspace.settings)) |
| 179 | NavIcon(symbol: screen.symbol, color: screen.colorIndex.map { palette.color($0) } ?? palette.muted, selected: on, |
| 180 | provider: screen == .ask ? answerer : nil) |
| 181 | Text(screen == .ask ? AskBucksley.title(for: answerer) : screen.title).font(.system(size: 13, weight: on ? .semibold : .medium)) |
| 182 | Spacer() |
| 183 | if let badge { |
| 184 | Text(badge).font(.system(size: 10, weight: .bold)).padding(.horizontal, 6).padding(.vertical, 2) |
| 185 | .foregroundStyle(on && palette.theme != .evergreen ? palette.accent : palette.accentText) |
| 186 | .background(on && palette.theme != .evergreen ? palette.accentText : palette.accent, in: Capsule()) |
| 187 | } |
| 188 | } |
| 189 | .padding(.leading, 6).padding(.trailing, 10).padding(.vertical, 5) |
| 190 | .foregroundStyle(foreground(on)) |
| 191 | .background(background(on), in: RoundedRectangle(cornerRadius: palette.theme == .evergreen ? 8 : 999, style: .continuous)) |
| 192 | .contentShape(Rectangle()) |
| 193 | } |
| 194 | .buttonStyle(.plain) |
| 195 | .onHover { hovering = $0 } |
| 196 | .animation(.easeOut(duration: 0.12), value: hovering) |
| 197 | .accessibilityAddTraits(on ? .isSelected : []) |
| 198 | .accessibilityIdentifier("nav.\(screen.title)") |
| 199 | } |
| 200 | |
| 201 | func foreground(_ on: Bool) -> Color { |
| 202 | guard on else { return palette.text } |
| 203 | switch palette.theme { case .afterhours: return palette.accentText; case .evergreen: return palette.accent; case .almanac: return palette.canvas } |
| 204 | } |
| 205 | func background(_ on: Bool) -> Color { |
| 206 | guard on else { return hovering ? palette.text.opacity(palette.dark ? 0.07 : 0.05) : .clear } |
| 207 | switch palette.theme { case .afterhours: return palette.accent; case .evergreen: return palette.accent.opacity(0.14); case .almanac: return palette.text } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /// The colored tile behind each sidebar icon, tinted from the theme's own chart colors so every |
| 212 | /// theme keeps its character. On a filled selection it turns into a quiet glass tile. |
| 213 | struct NavIcon: View { |
| 214 | @Environment(\.palette) private var palette |
| 215 | let symbol: String |
| 216 | let color: Color |
| 217 | let selected: Bool |
| 218 | /// Ask shows the mark of whoever answers instead of a symbol. |
| 219 | var provider: AskProvider? = nil |
| 220 | |
| 221 | var body: some View { |
| 222 | let onFill = selected && palette.theme != .evergreen |
| 223 | let ink = onFill ? (palette.theme == .almanac ? palette.canvas : palette.accentText) : color |
| 224 | Group { |
| 225 | if let provider { |
| 226 | ProviderMark(provider, size: 13, tint: onFill ? ink : (provider == .apple || provider == .claude || provider == .claudeCode ? nil : palette.text)) |
| 227 | } else { |
| 228 | Image(systemName: symbol).font(.system(size: 11.5, weight: .semibold)).foregroundStyle(ink) |
| 229 | } |
| 230 | } |
| 231 | .frame(width: 24, height: 24) |
| 232 | .background(onFill ? Color.white.opacity(palette.theme == .almanac ? 0.14 : 0.2) : color.opacity(palette.dark ? 0.2 : 0.14), |
| 233 | in: RoundedRectangle(cornerRadius: palette.theme == .almanac ? 5 : 7, style: .continuous)) |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /// Every screen's frame: a title row with optional controls, then scrolling content. |
| 238 | struct ScreenScaffold<Controls: View, Content: View>: View { |
| 239 | @Environment(\.palette) private var palette |
| 240 | @Environment(AppModel.self) private var model |
| 241 | let title: String |
| 242 | var subtitle: String? |
| 243 | var provider: AskProvider? |
| 244 | let controls: Controls |
| 245 | let content: Content |
| 246 | |
| 247 | init(_ title: String, subtitle: String? = nil, provider: AskProvider? = nil, @ViewBuilder controls: () -> Controls = { EmptyView() }, @ViewBuilder content: () -> Content) { |
| 248 | self.title = title; self.subtitle = subtitle; self.provider = provider; self.controls = controls(); self.content = content() |
| 249 | } |
| 250 | |
| 251 | var body: some View { |
| 252 | VStack(spacing: 0) { |
| 253 | HStack(alignment: .center, spacing: 12) { |
| 254 | VStack(alignment: .leading, spacing: 2) { |
| 255 | HStack(spacing: 9) { |
| 256 | if let provider { ProviderMark(provider, size: 20, tint: provider == .apple || provider == .claude || provider == .claudeCode ? nil : palette.text) } |
| 257 | Text(title).font(palette.title(24)).tracking(palette.titleTracking) |
| 258 | } |
| 259 | if let subtitle { Text(subtitle).font(palette.theme == .almanac ? .system(size: 13, design: .serif).italic() : .system(size: 12)).foregroundStyle(palette.muted) } |
| 260 | } |
| 261 | Spacer() |
| 262 | controls |
| 263 | } |
| 264 | .padding(.horizontal, 28).padding(.top, 22).padding(.bottom, 14) |
| 265 | ScrollView { |
| 266 | VStack(alignment: .leading, spacing: 0) { content } |
| 267 | .frame(maxWidth: .infinity, alignment: .leading) |
| 268 | .padding(.horizontal, 28).padding(.bottom, 28) |
| 269 | } |
| 270 | .defaultScrollAnchor(model.tourAnchor ?? (model.tourScrolledToEnd ? .bottom : .top)) |
| 271 | .id("\(model.tourScrolledToEnd)\(model.tourAnchor.map { "\($0.y)" } ?? "")") |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /// Shown on screens that need data before there is any. |
| 277 | struct ConnectFirst: View { |
| 278 | @Environment(AppModel.self) private var model |
| 279 | @Environment(\.palette) private var palette |
| 280 | var body: some View { |
| 281 | Panel(padding: 24) { |
| 282 | VStack(alignment: .leading, spacing: 12) { |
| 283 | Text("Connect your first account").font(palette.title(18)) |
| 284 | Text("Bucksley reads balances and transactions with your own SimpleFIN Bridge or Plaid keys, or a CSV export from another money app. Everything stays on this Mac and in your private iCloud. Bucksley never moves money.") |
| 285 | .font(.system(size: 13)).foregroundStyle(palette.muted).fixedSize(horizontal: false, vertical: true) |
| 286 | HStack { |
| 287 | Button("Connect a bank") { model.selection = .settings }.buttonStyle(PrimaryButtonStyle()) |
| 288 | Button("Import a CSV") { model.selection = .settings; model.wantsCSVImport = true }.buttonStyle(SecondaryButtonStyle()) |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |