changeset 358:280cbc5653b5

SearchView implemented
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Fri, 16 Apr 2021 17:03:48 +0200
parents eb97439e46cd
children 3d6f0329d691
files LazyBear.xcodeproj/project.pbxproj LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate LazyBear/ContentView.swift LazyBear/Views/Home/Networking/Home.swift LazyBear/Views/Search/CompanyList.swift LazyBear/Views/Search/Helpers/CompanyRow.swift LazyBear/Views/Search/Helpers/SearchedCompanyItem.swift LazyBear/Views/Search/Networking/Search.swift LazyBear/Views/Search/Networking/SearchResponse.swift LazyBear/Views/Search/SearchView.swift
diffstat 10 files changed, 181 insertions(+), 137 deletions(-) [+]
line wrap: on
line diff
--- a/LazyBear.xcodeproj/project.pbxproj	Thu Apr 15 23:37:25 2021 +0200
+++ b/LazyBear.xcodeproj/project.pbxproj	Fri Apr 16 17:03:48 2021 +0200
@@ -9,7 +9,7 @@
 /* Begin PBXBuildFile section */
 		950C36E3260FB6180081CF53 /* HapticsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 950C36E2260FB6180081CF53 /* HapticsManager.swift */; };
 		950D0E192618AA4900D17AD7 /* CompanyList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 950D0E182618AA4900D17AD7 /* CompanyList.swift */; };
-		950D0E292618AEC800D17AD7 /* CompanyRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 950D0E282618AEC800D17AD7 /* CompanyRow.swift */; };
+		950D0E292618AEC800D17AD7 /* SearchedCompanyItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 950D0E282618AEC800D17AD7 /* SearchedCompanyItem.swift */; };
 		950D0E2D2618B2B900D17AD7 /* HelpSheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 950D0E2C2618B2B900D17AD7 /* HelpSheet.swift */; };
 		950D0E302618B34600D17AD7 /* BlurBackground.swift in Sources */ = {isa = PBXBuildFile; fileRef = 950D0E2F2618B34600D17AD7 /* BlurBackground.swift */; };
 		950D0E332618B44800D17AD7 /* HudManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 950D0E322618B44800D17AD7 /* HudManager.swift */; };
@@ -20,6 +20,8 @@
 		951566EA2613A37C007C0F36 /* TradingDatesItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 951566E92613A37C007C0F36 /* TradingDatesItem.swift */; };
 		952045152610C7C600A76362 /* ConvertEpoch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 952045142610C7C600A76362 /* ConvertEpoch.swift */; };
 		9523ED422615BDB2006D3D6F /* phrases.json in Resources */ = {isa = PBXBuildFile; fileRef = 9523ED412615BDB2006D3D6F /* phrases.json */; };
+		9529947E2629CA3E005F0AB0 /* Search.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9529947D2629CA3E005F0AB0 /* Search.swift */; };
+		952994822629CA46005F0AB0 /* SearchResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 952994812629CA46005F0AB0 /* SearchResponse.swift */; };
 		954D7EA7260BBA6600A13C50 /* WatchlistCompany+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 954D7EA5260BBA6600A13C50 /* WatchlistCompany+CoreDataClass.swift */; };
 		954D7EA8260BBA6600A13C50 /* WatchlistCompany+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = 954D7EA6260BBA6600A13C50 /* WatchlistCompany+CoreDataProperties.swift */; };
 		954D7EC2260BE70C00A13C50 /* SwiftlySearch in Frameworks */ = {isa = PBXBuildFile; productRef = 954D7EC1260BE70C00A13C50 /* SwiftlySearch */; };
