changeset 378:6802c2393203

Implementing ProfileView (Watchlists)
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Wed, 21 Apr 2021 23:12:56 +0200
parents d01859776fe6
children a7e2c5a7b4f6
files LazyBear.xcodeproj/project.pbxproj LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate LazyBear/ContentView.swift LazyBear/Views/Profile/Networking/Profile.swift LazyBear/Views/Profile/Networking/ProfileResponse.swift LazyBear/Views/Profile/ProfileView.swift
diffstat 6 files changed, 54 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/LazyBear.xcodeproj/project.pbxproj	Wed Apr 21 16:44:11 2021 +0200
+++ b/LazyBear.xcodeproj/project.pbxproj	Wed Apr 21 23:12:56 2021 +0200
@@ -740,6 +740,7 @@
 				SDKROOT = iphoneos;
 				SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
 				SWIFT_OPTIMIZATION_LEVEL = "-Onone";
+				SWIFT_VERSION = 5.0;
 			};
 			name = Debug;
 		};
@@ -794,6 +795,7 @@
 				SDKROOT = iphoneos;
 				SWIFT_COMPILATION_MODE = wholemodule;
 				SWIFT_OPTIMIZATION_LEVEL = "-O";
+				SWIFT_VERSION = 5.0;
 				VALIDATE_PRODUCT = YES;
 			};
 			name = Release;
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- a/LazyBear/ContentView.swift	Wed Apr 21 16:44:11 2021 +0200
+++ b/LazyBear/ContentView.swift	Wed Apr 21 23:12:56 2021 +0200
@@ -9,26 +9,30 @@
 
 struct ContentView: View {
     @State private var showWelcome = false
+    @State var selectedView = 1
     @EnvironmentObject var hudManager: HudManager
     
     var body: some View {
         ZStack {
-            TabView {
+            TabView(selection: $selectedView) {
                 HomeView()
                     .tabItem {
                         Image(systemName: "house")
                         Text("Home")
                     }
+                    .tag(1)  // Do not remove tags. It causes an odd behaviour when showView is activated
                 SearchView()
                     .tabItem {
                         Image(systemName: "magnifyingglass")
                         Text("Search")
                     }
+                    .tag(2)
                 ProfileView()
                     .tabItem {
                         Image(systemName: "person")
                         Text("Profile")
                     }
+                    .tag(3)
 //                Text("The Last Tab")
 //                    .tabItem {
 //                        Image(systemName: "4.square.fill")
--- a/LazyBear/Views/Profile/Networking/Profile.swift	Wed Apr 21 16:44:11 2021 +0200
+++ b/LazyBear/Views/Profile/Networking/Profile.swift	Wed Apr 21 23:12:56 2021 +0200
@@ -16,14 +16,16 @@
     func request(_ url: String) {
         genericRequest(url: url, model: ProfileResponse.self) { response in
             self.streamingRequests += 1
-            
+
             // If is the first request -> init()
             if self.streamingRequests == 1 {
                 self.data = response
             } else {
-                // If not, request streaming data (without intradayPrices)
+                 // If not, request streaming data (without intradayPrices)
                 self.data.quotes = response.quotes
             }
+
+            self.showView = true
         }
     }
 }
--- a/LazyBear/Views/Profile/Networking/ProfileResponse.swift	Wed Apr 21 16:44:11 2021 +0200
+++ b/LazyBear/Views/Profile/Networking/ProfileResponse.swift	Wed Apr 21 23:12:56 2021 +0200
@@ -11,7 +11,8 @@
   var intradayPrices: [String: [IntradayPriceModel]]?
   var quotes: [String: QuoteModel]?
   
-  private enum CodingKeys : String, CodingKey {
+    private enum CodingKeys : String, CodingKey {
         case intradayPrices = "intraday_prices"
+        case quotes
     }
 }
--- a/LazyBear/Views/Profile/ProfileView.swift	Wed Apr 21 16:44:11 2021 +0200
+++ b/LazyBear/Views/Profile/ProfileView.swift	Wed Apr 21 23:12:56 2021 +0200
@@ -6,22 +6,55 @@
 //
 
 import SwiftUI
+import CoreData
 
 struct ProfileView: View {
     @ObservedObject var profile = Profile()
+    @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
+    var watchlistCompanies: FetchedResults<WatchlistCompany>
 
     var body: some View {
-        NavigationView {
-            List {
-                
-
+        if profile.showView {
+            NavigationView {
+                List {
+                    // Take all the different watchlist created
+                    let watchlists = Set(watchlistCompanies.map { $0.watchlist })  // Set -> avoid duplicates names
+                    ForEach(Array(watchlists), id: \.self) { watchlist in
+                        
+                        // Get all the symbols of this watchlist
+                        let symbols = watchlistCompanies.filter({ $0.watchlist == watchlist }).map { $0.symbol }
+                        
+                        if let companies = profile.data.quotes {
+                            let filteredCompanies = companies.filter({ symbols.contains($0.key) })
+                            StockRow(listName: watchlist, list: filteredCompanies, intradayPrices: profile.data.intradayPrices)
+                                .listRowInsets(EdgeInsets())
+                        }
+                    }
+                }
+                .navigationTitle("My profile")
+                .navigationBarTitleDisplayMode(.inline)
             }
-            .navigationTitle("My profile")
-            .navigationBarTitleDisplayMode(.inline)
-            .onAppear {
-                profile.request("https://api.lazybear.app/profile/type=init/symbols=aapl,fb")
+        } else {
+            ProgressView()
+                .onAppear { prepareUrl() }
+        }
+    }
+    
+    private func prepareUrl() {
+        let symbols = watchlistCompanies.map { $0.symbol }  // Get symbols in watchlists
+        var url = "https://api.lazybear.app/profile/type=init/symbols="
+        print(url)
+
+        var counter = 0
+        for symbol in symbols {
+            counter += 1
+            if counter == 1 {
+                url += symbol
+            } else {
+                url += ",\(symbol)"
             }
         }
+        profile.request(url)
     }
 }