comparison 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
comparison
equal deleted inserted replaced
392:13f3578def61 393:0a4c399170c4
1 //
2 // WatchlistCreatorList.swift
3 // LazyBear
4 //
5 // Created by Dennis Concepción Martín on 2/5/21.
6 //
7
8 import SwiftUI
9
10 struct WatchlistCreatorList: View {
11 @ObservedObject var watchlistCreatorClass: WatchlistCreatorClass
12 @State private var searchedCompany = String()
13 @State private var companies = [SearchResponse]()
14
15 @Environment(\.presentationMode) private var presentationMode
16
17 var body: some View {
18 NavigationView {
19 VStack {
20 WatchlistCreatorSearchBar(searchedCompany: $searchedCompany)
21
22 List(companies, id: \.self) { company in
23 WatchlistCreatorRow(company: company, presentationMode: presentationMode, watchlistCreatorClass: watchlistCreatorClass)
24 }
25 }
26 .navigationTitle("Search")
27 .navigationBarTitleDisplayMode(.inline)
28 .onChange(of: searchedCompany, perform: { searchedCompany in
29 if !searchedCompany.isEmpty {
30 // Encode string with spaces
31 let encodedSearchedCompany = searchedCompany.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
32 request("https://api.lazybear.app/search/text=\(encodedSearchedCompany ?? "")")
33 }
34 })
35
36 .toolbar {
37 ToolbarItem(placement: .navigationBarLeading) {
38 Button("Cancel", action: { presentationMode.wrappedValue.dismiss() })
39 }
40 }
41 }
42 }
43
44 private func request(_ url: String) {
45 genericRequest(url: url, model: [SearchResponse].self) { response in
46 self.companies = response
47 }
48 }
49 }
50
51 struct SearchCompanies_Previews: PreviewProvider {
52 static var previews: some View {
53 WatchlistCreatorList(watchlistCreatorClass: WatchlistCreatorClass())
54 }
55 }