diff LazyBear/Views/Profile/Helpers/WatchlistCreatorList.swift @ 417:5f21f7c23c5e

Add comments and clean code
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Fri, 11 Jun 2021 11:37:42 +0200
parents 444ec927d62f
children c78d5b5b3bda
line wrap: on
line diff
--- a/LazyBear/Views/Profile/Helpers/WatchlistCreatorList.swift	Wed Jun 09 20:26:28 2021 +0200
+++ b/LazyBear/Views/Profile/Helpers/WatchlistCreatorList.swift	Fri Jun 11 11:37:42 2021 +0200
@@ -10,6 +10,7 @@
 
 struct WatchlistCreatorList: View {
     @ObservedObject var watchlistCreatorClass: WatchlistCreatorClass
+    
     @State private var searchedCompany = String()
     @State private var companies = [SearchResponse]()
     
@@ -29,11 +30,7 @@
             .navigationTitle("Search")
             .navigationBarTitleDisplayMode(.inline)
             .onChange(of: searchedCompany, perform: { searchedCompany in
-                if !searchedCompany.isEmpty {
-                    // Encode string with spaces
-                    let encodedSearchedCompany = searchedCompany.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
-                    request("https://api.lazybear.app/search/text=\(encodedSearchedCompany ?? "")")
-                }
+                encodeAndRequest(searchedCompany)
             })
             
             .toolbar {
@@ -44,12 +41,27 @@
         }
     }
     
+    /*
+     Get companies from the API that matches the searched text
+     */
     private func request(_ url: String) {
         let bazooka = Bazooka()
         bazooka.request(url: url, model: [SearchResponse].self) { response in
             self.companies = response
         }
     }
+    
+    /*
+     1) Check if searchedCompany is empty
+     2) Encode white spaces
+     3) Request API
+     */
+    private func encodeAndRequest(_ searchedCompany: String) {
+        if !searchedCompany.isEmpty {
+            let encodedSearchedCompany = searchedCompany.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
+            request("https://api.lazybear.app/search/text=\(encodedSearchedCompany ?? "")")
+        }
+    }
 }
 
 struct SearchCompanies_Previews: PreviewProvider {