view LazyBear/Views/Profile/Helpers/WatchlistCreatorList.swift @ 404:444ec927d62f

Install Bazooka package
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Thu, 03 Jun 2021 16:05:01 +0200
parents 4c90e5b18632
children 5f21f7c23c5e
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
                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) {
        let bazooka = Bazooka()
        bazooka.request(url: url, model: [SearchResponse].self) { response in
            self.companies = response
        }
    }
}

struct SearchCompanies_Previews: PreviewProvider {
    static var previews: some View {
        WatchlistCreatorList(watchlistCreatorClass: WatchlistCreatorClass())
    }
}