comparison LazyBearWatchOS Extension/Views/WatchOSCompanyView.swift @ 455:b560babcd5ed

WatchOS views implemented
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Mon, 28 Jun 2021 11:55:19 +0200
parents
children 57471e7bcf08
comparison
equal deleted inserted replaced
454:c79a3ed3d230 455:b560babcd5ed
1 //
2 // WatchOSCompanyView.swift
3 // LazyBearWatchOS Extension
4 //
5 // Created by Dennis Concepción Martín on 27/06/2021.
6 //
7
8 import SwiftUI
9
10 struct WatchOSCompanyView: View {
11 var symbol: String
12 @ObservedObject var company = Company()
13 @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() /// Set recurrent price request
14
15 var body: some View {
16 if company.showView {
17 ScrollView {
18 VStack(alignment: .leading) {
19 Text(symbol)
20 .fontWeight(.semibold)
21
22 WatchOSChartHelper(company: company)
23 .padding(.bottom)
24
25 NavigationLink("Latest news", destination: WatchOSNewsList(latestNews: company.data.latestNews).navigationTitle("Latest news"))
26
27 Text("Real-time data")
28 .font(.footnote)
29 .opacity(0.7)
30 .padding(.top)
31 }
32 .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } /// Start timer
33 .onDisappear { self.timer.upstream.connect().cancel() } /// Stop timer
34 .onReceive(timer) { _ in company.request("https://api.lazybear.app/company/symbol=\(symbol)/type=initial/range=5d", .streaming) }
35 }
36 } else {
37 ProgressView()
38 .onAppear {
39 company.request("https://api.lazybear.app/company/symbol=\(symbol)/type=initial/range=5d", .initial)
40 }
41 }
42 }
43 }
44
45 struct WatchOSCompanyView_Previews: PreviewProvider {
46 static var previews: some View {
47 WatchOSCompanyView(symbol: "AAPL")
48 .navigationTitle("Apple Inc")
49 }
50 }