SPB Git

Repository Public

MarketsViews.swift 7.2 KB 129 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Features/Markets/MarketsViews.swift3// Description: Markets (indices, movers, exchange hours) — quotes from the server's data proxy, each with its freshness. The Overview and the Watchlists of 2.0 live in Features/Terminal.4import SwiftUI5import VQuantCore67// MARK: - Shared with Markets (the Overview of 2.0 is Features/Terminal/OverviewView.swift)89struct IndexStrip: View {10    let app: AppEnvironment11    let model: WindowModel12    var limit = 1213    var body: some View {14        LazyVGrid(columns: [GridItem(.adaptive(minimum: 170), spacing: 10)], spacing: 10) {15            ForEach(app.market.indices.prefix(limit), id: \.quote.symbol) { e in16                Button { model.show(.charting, symbol: e.quote.symbol) } label: {17                    VStack(alignment: .leading, spacing: 3) {18                        Text(e.quote.name).font(.caption).foregroundStyle(.secondary).lineLimit(1)19                        Text(Fmt.price(e.quote.price)).font(.system(.title3, design: .rounded).weight(.semibold)).monospacedDigit()20                        HStack(spacing: 6) {21                            ChangeText(value: e.quote.change, percent: false, settings: app.settings)22                            ChangeText(value: e.quote.changePercent, settings: app.settings)23                        }.font(.caption)24                        FreshnessBadge(freshness: e.freshness, compact: true)25                    }26                    .padding(10).frame(maxWidth: .infinity, alignment: .leading)27                    .background(Palette.panel, in: RoundedRectangle(cornerRadius: 8))28                    .overlay(RoundedRectangle(cornerRadius: 8).strokeBorder(Palette.hairline))29                    .contentShape(Rectangle())30                }31                .buttonStyle(.plain)32                .accessibilityLabel("\(e.quote.name), \(Fmt.price(e.quote.price)), \(Fmt.percent(e.quote.changePercent))")33            }34        }35        .overlay { if app.market.indices.isEmpty, app.market.lastError == nil { ProgressView() } }36    }37}3839struct QuoteRow: View {40    let symbol: String41    let entry: QuoteEntry?42    let unknown: Bool43    let settings: SettingsStore44    var body: some View {45        HStack {46            Text(symbol).font(.system(.body, design: .monospaced).weight(.medium)).frame(width: 84, alignment: .leading)47            Text(entry?.quote.name ?? (unknown ? "No quote for this symbol" : "")).foregroundStyle(.secondary).lineLimit(1)48            Spacer()49            if let e = entry {50                Text(Fmt.price(e.quote.price)).monospacedDigit()51                ChangeText(value: e.quote.changePercent, settings: settings).frame(width: 78, alignment: .trailing)52                FreshnessBadge(freshness: e.freshness, compact: true).frame(width: 74, alignment: .leading)53            } else if unknown {54                FreshnessBadge(freshness: .unavailable("The data provider has no quote for this symbol."), compact: true)55            } else { ProgressView().controlSize(.mini) }56        }57        .contentShape(Rectangle())58    }59}6061// MARK: - Markets6263struct MarketsView: View {64    let app: AppEnvironment65    let model: WindowModel66    @State private var mover: Mover = .gainers67    @State private var selection: Quote.ID?68    @State private var order = [KeyPathComparator(\Quote.symbol)]69    @State private var filter = ""7071    var body: some View {72        VStack(spacing: 0) {73            ScrollView {74                VStack(alignment: .leading, spacing: 14) {75                    IndexStrip(app: app, model: model)76                    Panel("Exchanges") {77                        let rows = app.market.hours.filter { ["NASDAQ", "NYSE", "TSX", "LSE", "XETRA", "EURONEXT", "JPX", "HKSE"].contains($0.exchange) }78                        LazyVGrid(columns: [GridItem(.adaptive(minimum: 220), spacing: 8)], alignment: .leading, spacing: 6) {79                            ForEach(rows) { h in80                                HStack(spacing: 6) {81                                    Circle().fill(h.isMarketOpen ? Palette.green : Color.secondary.opacity(0.5)).frame(width: 7, height: 7)82                                    Text(h.exchange).font(.callout.weight(.medium))83                                    Text(h.isMarketOpen ? "open" : "closed").font(.caption).foregroundStyle(.secondary)84                                    Spacer()85                                    Text("\(h.openingHour ?? "") – \(h.closingHour ?? "")").font(.caption2).foregroundStyle(.tertiary).lineLimit(1)86                                }87                            }88                        }89                        if rows.isEmpty { Text("Exchange hours are not loaded.").font(.callout).foregroundStyle(.secondary) }90                    }91                }92                .padding(16)93            }94            .frame(maxHeight: 330)95            Divider()96            HStack {97                Picker("", selection: $mover) { ForEach(Mover.allCases) { Text($0.title).tag($0) } }.pickerStyle(.segmented).labelsHidden().frame(width: 320)98                TextField("Filter", text: $filter).textFieldStyle(.roundedBorder).frame(width: 180)99                Spacer()100                if let t = app.market.moversFetched { Text("Received \(Fmt.time(t))").font(.caption).foregroundStyle(.secondary) }101            }102            .padding(.horizontal, 12).padding(.vertical, 8)103            Table(rows, selection: $selection, sortOrder: $order) {104                TableColumn("Symbol", value: \.symbol) { Text($0.symbol).font(.system(.body, design: .monospaced)) }.width(min: 70, ideal: 90)105                TableColumn("Name", value: \.name)106                TableColumn("Price", value: \.price) { Text(Fmt.price($0.price)).monospacedDigit().frame(maxWidth: .infinity, alignment: .trailing) }.width(min: 80, ideal: 100)107                TableColumn("Change") { q in Text(Fmt.signed(q.change)).monospacedDigit().foregroundStyle(app.settings.changeColor(q.change)).frame(maxWidth: .infinity, alignment: .trailing) }.width(min: 70, ideal: 90)108                TableColumn("%") { q in Text(Fmt.percent(q.changePercent)).monospacedDigit().foregroundStyle(app.settings.changeColor(q.changePercent)).frame(maxWidth: .infinity, alignment: .trailing) }.width(min: 70, ideal: 90)109                TableColumn("Exchange") { Text($0.exchange ?? "") }.width(min: 70, ideal: 90)110            }111            .contextMenu(forSelectionType: Quote.ID.self) { ids in112                if let s = ids.first {113                    Button("Chart \(s)") { model.show(.charting, symbol: s) }114                    Button("Explore Data for \(s)") { model.show(.data, symbol: s) }115                    WatchlistAddMenu(app: app, symbol: s)116                    Button("Statements") { model.show(.statements, symbol: s) }117                }118            } primaryAction: { ids in if let s = ids.first { model.show(.charting, symbol: s) } }119        }120        .task { await app.market.loadMovers() }121    }122123    private var rows: [Quote] {124        let all = app.market.movers[mover] ?? []125        let f = filter.trimmingCharacters(in: .whitespaces)126        return (f.isEmpty ? all : all.filter { $0.symbol.localizedCaseInsensitiveContains(f) || $0.name.localizedCaseInsensitiveContains(f) }).sorted(using: order)127    }128}129
Files main
Menu

Theme