Mercurial > public > lazybear
comparison LazyBear/Views/Company/CompanyView.swift @ 399:5c99883c7964
Implementing networking in CompanyView
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Sat, 15 May 2021 12:31:40 +0200 |
parents | 933546fa5651 |
children | 6055a867d2b6 |
comparison
equal
deleted
inserted
replaced
398:933546fa5651 | 399:5c99883c7964 |
---|---|
9 import StockCharts | 9 import StockCharts |
10 | 10 |
11 struct CompanyView: View { | 11 struct CompanyView: View { |
12 var symbol: String | 12 var symbol: String |
13 | 13 |
14 // Date picker | 14 @ObservedObject var company = Company() |
15 var ranges = ["1D", "5D", "1M", "3M", "6M", "1Y", "5Y"] | |
16 @State private var selectedRange = "Red" | |
17 | |
18 @ObservedObject var viewSelector = ViewSelector() | 15 @ObservedObject var viewSelector = ViewSelector() |
19 @State private var showViewSelector = false | 16 @State private var showViewSelector = false |
17 | |
18 // Set recurrent price request | |
19 @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() | |
20 | 20 |
21 var body: some View { | 21 var body: some View { |
22 NavigationView { | 22 NavigationView { |
23 ScrollView { | 23 ScrollView { |
24 VStack { | 24 VStack { |
25 CompanyHeader(symbol: symbol, showViewSelector: $showViewSelector) | 25 CompanyHeader(symbol: symbol, showViewSelector: $showViewSelector) |
26 DatePicker(ranges: ranges, selectedRange: $selectedRange) | 26 |
27 Chart() | 27 // <--- Chart View ---> |
28 if viewSelector.views["chart"]! { | |
29 let url = "https://api.lazybear.app/company/chart/type=init/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 } | |
28 } | 45 } |
29 .padding() | 46 .padding() |
30 } | 47 } |
31 .navigationTitle("Apple inc") | 48 .navigationTitle("Apple inc") |
32 .navigationBarTitleDisplayMode(.inline) | 49 .navigationBarTitleDisplayMode(.inline) |
33 } | 50 } |
34 .actionSheet(isPresented: $showViewSelector) { | 51 .actionSheet(isPresented: $showViewSelector) { |
35 ActionSheet(title: Text("Change background"), message: Text("Select a new color"), buttons: [ | 52 ActionSheet(title: Text("Select an option"), buttons: [ |
36 .default(Text("Chart")) { viewSelector.showView(.chart) }, | 53 .default(Text("Chart & News")) { viewSelector.showView(.chart) }, |
37 .cancel() | 54 .cancel() |
38 ]) | 55 ]) |
39 } | 56 } |
40 } | 57 } |
41 } | 58 } |