1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 13:50:32 +05:30
yattee/Shared/SearchView.swift

74 lines
2.1 KiB
Swift
Raw Normal View History

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-06-28 16:13:07 +05:30
@ObservedObject private var store = Store<[Video]>()
2021-07-08 04:09:18 +05:30
@ObservedObject private var query = SearchQuery()
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 {
if !store.collection.isEmpty {
VideosView(videos: store.collection)
2021-06-28 20:32:13 +05:30
}
2021-07-08 04:09:18 +05:30
2021-07-12 02:22:49 +05:30
if store.collection.isEmpty && !resource.isLoading && !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
}
.searchable(text: $queryText)
.onAppear {
changeQuery {
query.query = queryText
query.sortBy = searchSortOrder
query.date = searchDate
query.duration = searchDuration
}
}
.onChange(of: queryText) { queryText in
changeQuery { query.query = queryText }
}
.onChange(of: searchSortOrder) { order in
changeQuery { query.sortBy = order }
}
.onChange(of: searchDate) { date in
changeQuery { query.date = date }
}
.onChange(of: searchDuration) { duration in
changeQuery { query.duration = duration }
}
2021-07-12 02:22:49 +05:30
#if !os(tvOS)
.navigationTitle("Search")
#endif
2021-06-11 18:06:26 +05:30
}
2021-06-12 03:24:00 +05:30
2021-07-08 04:09:18 +05:30
func changeQuery(_ change: @escaping () -> Void = {}) {
resource.removeObservers(ownedBy: store)
change()
2021-06-28 16:13:07 +05:30
resource.addObserver(store)
resource.loadIfNeeded()
}
2021-06-11 18:06:26 +05:30
2021-07-08 04:09:18 +05:30
var resource: Resource {
2021-06-28 16:13:07 +05:30
InvidiousAPI.shared.search(query)
2021-06-11 18:06:26 +05:30
}
2021-07-08 04:09:18 +05:30
var searchFiltersActive: Bool {
searchDate != nil || searchDuration != nil
}
2021-06-11 18:06:26 +05:30
}