comparison LazyBear/Helpers/WatchlistRow.swift @ 465:6953d83060a4

New design
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Sat, 17 Jul 2021 17:58:57 +0100
parents
children
comparison
equal deleted inserted replaced
464:04e430ef254a 465:6953d83060a4
1 //
2 // WatchlistRow.swift
3 // lazybear
4 //
5 // Created by Dennis Concepción Martín on 17/07/2021.
6 //
7
8 import SwiftUI
9 import Alamofire
10
11 struct WatchlistRow: View {
12 var symbol: String
13 @State private var companyQuote = CompanyQuoteModel()
14 @State private var showingRow = false
15
16 var body: some View {
17 HStack {
18 if showingRow {
19 VStack(alignment: .leading) {
20 Text("\(symbol)")
21 .font(.headline)
22
23 Text("\(companyQuote.companyName!)")
24 .font(.subheadline)
25 .foregroundColor(.secondary)
26 }
27
28 Spacer()
29 VStack(alignment: .trailing) {
30 Text("\(companyQuote.latestPrice!, specifier: "%.2f")")
31 .font(.headline)
32
33 Text("\(companyQuote.change!, specifier: "%.2f") \(companyQuote.changePercent!*100, specifier: "%.2f")%")
34 .font(.subheadline)
35 .foregroundColor(generateColour())
36 }
37 } else {
38 ProgressView()
39 }
40 }
41 .onAppear(perform: requestApi)
42 }
43
44 private func generateColour() -> Color {
45 if companyQuote.change! >= 0 {
46 return Color(.systemGreen)
47 } else {
48 return Color(.systemRed)
49 }
50 }
51
52 private func requestApi() {
53 let url = "https://cloud.iexapis.com/stable/stock/\(symbol)/quote?token=pk_58fd944e924e4a70acf8635bc335cec4"
54 AF.request(url).responseDecodable(of: CompanyQuoteModel.self) { response in
55 if let value = response.value {
56 self.companyQuote = value
57 self.showingRow = true
58 }
59 else {
60 // Handle error
61 }
62 }
63 }
64 }
65
66 struct WatchlistRow_Previews: PreviewProvider {
67 static var previews: some View {
68 WatchlistRow(symbol: "AAPL")
69 }
70 }