Mercurial > public > lazybear
view 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 source
// // WatchlistCreatorList.swift // LazyBear // // Created by Dennis Concepción Martín on 2/5/21. // import SwiftUI import Bazooka struct WatchlistCreatorList: View { @ObservedObject var watchlistCreatorClass: WatchlistCreatorClass @State private var searchedCompany = String() @State private var companies = [SearchResponse]() @Environment(\.presentationMode) private var presentationMode @Environment(\.managedObjectContext) private var moc var body: some View { NavigationView { VStack { WatchlistCreatorSearchBar(searchedCompany: $searchedCompany) List(companies, id: \.self) { company in WatchlistCreatorRow(company: company, presentationMode: presentationMode, watchlistCreatorClass: watchlistCreatorClass) .environment(\.managedObjectContext, self.moc) } } .navigationTitle("Search") .navigationBarTitleDisplayMode(.inline) .onChange(of: searchedCompany, perform: { searchedCompany in encodeAndRequest(searchedCompany) }) .toolbar { ToolbarItem(placement: .navigationBarLeading) { Button("Cancel", action: { presentationMode.wrappedValue.dismiss() }) } } } } /* 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 { static var previews: some View { WatchlistCreatorList(watchlistCreatorClass: WatchlistCreatorClass()) } }