@@ -58,7 +60,7 @@
 /* Begin PBXFileReference section */
 		950C36E2260FB6180081CF53 /* HapticsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HapticsManager.swift; sourceTree = "<group>"; };
 		950D0E182618AA4900D17AD7 /* CompanyList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompanyList.swift; sourceTree = "<group>"; };
-		950D0E282618AEC800D17AD7 /* CompanyRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompanyRow.swift; sourceTree = "<group>"; };
+		950D0E282618AEC800D17AD7 /* SearchedCompanyItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchedCompanyItem.swift; sourceTree = "<group>"; };
 		950D0E2C2618B2B900D17AD7 /* HelpSheet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HelpSheet.swift; sourceTree = "<group>"; };
 		950D0E2F2618B34600D17AD7 /* BlurBackground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlurBackground.swift; sourceTree = "<group>"; };
 		950D0E322618B44800D17AD7 /* HudManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HudManager.swift; sourceTree = "<group>"; };
@@ -69,6 +71,8 @@
 		951566E92613A37C007C0F36 /* TradingDatesItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TradingDatesItem.swift; sourceTree = "<group>"; };
 		952045142610C7C600A76362 /* ConvertEpoch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConvertEpoch.swift; sourceTree = "<group>"; };
 		9523ED412615BDB2006D3D6F /* phrases.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = phrases.json; sourceTree = "<group>"; };
+		9529947D2629CA3E005F0AB0 /* Search.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Search.swift; sourceTree = "<group>"; };
+		952994812629CA46005F0AB0 /* SearchResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResponse.swift; sourceTree = "<group>"; };
 		954D7EA5260BBA6600A13C50 /* WatchlistCompany+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WatchlistCompany+CoreDataClass.swift"; sourceTree = "<group>"; };
 		954D7EA6260BBA6600A13C50 /* WatchlistCompany+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WatchlistCompany+CoreDataProperties.swift"; sourceTree = "<group>"; };
 		9550443926111B2B000E0BCB /* HomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = "<group>"; };
@@ -122,7 +126,7 @@
 		950D0E272618AEBB00D17AD7 /* Helpers */ = {
 			isa = PBXGroup;
 			children = (
-				950D0E282618AEC800D17AD7 /* CompanyRow.swift */,
+				950D0E282618AEC800D17AD7 /* SearchedCompanyItem.swift */,
 				950D0E2C2618B2B900D17AD7 /* HelpSheet.swift */,
 			);
 			path = Helpers;
@@ -161,6 +165,15 @@
 			path = Resources;
 			sourceTree = "<group>";
 		};
+		9529947C2629CA2F005F0AB0 /* Networking */ = {
+			isa = PBXGroup;
+			children = (
+				9529947D2629CA3E005F0AB0 /* Search.swift */,
+				952994812629CA46005F0AB0 /* SearchResponse.swift */,
+			);
+			path = Networking;
+			sourceTree = "<group>";
+		};
 		954D7E9D260BBA5200A13C50 /* Global Models */ = {
 			isa = PBXGroup;
 			children = (
@@ -285,6 +298,7 @@
 				95A7C065261639DF003E2EC1 /* SearchView.swift */,
 				950D0E182618AA4900D17AD7 /* CompanyList.swift */,
 				950D0E272618AEBB00D17AD7 /* Helpers */,
+				9529947C2629CA2F005F0AB0 /* Networking */,
 			);
 			path = Search;
 			sourceTree = "<group>";
@@ -321,18 +335,10 @@
 			isa = PBXGroup;
 			children = (
 				95FBE0DB2619CA7200440386 /* ProfileView.swift */,
-				95FD09C9261BA12800393042 /* Networking */,
 			);
 			path = Profile;
 			sourceTree = "<group>";
 		};
-		95FD09C9261BA12800393042 /* Networking */ = {
-			isa = PBXGroup;
-			children = (
-			);
-			path = Networking;
-			sourceTree = "<group>";
-		};
 /* End PBXGroup section */
 
 /* Begin PBXNativeTarget section */
@@ -434,9 +440,11 @@
 				95ECCA5D2612169200A67EFA /* LineShape.swift in Sources */,
 				9550444C26111FED000E0BCB /* StockItem.swift in Sources */,
 				950D0E192618AA4900D17AD7 /* CompanyList.swift in Sources */,
