yattee/Shared/SearchView.swift
2021-07-30 00:45:24 +02:00

59 lines
1.8 KiB
Swift

import Defaults
import Siesta
import SwiftUI
struct SearchView: View {
@Default(.searchQuery) private var queryText
@Default(.searchSortOrder) private var searchSortOrder
@Default(.searchDate) private var searchDate
@Default(.searchDuration) private var searchDuration
@EnvironmentObject<SearchState> private var state
var body: some View {
VStack {
VideosView(videos: state.store.collection)
if state.store.collection.isEmpty && !state.isLoading && !state.query.isEmpty {
Text("No results")
if searchFiltersActive {
Button("Reset search filters") {
Defaults.reset(.searchDate, .searchDuration)
}
}
Spacer()
}
}
.searchable(text: $queryText)
.onAppear {
state.changeQuery { query in
query.query = queryText
query.sortBy = searchSortOrder
query.date = searchDate
query.duration = searchDuration
}
}
.onChange(of: queryText) { queryText in
state.changeQuery { query in query.query = queryText }
}
.onChange(of: searchSortOrder) { order in
state.changeQuery { query in query.sortBy = order }
}
.onChange(of: searchDate) { date in
state.changeQuery { query in query.date = date }
}
.onChange(of: searchDuration) { duration in
state.changeQuery { query in query.duration = duration }
}
#if !os(tvOS)
.navigationTitle("Search")
#endif
}
var searchFiltersActive: Bool {
searchDate != nil || searchDuration != nil
}
}