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-09-25 12:17:58 +00:00
|
|
|
private var query: SearchQuery?
|
2021-06-28 10:43:07 +00:00
|
|
|
|
2021-09-25 12:17:58 +00:00
|
|
|
@State private var searchSortOrder: SearchQuery.SortOrder = .relevance
|
|
|
|
@State private var searchDate: SearchQuery.Date?
|
|
|
|
@State private var searchDuration: SearchQuery.Duration?
|
2021-09-19 12:42:47 +00:00
|
|
|
|
|
|
|
@State private var presentingClearConfirmation = false
|
|
|
|
@State private var recentsChanged = false
|
|
|
|
|
2021-09-25 12:17:58 +00:00
|
|
|
@Environment(\.navigationStyle) private var navigationStyle
|
|
|
|
|
|
|
|
@EnvironmentObject<RecentsModel> private var recents
|
|
|
|
@EnvironmentObject<SearchModel> private var state
|
2021-09-19 11:06:54 +00:00
|
|
|
|
|
|
|
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") {
|
2021-09-25 12:17:58 +00:00
|
|
|
self.searchSortOrder = .relevance
|
|
|
|
self.searchDate = nil
|
|
|
|
self.searchDuration = nil
|
2021-09-19 12:42:47 +00:00
|
|
|
}
|
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
|
|
|
}
|
2021-09-25 12:17:58 +00:00
|
|
|
.toolbar {
|
|
|
|
#if os(iOS)
|
|
|
|
ToolbarItemGroup(placement: .bottomBar) {
|
|
|
|
Section {
|
|
|
|
if !state.queryText.isEmpty {
|
|
|
|
Text("Sort:")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
|
|
|
Menu(searchSortOrder.name) {
|
|
|
|
ForEach(SearchQuery.SortOrder.allCases) { sortOrder in
|
|
|
|
Button(sortOrder.name) {
|
|
|
|
searchSortOrder = sortOrder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
Text("Filter:")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
|
|
|
Menu(searchDuration?.name ?? "Duration") {
|
|
|
|
Button("All") {
|
|
|
|
searchDuration = nil
|
|
|
|
}
|
|
|
|
ForEach(SearchQuery.Duration.allCases) { duration in
|
|
|
|
Button(duration.name) {
|
|
|
|
searchDuration = duration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.foregroundColor(searchDuration.isNil ? .secondary : .accentColor)
|
|
|
|
|
|
|
|
Menu(searchDate?.name ?? "Date") {
|
|
|
|
Button("All") {
|
|
|
|
searchDate = nil
|
|
|
|
}
|
|
|
|
ForEach(SearchQuery.Date.allCases) { date in
|
|
|
|
Button(date.name) {
|
|
|
|
searchDate = date
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.foregroundColor(searchDate.isNil ? .secondary : .accentColor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.transaction { t in t.animation = .none }
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2021-07-07 22:39:18 +00:00
|
|
|
.onAppear {
|
2021-09-19 11:06:54 +00:00
|
|
|
if query != nil {
|
2021-09-25 12:17:58 +00:00
|
|
|
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-25 12:17:58 +00:00
|
|
|
.searchable(text: $state.queryText, placement: searchFieldPlacement) {
|
|
|
|
ForEach(state.querySuggestions.collection, id: \.self) { suggestion in
|
|
|
|
Text(suggestion)
|
|
|
|
.searchCompletion(suggestion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onChange(of: state.queryText) { query in
|
|
|
|
state.loadSuggestions(query)
|
|
|
|
}
|
|
|
|
.onSubmit(of: .search) {
|
|
|
|
state.changeQuery { query in query.query = state.queryText }
|
|
|
|
recents.addQuery(state.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-25 12:17:58 +00:00
|
|
|
.navigationTitle("Search")
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
var searchFieldPlacement: SearchFieldPlacement {
|
|
|
|
#if os(iOS)
|
|
|
|
.navigationBarDrawer(displayMode: .always)
|
|
|
|
#else
|
|
|
|
.automatic
|
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
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
.redrawOn(change: recentsChanged)
|
2021-09-19 12:42:47 +00:00
|
|
|
|
|
|
|
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-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
|
|
|
}
|