+				952994822629CA46005F0AB0 /* SearchResponse.swift in Sources */,
 				950D0E362618B61000D17AD7 /* BackgroundShadow.swift in Sources */,
 				955E732D26235561005652FF /* QuoteModel.swift in Sources */,
-				950D0E292618AEC800D17AD7 /* CompanyRow.swift in Sources */,
+				9529947E2629CA3E005F0AB0 /* Search.swift in Sources */,
+				950D0E292618AEC800D17AD7 /* SearchedCompanyItem.swift in Sources */,
 				955E733C262356F3005652FF /* HomeResponse.swift in Sources */,
 				955E733326235633005652FF /* IntradayPricesModel.swift in Sources */,
 				951566EA2613A37C007C0F36 /* TradingDatesItem.swift in Sources */,
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- a/LazyBear/ContentView.swift	Thu Apr 15 23:37:25 2021 +0200
+++ b/LazyBear/ContentView.swift	Fri Apr 16 17:03:48 2021 +0200
@@ -19,11 +19,11 @@
                         Image(systemName: "house")
                         Text("Home")
                     }
-//                SearchView()
-//                    .tabItem {
-//                        Image(systemName: "magnifyingglass")
-//                        Text("Search")
-//                    }
+                SearchView()
+                    .tabItem {
+                        Image(systemName: "magnifyingglass")
+                        Text("Search")
+                    }
 //                ProfileView()
 //                    .tabItem {
 //                        Image(systemName: "person")
@@ -42,11 +42,12 @@
             
             Group {
                 BackgroundShadow()
+                    
                 HelpSheet()
                     .offset(y: hudManager.showSearchHelper ? 0 : 700)
+                    .padding(.horizontal)
             }
             .animation(.easeInOut)
-            .padding(.horizontal)
         }
     }
     
--- a/LazyBear/Views/Home/Networking/Home.swift	Thu Apr 15 23:37:25 2021 +0200
+++ b/LazyBear/Views/Home/Networking/Home.swift	Fri Apr 16 17:03:48 2021 +0200
@@ -11,13 +11,17 @@
     @Published var showView = false
     @Published var data = HomeResponse()
     
