import SwiftUI import BucksleyKit import BucksleyUI struct OverviewScreen: View { @Environment(AppModel.self) private var model @Environment(\.palette) private var palette @State private var question = "" var body: some View { @Bindable var model = model let ledger = model.ledger ScreenScaffold("Overview", subtitle: Date.now.formatted(.dateTime.weekday(.wide).month(.wide).day())) { PillPicker([(30, "1M"), (90, "3M"), (365, "1Y"), (730, "All")], selection: $model.range) } content: { if ledger.visibleAccounts.isEmpty { ConnectFirst() } else { VStack(alignment: .leading, spacing: 18) { hero(ledger) NetWorthChart(ledger.netWorthSeries(days: model.range)).frame(height: 230) HStack(alignment: .top, spacing: 18) { Panel { VStack(alignment: .leading, spacing: 12) { SectionHeader("Spending in \(MonthKey.title(MonthKey.current).components(separatedBy: " ").first ?? "")") { Button("Details") { model.selection = .budgets }.buttonStyle(.plain).font(.system(size: 12, weight: .semibold)).foregroundStyle(palette.accent) } SpendingChart(ledger.spending(), workspace: ledger.workspace) } } Panel { VStack(alignment: .leading, spacing: 10) { SectionHeader("Budgets") { Button("All budgets") { model.selection = .budgets }.buttonStyle(.plain).font(.system(size: 12, weight: .semibold)).foregroundStyle(palette.accent) } let budgets = ledger.budgetStatus() if budgets.isEmpty { Text("No budgets yet. Bucksley can suggest them from your spending.").font(.system(size: 13)).foregroundStyle(palette.muted) Button("See suggestions") { model.selection = .budgets }.buttonStyle(SecondaryButtonStyle()) } ForEach(budgets.prefix(4)) { status in BudgetProgress(status, name: ledger.budgetName(status.id), colorIndex: ledger.workspace.category(status.id)?.colorIndex ?? 1) } } } } HStack(alignment: .top, spacing: 18) { Panel { VStack(alignment: .leading, spacing: 4) { SectionHeader("Recent transactions") { Button("See all") { model.selection = .transactions }.buttonStyle(.plain).font(.system(size: 12, weight: .semibold)).foregroundStyle(palette.accent) } ForEach(ledger.entries.prefix(7)) { entry in Button { model.open(entry); model.selection = .transactions } label: { TransactionRow(entry, workspace: ledger.workspace) } .buttonStyle(.plain) } } } .frame(maxWidth: .infinity) VStack(alignment: .leading, spacing: 18) { WorthALook() if let tip = AskBucksley.suggestion(ledger) { Panel { VStack(alignment: .leading, spacing: 8) { Label("Bucksley noticed", systemImage: "sparkles").font(.system(size: 12, weight: .bold)).foregroundStyle(palette.accent) Text(tip).font(palette.theme == .almanac ? .system(size: 15, design: .serif) : .system(size: 14)).fixedSize(horizontal: false, vertical: true) } } } askField } .frame(width: 320) } } } } } @ViewBuilder func hero(_ ledger: Ledger) -> some View { let change = ledger.change(days: model.range) let start = ledger.netWorth - change let pct = start != 0 ? (change / abs(start)).double * 100 : 0 VStack(alignment: .leading, spacing: 6) { if palette.theme == .almanac { Text(almanacHeadline(ledger)).font(.system(size: 26, weight: .medium, design: .serif)).tracking(-0.3).fixedSize(horizontal: false, vertical: true) } else { Text(palette.theme == .afterhours ? "NET WORTH" : "Net worth").font(palette.theme == .afterhours ? .system(size: 11, weight: .bold) : .system(size: 13, weight: .medium)) .tracking(palette.theme == .afterhours ? 1.1 : 0).foregroundStyle(palette.muted) } HStack(alignment: .firstTextBaseline, spacing: 12) { HeroMoney(ledger.netWorth, size: palette.theme == .almanac ? 30 : 42) Text("\(Money.format(change, cents: false, signed: true)) · \(String(format: "%.1f", pct))%") .font(palette.figure(13, weight: .medium)).foregroundStyle(change >= 0 ? palette.gain : palette.loss) Text(rangeLabel).font(.system(size: 12)).foregroundStyle(palette.muted) } if palette.theme == .afterhours { HStack(spacing: 18) { ForEach(AccountGroup.allCases, id: \.self) { g in let total = ledger.total(g) if total != 0 { HStack(spacing: 6) { RoundedRectangle(cornerRadius: 2).fill(palette.color(for: g)).frame(width: 9, height: 9) Text(g.title).font(.system(size: 12)).foregroundStyle(palette.muted) Text(Money.short(total)).font(palette.figure(12, weight: .medium)) } } } } } } } var rangeLabel: String { switch model.range { case 30: "past month"; case 90: "past 3 months"; case 365: "past year"; default: "all time" } } func almanacHeadline(_ ledger: Ledger) -> String { let month = ledger.change(days: Int(DayKey.today.suffix(2)) ?? 30), year = ledger.change(days: 365) let dir = month >= 0 ? "up" : "down" let yearStart = DayKey.adding(-365, to: DayKey.today) let since = ledger.historyStart.flatMap { $0 > yearStart ? DayKey.date($0) : nil } let span = since.map { "since " + $0.formatted(.dateTime.month(.wide)) } ?? "on the year" return "Net worth is \(dir) \(Money.format(abs(month), cents: false)) this month, and \(Money.format(year, cents: false, signed: true)) \(span)." } var askField: some View { HStack(spacing: 8) { Image(systemName: "sparkles").foregroundStyle(palette.accent) TextField("\(AskBucksley.title(for: AskBucksley.provider(for: model.workspace.settings))) about your money…", text: $question) .textFieldStyle(.plain).font(.system(size: 13)) .onSubmit { send() } Button { send() } label: { Image(systemName: "arrow.up").font(.system(size: 12, weight: .bold)).frame(width: 26, height: 26) } .buttonStyle(.plain).foregroundStyle(palette.accentText).background(palette.accent, in: Circle()) .accessibilityLabel("Ask") } .padding(.leading, 14).padding(.trailing, 5).padding(.vertical, 5) .background(palette.card, in: Capsule()) .overlay { Capsule().strokeBorder(palette.line) } } func send() { let q = question.trimmingCharacters(in: .whitespaces) guard !q.isEmpty else { return } AskModel.shared.pending = q question = "" model.selection = .ask } } struct AccountsScreen: View { @Environment(AppModel.self) private var model @Environment(\.palette) private var palette @State private var selected: String? var body: some View { let ledger = model.ledger ScreenScaffold("Accounts", subtitle: "\(ledger.visibleAccounts.count) accounts · net worth \(Money.format(ledger.netWorth, cents: false))") { Button("Add account") { model.selection = .settings }.buttonStyle(SecondaryButtonStyle()) } content: { if ledger.workspace.accounts.isEmpty { ConnectFirst() } VStack(alignment: .leading, spacing: 28) { HStack(spacing: 14) { stat("Assets", ledger.assets) stat("Debts", ledger.debts) stat("Net worth", ledger.netWorth) } ForEach(AccountGroup.allCases, id: \.self) { group in let accounts = ledger.workspace.accounts.filter { $0.kind.group == group } if !accounts.isEmpty { VStack(alignment: .leading, spacing: 10) { SectionHeader(group.title) { MoneyText(ledger.total(group), size: 13, cents: false) } ForEach(accounts) { account in AccountRow(account: account, open: Binding(get: { selected == account.id }, set: { selected = $0 ? account.id : nil })) } } } } } } } func stat(_ label: String, _ value: Decimal) -> some View { Panel { VStack(alignment: .leading, spacing: 4) { Text(label).font(.system(size: 12, weight: .medium)).foregroundStyle(palette.muted) MoneyText(value, size: 22, cents: false, colored: label == "Debts") } } } } /// One account on the Accounts screen: a roomy row that lights up on hover and opens in place to /// show its balance history and settings. struct AccountRow: View { @Environment(AppModel.self) private var model @Environment(\.palette) private var palette let account: Account @Binding var open: Bool @State private var hovering = false var body: some View { let ledger = model.ledger let history = ledger.accountSeries(account.id, days: 60) let connection = ledger.workspace.connections.first { $0.id == account.connectionID } let tint = account.kind.group == .credit ? 4 : account.kind.group == .investments ? 0 : account.kind.group == .other ? 2 : 1 VStack(alignment: .leading, spacing: 14) { Button { withAnimation(.snappy) { open.toggle() } } label: { HStack(spacing: 14) { MerchantBadge(name: account.institution, colorIndex: tint, size: 38, brand: palette.color(for: account)) VStack(alignment: .leading, spacing: 4) { HStack(spacing: 6) { Text(account.displayName).font(.system(size: 14, weight: .semibold)).lineLimit(1) if account.hidden { Text("Hidden").font(.system(size: 10, weight: .bold)).foregroundStyle(palette.muted) } } Text([account.institution, account.kind.title, connection?.kind.title].compactMap { $0 }.reduce(into: [String]()) { if !$0.contains($1) { $0.append($1) } }.joined(separator: " · ")) .font(.system(size: 12)).foregroundStyle(palette.muted).lineLimit(1) } Spacer(minLength: 16) if palette.theme == .afterhours, history.count > 4 { Sparkline(stride(from: 0, to: history.count, by: max(1, history.count / 14)).map { history[$0].balance.double }, colorIndex: tint) } VStack(alignment: .trailing, spacing: 4) { MoneyText(account.balance, size: 16) Text("as of " + account.balanceDate.formatted(date: .abbreviated, time: .omitted)).font(.system(size: 11)).foregroundStyle(palette.muted) } Image(systemName: "chevron.right") .font(.system(size: 11, weight: .bold)) .foregroundStyle(hovering || open ? palette.accent : palette.muted) .rotationEffect(.degrees(open ? 90 : 0)) .frame(width: 16) } .contentShape(Rectangle()) } .buttonStyle(.plain) .help(open ? "Hide details" : "Show balance history and settings") .accessibilityHint("Shows balance history and account settings") if open { details(history: history, connection: connection) } } .padding(.horizontal, palette.ruled ? 6 : 18).padding(.vertical, 16) .background { background } .onHover { inside in hovering = inside if inside { NSCursor.pointingHand.push() } else { NSCursor.pop() } } .animation(.easeOut(duration: 0.14), value: hovering) } @ViewBuilder var background: some View { let shape = RoundedRectangle(cornerRadius: palette.cardRadius, style: .continuous) if palette.ruled { ZStack(alignment: .top) { shape.fill(hovering || open ? palette.text.opacity(0.045) : .clear) Rectangle().fill(palette.line).frame(height: 1) } } else { shape.fill(hovering ? palette.card.mix(with: palette.text, by: palette.dark ? 0.05 : 0.025) : palette.card) .overlay { shape.strokeBorder(hovering || open ? palette.accent.opacity(open ? 0.45 : 0.6) : palette.line.opacity(0.7), lineWidth: 1) } .shadow(color: .black.opacity(hovering ? (palette.dark ? 0.35 : 0.08) : 0), radius: 10, y: 4) } } func details(history: [(day: String, balance: Decimal)], connection: Connection?) -> some View { VStack(alignment: .leading, spacing: 12) { if history.count > 1 { NetWorthChart(history.map { Ledger.NetWorthPoint(day: $0.day, total: $0.balance, byGroup: [account.kind.group: max(0, $0.balance)]) }).frame(height: 120) } HStack { TextField("Nickname", text: Binding(get: { account.nickname ?? "" }, set: { v in model.updateWorkspace { ws in if let i = ws.accounts.firstIndex(where: { $0.id == account.id }) { ws.accounts[i].nickname = v.isEmpty ? nil : v } } })) .textFieldStyle(.roundedBorder).frame(width: 200) ChoiceButton(AccountKind.allCases.map { (value: $0, title: $0.title, emoji: nil) }, selection: Binding(get: { account.kind }, set: { v in model.updateWorkspace { ws in if let i = ws.accounts.firstIndex(where: { $0.id == account.id }) { ws.accounts[i].kind = v; ws.accounts[i].kindChosen = true } } })) .frame(width: 200) Toggle("Hide from totals", isOn: Binding(get: { account.hidden }, set: { v in model.updateWorkspace { ws in if let i = ws.accounts.firstIndex(where: { $0.id == account.id }) { ws.accounts[i].hidden = v } } })) if connection?.kind == .csv || connection?.kind == .manual { TextField("Balance", value: Binding(get: { account.balance }, set: { v in do { try BankSync(store: model.store).setManualBalance(account.id, balance: v); model.reload(force: true); model.shareSoon() } catch { model.message = error.localizedDescription } }), format: .currency(code: account.currency)).textFieldStyle(.roundedBorder).frame(width: 120).help("Imported accounts have no live balance; type it here") } Spacer() Button("Transactions") { model.selection = .transactions; TransactionsFilter.shared.accountID = account.id }.buttonStyle(SecondaryButtonStyle()) } .font(.system(size: 12)) } } } struct RecurringScreen: View { @Environment(AppModel.self) private var model @Environment(\.palette) private var palette var body: some View { let ledger = model.ledger let items = ledger.recurring() let active = items.filter { $0.status != .dismissed } let monthly = active.reduce(Decimal(0)) { $0 + $1.amount * Decimal(30) / Decimal($1.cadence.days) }.rounded(0) ScreenScaffold("Recurring", subtitle: active.isEmpty ? "Charges that repeat show up here after three on a steady schedule." : "\(active.count) recurring · about \(Money.format(monthly, cents: false)) a month") { EmptyView() } content: { VStack(alignment: .leading, spacing: 16) { if items.isEmpty && ledger.entries.isEmpty { ConnectFirst() } let bills = ledger.monthBills() if !bills.isEmpty { monthSummary(bills) VStack(alignment: .leading, spacing: 4) { SectionHeader("This month") ForEach(bills) { bill in billRow(bill, ledger.workspace) } } } ForEach([RecurringStatus.confirmed, .detected, .dismissed], id: \.self) { status in let group = items.filter { $0.status == status } if !group.isEmpty { VStack(alignment: .leading, spacing: 6) { SectionHeader(status == .confirmed ? "All recurring" : status == .detected ? "Found by Bucksley" : "Not recurring") ForEach(group) { item in row(item, ledger.workspace) } } } } } } } func monthSummary(_ bills: [Ledger.MonthBill]) -> some View { let paid = bills.reduce(Decimal(0)) { $0 + ($1.paid ?? 0) } let left = bills.filter { $0.paid == nil }.reduce(Decimal(0)) { $0 + $1.item.amount } let share = (paid + left) > 0 ? (paid / (paid + left)).double : 0 return Panel(padding: 22) { HStack(spacing: 28) { VStack(alignment: .leading, spacing: 4) { MoneyText(left, size: 26, cents: false) Text("left to pay").font(.system(size: 12, weight: .medium)).foregroundStyle(palette.muted) } Spacer() ZStack { Circle().stroke(palette.line, lineWidth: 10) Circle().trim(from: 0, to: share).stroke(palette.accent, style: StrokeStyle(lineWidth: 10, lineCap: .round)).rotationEffect(.degrees(-90)) Text("\(Int((share * 100).rounded()))%").font(palette.figure(13)).foregroundStyle(palette.muted) } .frame(width: 74, height: 74) .accessibilityLabel("\(Int((share * 100).rounded())) percent of this month's recurring paid") Spacer() VStack(alignment: .trailing, spacing: 4) { MoneyText(paid, size: 26, cents: false) Text("paid so far").font(.system(size: 12, weight: .medium)).foregroundStyle(palette.muted) } } } } func billRow(_ bill: Ledger.MonthBill, _ ws: Workspace) -> some View { let category = ws.category(bill.item.categoryID) return HStack(spacing: 12) { Text(bill.overdue ? "Overdue" : TransactionRow.shortDay(bill.day)) .font(palette.figure(12, weight: .regular)).foregroundStyle(bill.overdue ? palette.loss : palette.muted) .frame(width: 64, alignment: .leading) Text(category?.icon ?? "🔁").font(.system(size: 14)) Text(bill.item.name).font(.system(size: 13, weight: .semibold)).lineLimit(1) Text(bill.item.cadence.title).font(.system(size: 12)).foregroundStyle(palette.muted) Spacer(minLength: 8) CategoryTag(category) MoneyText(bill.paid ?? bill.item.amount, size: 13).frame(width: 90, alignment: .trailing) Image(systemName: bill.paid != nil ? "checkmark.circle.fill" : "circle") .foregroundStyle(bill.paid != nil ? palette.gain : palette.line) .accessibilityLabel(bill.paid != nil ? "Paid" : "Not paid yet") } .padding(.vertical, 8).padding(.horizontal, palette.ruled ? 0 : 12) .overlay(alignment: .bottom) { Rectangle().fill(palette.line.opacity(0.6)).frame(height: 0.5) } } func row(_ item: RecurringItem, _ ws: Workspace) -> some View { HStack(spacing: 12) { MerchantBadge(name: item.name, colorIndex: ws.category(item.categoryID)?.colorIndex ?? 5) VStack(alignment: .leading, spacing: 2) { Text(item.name).font(.system(size: 13, weight: .semibold)) Text("\(item.cadence.title) · \(item.count) charges · last \(TransactionRow.shortDay(item.lastDay))").font(.system(size: 11)).foregroundStyle(palette.muted) } Spacer() VStack(alignment: .trailing, spacing: 2) { MoneyText(item.amount, size: 13) Text("next \(TransactionRow.shortDay(item.nextDay))").font(.system(size: 11)).foregroundStyle(palette.muted) } .frame(width: 110, alignment: .trailing) Menu { Button("Confirm recurring") { model.updateWorkspace { $0.recurring[item.id] = .confirmed } } Button("Not recurring") { model.updateWorkspace { $0.recurring[item.id] = .dismissed } } Button("Reset") { model.updateWorkspace { $0.recurring[item.id] = nil } } } label: { Image(systemName: "ellipsis.circle") } .menuStyle(.borderlessButton).menuIndicator(.hidden).frame(width: 28) } .padding(.vertical, 6).padding(.horizontal, palette.ruled ? 0 : 12) .background(palette.ruled ? Color.clear : palette.card, in: RoundedRectangle(cornerRadius: palette.cardRadius, style: .continuous)) .overlay(alignment: .bottom) { if palette.ruled { Rectangle().fill(palette.line).frame(height: 0.5) } } } } struct AgentsScreen: View { @Environment(AppModel.self) private var model @Environment(\.palette) private var palette @State private var client = Client.claudeCode @State private var message: String? @State private var testing = false enum Client: String, CaseIterable { case claudeCode = "Claude Code", codex = "Codex", gemini = "Gemini CLI", claudeChat = "Claude chat" } var body: some View { let ws = model.workspace ScreenScaffold("Agents", subtitle: "Let Claude Code, Codex or Gemini read your money and help tidy it up.") { EmptyView() } content: { VStack(alignment: .leading, spacing: 18) { Panel { VStack(alignment: .leading, spacing: 12) { SectionHeader("Connect an agent") Text("Bucksley runs its own MCP server on this Mac. MCP requests are handled locally.") .font(.system(size: 13)).foregroundStyle(palette.muted).fixedSize(horizontal: false, vertical: true) PillPicker(Client.allCases.map { ($0, $0.rawValue) }, selection: $client) .accessibilityIdentifier("agents.client") clientSetup HStack(spacing: 10) { Button { test() } label: { Label("Test connection", systemImage: "bolt.horizontal") } .buttonStyle(SecondaryButtonStyle()).disabled(testing).accessibilityIdentifier("agents.test") if testing { ProgressView().controlSize(.small) } } if let message { Text(message).font(.system(size: 12)).foregroundStyle(palette.muted).textSelection(.enabled).fixedSize(horizontal: false, vertical: true) } } } .onChange(of: client) { message = nil } Panel { VStack(alignment: .leading, spacing: 10) { SectionHeader("Ask Bucksley") Text("Connecting an agent lets it work in Bucksley from Claude Code, Codex or Gemini. The Ask screen inside Bucksley is separate: pick who answers there.") .font(.system(size: 13)).foregroundStyle(palette.muted).fixedSize(horizontal: false, vertical: true) AskSourcePicker() Text(askNote(ws)).font(.system(size: 12)).foregroundStyle(palette.muted).fixedSize(horizontal: false, vertical: true) } } Panel { VStack(alignment: .leading, spacing: 8) { SectionHeader("Access") Toggle("Let agents make changes (review, recategorize, notes, rules, budgets)", isOn: Binding(get: { ws.settings.agentsCanWrite }, set: { v in model.updateWorkspace { $0.settings.agentsCanWrite = v } })) .font(.system(size: 13)).accessibilityIdentifier("agents.write") Text("Agents can always read. No tool can move money.").font(.system(size: 12)).foregroundStyle(palette.muted) } } Panel { AgentSkillSection() } VStack(alignment: .leading, spacing: 6) { SectionHeader("Agent activity") { Text("\(ws.agentLog.count)").font(.system(size: 12)).foregroundStyle(palette.muted) } if ws.agentLog.isEmpty { Text("Nothing yet. Every change an agent makes shows up here.").font(.system(size: 13)).foregroundStyle(palette.muted).padding(.vertical, 8) } ForEach(ws.agentLog.prefix(100)) { action in HStack(alignment: .firstTextBaseline, spacing: 10) { Text(action.at.formatted(date: .abbreviated, time: .shortened)).font(palette.figure(11, weight: .regular)).foregroundStyle(palette.muted).frame(width: 140, alignment: .leading) Text(action.client).font(.system(size: 12, weight: .semibold)).frame(width: 110, alignment: .leading) Text(action.summary).font(.system(size: 12)) Spacer() Text(action.tool).font(palette.figure(11, weight: .regular)).foregroundStyle(palette.muted) } .padding(.vertical, 5) .overlay(alignment: .bottom) { Rectangle().fill(palette.line).frame(height: 0.5) } } } } } } func askNote(_ ws: Workspace) -> String { let provider = AskBucksley.provider(for: ws.settings) if provider == .apple { return "Ask uses Apple Intelligence, which keeps everything on this Mac. Choosing an agent shows what it would read before anything is sent." } return "Ask uses \(provider.title). Change the model in Settings → Ask." } @ViewBuilder var clientSetup: some View { switch client { case .claudeCode: Text("Save the connection in the folder you open in Claude Code, then start a new session there and approve Bucksley.") .font(.system(size: 13)).fixedSize(horizontal: false, vertical: true) Button { run { try AgentSetup.saveProjectConnection() } } label: { Label("Save project connection…", systemImage: "folder.badge.plus") } .buttonStyle(PrimaryButtonStyle()).accessibilityIdentifier("agents.claudeCode") case .codex: Text("In Codex Settings → MCP servers, add a STDIO server named bucksley. Paste both fields, save, then type /mcp in a new chat.") .font(.system(size: 13)).fixedSize(horizontal: false, vertical: true) copyField("Command", AgentSetup.command) copyField("Argument", AgentSetup.argument) case .gemini: Text("Choose the project folder you use with Gemini CLI. Bucksley adds itself to .gemini/settings.json and keeps your other settings.") .font(.system(size: 13)).fixedSize(horizontal: false, vertical: true) Button { run { try AgentSetup.saveProjectConnection(gemini: true) } } label: { Label("Save Gemini connection…", systemImage: "folder.badge.plus") } .buttonStyle(PrimaryButtonStyle()) case .claudeChat: Text("Save the extension, then in the Claude app open Settings → Extensions → Advanced settings → Install Extension… and choose Bucksley.mcpb.") .font(.system(size: 13)).fixedSize(horizontal: false, vertical: true) Button { run(AgentSetup.saveClaudeExtension) } label: { Label("Save Claude extension…", systemImage: "puzzlepiece.extension") } .buttonStyle(PrimaryButtonStyle()) } } func copyField(_ label: String, _ value: String) -> some View { HStack(spacing: 8) { Text(label).font(.system(size: 12, weight: .semibold)).frame(width: 76, alignment: .leading) Text(value).font(.system(size: 11, design: .monospaced)).textSelection(.enabled).lineLimit(1).truncationMode(.middle).help(value) .padding(.horizontal, 10).padding(.vertical, 6).frame(maxWidth: .infinity, alignment: .leading) .background(palette.canvas, in: RoundedRectangle(cornerRadius: 8)) Button { AgentSetup.copy(value); message = "\(label) copied." } label: { Label("Copy", systemImage: "doc.on.doc") } .buttonStyle(SecondaryButtonStyle()) } } func run(_ action: () throws -> String?) { do { if let result = try action() { message = result } } catch { message = error.localizedDescription } } func test() { testing = true message = nil let mode = model.workspace.settings.agentsCanWrite ? "reading and making changes" : "reading" Task { switch await AgentSetup.testConnection(executable: AgentSetup.command) { case .success(let count): message = "Connection check passed. Bucksley offers \(count) tools, allowed for \(mode)." case .failure(let error): message = "Connection check failed. \(error.localizedDescription)" } testing = false } } } /// The charges worth a second look, likely refunds and transactions Bucksley can file for you. struct WorthALook: View { @Environment(AppModel.self) private var model @Environment(\.palette) private var palette var body: some View { let ledger = model.ledger, insights = model.insights let suggestions = insights.suggestions.values.filter { !$0.categoryID.isEmpty } if !insights.flags.isEmpty || !insights.refunds.isEmpty || !suggestions.isEmpty { Panel { VStack(alignment: .leading, spacing: 10) { Label("Worth a look", systemImage: "eye").font(.system(size: 12, weight: .bold)).foregroundStyle(palette.accent) ForEach(insights.flags.prefix(3)) { flag in if let entry = ledger.entry(flag.entryID) { Button { model.open(entry); model.selection = .transactions } label: { HStack(alignment: .top, spacing: 8) { Image(systemName: symbol(flag.kind)).foregroundStyle(palette.loss).frame(width: 16) Text(flag.reason).font(.system(size: 12)).fixedSize(horizontal: false, vertical: true).multilineTextAlignment(.leading) Spacer(minLength: 0) } .contentShape(Rectangle()) } .buttonStyle(.plain) } } if insights.flags.count > 3 { Text("\(insights.flags.count - 3) more in Transactions → To review").font(.system(size: 11)).foregroundStyle(palette.muted) } if let match = insights.refunds.first, let refund = ledger.entry(match.refundID), let purchase = ledger.entry(match.purchaseID) { HStack(alignment: .top, spacing: 8) { Image(systemName: "arrow.uturn.backward").foregroundStyle(palette.gain).frame(width: 16) VStack(alignment: .leading, spacing: 6) { Text("\(Money.format(refund.amount)) from \(refund.name) looks like a refund of your \(DayKey.short(purchase.day)) purchase.") .font(.system(size: 12)).fixedSize(horizontal: false, vertical: true) Button("Count it as a refund") { model.acceptRefund(match) }.buttonStyle(SecondaryButtonStyle()) } } } if !suggestions.isEmpty { HStack(alignment: .top, spacing: 8) { Image(systemName: "tag").foregroundStyle(palette.accent).frame(width: 16) VStack(alignment: .leading, spacing: 6) { Text("Bucksley can file \(suggestions.count) uncategorized transaction\(suggestions.count == 1 ? "" : "s").") .font(.system(size: 12)).fixedSize(horizontal: false, vertical: true) Button("Review suggestions") { TransactionsFilter.shared.unreviewedOnly = false TransactionsFilter.shared.suggestedOnly = true model.selection = .transactions } .buttonStyle(SecondaryButtonStyle()) } } } } .frame(maxWidth: .infinity, alignment: .leading) } .accessibilityIdentifier("worthALook") } } func symbol(_ kind: Ledger.FlagKind) -> String { switch kind { case .duplicate: "doc.on.doc" case .aboveUsual: "arrow.up.right" case .newMerchant: "sparkle" case .priceChange: "tag" } } }