-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGoldPriceView.swift
More file actions
183 lines (156 loc) · 6.75 KB
/
GoldPriceView.swift
File metadata and controls
183 lines (156 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import SwiftUI
struct GoldPriceView: View {
@ObservedObject var dataService: GoldPriceService
private let dateFormatter: DateFormatter
init(dataService: GoldPriceService) {
self.dataService = dataService
// 初始化日期格式化器
dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
}
var body: some View {
VStack(alignment: .leading, spacing: 16) {
// 标题
Text("黄金价格")
.font(.system(size: 18, weight: .bold))
.foregroundColor(.black)
.frame(maxWidth: .infinity, alignment: .center)
.padding(.top, 8)
Divider()
// 价格信息
HStack {
Text("当前价格:")
.font(.system(size: 14))
.foregroundColor(.gray)
Spacer()
if let isAvailable = dataService.allSourcePriceAvailability[dataService.currentSource],
isAvailable,
let price = dataService.allSourcePrices[dataService.currentSource] {
// 根据数据源类型决定显示格式,界面中不需要固定宽度
let formatString = dataService.currentSource == .jdZsFinance || dataService.currentSource == .jdMsFinance ? "G%.2f" : "G%.0f"
Text(String(format: formatString, price))
.font(.system(size: 20, weight: .bold))
.foregroundColor(.black)
} else {
Text("G0.00")
.font(.system(size: 20, weight: .bold))
.foregroundColor(.gray)
}
}
.padding(.horizontal, 16)
// 数据源信息
HStack {
Text("数据来源:")
.font(.system(size: 14))
.foregroundColor(.gray)
Spacer()
Text(dataService.currentSource.rawValue)
.font(.system(size: 14))
.foregroundColor(.black)
}
.padding(.horizontal, 16)
if dataService.currentSource == .shuibeiGold && !dataService.shuibeiDetailPrices.isEmpty {
Divider()
Text("水贝市场详情:")
.font(.system(size: 14))
.foregroundColor(.gray)
.padding(.horizontal, 16)
ScrollView {
LazyVStack(spacing: 4) {
ForEach(dataService.shuibeiDetailPrices) { item in
HStack {
Text(item.name)
.font(.system(size: 13))
.foregroundColor(.black)
Spacer()
Text("\(Int(item.price))")
.font(.system(size: 13, weight: .bold))
.foregroundColor(.black)
}
.padding(.horizontal, 16)
.padding(.vertical, 2)
}
}
}
.frame(maxHeight: 100)
}
Divider()
// 数据源选择
Text("切换数据源:")
.font(.system(size: 14))
.foregroundColor(.gray)
.padding(.horizontal, 16)
ScrollView {
LazyVStack(spacing: 4) {
ForEach(GoldPriceSource.allCases, id: \.self) { source in
Button(action: {
dataService.setDataSource(source)
}) {
HStack {
Text(source.rawValue)
.font(.system(size: 14))
.foregroundColor(.black)
Spacer()
if dataService.currentSource == source {
Image(systemName: "checkmark")
.foregroundColor(.blue)
}
}
.padding(.horizontal, 16)
.padding(.vertical, 8)
.background(dataService.currentSource == source ? Color.blue.opacity(0.1) : Color.clear)
.cornerRadius(4)
}
.buttonStyle(PlainButtonStyle())
}
}
}
.frame(maxHeight: 200)
Spacer()
// 刷新按钮
Button(action: {
dataService.fetchGoldPrice()
}) {
HStack {
Spacer()
if dataService.isLoading {
ProgressView()
.scaleEffect(0.8)
.padding(.trailing, 8)
} else {
Image(systemName: "arrow.clockwise")
.foregroundColor(.white)
.padding(.trailing, 8)
}
Text("刷新")
.font(.system(size: 14, weight: .medium))
.foregroundColor(.white)
Spacer()
}
.padding(.vertical, 8)
.background(Color.blue)
.cornerRadius(8)
.padding(.horizontal, 16)
}
.buttonStyle(PlainButtonStyle())
.padding(.bottom, 8)
// 全局更新时间(水贝和品牌金店)
Divider()
if let nonJDTime = dataService.lastNonJDUpdateTime {
Text("更新时间: \(dateFormatter.string(from: nonJDTime))")
.font(.system(size: 11))
.foregroundColor(.gray)
.frame(maxWidth: .infinity, alignment: .center)
.padding(.bottom, 8)
}
}
.frame(width: 300, height: 450)
.background(Color.white)
}
}
// 预览
struct GoldPriceView_Previews: PreviewProvider {
static var previews: some View {
GoldPriceView(dataService: GoldPriceService())
}
}