+    var streamingRequests = 0  // Count streaming requests
+    
     func request(_ url: String) {
         genericRequest(url: url, model: HomeResponse.self) { response in
-            // Is intradayPrices is empty -> request all data
-            if self.data.intradayPrices == nil {
+            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 and latestCurrencies)
                 self.data.lists = response.lists
                 self.data.sectorPerformance = response.sectorPerformance
             }
--- a/LazyBear/Views/Search/CompanyList.swift	Thu Apr 15 23:37:25 2021 +0200
+++ b/LazyBear/Views/Search/CompanyList.swift	Fri Apr 16 17:03:48 2021 +0200
@@ -7,19 +7,19 @@
 
 import SwiftUI
 
-//struct CompanyList: View {
-//    var searchResult: [SearchModel]
-//    
-//    var body: some View {
-//        List(searchResult, id: \.self) { company in
-//            CompanyRow(company: company)
-//        }
-//        .listStyle(GroupedListStyle())
-//    }
-//}
-//
-//struct CompanyList_Previews: PreviewProvider {
-//    static var previews: some View {
-//        CompanyList(searchResult: [SearchModel(securityName: "aaple inc", symbol: "aapl", region: "US", currency: "USD"), SearchModel(securityName: "aaple inc", symbol: "aapl", region: "US", currency: "USD"), SearchModel(securityName: "aaple inc", symbol: "aapl", region: "US", currency: "USD"), SearchModel(securityName: "aaple inc", symbol: "aapl", region: "US", currency: "USD"), SearchModel(securityName: "aaple inc", symbol: "aapl", region: "US", currency: "USD")])
-//    }
-//}
+struct CompanyList: View {
+    var searchResult: [SearchResponse]
+    
+    var body: some View {
+        List(searchResult, id: \.self) { company in
+            SearchedCompanyItem(company: company)
+        }
+        .listStyle(GroupedListStyle())
+    }
+}
+
+struct CompanyList_Previews: PreviewProvider {
+    static var previews: some View {
+        CompanyList(searchResult: [SearchResponse(currency: "USD", region: "US", securityName: "aaple inc", symbol: "aapl"), SearchResponse(currency: "USD", region: "US", securityName: "aaple inc", symbol: "aapl"), SearchResponse(currency: "USD", region: "US", securityName: "aaple inc", symbol: "aapl"), SearchResponse(currency: "USD", region: "US", securityName: "aaple inc", symbol: "aapl"), SearchResponse(currency: "USD", region: "US", securityName: "aaple inc", symbol: "aapl")])
+    }
+}
--- a/LazyBear/Views/Search/Helpers/CompanyRow.swift	Thu Apr 15 23:37:25 2021 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,39 +0,0 @@
-//
-//  CompanyRow.swift
-//  LazyBear
-//
-//  Created by Dennis Concepción Martín on 3/4/21.
-//
-
-import SwiftUI
-
-//struct CompanyRow: View {
-//    var company: SearchModel
-//    
-//    var body: some View {
-//        HStack {
-//            VStack(alignment: .leading) {
-//                Text(company.symbol.uppercased())
-//                    .fontWeight(.semibold)
-//
-//                Text(company.securityName.capitalized)
-//                    .lineLimit(1)
-//            }
-//            
-//            Spacer()
-//            
-//            VStack(alignment: .trailing) {
-//                Text(company.currency)
-//                    .fontWeight(.semibold)
-//                
-//                Text(company.region)
-//            }
-//        }
-//    }
-//}
-//
-//struct CompanyRow_Previews: PreviewProvider {
-//    static var previews: some View {
-//        CompanyRow(company: SearchModel(securityName: "apple inc", symbol: "aapl", region: "US", currency: "USD"))
-//    }
-//}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LazyBear/Views/Search/Helpers/SearchedCompanyItem.swift	Fri Apr 16 17:03:48 2021 +0200
@@ -0,0 +1,39 @@
+//
+//  SearchedCompanyItem.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 3/4/21.
+//
+
+import SwiftUI
+
+struct SearchedCompanyItem: View {
+    var company: SearchResponse
+    
+    var body: some View {
+        HStack {
+            VStack(alignment: .leading) {
+                Text(company.symbol!.uppercased())
+                    .fontWeight(.semibold)
+
+                Text(company.securityName!.capitalized)
+                    .lineLimit(1)
+            }
+            
+            Spacer()
+            
+            VStack(alignment: .trailing) {
+                Text(company.currency!)
+                    .fontWeight(.semibold)
+                
+                Text(company.region!)
+            }
+        }
+    }
+}
+
+struct CompanyRow_Previews: PreviewProvider {
+    static var previews: some View {
+        SearchedCompanyItem(company: SearchResponse(currency: "USD", region: "US", securityName: "apple inc", symbol: "aapl"))
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LazyBear/Views/Search/Networking/Search.swift	Fri Apr 16 17:03:48 2021 +0200
@@ -0,0 +1,20 @@
+//
+//  Search.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 16/4/21.
+//
+
+import SwiftUI
+
+class Search: ObservableObject {
+    @Published var data = [SearchResponse()]
+    @Published var showSearchList = false
+    
+    func request(_ url: String) {
+        genericRequest(url: url, model: [SearchResponse].self) { response in
+            self.data = response
+            self.showSearchList = true
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LazyBear/Views/Search/Networking/SearchResponse.swift	Fri Apr 16 17:03:48 2021 +0200
@@ -0,0 +1,16 @@
+//
+//  SearchResponse.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 16/4/21.
+//
+
+import SwiftUI
+
+struct SearchResponse: Codable, Hashable {
+    var currency: String?
+    var exchange: String?
+    var region: String?
+    var securityName: String?
+    var symbol: String?
+}
--- a/LazyBear/Views/Search/SearchView.swift	Thu Apr 15 23:37:25 2021 +0200
+++ b/LazyBear/Views/Search/SearchView.swift	Fri Apr 16 17:03:48 2021 +0200
@@ -8,64 +8,59 @@
 import SwiftUI
 import SwiftlySearch
 
-//struct SearchView: View {
-//    @ObservedObject var searchData = SearchData()
-//    @State private var searchedText: String = ""
-//    @State private var showingSearchList = false
-//    @EnvironmentObject var hudManager: HudManager
-//    
-//    var body: some View {
-//        NavigationView {
-//            VStack(alignment: .leading) {
-//                if showingSearchList {
-//                    CompanyList(searchResult: searchData.searchResult)
-//                } else {
-//                    VStack(alignment: .center) {
-//                        Image("bearSleeping")
-//                            .resizable()
-//                            .scaledToFit()
-//                        
-//                        Text("Are you looking for something?")
-//                            .font(.title2)
-//                            .fontWeight(.semibold)
-//                            .multilineTextAlignment(.center)
-//                            .padding(.bottom)
-//                        
-//                        Text("Use the search bar to find your favourite company. Type the stock symbol or name, we'll find it!")
-//                            .multilineTextAlignment(.center)
-//                    }
-//                    .padding(.horizontal)
-//                }
-//            }
-//            .onDisappear { self.searchedText = ""; self.showingSearchList = false }
-//            .navigationTitle("Search")
-//            .navigationBarTitleDisplayMode(.inline)
-//            .navigationBarSearch($searchedText)
-//            .onChange(of: searchedText, perform: { searchedText in
-//                showSearchList(searchedText)
-//            })
-//            .toolbar {
-//                ToolbarItem(placement: .navigationBarTrailing) {
-//                    Button(action: { self.hudManager.showSearchHelper = true }) {
-//                        Image(systemName: "questionmark.circle")
-//                    }
-//                }
-//            }
-//        }
-//    }
-//    
-//    private func showSearchList(_ searchedText: String) {
-//        if !searchedText.isEmpty {
-//            // Encode string with spaces
-//            let encodedSearchedText = searchedText.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
-//            searchData.get(encodedSearchedText!)
-//            self.showingSearchList = true
-//        }
-//    }
-//}
-//
-//struct SearchView_Previews: PreviewProvider {
-//    static var previews: some View {
-//        SearchView()
-//    }
-//}
+struct SearchView: View {
+    @ObservedObject var search = Search()
+    @State private var searchedText = String()
+    @EnvironmentObject var hudManager: HudManager
+    
+    var body: some View {
+        NavigationView {
+            VStack(alignment: .leading) {
+                if search.showSearchList {
+                    CompanyList(searchResult: search.data)
+                } else {
+                    VStack(alignment: .center) {
+                        Image("bearSleeping")
+                            .resizable()
+                            .scaledToFit()
+                        
+                        Text("Are you looking for something?")
+                            .font(.title2)
+                            .fontWeight(.semibold)
+                            .multilineTextAlignment(.center)
+                            .padding(.bottom)
+                        
+                        Text("Use the search bar to find your favourite company. Type the stock symbol or name, we'll find it!")
+                            .multilineTextAlignment(.center)
+                    }
+                    .padding(.horizontal)
+                }
+            }
+            .navigationTitle("Search")
+            .navigationBarTitleDisplayMode(.inline)
+            .navigationBarSearch($searchedText)
+            .onChange(of: searchedText, perform: { searchedText in
+                if !searchedText.isEmpty {
+                    // Encode string with spaces
+                    let encodedSearchedText = searchedText.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
+                    search.request("https://api.lazybear.app/search/\(encodedSearchedText ?? "")")
+                } else {
+                    search.showSearchList = false
+                }
+            })
+            .toolbar {
+                ToolbarItem(placement: .navigationBarTrailing) {
+                    Button(action: { self.hudManager.showSearchHelper = true }) {
+                        Image(systemName: "questionmark.circle")
+                    }
+                }
+            }
+        }
+    }
+}
+
+struct SearchView_Previews: PreviewProvider {
+    static var previews: some View {
+        SearchView()
+    }
+}