140 lines
4.3 KiB
Swift
140 lines
4.3 KiB
Swift
//
|
|
// SearchView.swift - The view to execute searches and display the results.
|
|
//
|
|
// Created by Marcus Handte on 28.05.22.
|
|
//
|
|
|
|
import SwiftUI
|
|
import class MoWeStaApi.LocationHierarchyPage
|
|
import class MoWeStaApi.LocationHierarchy
|
|
import class MoWeStaApi.DefaultAPI
|
|
|
|
struct SearchView: View {
|
|
|
|
@ObservedObject var state: SearchState
|
|
|
|
var body: some View {
|
|
VStack(spacing: 0) {
|
|
List {
|
|
HStack {
|
|
Image(systemName: "magnifyingglass")
|
|
.font(.body)
|
|
.foregroundColor(.gray)
|
|
TextField("action_search", text: $state.searchTerm)
|
|
.font(.body)
|
|
.frame(maxWidth: .infinity)
|
|
.disableAutocorrection(true)
|
|
.onChange(of: state.searchTerm) { _ in
|
|
scheduleSearch(delay: 0.25)
|
|
}
|
|
}
|
|
.padding(8)
|
|
.overlay(RoundedRectangle(cornerRadius: 6)
|
|
.stroke(lineWidth: 2)
|
|
.foregroundColor(Color.gray))
|
|
if let r = state.searchResult {
|
|
ForEach(r.elements, id: \.id) {
|
|
element in
|
|
SearchEntryView(location: element)
|
|
}
|
|
}
|
|
}.listStyle(.plain)
|
|
if state.loading {
|
|
ProgressBarView(animating: $state.loading)
|
|
.frame(height: 3)
|
|
}
|
|
}
|
|
.navigationTitle("navigation_search")
|
|
}
|
|
|
|
func scheduleSearch(delay: Double) {
|
|
state.searchResult = nil
|
|
state.requestId += 1
|
|
state.loading = true
|
|
let request = state.requestId
|
|
let term = state.searchTerm
|
|
if term.count > 1 {
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
|
|
if request == state.requestId {
|
|
executeSearch(request: request, term: term)
|
|
}
|
|
}
|
|
} else {
|
|
state.loading = false
|
|
}
|
|
}
|
|
|
|
func executeSearch(request: Int, term: String) {
|
|
DefaultAPI.searchLocations(language: getSearchLanguage(), term: state.searchTerm, pageSize: 40) {
|
|
result, error in
|
|
if (request == state.requestId) {
|
|
if let r = result {
|
|
state.searchResult = r;
|
|
state.loading = false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func getSearchLanguage() -> DefaultAPI.Language_searchLocations {
|
|
let lang = TextBindings.getLanguageCode()
|
|
if lang == "de" {
|
|
return DefaultAPI.Language_searchLocations.de
|
|
} else {
|
|
return DefaultAPI.Language_searchLocations.en
|
|
}
|
|
}
|
|
}
|
|
|
|
struct SearchEntryView: View {
|
|
|
|
var location: LocationHierarchy
|
|
|
|
var body: some View {
|
|
NavigationLink(destination: {
|
|
switch location.type {
|
|
case .country:
|
|
CountryView(countryId: location.id!)
|
|
case .state:
|
|
StateView(stateId: location.id!)
|
|
case .city:
|
|
CityView(cityId: location.id!)
|
|
}
|
|
}) {
|
|
HStack {
|
|
let name = TextBindings.getLocationName(location)
|
|
let region = TextBindings.getRegionName(location)
|
|
Image(systemName: ImageBindings.getLocationIcon(location))
|
|
.font(.title)
|
|
.padding(.trailing, 2)
|
|
VStack(alignment:.leading) {
|
|
Text(name)
|
|
.font(.body)
|
|
if region.count > 0 {
|
|
Text(region)
|
|
.font(.caption)
|
|
}
|
|
}
|
|
}.padding(2)
|
|
}
|
|
}
|
|
}
|
|
|
|
class SearchState: ObservableObject {
|
|
|
|
var searchTerm = "" { willSet { objectWillChange.send() }}
|
|
var requestId = 0 { willSet { objectWillChange.send() }}
|
|
var searchResult: LocationHierarchyPage? { willSet { objectWillChange.send() }}
|
|
var searchError = false { willSet { objectWillChange.send() }}
|
|
var loading = false { willSet { objectWillChange.send() }}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SearchView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
SearchView(state: SearchState())
|
|
}
|
|
}
|