comparison LazyBear/Views/Company/Helpers/ChartHelper.swift @ 440:01fa77358b82

Fixes #47
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Sun, 20 Jun 2021 16:58:36 +0200
parents aa1f4b614b2b
children 417148200aaf
comparison
equal deleted inserted replaced
439:aa1f4b614b2b 440:01fa77358b82
4 // 4 //
5 // Created by Dennis Concepción Martín on 20/6/21. 5 // Created by Dennis Concepción Martín on 20/6/21.
6 // 6 //
7 7
8 import SwiftUI 8 import SwiftUI
9 import StockCharts
9 10
10 struct ChartHelper: View { 11 struct ChartHelper: View {
11 var quote: [QuoteModel]? 12 @ObservedObject var company: Company
12 var historicalPrices: [HistoricalPricesModel]?
13 13
14 var body: some View { 14 var body: some View {
15 CustomRectangleBox() 15 CustomRectangleBox()
16 .frame(height: 270) 16 .frame(height: 270)
17 .padding(.horizontal)
18 .overlay( 17 .overlay(
19 VStack { 18 VStack {
20 if let quote = quote?.first { 19 if let quote = company.data.quote?.first {
21 HStack(alignment: .center) { 20 HStack(alignment: .center) {
22 Text("\(quote.latestPrice ?? 0, specifier: "%.2f")") 21 Text("\(quote.latestPrice ?? 0, specifier: "%.2f")")
23 .foregroundColor(quote.changePercent ?? 0 < 0 ? .red: .green) 22 .foregroundColor(quote.changePercent ?? 0 < 0 ? .red: .green)
24 .fontWeight(.semibold) 23 .fontWeight(.semibold)
25 24
26 Text("\(quote.changePercent ?? 0 * 100, specifier: "%.2f")%") 25 Text("\(quote.changePercent ?? 0 * 100, specifier: "%.2f")%")
27 .foregroundColor(quote.changePercent ?? 0 < 0 ? .red: .green) 26 .foregroundColor(quote.changePercent ?? 0 < 0 ? .red: .green)
28 .font(.callout) 27 .font(.callout)
29 .fontWeight(.semibold) 28 .fontWeight(.semibold)
29
30 Spacer()
30 } 31 }
31 .padding(.top) 32 .padding()
33
34 if let historicalPrices = company.data.historicalPrices {
35 let prices = historicalPrices.compactMap { $0.close }
36 let dates = historicalPrices.compactMap { $0.date }
37 if company.showChart {
38 LineChartView(data: prices, dates: dates, hours: nil, dragGesture: true)
39 } else {
40 Spacer()
41 ProgressView()
42 Spacer()
43 }
44 }
32 } 45 }
46
47 Spacer()
33 } 48 }
34 ) 49 )
50 .padding(.horizontal)
35 } 51 }
36 } 52 }
37 53
38 struct ChartHelper_Previews: PreviewProvider { 54 struct ChartHelper_Previews: PreviewProvider {
39 static var previews: some View { 55 static var previews: some View {
40 ChartHelper( 56 ChartHelper(company: Company())
41 quote: [
42 QuoteModel(companyName: "apple inc", latestPrice: 120.3, changePercent: 0.03)
43 ],
44 historicalPrices: [
45 HistoricalPricesModel(close: 120.3, date: "2020-01-01", minute: nil)
46 ]
47 )
48 } 57 }
49 } 58 }