Mercurial > public > lazybear
comparison 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 |
comparison
equal
deleted
inserted
replaced
416:1662a41e2c1a | 417:5f21f7c23c5e |
---|---|
8 import SwiftUI | 8 import SwiftUI |
9 import Bazooka | 9 import Bazooka |
10 | 10 |
11 struct WatchlistCreatorList: View { | 11 struct WatchlistCreatorList: View { |
12 @ObservedObject var watchlistCreatorClass: WatchlistCreatorClass | 12 @ObservedObject var watchlistCreatorClass: WatchlistCreatorClass |
13 | |
13 @State private var searchedCompany = String() | 14 @State private var searchedCompany = String() |
14 @State private var companies = [SearchResponse]() | 15 @State private var companies = [SearchResponse]() |
15 | 16 |
16 @Environment(\.presentationMode) private var presentationMode | 17 @Environment(\.presentationMode) private var presentationMode |
17 @Environment(\.managedObjectContext) private var moc | 18 @Environment(\.managedObjectContext) private var moc |
27 } | 28 } |
28 } | 29 } |
29 .navigationTitle("Search") | 30 .navigationTitle("Search") |
30 .navigationBarTitleDisplayMode(.inline) | 31 .navigationBarTitleDisplayMode(.inline) |
31 .onChange(of: searchedCompany, perform: { searchedCompany in | 32 .onChange(of: searchedCompany, perform: { searchedCompany in |
32 if !searchedCompany.isEmpty { | 33 encodeAndRequest(searchedCompany) |
33 // Encode string with spaces | |
34 let encodedSearchedCompany = searchedCompany.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) | |
35 request("https://api.lazybear.app/search/text=\(encodedSearchedCompany ?? "")") | |
36 } | |
37 }) | 34 }) |
38 | 35 |
39 .toolbar { | 36 .toolbar { |
40 ToolbarItem(placement: .navigationBarLeading) { | 37 ToolbarItem(placement: .navigationBarLeading) { |
41 Button("Cancel", action: { presentationMode.wrappedValue.dismiss() }) | 38 Button("Cancel", action: { presentationMode.wrappedValue.dismiss() }) |
42 } | 39 } |
43 } | 40 } |
44 } | 41 } |
45 } | 42 } |
46 | 43 |
44 /* | |
45 Get companies from the API that matches the searched text | |
46 */ | |
47 private func request(_ url: String) { | 47 private func request(_ url: String) { |
48 let bazooka = Bazooka() | 48 let bazooka = Bazooka() |
49 bazooka.request(url: url, model: [SearchResponse].self) { response in | 49 bazooka.request(url: url, model: [SearchResponse].self) { response in |
50 self.companies = response | 50 self.companies = response |
51 } | |
52 } | |
53 | |
54 /* | |
55 1) Check if searchedCompany is empty | |
56 2) Encode white spaces | |
57 3) Request API | |
58 */ | |
59 private func encodeAndRequest(_ searchedCompany: String) { | |
60 if !searchedCompany.isEmpty { | |
61 let encodedSearchedCompany = searchedCompany.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) | |
62 request("https://api.lazybear.app/search/text=\(encodedSearchedCompany ?? "")") | |
51 } | 63 } |
52 } | 64 } |
53 } | 65 } |
54 | 66 |
55 struct SearchCompanies_Previews: PreviewProvider { | 67 struct SearchCompanies_Previews: PreviewProvider { |