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