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(.searchSortOrder) private var searchSortOrder
|
|
|
|
@Default(.searchDate) private var searchDate
|
|
|
|
@Default(.searchDuration) private var searchDuration
|
2021-06-12 02:41:59 +05:30
|
|
|
|
2021-09-19 18:12:47 +05:30
|
|
|
@EnvironmentObject<Recents> private var recents
|
2021-09-25 13:48:22 +05:30
|
|
|
@EnvironmentObject<SearchModel> private var state
|
2021-06-28 16:13:07 +05:30
|
|
|
|
2021-09-19 18:12:47 +05:30
|
|
|
@Environment(\.navigationStyle) private var navigationStyle
|
|
|
|
|
|
|
|
@State private var presentingClearConfirmation = false
|
|
|
|
@State private var recentsChanged = false
|
|
|
|
|
2021-09-19 16:36:54 +05:30
|
|
|
private var query: SearchQuery?
|
|
|
|
|
|
|
|
init(_ query: SearchQuery? = nil) {
|
|
|
|
self.query = query
|
|
|
|
}
|
|
|
|
|
2021-06-11 18:06:26 +05:30
|
|
|
var body: some View {
|
2021-09-19 18:12:47 +05:30
|
|
|
Group {
|
|
|
|
if navigationStyle == .tab && state.queryText.isEmpty {
|
|
|
|
VStack {
|
|
|
|
if !recentItems.isEmpty {
|
|
|
|
recentQueries
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
VideosView(videos: state.store.collection)
|
2021-07-08 04:09:18 +05:30
|
|
|
|
2021-09-19 18:12:47 +05:30
|
|
|
if state.store.collection.isEmpty && !state.isLoading && !state.query.isEmpty {
|
|
|
|
Text("No results")
|
2021-07-08 04:09:18 +05:30
|
|
|
|
2021-09-19 18:12:47 +05:30
|
|
|
if searchFiltersActive {
|
|
|
|
Button("Reset search filters") {
|
|
|
|
Defaults.reset(.searchDate, .searchDuration)
|
|
|
|
}
|
2021-07-08 04:09:18 +05:30
|
|
|
}
|
|
|
|
|
2021-09-19 18:12:47 +05:30
|
|
|
Spacer()
|
|
|
|
}
|
2021-06-28 16:13:07 +05:30
|
|
|
}
|
2021-07-08 04:09:18 +05:30
|
|
|
}
|
|
|
|
.onAppear {
|
2021-09-19 16:36:54 +05:30
|
|
|
if query != nil {
|
2021-09-19 18:12:47 +05:30
|
|
|
if navigationStyle == .tab {
|
|
|
|
state.queryText = query!.query
|
|
|
|
}
|
2021-09-19 16:36:54 +05:30
|
|
|
state.resetQuery(query!)
|
2021-07-08 04:09:18 +05:30
|
|
|
}
|
|
|
|
}
|
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-19 18:12:47 +05:30
|
|
|
var recentQueries: some View {
|
|
|
|
List {
|
|
|
|
Section(header: Text("Recents")) {
|
|
|
|
ForEach(recentItems) { item in
|
|
|
|
Button(item.title) {
|
|
|
|
state.queryText = item.title
|
|
|
|
state.changeQuery { query in query.query = item.title }
|
|
|
|
}
|
|
|
|
#if os(iOS)
|
|
|
|
.swipeActions(edge: .trailing) {
|
|
|
|
clearButton(item)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
.redrawOn(change: recentsChanged)
|
2021-09-19 18:12:47 +05:30
|
|
|
|
|
|
|
clearAllButton
|
|
|
|
}
|
|
|
|
#if os(iOS)
|
|
|
|
.listStyle(.insetGrouped)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
func clearButton(_ item: RecentItem) -> some View {
|
|
|
|
Button(role: .destructive) {
|
|
|
|
recents.close(item)
|
|
|
|
recentsChanged.toggle()
|
|
|
|
} label: {
|
|
|
|
Label("Delete", systemImage: "trash")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var clearAllButton: some View {
|
|
|
|
Button("Clear All", role: .destructive) {
|
|
|
|
presentingClearConfirmation = true
|
|
|
|
}
|
|
|
|
.confirmationDialog("Clear All", isPresented: $presentingClearConfirmation) {
|
|
|
|
Button("Clear All", role: .destructive) {
|
|
|
|
recents.clearQueries()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 02:11:16 +05:30
|
|
|
var navigationTitle: String {
|
2021-09-19 18:12:47 +05:30
|
|
|
if state.query.query.isEmpty || (navigationStyle == .tab && state.queryText.isEmpty) {
|
|
|
|
return "Search"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Search: \"\(state.query.query)\""
|
2021-09-14 02:11:16 +05:30
|
|
|
}
|
|
|
|
|
2021-07-08 04:09:18 +05:30
|
|
|
var searchFiltersActive: Bool {
|
|
|
|
searchDate != nil || searchDuration != nil
|
|
|
|
}
|
2021-09-19 18:12:47 +05:30
|
|
|
|
|
|
|
var recentItems: [RecentItem] {
|
|
|
|
Defaults[.recentlyOpened].filter { $0.type == .query }.reversed()
|
|
|
|
}
|
2021-06-11 18:06:26 +05:30
|
|
|
}
|