diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LazyBearWatchOS Extension/Views/WatchOSProfileView.swift	Mon Jun 28 11:55:19 2021 +0200
@@ -0,0 +1,67 @@
+//
+//  WatchOSProfileView.swift
+//  LazyBearWatchOS Extension
+//
+//  Created by Dennis Concepción Martín on 22/6/21.
+//
+
+import SwiftUI
+import CoreData
+
+struct WatchOSProfileView: View {
+    @ObservedObject var profile = Profile()
+    @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) var watchlistCompanies: FetchedResults<WatchlistCompany>
+    @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect()  /// Set recurrent price request
+    
+    var body: some View {
+        if profile.showView {
+            NavigationView {
+                ScrollView {
+                    VStack {
+                        if let companies = profile.data.quotes {
+                            ForEach(companies, id: \.self) { company in
+                                NavigationLink(destination: WatchOSCompanyView(symbol: company.symbol)
+                                                .navigationTitle(company.companyName.capitalized)
+                                ) {
+                                    WatchOSCompanyRow(company: company)
+                                }
+                            }
+                        }
+                    }
+                }
+                .navigationTitle("Lazybear")
+                .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 prepareUrl(.streaming) }
+            }
+        } else {
+            ProgressView()
+                .onAppear { prepareUrl(.initial) }
+        }
+    }
+    
+    /*
+     Get symbols in watchlists (Core Data) -> Prepare url -> Request
+     */
+    private func prepareUrl(_ requestType: RequestType) {
+        let symbols = Set(watchlistCompanies.map { $0.symbol })
+        let symbolsString = symbols.joined(separator:",")
+        
+        switch requestType {
+        case .initial:
+            let url = "https://api.lazybear.app/profile/type=initial/symbols=\(symbolsString)"
+            print(watchlistCompanies)
+            profile.request(url, .initial)
+            
+        default:
+            let url = "https://api.lazybear.app/profile/type=streaming/symbols=\(symbolsString)"
+            profile.request(url, .streaming)
+        }
+    }
+}
+
+struct WatchOSProfileView_Previews: PreviewProvider {
+    static var previews: some View {
+        WatchOSProfileView()
+    }
+}