This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContentView.swift
93 lines (89 loc) · 3.38 KB
/
ContentView.swift
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
//
// ContentView.swift
// crypto
//
// Created by Tiancheng Shuai on 12/3/21.
//
//
import SwiftUI
struct ContentView: View {
@State var cryptos: [crypto_name] = []
@State private var selectedCrypto = "BTC"
@State var firstAppear: Bool = true
@State var price = NSNumber(0)
@State var lastUpdated = NSNumber(0)
@State var lastUpdatedConverted = ""
init(){
/*
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
print("毫无意义的延时")
}
*/
print("程序正在启动...")
}
var body: some View {
NavigationView {
Form {
// Text("test")
Section {
Picker("Select Crypto", selection: $selectedCrypto) {
ForEach(cryptos, id:\.self.symbol) {crypto in
Text(crypto.name + ", " + crypto.symbol)
// Text(crypto.name).tag(String(crypto.symbol))
}
}
HStack {
Text("Crypto Price (USD):")
Spacer()
Text("\(price)")
.padding(.trailing, 0)
}
HStack {
Text("Last Updated:")
Spacer()
Text(lastUpdatedConverted)
.padding(.trailing, 0)
}
// Button("Print cryptos") {
// print(selectedCrypto)
// }
// Button("Get crypto price") {
// APIRequest.get_crypto_price(symbol: selectedCrypto){ res in
// lastUpdated = res["last_updated"]
// price = res["price"]
// }
// }
}
}
.onAppear {
if firstAppear {
print("只执行一次,不要重复!")
APIRequest.get_crypto_list {res in
for i in 0 ..< res.count {
cryptos.append(res[i])
}
print(cryptos.count, "cryptos loaded.")
}
firstAppear = false
}
APIRequest.get_crypto_price(symbol: selectedCrypto){ res in
print("selected_crypto:", selectedCrypto)
print("last_update:", res["last_updated"] as Any)
print("price:", res["price"] as Any)
price = res["price"] as! NSNumber
lastUpdated = res["last_updated"] as! NSNumber
let epocTime = TimeInterval(truncating: lastUpdated)
// lastUpdatedConverted = "\(NSDate(timeIntervalSince1970: epocTime))"
lastUpdatedConverted = NSDate(timeIntervalSince1970: epocTime).description(with: TimeZone.current.abbreviation())
print(NSDate(timeIntervalSince1970: epocTime).description(with: TimeZone.current.abbreviation()))
}
}
.navigationBarTitle(Text("CryptosHelper"), displayMode: .inline)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}