comparison LazyBearWatchOS Extension/Views/WatchOSProfileView.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 LazyBearWatchOS Extension/Views/Home/HomeView.swift@c79a3ed3d230
children c6913f0ce46e
comparison
equal deleted inserted replaced
454:c79a3ed3d230 455:b560babcd5ed
1 //
2 // WatchOSProfileView.swift
3 // LazyBearWatchOS Extension
4 //
5 // Created by Dennis Concepción Martín on 22/6/21.
6 //
7
8 import SwiftUI
9 import CoreData
10
11 struct WatchOSProfileView: View {
12 @ObservedObject var profile = Profile()
13 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) var watchlistCompanies: FetchedResults<WatchlistCompany>
14 @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() /// Set recurrent price request
15
16 var body: some View {
17 if profile.showView {
18 NavigationView {
19 ScrollView {
20 VStack {
21 if let companies = profile.data.quotes {
22 ForEach(companies, id: \.self) { company in
23 NavigationLink(destination: WatchOSCompanyView(symbol: company.symbol)
24 .navigationTitle(company.companyName.capitalized)
25 ) {
26 WatchOSCompanyRow(company: company)
27 }
28 }
29 }
30 }
31 }
32 .navigationTitle("Lazybear")
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 prepareUrl(.streaming) }
36 }
37 } else {
38 ProgressView()
39 .onAppear { prepareUrl(.initial) }
40 }
41 }
42
43 /*
44 Get symbols in watchlists (Core Data) -> Prepare url -> Request
45 */
46 private func prepareUrl(_ requestType: RequestType) {
47 let symbols = Set(watchlistCompanies.map { $0.symbol })
48 let symbolsString = symbols.joined(separator:",")
49
50 switch requestType {
51 case .initial:
52 let url = "https://api.lazybear.app/profile/type=initial/symbols=\(symbolsString)"
53 print(watchlistCompanies)
54 profile.request(url, .initial)
55
56 default:
57 let url = "https://api.lazybear.app/profile/type=streaming/symbols=\(symbolsString)"
58 profile.request(url, .streaming)
59 }
60 }
61 }
62
63 struct WatchOSProfileView_Previews: PreviewProvider {
64 static var previews: some View {
65 WatchOSProfileView()
66 }
67 }