Mercurial > public > lazybear
comparison LazyBear/Views/Company/Chart.swift @ 400:6055a867d2b6
Implementing Historical Prices in Company.swift
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Sat, 15 May 2021 19:54:20 +0200 |
parents | 5c99883c7964 |
children | f843c6382529 |
comparison
equal
deleted
inserted
replaced
399:5c99883c7964 | 400:6055a867d2b6 |
---|---|
7 | 7 |
8 import SwiftUI | 8 import SwiftUI |
9 import StockCharts | 9 import StockCharts |
10 | 10 |
11 struct Chart: View { | 11 struct Chart: View { |
12 var chartData: ChartResponse | 12 @ObservedObject var company: Company |
13 var symbol: String | 13 var symbol: String |
14 | 14 |
15 // Date picker | 15 // Date picker |
16 var ranges = ["1D", "5D", "1M", "3M", "6M", "1Y", "5Y"] | 16 var ranges = ["1D", "5D", "1M", "3M", "6M", "1Y", "5Y"] |
17 @State private var selectedRange = "3M" | 17 @State private var selectedRange = "3M" |
18 | 18 |
19 // Set recurrent price request | |
20 @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() | |
19 | 21 |
20 var body: some View { | 22 var body: some View { |
21 let priceViewStyle = PriceViewStyle( | 23 if company.showChartView { |
22 alignment: .leading, | 24 VStack { |
23 priceFont: .headline, | 25 DatePicker(ranges: ranges, selectedRange: $selectedRange) |
24 priceFontWeight: .semibold, | 26 .onChange(of: selectedRange, perform: { value in |
25 percentFont: .body, | 27 print(value.lowercased()) |
26 percentFontWeight: .medium | 28 }) |
27 ) | 29 |
28 | 30 RoundedRectangle(cornerRadius: 15) |
29 VStack { | 31 .foregroundColor(Color(.secondarySystemBackground)) |
30 DatePicker(ranges: ranges, selectedRange: $selectedRange) | 32 .frame(height: 270) |
31 | 33 .overlay( |
32 RoundedRectangle(cornerRadius: 15) | 34 VStack { |
33 .foregroundColor(Color(.secondarySystemBackground)) | 35 HStack { |
34 .frame(height: 270) | 36 if let quote = company.chartData.quote![symbol.uppercased()] { |
35 .overlay( | 37 let latestPrice = quote.latestPrice ?? 0 |
36 VStack { | 38 let changePercent = quote.changePercent ?? 0 |
37 HStack { | 39 let priceViewStyle = PriceViewStyle( |
38 if let quote = chartData.quote![symbol.uppercased()] { | 40 alignment: .leading, |
39 let latestPrice = quote.latestPrice ?? 0 | 41 priceFont: .headline, |
40 let changePercent = quote.changePercent ?? 0 | 42 priceFontWeight: .semibold, |
41 PriceView(latestPrice: latestPrice, changePercent: changePercent, style: priceViewStyle) | 43 percentFont: .body, |
44 percentFontWeight: .medium | |
45 ) | |
46 PriceView(latestPrice: latestPrice, changePercent: changePercent, style: priceViewStyle) | |
47 } | |
48 Spacer() | |
42 } | 49 } |
43 Spacer() | 50 .padding([.top, .leading, .trailing]) |
44 } | 51 |
45 .padding([.top, .leading, .trailing]) | 52 if let intradayPrices = company.chartData.intradayPrices![symbol.uppercased()] { |
46 | 53 if let prices = intradayPrices.compactMap { $0.open } { // Map without nil |
47 if let intradayPrices = chartData.intradayPrices![symbol.uppercased()] { | 54 LineChartView(data: prices, dates: nil, hours: nil, dragGesture: true) |
48 if let prices = intradayPrices.compactMap { $0.open } { | 55 .padding(.bottom) |
49 LineChartView(data: prices, | 56 } |
50 dates: nil, | |
51 hours: nil, | |
52 dragGesture: true | |
53 ) | |
54 .padding(.bottom) | |
55 } | 57 } |
56 } | 58 } |
59 ) | |
60 | |
61 if let latestNews = company.chartData.latestNews { | |
62 ForEach(latestNews, id: \.self) { new in | |
63 NewsRow(new: new) | |
57 } | 64 } |
58 ) | |
59 | |
60 if let latestNews = chartData.latestNews { | |
61 ForEach(latestNews, id: \.self) { new in | |
62 NewsRow(new: new) | |
63 } | 65 } |
64 } | 66 } |
67 .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } // Start timer | |
68 .onDisappear { self.timer.upstream.connect().cancel() } // Stop timer | |
69 .onReceive(timer) { _ in | |
70 let url = "https://api.lazybear.app/company/chart/type=streaming/symbol=\(symbol)" | |
71 company.request(url, isInitRequest: false, "chart") } // Receive timer notification | |
72 } else { | |
73 ProgressView() | |
74 .onAppear { | |
75 let url = "https://api.lazybear.app/company/chart/type=init/symbol=\(symbol)" | |
76 company.request(url, isInitRequest: true, "chart") | |
77 } | |
65 } | 78 } |
66 } | 79 } |
67 } | 80 } |
68 | 81 |
69 struct Chart_Previews: PreviewProvider { | 82 struct Chart_Previews: PreviewProvider { |
70 static var previews: some View { | 83 static var previews: some View { |
71 Chart( | 84 Chart(company: Company(), symbol: "aapl") |
72 chartData: ChartResponse( | |
73 intradayPrices: ["AAPL": [IntradayPriceModel(open: 120.3)]], | |
74 quote: ["aapl": QuoteModel(changePercent: 0.03, companyName: "Apple Inc", latestPrice: 120.3)], | |
75 latestNews: [LatestNewsModel( | |
76 datetime: 1621037430000, | |
77 headline: "Chaos Monkeys' author calls Apple's statement on his departure defamatory", | |
78 image: "https://cloud.iexapis.com/v1/news/image/99abeb99-6d9e-47c8-ae7b-53404eacccec", | |
79 source: "Investing.com", | |
80 summary: "https://www.investing.com/news/stock-market-news", | |
81 url: "https://cloud.iexapis.com/v1/news/article/99abeb99-6d9e-47c8-ae7b-53404eacccec")]), | |
82 | |
83 symbol: "aapl" | |
84 ) | |
85 } | 85 } |
86 } | 86 } |