view LazyBear/Views/Search/SearchView.swift @ 391:8ec37b2baafd

Implementing CreateNewWatchlist
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Fri, 30 Apr 2021 20:25:52 +0200
parents 6303385b3629
children 13f3578def61
line wrap: on
line source

//
//  SearchView.swift
//  LazyBear
//
//  Created by Dennis Concepción Martín on 1/4/21.
//

import SwiftUI
import SwiftlySearch

struct SearchView: View {
    // Only unseful when it's called from Profile View
    var calledFromProfileView: Bool?
    var newWatchlistClass: NewWatchlistClass?
    
    @ObservedObject var search = Search()
    @Environment(\.presentationMode) private var presentationMode
    @State private var searchedText = String()
    
    var body: some View {
        NavigationView {
            VStack(alignment: .leading) {
                if search.showSearchList {
                    CompanyList(searchResult: search.data, calledFromProfileView: calledFromProfileView, newWatchlistClass: newWatchlistClass)
                } else {
                    VStack(alignment: .center) {
                        Image("bearSleeping")
                            .resizable()
                            .scaledToFit()
                        
                        Text("Are you looking for something?")
                            .font(.title2)
                            .fontWeight(.semibold)
                            .multilineTextAlignment(.center)
                            .padding(.bottom)
                        
                        Text("Use the search bar to find your favourite company. Type the stock symbol or name, we'll find it!")
                            .multilineTextAlignment(.center)
                    }
                    .padding(.horizontal)
                }
            }
            .navigationTitle("Search")
            .navigationBarTitleDisplayMode(.inline)
            .navigationBarSearch($searchedText)
            .onChange(of: searchedText, perform: { searchedText in
                if !searchedText.isEmpty {
                    // Encode string with spaces
                    let encodedSearchedText = searchedText.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
                    search.request("https://api.lazybear.app/search/text=\(encodedSearchedText ?? "")")
                } else {
                    search.showSearchList = false
                }
            })
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

struct SearchView_Previews: PreviewProvider {
    static var previews: some View {
        SearchView()
    }
}