Mercurial > public > lazybear
diff 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 |
line wrap: on
line diff
--- a/LazyBear/Views/Company/CompanyView.swift Sun May 09 00:07:44 2021 +0200 +++ b/LazyBear/Views/Company/CompanyView.swift Sat May 15 12:31:40 2021 +0200 @@ -11,20 +11,37 @@ struct CompanyView: View { var symbol: String - // Date picker - var ranges = ["1D", "5D", "1M", "3M", "6M", "1Y", "5Y"] - @State private var selectedRange = "Red" - + @ObservedObject var company = Company() @ObservedObject var viewSelector = ViewSelector() @State private var showViewSelector = false + // Set recurrent price request + @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() + var body: some View { NavigationView { ScrollView { VStack { CompanyHeader(symbol: symbol, showViewSelector: $showViewSelector) - DatePicker(ranges: ranges, selectedRange: $selectedRange) - Chart() + + // <--- Chart View ---> + if viewSelector.views["chart"]! { + let url = "https://api.lazybear.app/company/chart/type=init/symbol=\(symbol)" + + if company.showChartView { + Chart(chartData: company.chartData, symbol: symbol) + .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } // Start timer + .onDisappear { self.timer.upstream.connect().cancel() } // Stop timer + .onReceive(timer) { _ in + let url = "https://api.lazybear.app/company/chart/type=streaming/symbol=\(symbol)" + company.request(url, isInitRequest: false, "chart") } // Receive timer notification + } + else { + ProgressView() + .onAppear { company.request(url, isInitRequest: true, "chart") } } + + // ---> Chart View <--- + } } .padding() } @@ -32,8 +49,8 @@ .navigationBarTitleDisplayMode(.inline) } .actionSheet(isPresented: $showViewSelector) { - ActionSheet(title: Text("Change background"), message: Text("Select a new color"), buttons: [ - .default(Text("Chart")) { viewSelector.showView(.chart) }, + ActionSheet(title: Text("Select an option"), buttons: [ + .default(Text("Chart & News")) { viewSelector.showView(.chart) }, .cancel() ]) }