comparison LazyBearWatchOS Extension/Views/Home/HomeView.swift @ 452:bb69f9d1d20f

Implement HomeView in WatchOS
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Sat, 26 Jun 2021 18:45:31 +0200
parents 8621ba6fd457
children 37c13ebda381
comparison
equal deleted inserted replaced
451:bb130738b816 452:bb69f9d1d20f
7 7
8 import SwiftUI 8 import SwiftUI
9 import CoreData 9 import CoreData
10 10
11 struct HomeView: View { 11 struct HomeView: View {
12 @ObservedObject var profile = Profile()
13
12 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) 14 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
13 var watchlistCompanies: FetchedResults<WatchlistCompany> 15 var watchlistCompanies: FetchedResults<WatchlistCompany>
14 16
17 @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() /// Set recurrent price request
18
15 var body: some View { 19 var body: some View {
16 VStack { 20 if profile.showView {
21 NavigationView {
22 ScrollView {
23 VStack {
24 if let companies = profile.data.quotes {
25 ForEach(companies, id: \.self) { company in
26 CompanyRow(company: company)
27 }
28 }
29 }
30 }
31 .navigationTitle("Lazybear")
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 prepareUrl(.streaming) }
35 }
36 } else {
37 ProgressView()
38 .onAppear { prepareUrl(.initial) }
39 }
40 }
41
42 /*
43 Get symbols in watchlists (Core Data) -> Prepare url -> Request
44 */
45 private func prepareUrl(_ requestType: RequestType) {
46 let symbols = Set(watchlistCompanies.map { $0.symbol })
47 let symbolsString = symbols.joined(separator:",")
48
49 switch requestType {
50 case .initial:
51 let url = "https://api.lazybear.app/profile/type=initial/symbols=\(symbolsString)"
52 profile.request(url, .initial)
17 53
54 default:
55 let url = "https://api.lazybear.app/profile/type=streaming/symbols=\(symbolsString)"
56 profile.request(url, .streaming)
18 } 57 }
19 } 58 }
20 } 59 }
21 60
22 struct HomeView_Previews: PreviewProvider { 61 struct HomeView_Previews: PreviewProvider {