Mercurial > public > lazybear
diff LazyBear/Views/Profile/Helpers/WatchlistCreatorList.swift @ 393:0a4c399170c4
Implementing WatchlistCreator
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Sun, 02 May 2021 12:41:20 +0200 |
parents | |
children | 4c90e5b18632 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LazyBear/Views/Profile/Helpers/WatchlistCreatorList.swift Sun May 02 12:41:20 2021 +0200 @@ -0,0 +1,55 @@ +// +// WatchlistCreatorList.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 2/5/21. +// + +import SwiftUI + +struct WatchlistCreatorList: View { + @ObservedObject var watchlistCreatorClass: WatchlistCreatorClass + @State private var searchedCompany = String() + @State private var companies = [SearchResponse]() + + @Environment(\.presentationMode) private var presentationMode + + var body: some View { + NavigationView { + VStack { + WatchlistCreatorSearchBar(searchedCompany: $searchedCompany) + + List(companies, id: \.self) { company in + WatchlistCreatorRow(company: company, presentationMode: presentationMode, watchlistCreatorClass: watchlistCreatorClass) + } + } + .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 ?? "")") + } + }) + + .toolbar { + ToolbarItem(placement: .navigationBarLeading) { + Button("Cancel", action: { presentationMode.wrappedValue.dismiss() }) + } + } + } + } + + private func request(_ url: String) { + genericRequest(url: url, model: [SearchResponse].self) { response in + self.companies = response + } + } +} + +struct SearchCompanies_Previews: PreviewProvider { + static var previews: some View { + WatchlistCreatorList(watchlistCreatorClass: WatchlistCreatorClass()) + } +}