Intelligence, Advanced Charting & Trading Platform

1 import SwiftUI
2 import WidgetKit
3
4 struct MarketHomeView: View {
5 @StateObject private var model = MarketViewModel()
6
7 var body: some View {
8 TabView {
9 NavigationStack {
10 List {
11 Section {
12 VStack(alignment: .leading, spacing: 8) {
13 Text("ChartNova").font(.title2.bold()).foregroundStyle(.cyan)
14 Text("ویجت چندنمادی آیفون").font(.subheadline)
15 Text("قیمت‌ها در میان آیکون‌های صفحه اصلی نمایش داده می‌شوند. اندازه کوچک ۲، متوسط ۴ و بزرگ ۶ نماد نشان می‌دهد.")
16 .font(.footnote).foregroundStyle(.secondary)
17 }.padding(.vertical, 8)
18 if !WidgetSettingsStore.groupAvailable {
19 Label("اشتراک داده App Group فعال نیست. تنظیمات امضای اپ را بررسی کن.", systemImage: "exclamationmark.triangle")
20 .font(.footnote).foregroundStyle(.red)
21 }
22 }
23 Section("نمادهای انتخابی · حداکثر ۶") {
24 if model.preferences.selectedSymbols.isEmpty {
25 Text("از فهرست نمادها، حداقل یک مورد انتخاب کن.").foregroundStyle(.secondary)
26 }
27 ForEach(model.preferences.selectedSymbols, id: \.self) { symbol in
28 selectedRow(symbol)
29 }
30 .onMove(perform: model.moveSelected)
31 }
32 Section("ظاهر ویجت") {
33 Picker("رنگ‌بندی", selection: Binding(
34 get: { model.preferences.appearance },
35 set: { model.updateAppearance($0) }
36 )) {
37 ForEach(WidgetAppearance.allCases) { theme in
38 Text(theme.title).tag(theme)
39 }
40 }.pickerStyle(.segmented)
41 }
42 Section("وضعیت دریافت داده") {
43 if let feed = model.feed {
44 LabeledContent("منبع", value: feed.source ?? "نامشخص")
45 LabeledContent("زمان دریافت", value: MarketDate.format(feed.fetchedAt))
46 if feed.isStale() {
47 Label("قیمت‌ها ممکن است قدیمی باشند", systemImage: "clock.badge.exclamationmark")
48 .foregroundStyle(.orange)
49 }
50 } else {
51 Text("در انتظار قیمت معتبر").foregroundStyle(.secondary)
52 }
53 if let error = model.errorMessage {
54 Text(error).font(.footnote).foregroundStyle(.orange)
55 }
56 }
57 Section("روش افزودن به صفحه اصلی") {
58 Text("پس از نصب و اجرای اپ: روی فضای خالی Home Screen لمس طولانی کن ← Edit ← Add Widget ← جست‌وجوی ChartNova ← انتخاب اندازه و افزودن.")
59 .font(.footnote)
60 }
61 }
62 .navigationTitle("ویجت چارت‌نوا")
63 .toolbar {
64 ToolbarItem(placement: .topBarLeading) {
65 EditButton()
66 }
67 ToolbarItem(placement: .topBarTrailing) {
68 Button {
69 Task { await model.refresh() }
70 } label: {
71 Image(systemName: "arrow.clockwise")
72 }
73 .disabled(model.refreshing)
74 .accessibilityLabel("تازه‌سازی قیمت")
75 }
76 }
77 }
78 .tabItem { Label("ویجت من", systemImage: "square.grid.2x2") }
79
80 NavigationStack {
81 List {
82 Section("جست‌وجو و انتخاب") {
83 Text("ستاره: علاقه‌مندی · مربع: افزودن به ویجت. محدودیت ۶ نماد.")
84 .font(.footnote).foregroundStyle(.secondary)
85 ForEach(model.filteredMarkets) { instrument in
86 marketRow(instrument)
87 }
88 if model.markets.isEmpty {
89 ContentUnavailableView(
90 "دریافت نمادها",
91 systemImage: "wifi.exclamationmark",
92 description: Text(model.errorMessage ?? "در حال دریافت اطلاعات بازار")
93 )
94 }
95 }
96 }
97 .searchable(text: $model.search, prompt: "نام یا شناسه نماد، مثلاً GOLD18")
98 .navigationTitle("نمادها")
99 }
100 .tabItem { Label("نمادها", systemImage: "star.circle") }
101 }
102 .task { await model.refresh() }
103 }
104
105 @ViewBuilder
106 private func selectedRow(_ symbol: String) -> some View {
107 let quote = model.quote(symbol)
108 HStack(spacing: 12) {
109 Image(systemName: "line.3.horizontal").foregroundStyle(.secondary)
110 VStack(alignment: .leading, spacing: 4) {
111 Text(model.name(symbol)).font(.headline)
112 Text(symbol).font(.caption.monospaced()).foregroundStyle(.secondary)
113 }
114 Spacer()
115 VStack(alignment: .trailing, spacing: 4) {
116 if let quote {
117 Text(quote.price.formatted(.number.precision(.fractionLength(0...2))))
118 .font(.subheadline.bold()).monospacedDigit()
119 Text(quote.quoteLabel).font(.caption2).foregroundStyle(.secondary)
120 } else { Text("—").foregroundStyle(.secondary) }
121 }
122 Button {
123 model.toggleSelection(symbol)
124 } label: {
125 Image(systemName: "minus.circle.fill").foregroundStyle(.red)
126 }
127 .buttonStyle(.borderless)
128 .accessibilityLabel("حذف \(symbol) از ویجت")
129 }
130 }
131
132 private func marketRow(_ instrument: CatalogInstrument) -> some View {
133 let selected = model.preferences.selectedSymbols.contains(instrument.symbol)
134 let favorite = model.preferences.favoriteSymbols.contains(instrument.symbol)
135 return HStack(spacing: 10) {
136 Button {
137 model.toggleFavorite(instrument.symbol)
138 } label: {
139 Image(systemName: favorite ? "star.fill" : "star")
140 .foregroundStyle(favorite ? Color.yellow : Color.secondary)
141 }
142 .buttonStyle(.borderless)
143 .accessibilityLabel(favorite ? "حذف از علاقه‌مندی" : "افزودن به علاقه‌مندی")
144
145 VStack(alignment: .leading, spacing: 3) {
146 Text(instrument.name).font(.subheadline.bold())
147 Text(instrument.symbol).font(.caption2.monospaced()).foregroundStyle(.secondary)
148 }
149 Spacer()
150 VStack(alignment: .trailing, spacing: 2) {
151 Text(instrument.sellPrice.formatted(.number.precision(.fractionLength(0...2))))
152 .font(.subheadline.monospacedDigit())
153 Text(instrument.quote == "IRT" ? "تومان" : instrument.quote)
154 .font(.caption2).foregroundStyle(.secondary)
155 }
156 Button {
157 model.toggleSelection(instrument.symbol)
158 } label: {
159 Image(systemName: selected ? "checkmark.square.fill" : "plus.square")
160 .foregroundStyle(.cyan)
161 }
162 .buttonStyle(.borderless)
163 .disabled(!selected && model.preferences.selectedSymbols.count >= MarketSymbol.maximum)
164 .accessibilityLabel(selected ? "حذف از ویجت" : "افزودن به ویجت")
165 }
166 .padding(.vertical, 3)
167 }
168 }