mirror of
https://github.com/yattee/yattee.git
synced 2025-08-06 10:44:06 +00:00
Add search state object
This commit is contained in:
@@ -2,6 +2,7 @@ import SwiftUI
|
||||
|
||||
struct ContentView: View {
|
||||
@StateObject private var navigationState = NavigationState()
|
||||
@StateObject private var searchState = SearchState()
|
||||
|
||||
#if os(iOS)
|
||||
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
|
||||
@@ -20,7 +21,9 @@ struct ContentView: View {
|
||||
#elseif os(tvOS)
|
||||
TVNavigationView()
|
||||
#endif
|
||||
}.environmentObject(navigationState)
|
||||
}
|
||||
.environmentObject(navigationState)
|
||||
.environmentObject(searchState)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -8,16 +8,13 @@ struct SearchView: View {
|
||||
@Default(.searchDate) private var searchDate
|
||||
@Default(.searchDuration) private var searchDuration
|
||||
|
||||
@ObservedObject private var store = Store<[Video]>()
|
||||
@ObservedObject private var query = SearchQuery()
|
||||
@EnvironmentObject<SearchState> private var state
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
if !store.collection.isEmpty {
|
||||
VideosView(videos: store.collection)
|
||||
}
|
||||
VideosView(videos: state.store.collection)
|
||||
|
||||
if store.collection.isEmpty && !resource.isLoading && !query.isEmpty {
|
||||
if state.store.collection.isEmpty && !state.isLoading && !state.query.isEmpty {
|
||||
Text("No results")
|
||||
|
||||
if searchFiltersActive {
|
||||
@@ -31,7 +28,7 @@ struct SearchView: View {
|
||||
}
|
||||
.searchable(text: $queryText)
|
||||
.onAppear {
|
||||
changeQuery {
|
||||
state.changeQuery { query in
|
||||
query.query = queryText
|
||||
query.sortBy = searchSortOrder
|
||||
query.date = searchDate
|
||||
@@ -39,34 +36,22 @@ struct SearchView: View {
|
||||
}
|
||||
}
|
||||
.onChange(of: queryText) { queryText in
|
||||
changeQuery { query.query = queryText }
|
||||
state.changeQuery { query in query.query = queryText }
|
||||
}
|
||||
.onChange(of: searchSortOrder) { order in
|
||||
changeQuery { query.sortBy = order }
|
||||
state.changeQuery { query in query.sortBy = order }
|
||||
}
|
||||
.onChange(of: searchDate) { date in
|
||||
changeQuery { query.date = date }
|
||||
state.changeQuery { query in query.date = date }
|
||||
}
|
||||
.onChange(of: searchDuration) { duration in
|
||||
changeQuery { query.duration = duration }
|
||||
state.changeQuery { query in query.duration = duration }
|
||||
}
|
||||
#if !os(tvOS)
|
||||
.navigationTitle("Search")
|
||||
#endif
|
||||
}
|
||||
|
||||
func changeQuery(_ change: @escaping () -> Void = {}) {
|
||||
resource.removeObservers(ownedBy: store)
|
||||
change()
|
||||
|
||||
resource.addObserver(store)
|
||||
resource.loadIfNeeded()
|
||||
}
|
||||
|
||||
var resource: Resource {
|
||||
InvidiousAPI.shared.search(query)
|
||||
}
|
||||
|
||||
var searchFiltersActive: Bool {
|
||||
searchDate != nil || searchDuration != nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user