comparison LazyBear/Views/Company/CompanyView.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 8357b101df67
comparison
equal deleted inserted replaced
399:5c99883c7964 400:6055a867d2b6
10 10
11 struct CompanyView: View { 11 struct CompanyView: View {
12 var symbol: String 12 var symbol: String
13 13
14 @ObservedObject var company = Company() 14 @ObservedObject var company = Company()
15 @ObservedObject var viewSelector = ViewSelector()
16 @State private var showViewSelector = false 15 @State private var showViewSelector = false
17 16
18 // Set recurrent price request 17 // Views
19 @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() 18 @State private var showChartView = true
20 19
21 var body: some View { 20 var body: some View {
22 NavigationView { 21 NavigationView {
23 ScrollView { 22 ScrollView {
24 VStack { 23 VStack {
25 CompanyHeader(symbol: symbol, showViewSelector: $showViewSelector) 24 CompanyHeader(symbol: symbol, showViewSelector: $showViewSelector)
26 25
27 // <--- Chart View ---> 26 // Chart View
28 if viewSelector.views["chart"]! { 27 if showChartView {
29 let url = "https://api.lazybear.app/company/chart/type=init/symbol=\(symbol)" 28 Chart(company: company, symbol: symbol)
30
31 if company.showChartView {
32 Chart(chartData: company.chartData, symbol: symbol)
33 .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } // Start timer
34 .onDisappear { self.timer.upstream.connect().cancel() } // Stop timer
35 .onReceive(timer) { _ in
36 let url = "https://api.lazybear.app/company/chart/type=streaming/symbol=\(symbol)"
37 company.request(url, isInitRequest: false, "chart") } // Receive timer notification
38 }
39 else {
40 ProgressView()
41 .onAppear { company.request(url, isInitRequest: true, "chart") } }
42
43 // ---> Chart View <---
44 } 29 }
45 } 30 }
46 .padding() 31 .padding()
47 } 32 }
48 .navigationTitle("Apple inc") 33 .navigationTitle("Apple inc")
49 .navigationBarTitleDisplayMode(.inline) 34 .navigationBarTitleDisplayMode(.inline)
50 } 35 }
51 .actionSheet(isPresented: $showViewSelector) { 36 .actionSheet(isPresented: $showViewSelector) {
52 ActionSheet(title: Text("Select an option"), buttons: [ 37 ActionSheet(title: Text("Select an option"), buttons: [
53 .default(Text("Chart & News")) { viewSelector.showView(.chart) }, 38 .default(Text("Chart & News")) { showChartView = true },
54 .cancel() 39 .cancel()
55 ]) 40 ])
56 } 41 }
57 } 42 }
58 } 43 }