2021-06-28 15:02:13 +00:00
|
|
|
import Defaults
|
2021-06-28 10:43:07 +00:00
|
|
|
import Siesta
|
2021-06-11 12:36:26 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct SearchView: View {
|
2021-07-07 22:39:18 +00:00
|
|
|
@Default(.searchSortOrder) private var searchSortOrder
|
|
|
|
@Default(.searchDate) private var searchDate
|
|
|
|
@Default(.searchDuration) private var searchDuration
|
2021-06-11 21:11:59 +00:00
|
|
|
|
2021-09-19 12:42:47 +00:00
|
|
|
@EnvironmentObject<Recents> private var recents
|
2021-07-29 22:34:13 +00:00
|
|
|
@EnvironmentObject<SearchState> private var state
|
2021-06-28 10:43:07 +00:00
|
|
|
|
2021-09-19 12:42:47 +00:00
|
|
|
@Environment(\.navigationStyle) private var navigationStyle
|
|
|
|
|
|
|
|
@State private var presentingClearConfirmation = false
|
|
|
|
@State private var recentsChanged = false
|
|
|
|
|
2021-09-19 11:06:54 +00:00
|
|
|
private var query: SearchQuery?
|
|
|
|
|
|
|
|
init(_ query: SearchQuery? = nil) {
|
|
|
|
self.query = query
|
|
|
|
}
|
|
|
|
|
2021-06-11 12:36:26 +00:00
|
|
|
var body: some View {
|
2021-09-19 12:42:47 +00:00
|
|
|
Group {
|
|
|
|
if navigationStyle == .tab && state.queryText.isEmpty {
|
|
|
|
VStack {
|
|
|
|
if !recentItems.isEmpty {
|
|
|
|
recentQueries
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
VideosView(videos: state.store.collection)
|
2021-07-07 22:39:18 +00:00
|
|
|
|
2021-09-19 12:42:47 +00:00
|
|
|
if state.store.collection.isEmpty && !state.isLoading && !state.query.isEmpty {
|
|
|
|
Text("No results")
|
2021-07-07 22:39:18 +00:00
|
|
|
|
2021-09-19 12:42:47 +00:00
|
|
|
if searchFiltersActive {
|
|
|
|
Button("Reset search filters") {
|
|
|
|
Defaults.reset(.searchDate, .searchDuration)
|
|
|
|
}
|
2021-07-07 22:39:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 12:42:47 +00:00
|
|
|
Spacer()
|
|
|
|
}
|
2021-06-28 10:43:07 +00:00
|
|
|
}
|
2021-07-07 22:39:18 +00:00
|
|
|
}
|
|
|
|
.onAppear {
|
2021-09-19 11:06:54 +00:00
|
|
|
if query != nil {
|
2021-09-19 12:42:47 +00:00
|
|
|
if navigationStyle == .tab {
|
|
|
|
state.queryText = query!.query
|
|
|
|
}
|
2021-09-19 11:06:54 +00:00
|
|
|
state.resetQuery(query!)
|
2021-07-07 22:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-13 20:41:16 +00:00
|
|
|
.onChange(of: state.query.query) { queryText in
|
2021-07-29 22:34:13 +00:00
|
|
|
state.changeQuery { query in query.query = queryText }
|
2021-07-07 22:39:18 +00:00
|
|
|
}
|
|
|
|
.onChange(of: searchSortOrder) { order in
|
2021-07-29 22:34:13 +00:00
|
|
|
state.changeQuery { query in query.sortBy = order }
|
2021-07-07 22:39:18 +00:00
|
|
|
}
|
|
|
|
.onChange(of: searchDate) { date in
|
2021-07-29 22:34:13 +00:00
|
|
|
state.changeQuery { query in query.date = date }
|
2021-07-07 22:39:18 +00:00
|
|
|
}
|
|
|
|
.onChange(of: searchDuration) { duration in
|
2021-07-29 22:34:13 +00:00
|
|
|
state.changeQuery { query in query.duration = duration }
|
2021-07-07 22:39:18 +00:00
|
|
|
}
|
2021-07-11 20:52:49 +00:00
|
|
|
#if !os(tvOS)
|
2021-09-13 20:41:16 +00:00
|
|
|
.navigationTitle(navigationTitle)
|
2021-07-11 20:52:49 +00:00
|
|
|
#endif
|
2021-06-11 12:36:26 +00:00
|
|
|
}
|
2021-06-11 21:54:00 +00:00
|
|
|
|
2021-09-19 12:42:47 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.opacity(recentsChanged ? 1 : 1)
|
|
|
|
|
|
|
|
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-13 20:41:16 +00:00
|
|
|
var navigationTitle: String {
|
2021-09-19 12:42:47 +00:00
|
|
|
if state.query.query.isEmpty || (navigationStyle == .tab && state.queryText.isEmpty) {
|
|
|
|
return "Search"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Search: \"\(state.query.query)\""
|
2021-09-13 20:41:16 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 22:39:18 +00:00
|
|
|
var searchFiltersActive: Bool {
|
|
|
|
searchDate != nil || searchDuration != nil
|
|
|
|
}
|
2021-09-19 12:42:47 +00:00
|
|
|
|
|
|
|
var recentItems: [RecentItem] {
|
|
|
|
Defaults[.recentlyOpened].filter { $0.type == .query }.reversed()
|
|
|
|
}
|
2021-06-11 12:36:26 +00:00
|
|
|
}
|