comparison LazyBear/Views/Company/Chart.swift @ 409:dc8dccd18e86

Minor UI changes
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sun, 06 Jun 2021 19:07:21 +0200
parents c804ce7a1560
children a7c9dd0c5822
comparison
equal deleted inserted replaced
408:f9611c94d636 409:dc8dccd18e86
37 if let quote = company.chartData.quote![symbol.uppercased()] { 37 if let quote = company.chartData.quote![symbol.uppercased()] {
38 let latestPrice = quote.latestPrice ?? 0 38 let latestPrice = quote.latestPrice ?? 0
39 let changePercent = quote.changePercent ?? 0 39 let changePercent = quote.changePercent ?? 0
40 let priceViewStyle = PriceViewStyle( 40 let priceViewStyle = PriceViewStyle(
41 alignment: .leading, 41 alignment: .leading,
42 priceFont: .headline, 42 priceFont: .title3,
43 priceFontWeight: .semibold, 43 priceFontWeight: .semibold,
44 percentFont: .body, 44 percentFont: .headline,
45 percentFontWeight: .medium 45 percentFontWeight: .medium
46 ) 46 )
47 PriceView(latestPrice: latestPrice, changePercent: changePercent, style: priceViewStyle) 47 PriceView(latestPrice: latestPrice, changePercent: changePercent, style: priceViewStyle)
48 } 48 }
49 Spacer() 49 Spacer()
50 } 50 }
51 .padding([.top, .leading, .trailing]) 51 .padding([.top, .leading, .trailing])
52 52
53 Spacer()
53 if let historicalPrices = company.chartData.historicalPrices { 54 if let historicalPrices = company.chartData.historicalPrices {
54 if let prices = historicalPrices.compactMap { $0.close } { // Map without nil 55 let prices = historicalPrices.compactMap { $0.close }
55 LineChartView(data: prices, dates: nil, hours: nil, dragGesture: true) 56 let dates = historicalPrices.compactMap { $0.date }
56 .padding(.bottom) 57 // let hours = historicalPrices.compactMap { $0.minute }
57 } 58 LineChartView(data: prices, dates: dates, hours: nil, dragGesture: true)
59 .padding(.bottom)
58 } 60 }
59 } 61 }
60 ) 62 )
61 63
62 if let latestNews = company.chartData.latestNews { 64 if let latestNews = company.chartData.latestNews {
66 } 68 }
67 } 69 }
68 .padding(.top) 70 .padding(.top)
69 } 71 }
70 } 72 }
71 .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } // Start timer 73 .onAppear { print("appeared"); self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } // Start timer
72 .onDisappear { self.timer.upstream.connect().cancel() } // Stop timer 74 .onDisappear { self.timer.upstream.connect().cancel() } // Stop timer
73 .onReceive(timer) { _ in 75 .onReceive(timer) { _ in
74 let url = "https://api.lazybear.app/company/chart/symbol=\(symbol)/type=streaming" 76 let url = "https://api.lazybear.app/company/chart/symbol=\(symbol)/type=streaming"
75 company.request(url, .streaming, "chart") } // Receive timer notification 77 company.request(url, .streaming, "chart") } // Receive timer notification
76 } else { 78 } else {
79 let url = "https://api.lazybear.app/company/chart/symbol=\(symbol)/type=init" 81 let url = "https://api.lazybear.app/company/chart/symbol=\(symbol)/type=init"
80 company.request(url, .initial, "chart") 82 company.request(url, .initial, "chart")
81 } 83 }
82 } 84 }
83 } 85 }
86
87 /*
88 Price data is cached, so when the view disappears and appears again in a few seconds, the cached data is shown up. But the date range
89 is incorrect because, always when this view appears, selectedRange is "3M".
90 */
91 private func checkDateRange() {
92
93 }
84 } 94 }
85 95
86 struct Chart_Previews: PreviewProvider { 96 struct Chart_Previews: PreviewProvider {
87 static var previews: some View { 97 static var previews: some View {
88 Chart(company: Company(), symbol: "aapl") 98 Chart(company: Company(), symbol: "aapl")