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-06-28 20:32:13 +05:30
|
|
|
@Default(.searchQuery) var query
|
2021-06-12 02:41:59 +05:30
|
|
|
|
2021-06-28 16:13:07 +05:30
|
|
|
@ObservedObject private var store = Store<[Video]>()
|
|
|
|
|
2021-06-11 18:06:26 +05:30
|
|
|
var body: some View {
|
2021-06-28 16:13:07 +05:30
|
|
|
VideosView(videos: store.collection)
|
2021-06-12 02:41:59 +05:30
|
|
|
.searchable(text: $query)
|
2021-06-28 20:32:13 +05:30
|
|
|
.onAppear {
|
|
|
|
queryChanged(new: query)
|
|
|
|
}
|
2021-06-28 16:13:07 +05:30
|
|
|
.onChange(of: query) { newQuery in
|
2021-06-28 20:32:13 +05:30
|
|
|
queryChanged(old: query, new: newQuery)
|
2021-06-28 16:13:07 +05:30
|
|
|
}
|
2021-06-11 18:06:26 +05:30
|
|
|
}
|
2021-06-12 03:24:00 +05:30
|
|
|
|
2021-06-28 20:32:13 +05:30
|
|
|
func queryChanged(old: String? = nil, new: String) {
|
|
|
|
if old != nil {
|
|
|
|
let oldResource = resource(old!)
|
|
|
|
oldResource.removeObservers(ownedBy: store)
|
|
|
|
}
|
2021-06-28 16:13:07 +05:30
|
|
|
|
|
|
|
let resource = resource(new)
|
|
|
|
resource.addObserver(store)
|
|
|
|
resource.loadIfNeeded()
|
|
|
|
}
|
2021-06-11 18:06:26 +05:30
|
|
|
|
2021-06-28 16:13:07 +05:30
|
|
|
func resource(_ query: String) -> Resource {
|
|
|
|
InvidiousAPI.shared.search(query)
|
2021-06-11 18:06:26 +05:30
|
|
|
}
|
|
|
|
}
|