comparison LazyBear/Views/Global Helpers/StockItem.swift @ 342:a6c49f1409f3

Implementing Watchlists
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Mon, 05 Apr 2021 20:08:19 +0200
parents
children ab909fc9ce55
comparison
equal deleted inserted replaced
341:4e6c47a81b80 342:a6c49f1409f3
1 //
2 // StockItem.swift
3 // LazyBear
4 //
5 // Created by Dennis Concepción Martín on 28/3/21.
6 //
7
8 import SwiftUI
9
10 struct StockItem: View {
11 var company: CompanyQuoteModel
12 @State private var intradayPrices = [IntradayPricesModel]()
13
14 private let baseUrl = Bundle.main.infoDictionary?["IEX_URL"] as? String ?? "Empty url"
15 private let apiKey = Bundle.main.infoDictionary?["IEX_API"] as? String ?? "Empty key"
16
17 var body: some View {
18 RoundedRectangle(cornerRadius: 20)
19 .foregroundColor(Color(.secondarySystemBackground))
20 .aspectRatio(0.8, contentMode: .fit)
21 .clipShape(RoundedRectangle(cornerRadius: 20))
22 .overlay(
23 VStack(alignment: .leading) {
24 Group {
25 Text(company.symbol.uppercased())
26 .fontWeight(.semibold)
27 .padding(.top)
28
29 Text(company.companyName.capitalized)
30 .font(.callout)
31 .fontWeight(.semibold)
32 .opacity(0.6)
33 .lineLimit(1)
34
35 PriceView(latestPrice: company.latestPrice, changePercent: company.changePercent)
36
37 }
38 .padding(.horizontal)
39
40 Spacer()
41
42 let prices = intradayPrices.compactMap { $0.open }
43 if prices.isEmpty {
44 Text("No data available")
45 .font(.caption)
46 .opacity(0.6)
47
48 Spacer()
49 } else {
50 LineView(data: prices)
51 .foregroundColor(company.changePercent < 0 ? .red: .green)
52 .padding(.vertical)
53 .clipped()
54 }
55
56 }
57 ,alignment: .leading
58 )
59 .onAppear { requestIntradayPrices(company.symbol) }
60 }
61
62 private func requestIntradayPrices(_ symbol: String) {
63 let url = "\(baseUrl)/stock/\(symbol)/intraday-prices?token=\(apiKey)"
64 genericRequest(url: url, model: [IntradayPricesModel].self) {
65 self.intradayPrices = $0
66 }
67 }
68 }
69
70 struct StockItem_Previews: PreviewProvider {
71 static var previews: some View {
72 StockItem(company: CompanyQuoteModel(companyName: "Akumin Inc", symbol: "AKU", latestPrice: 120.30, changePercent: 0.03))
73
74 }
75 }