comparison LazyBear/Views/Global Helpers/StockRow.swift @ 375:f3cb5bdea8e5

Update Codable requests in HomeView
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Wed, 21 Apr 2021 16:19:50 +0200
parents eb97439e46cd
children a7e2c5a7b4f6
comparison
equal deleted inserted replaced
374:d402bfa367c2 375:f3cb5bdea8e5
8 import SwiftUI 8 import SwiftUI
9 9
10 10
11 struct StockRow: View { 11 struct StockRow: View {
12 var listName: String 12 var listName: String
13 var list: [QuoteModel] 13 var list: [String: QuoteModel]
14 var nestedIntradayPrices: [String: NestedIntradayPricesModel]? 14 var intradayPrices: [String: [IntradayPriceModel]]?
15 15
16 @State private var showExtensiveList = false 16 @State private var showExtensiveList = false
17 17
18 var body: some View { 18 var body: some View {
19 VStack(alignment: .leading) { 19 VStack(alignment: .leading) {
20 HStack(alignment: .bottom) { 20 HStack(alignment: .bottom) {
21 VStack(alignment: .leading) { 21 VStack(alignment: .leading) {
22 Text(adaptTitle(listName)) 22 Text(listName)
23 .font(.title3) 23 .font(.title3)
24 .fontWeight(.semibold) 24 .fontWeight(.semibold)
25 .padding([.top, .horizontal]) 25 .padding([.top, .horizontal])
26 26
27 Text("Real-time quotes") 27 Text("Real-time quotes")
36 .padding(.horizontal) 36 .padding(.horizontal)
37 } 37 }
38 38
39 ScrollView(.horizontal, showsIndicators: false) { 39 ScrollView(.horizontal, showsIndicators: false) {
40 HStack(spacing: 20) { 40 HStack(spacing: 20) {
41 ForEach(list, id: \.self) { company in 41 ForEach(Array(list.keys.sorted()), id: \.self) { companySymbol in
42 if let intradayPrices = nestedIntradayPrices?[company.symbol.uppercased()] { 42 StockItem(symbol: companySymbol, company: list[companySymbol]!, intradayPrices: intradayPrices?[companySymbol], orientation: .vertical)
43 StockItem(company: company, intradayPrices: intradayPrices.nestedIntradayPrices, orientation: .vertical)
44 } else {
45 StockItem(company: company, intradayPrices: nil, orientation: .vertical)
46 }
47 } 43 }
48 } 44 }
49 .padding() 45 .padding()
50 } 46 }
51 .frame(height: 250) 47 .frame(height: 250)
52 } 48 }
53 .padding(.bottom) 49 .padding(.bottom)
54 .sheet(isPresented: $showExtensiveList) { 50 .sheet(isPresented: $showExtensiveList) {
55 ExtensiveList(listName: adaptTitle(listName), list: list, nestedIntradayPrices: nestedIntradayPrices, latestCurrencies: nil) 51 ExtensiveList(listName: listName, list: list, intradayPrices: intradayPrices, latestCurrencies: nil)
56 } 52 }
57 }
58 }
59
60 private func adaptTitle(_ listType: String) -> String {
61 if listType == "mostactive" {
62 return "Most active"
63 } else {
64 return listType.capitalized
65 } 53 }
66 } 54 }
67 55
68 56
69 struct StockRectangleRow_Previews: PreviewProvider { 57 struct StockRectangleRow_Previews: PreviewProvider {
70 static var previews: some View { 58 static var previews: some View {
71 StockRow( 59 StockRow(
72 listName: "mostactive", 60 listName: "Gainers",
73 list: [QuoteModel(companyName: "apple inc", symbol: "aapl", latestPrice: 130.3, changePercent: 0.03)], 61 list: ["AAPL": QuoteModel(changePercent: 0.03, companyName: "Apple Inc", latestPrice: 130.3)],
74 nestedIntradayPrices: ["AAPL": NestedIntradayPricesModel(nestedIntradayPrices: [IntradayPricesModel(open: 130.3)])] 62 intradayPrices: ["AAPL": [IntradayPriceModel(open: 130.2)]]
75 ) 63 )
76 } 64 }
77 } 65 }