Intelligence, Advanced Charting & Trading Platform

1 import SwiftUI
2 import WidgetKit
3
4 struct ChartNovaWidgetView: View {
5 @Environment(\.widgetFamily) private var family
6 let entry: ChartNovaTimelineEntry
7
8 private var capacity: Int {
9 switch family {
10 case .systemSmall: return 2
11 case .systemMedium: return 4
12 case .systemLarge: return 6
13 default: return 2
14 }
15 }
16
17 private var columns: [GridItem] {
18 family == .systemSmall
19 ? [GridItem(.flexible(), spacing: 6)]
20 : [GridItem(.flexible(), spacing: 7), GridItem(.flexible(), spacing: 7)]
21 }
22
23 private var background: [Color] {
24 switch entry.appearance {
25 case .glass: return [Color(red: 0.89, green: 0.99, blue: 0.98), Color(red: 0.83, green: 0.93, blue: 0.97)]
26 case .dark: return [Color(red: 0.07, green: 0.15, blue: 0.23), Color(red: 0.04, green: 0.09, blue: 0.15)]
27 case .light: return [.white, Color(red: 0.94, green: 0.96, blue: 0.99)]
28 }
29 }
30
31 private var foreground: Color { entry.appearance == .dark ? .white : Color(red: 0.09, green: 0.23, blue: 0.29) }
32 private var subdued: Color { entry.appearance == .dark ? Color(red: 0.66, green: 0.78, blue: 0.82) : Color(red: 0.33, green: 0.48, blue: 0.53) }
33 private var accent: Color { entry.appearance == .dark ? Color(red: 0.45, green: 0.91, blue: 0.86) : Color(red: 0.02, green: 0.58, blue: 0.64) }
34
35 var body: some View {
36 VStack(alignment: .leading, spacing: family == .systemSmall ? 5 : 9) {
37 header
38 if entry.selectedSymbols.isEmpty {
39 Text("نمادها را در اپ ChartNova انتخاب کن.")
40 .font(.caption2).foregroundStyle(subdued)
41 Spacer(minLength: 0)
42 } else {
43 LazyVGrid(columns: columns, alignment: .leading, spacing: family == .systemSmall ? 7 : 10) {
44 ForEach(Array(entry.selectedSymbols.prefix(capacity)), id: \.self) { symbol in
45 quoteCell(symbol)
46 }
47 }
48 Spacer(minLength: 0)
49 }
50 footer
51 }
52 .padding(family == .systemSmall ? 11 : 14)
53 .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
54 .environment(\.layoutDirection, .rightToLeft)
55 .containerBackground(for: .widget) {
56 LinearGradient(colors: background, startPoint: .topLeading, endPoint: .bottomTrailing)
57 }
58 .widgetURL(URL(string: "chartnova://widget"))
59 }
60
61 private var header: some View {
62 HStack(spacing: 4) {
63 Image(systemName: "chart.bar.fill").font(.system(size: family == .systemSmall ? 12 : 15))
64 Text("ChartNova").font(.system(size: family == .systemSmall ? 11 : 14, weight: .bold))
65 Spacer(minLength: 0)
66 if entry.stale {
67 Image(systemName: "clock.badge.exclamationmark")
68 .font(.system(size: 10))
69 .accessibilityLabel("قیمت قدیمی")
70 } else {
71 Circle().fill(Color.green).frame(width: 6, height: 6)
72 }
73 }
74 .foregroundStyle(accent)
75 }
76
77 private func quoteCell(_ symbol: String) -> some View {
78 let quote = entry.quote(for: symbol)
79 return VStack(alignment: .leading, spacing: family == .systemSmall ? 2 : 3) {
80 Text(quote?.name ?? symbol)
81 .font(.system(size: family == .systemSmall ? 10 : 11, weight: .semibold))
82 .lineLimit(1)
83 .foregroundStyle(foreground)
84
85 HStack(alignment: .firstTextBaseline, spacing: 3) {
86 Text(quote.map { formatPrice($0.price) } ?? "—")
87 .font(.system(size: family == .systemSmall ? 13 : 15, weight: .bold, design: .rounded))
88 .lineLimit(1)
89 .minimumScaleFactor(0.65)
90 .monospacedDigit()
91 .foregroundStyle(foreground)
92 Spacer(minLength: 0)
93 }
94 HStack(spacing: 4) {
95 Text(quote?.quoteLabel ?? "—")
96 .font(.system(size: 8))
97 .foregroundStyle(subdued)
98 .lineLimit(1)
99 if let change = quote?.changePercent {
100 Text(String(format: "%+.2f%%", change))
101 .font(.system(size: 9, weight: .semibold))
102 .foregroundStyle(change >= 0 ? Color.green : Color.red)
103 .lineLimit(1)
104 }
105 }
106 }
107 .frame(maxWidth: .infinity, alignment: .leading)
108 .accessibilityElement(children: .combine)
109 .accessibilityLabel("\(quote?.name ?? symbol)، \(quote.map { formatPrice($0.price) } ?? "ناموجود") \(quote?.quoteLabel ?? "")")
110 }
111
112 private var footer: some View {
113 HStack(spacing: 4) {
114 Text(entry.usedOfflineCache ? "آفلاین · آخرین قیمت ذخیره‌شده"
115 : entry.stale ? "داده قدیمی · غیرلحظه‌ای"
116 : "آخرین نرخ منبع · غیرلحظه‌ای")
117 .lineLimit(1).minimumScaleFactor(0.72)
118 Spacer(minLength: 0)
119 if family != .systemSmall {
120 Text(MarketDate.format(entry.fetchedAt))
121 .lineLimit(1)
122 }
123 }
124 .font(.system(size: 8))
125 .foregroundStyle(subdued)
126 }
127
128 private func formatPrice(_ value: Double) -> String {
129 value.formatted(.number.precision(.fractionLength(0...2)))
130 }
131 }