1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/Apple TV/SearchView.swift
2021-06-28 17:03:22 +02:00

36 lines
884 B
Swift

import Defaults
import Siesta
import SwiftUI
struct SearchView: View {
@Default(.searchQuery) var query
@ObservedObject private var store = Store<[Video]>()
var body: some View {
VideosView(videos: store.collection)
.searchable(text: $query)
.onAppear {
queryChanged(new: query)
}
.onChange(of: query) { newQuery in
queryChanged(old: query, new: newQuery)
}
}
func queryChanged(old: String? = nil, new: String) {
if old != nil {
let oldResource = resource(old!)
oldResource.removeObservers(ownedBy: store)
}
let resource = resource(new)
resource.addObserver(store)
resource.loadIfNeeded()
}
func resource(_ query: String) -> Resource {
InvidiousAPI.shared.search(query)
}
}