annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
398
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
1 //
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
2 // CompanyView.swift
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
3 // LazyBear
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
4 //
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
5 // Created by Dennis Concepción Martín on 8/5/21.
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
6 //
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
7
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
8 import SwiftUI
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
9 import StockCharts
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
10
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
11 struct CompanyView: View {
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
12 var symbol: String
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
13
399
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
14 @ObservedObject var company = Company()
398
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
15 @ObservedObject var viewSelector = ViewSelector()
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
16 @State private var showViewSelector = false
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
17
399
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
18 // Set recurrent price request
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
19 @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect()
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
20
398
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
21 var body: some View {
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
22 NavigationView {
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
23 ScrollView {
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
24 VStack {
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
25 CompanyHeader(symbol: symbol, showViewSelector: $showViewSelector)
399
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
26
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
27 // <--- Chart View --->
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
28 if viewSelector.views["chart"]! {
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
29 let url = "https://api.lazybear.app/company/chart/type=init/symbol=\(symbol)"
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
30
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
31 if company.showChartView {
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
32 Chart(chartData: company.chartData, symbol: symbol)
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
33 .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } // Start timer
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
34 .onDisappear { self.timer.upstream.connect().cancel() } // Stop timer
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
35 .onReceive(timer) { _ in
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
36 let url = "https://api.lazybear.app/company/chart/type=streaming/symbol=\(symbol)"
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
37 company.request(url, isInitRequest: false, "chart") } // Receive timer notification
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
38 }
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
39 else {
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
40 ProgressView()
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
41 .onAppear { company.request(url, isInitRequest: true, "chart") } }
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
42
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
43 // ---> Chart View <---
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
44 }
398
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
45 }
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
46 .padding()
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
47 }
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
48 .navigationTitle("Apple inc")
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
49 .navigationBarTitleDisplayMode(.inline)
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
50 }
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
51 .actionSheet(isPresented: $showViewSelector) {
399
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
52 ActionSheet(title: Text("Select an option"), buttons: [
5c99883c7964 Implementing networking in CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents: 398
diff changeset
53 .default(Text("Chart & News")) { viewSelector.showView(.chart) },
398
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
54 .cancel()
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
55 ])
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
56 }
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
57 }
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
58 }
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
59
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
60 struct CompanyView_Previews: PreviewProvider {
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
61 static var previews: some View {
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
62 CompanyView(symbol: "AAPL")
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
63 }
933546fa5651 Implementing CompanyView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff changeset
64 }