comparison LazyBear/Views/Company/Chart.swift @ 417:5f21f7c23c5e

Add comments and clean code
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Fri, 11 Jun 2021 11:37:42 +0200
parents 34f9e408b861
children 6dd97877f575
comparison
equal deleted inserted replaced
416:1662a41e2c1a 417:5f21f7c23c5e
9 import StockCharts 9 import StockCharts
10 10
11 struct Chart: View { 11 struct Chart: View {
12 @ObservedObject var company: Company 12 @ObservedObject var company: Company
13 var symbol: String 13 var symbol: String
14 var ranges = ["1D", "5D", "1M", "3M", "6M", "1Y", "5Y"] /// DatePicker ranges
14 15
15 // Date picker 16 @State private var selectedRange = "3M" /// Selected DatePicker range
16 var ranges = ["1D", "5D", "1M", "3M", "6M", "1Y", "5Y"] 17 @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() /// Set recurrent price request
17 @State private var selectedRange = "3M" 18 @State private var showingStatistics = false /// Show StatisticsView of the company
18
19 // Set recurrent price request
20 @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect()
21
22 @State private var showingStatistics = false
23 19
24 var body: some View { 20 var body: some View {
25 if company.showChartView { 21 if company.showChartView {
26 VStack { 22 VStack {
27 DatePicker(ranges: ranges, selectedRange: $selectedRange) 23 DatePicker(ranges: ranges, selectedRange: $selectedRange)
32 28
33 RoundedRectangle(cornerRadius: 15) 29 RoundedRectangle(cornerRadius: 15)
34 .foregroundColor(Color(.secondarySystemBackground)) 30 .foregroundColor(Color(.secondarySystemBackground))
35 .frame(height: 270) 31 .frame(height: 270)
36 .overlay( 32 .overlay(
33 /*
34 Show PriceView and Chart
35 */
37 VStack { 36 VStack {
38 HStack { 37 HStack {
39 if let quote = company.chartData.quote![symbol.uppercased()] { 38 if let quote = company.chartData.quote![symbol.uppercased()] {
40 let latestPrice = quote.latestPrice ?? 0 39 let latestPrice = quote.latestPrice ?? 0
41 let changePercent = quote.changePercent ?? 0 40 let changePercent = quote.changePercent ?? 0
42 let priceViewStyle = PriceViewStyle( 41 let priceViewStyle = PriceViewStyle( /// Define PriceView style
43 horizontalAlignment: .leading, 42 horizontalAlignment: .leading,
44 verticalAlignment: .center, 43 verticalAlignment: .center,
45 orientation: .HStack, 44 orientation: .HStack,
46 priceFont: .title3, 45 priceFont: .title3,
47 priceFontWeight: .semibold, 46 priceFontWeight: .semibold,
51 ) 50 )
52 PriceView(latestPrice: latestPrice, changePercent: changePercent, style: priceViewStyle) 51 PriceView(latestPrice: latestPrice, changePercent: changePercent, style: priceViewStyle)
53 } 52 }
54 Spacer() 53 Spacer()
55 54
56 if let _ = company.chartData.keyStats { 55 if let _ = company.chartData.keyStats { /// Check if keyStats is empty -> Hide button
57 Button("See stats", action: { showingStatistics = true }) 56 Button("See stats", action: { showingStatistics = true })
58 } 57 }
59 } 58 }
60 .padding([.top, .leading, .trailing]) 59 .padding([.top, .leading, .trailing])
61 60
62 Spacer() 61 Spacer()
63 if let historicalPrices = company.chartData.historicalPrices { 62 if let historicalPrices = company.chartData.historicalPrices {
64 let prices = historicalPrices.compactMap { $0.close } 63 let prices = historicalPrices.compactMap { $0.close }
65 let dates = historicalPrices.compactMap { $0.date } 64 let dates = historicalPrices.compactMap { $0.date }
66 // let hours = historicalPrices.compactMap { $0.minute }
67 LineChartView(data: prices, dates: dates, hours: nil, dragGesture: true) 65 LineChartView(data: prices, dates: dates, hours: nil, dragGesture: true)
68 .padding(.bottom) 66 .padding(.bottom)
69 } 67 }
70 } 68 }
71 ) 69 )
72 70
71 /*
72 Show latest news
73 */
73 if let latestNews = company.chartData.latestNews { 74 if let latestNews = company.chartData.latestNews {
74 VStack(spacing: 20) { 75 VStack(spacing: 20) {
75 ForEach(latestNews, id: \.self) { new in 76 ForEach(latestNews, id: \.self) { new in
76 NewsRow(new: new) 77 NewsRow(new: new)
77 } 78 }
81 } 82 }
82 .sheet(isPresented: $showingStatistics) { 83 .sheet(isPresented: $showingStatistics) {
83 StatsView(keyStats: company.chartData.keyStats!) 84 StatsView(keyStats: company.chartData.keyStats!)
84 } 85 }
85 .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } // Start timer 86 .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } // Start timer
86 .onDisappear { self.timer.upstream.connect().cancel() } // Stop timer 87 .onDisappear { self.timer.upstream.connect().cancel() } /// Stop timer
87 .onReceive(timer) { _ in 88 .onReceive(timer) { _ in
88 let url = "https://api.lazybear.app/company/chart/symbol=\(symbol)/type=streaming" 89 let url = "https://api.lazybear.app/company/chart/symbol=\(symbol)/type=streaming"
89 company.request(url, .streaming, "chart") } // Receive timer notification 90 company.request(url, .streaming, "chart") } /// Receive timer notification
90 } else { 91 } else {
91 ProgressView() 92 ProgressView()
92 .onAppear { 93 .onAppear {
93 let url = "https://api.lazybear.app/company/chart/symbol=\(symbol)/type=init" 94 let url = "https://api.lazybear.app/company/chart/symbol=\(symbol)/type=init"
94 company.request(url, .initial, "chart") 95 company.request(url, .initial, "chart")