Intelligence, Advanced Charting & Trading Platform
feature/chartnova-multimarket-chart-ws-foundation
diagnostics/chartnova-blank-screen-2026-10-15
diagnostics/chartnova-stage2-v27-preview-2026-10-15
docs/chartnova-stage1-reconcile-manifest-2026-10-15
feature/chartnova-ios-widget
feature/chartnova-light-catalog-lazy-series-2026-10-15
feature/chartnova-market-terminal
feature/chartnova-multimarket-chart-ws-foundation
feature/chartnova-neon-direct-history
feature/chartnova-neon-history
feature/paper-trading-risk-controls-2026-10-14
fix/alpha-vantage-live-payload
fix/chartnova-progressive-loading-2026-10-15
fix/live-proof-runner-cwd-2026-10-14
foundation/phase-0
foundation/postgres-core
main
ops/chartnova-emergency-rollback-2026-10-15
phase-1/candle-builder
phase-1/data-proof-artifact
phase-1/data-proof-metrics
phase-1/data-proof-report
phase-1/external-gates-state
phase-1/gold-api-fallback
phase-1/ingestion-pipeline
phase-1/live-proof-runner
phase-1/market-calendar
phase-1/market-domain
phase-1/neon-live-proof
phase-1/persistence-contract
phase-1/postgres-ci
phase-1/provider-adapters
phase-1/provider-recovery
phase-1/quality-propagation
phase-1/spike-detection
phase-1/xau-provider-adapter
risk/chartnova-alert-runtime-atomic-2026-10-22
risk/chartnova-atomic-outbox-2026-10-22
risk/chartnova-seven-track-safety-2026-10-21
safety/p0-triage-2026-10-13
| 1 | import SwiftUI |
| 2 | import WidgetKit |
| 3 | |
| 4 | @MainActor |
| 5 | final class MarketViewModel: ObservableObject { |
| 6 | @Published private(set) var markets: [CatalogInstrument] = [] |
| 7 | @Published private(set) var feed: WidgetFeed? |
| 8 | @Published private(set) var preferences: WidgetPreferences = WidgetSettingsStore.load() |
| 9 | @Published private(set) var refreshing = false |
| 10 | @Published var search = "" |
| 11 | @Published var errorMessage: String? |
| 12 | |
| 13 | var filteredMarkets: [CatalogInstrument] { |
| 14 | let query = search.trimmingCharacters(in: .whitespacesAndNewlines) |
| 15 | let ordered = markets.sorted { |
| 16 | let favA = preferences.favoriteSymbols.contains($0.symbol) |
| 17 | let favB = preferences.favoriteSymbols.contains($1.symbol) |
| 18 | return favA == favB ? $0.symbol < $1.symbol : favA |
| 19 | } |
| 20 | if query.isEmpty { return ordered } |
| 21 | return ordered.filter { |
| 22 | $0.symbol.localizedCaseInsensitiveContains(query) || |
| 23 | $0.name.localizedCaseInsensitiveContains(query) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func quote(_ symbol: String) -> MarketQuote? { |
| 28 | feed?.items.first(where: { $0.symbol == symbol }) |
| 29 | } |
| 30 | |
| 31 | func name(_ symbol: String) -> String { |
| 32 | markets.first(where: { $0.symbol == symbol })?.name ?? symbol |
| 33 | } |
| 34 | |
| 35 | func refresh() async { |
| 36 | guard !refreshing else { return } |
| 37 | refreshing = true |
| 38 | defer { refreshing = false } |
| 39 | errorMessage = nil |
| 40 | |
| 41 | do { |
| 42 | markets = try await MarketAPI().fetchCatalog() |
| 43 | } catch { |
| 44 | errorMessage = "دریافت فهرست نمادها ناموفق بود: \(error.localizedDescription)" |
| 45 | } |
| 46 | do { |
| 47 | let selected = preferences.selectedSymbols |
| 48 | if !selected.isEmpty { |
| 49 | feed = try await MarketAPI().fetchWidget(symbols: selected) |
| 50 | if let feed { WidgetQuoteCache.save(feed) } |
| 51 | } else { feed = nil } |
| 52 | } catch { |
| 53 | feed = WidgetQuoteCache.load() |
| 54 | let detail = "قیمت تازه دریافت نشد؛ در صورت وجود آخرین داده ذخیرهشده نمایش داده میشود." |
| 55 | errorMessage = [errorMessage, detail].compactMap { $0 }.joined(separator: "\n") |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func toggleFavorite(_ symbol: String) { |
| 60 | preferences.toggleFavorite(symbol) |
| 61 | persist() |
| 62 | } |
| 63 | |
| 64 | func toggleSelection(_ symbol: String) { |
| 65 | preferences.toggleSelection(symbol) |
| 66 | persist() |
| 67 | Task { await refreshQuotesOnly() } |
| 68 | } |
| 69 | |
| 70 | func updateAppearance(_ value: WidgetAppearance) { |
| 71 | preferences.appearance = value |
| 72 | persist() |
| 73 | } |
| 74 | |
| 75 | func moveSelected(from indices: IndexSet, to destination: Int) { |
| 76 | preferences.selectedSymbols.move(fromOffsets: indices, toOffset: destination) |
| 77 | persist() |
| 78 | } |
| 79 | |
| 80 | private func persist() { |
| 81 | WidgetSettingsStore.save(preferences) |
| 82 | WidgetCenter.shared.reloadTimelines(ofKind: "ChartNovaMultiAssetWidget") |
| 83 | } |
| 84 | |
| 85 | private func refreshQuotesOnly() async { |
| 86 | let symbols = preferences.selectedSymbols |
| 87 | if symbols.isEmpty { feed = nil; return } |
| 88 | do { |
| 89 | let result = try await MarketAPI().fetchWidget(symbols: symbols) |
| 90 | feed = result |
| 91 | WidgetQuoteCache.save(result) |
| 92 | } catch { |
| 93 | feed = WidgetQuoteCache.load() |
| 94 | } |
| 95 | WidgetCenter.shared.reloadTimelines(ofKind: "ChartNovaMultiAssetWidget") |
| 96 | } |
| 97 | } |