yattee/Shared/Views/SearchView.swift

62 lines
1.8 KiB
Swift
Raw Normal View History

2021-06-28 15:02:13 +00:00
import Defaults
2021-06-28 10:43:07 +00:00
import Siesta
2021-06-11 12:36:26 +00:00
import SwiftUI
struct SearchView: View {
2021-07-07 22:39:18 +00:00
@Default(.searchQuery) private var queryText
@Default(.searchSortOrder) private var searchSortOrder
@Default(.searchDate) private var searchDate
@Default(.searchDuration) private var searchDuration
2021-06-11 21:11:59 +00:00
2021-07-29 22:34:13 +00:00
@EnvironmentObject<SearchState> private var state
2021-06-28 10:43:07 +00:00
2021-06-11 12:36:26 +00:00
var body: some View {
2021-07-07 22:39:18 +00:00
VStack {
2021-07-29 22:34:13 +00:00
VideosView(videos: state.store.collection)
2021-07-07 22:39:18 +00:00
2021-07-29 22:34:13 +00:00
if state.store.collection.isEmpty && !state.isLoading && !state.query.isEmpty {
2021-07-07 22:39:18 +00:00
Text("No results")
if searchFiltersActive {
Button("Reset search filters") {
Defaults.reset(.searchDate, .searchDuration)
}
}
Spacer()
2021-06-28 10:43:07 +00:00
}
2021-07-07 22:39:18 +00:00
}
.onAppear {
2021-07-29 22:34:13 +00:00
state.changeQuery { query in
2021-07-07 22:39:18 +00:00
query.query = queryText
query.sortBy = searchSortOrder
query.date = searchDate
query.duration = searchDuration
}
}
2021-09-13 20:41:16 +00:00
.onChange(of: state.query.query) { queryText in
2021-07-29 22:34:13 +00:00
state.changeQuery { query in query.query = queryText }
2021-07-07 22:39:18 +00:00
}
.onChange(of: searchSortOrder) { order in
2021-07-29 22:34:13 +00:00
state.changeQuery { query in query.sortBy = order }
2021-07-07 22:39:18 +00:00
}
.onChange(of: searchDate) { date in
2021-07-29 22:34:13 +00:00
state.changeQuery { query in query.date = date }
2021-07-07 22:39:18 +00:00
}
.onChange(of: searchDuration) { duration in
2021-07-29 22:34:13 +00:00
state.changeQuery { query in query.duration = duration }
2021-07-07 22:39:18 +00:00
}
2021-07-11 20:52:49 +00:00
#if !os(tvOS)
2021-09-13 20:41:16 +00:00
.navigationTitle(navigationTitle)
2021-07-11 20:52:49 +00:00
#endif
2021-06-11 12:36:26 +00:00
}
2021-06-11 21:54:00 +00:00
2021-09-13 20:41:16 +00:00
var navigationTitle: String {
state.query.query.isEmpty ? "Search" : "Search: \"\(state.query.query)\""
}
2021-07-07 22:39:18 +00:00
var searchFiltersActive: Bool {
searchDate != nil || searchDuration != nil
}
2021-06-11 12:36:26 +00:00
}