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-09-25 17:47:58 +05:30
|
|
|
private var query: SearchQuery?
|
2021-06-28 16:13:07 +05:30
|
|
|
|
2021-09-26 23:10:25 +05:30
|
|
|
@State private var searchSortOrder = SearchQuery.SortOrder.relevance
|
|
|
|
@State private var searchDate = SearchQuery.Date.any
|
|
|
|
@State private var searchDuration = SearchQuery.Duration.any
|
2021-09-19 18:12:47 +05:30
|
|
|
|
|
|
|
@State private var presentingClearConfirmation = false
|
|
|
|
@State private var recentsChanged = false
|
|
|
|
|
2021-09-29 19:59:17 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
@State private var searchDebounce = Debounce()
|
|
|
|
@State private var recentsDebounce = Debounce()
|
|
|
|
#endif
|
2021-09-27 01:42:43 +05:30
|
|
|
|
2021-09-25 17:47:58 +05:30
|
|
|
@Environment(\.navigationStyle) private var navigationStyle
|
|
|
|
|
2021-10-21 03:51:50 +05:30
|
|
|
@EnvironmentObject<AccountsModel> private var accounts
|
2021-09-25 17:47:58 +05:30
|
|
|
@EnvironmentObject<RecentsModel> private var recents
|
|
|
|
@EnvironmentObject<SearchModel> private var state
|
2021-09-19 16:36:54 +05:30
|
|
|
|
2021-09-29 17:15:00 +05:30
|
|
|
private var videos = [Video]()
|
|
|
|
|
2021-10-23 04:34:03 +05:30
|
|
|
var items: [ContentItem] {
|
|
|
|
state.store.collection.sorted { $0 < $1 }
|
|
|
|
}
|
|
|
|
|
2021-09-29 17:15:00 +05:30
|
|
|
init(_ query: SearchQuery? = nil, videos: [Video] = [Video]()) {
|
2021-09-19 16:36:54 +05:30
|
|
|
self.query = query
|
2021-09-29 17:15:00 +05:30
|
|
|
self.videos = videos
|
2021-09-19 16:36:54 +05:30
|
|
|
}
|
|
|
|
|
2021-06-11 18:06:26 +05:30
|
|
|
var body: some View {
|
2021-10-06 01:50:09 +05:30
|
|
|
PlayerControlsView {
|
|
|
|
VStack {
|
|
|
|
if showRecentQueries {
|
|
|
|
recentQueries
|
|
|
|
} else {
|
|
|
|
#if os(tvOS)
|
|
|
|
ScrollView(.vertical, showsIndicators: false) {
|
2021-10-21 03:51:50 +05:30
|
|
|
if accounts.app.supportsSearchFilters {
|
|
|
|
filtersHorizontalStack
|
|
|
|
}
|
2021-09-26 23:10:25 +05:30
|
|
|
|
2021-10-23 04:34:03 +05:30
|
|
|
HorizontalCells(items: items)
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
.edgesIgnoringSafeArea(.horizontal)
|
|
|
|
#else
|
2021-10-23 04:34:03 +05:30
|
|
|
VerticalCells(items: items)
|
2021-10-06 01:50:09 +05:30
|
|
|
#endif
|
2021-07-08 04:09:18 +05:30
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
if noResults {
|
|
|
|
Text("No results")
|
2021-07-08 04:09:18 +05:30
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
if searchFiltersActive {
|
|
|
|
Button("Reset search filters", action: resetFilters)
|
|
|
|
}
|
2021-07-08 04:09:18 +05:30
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
Spacer()
|
|
|
|
}
|
2021-09-19 18:12:47 +05:30
|
|
|
}
|
2021-06-28 16:13:07 +05:30
|
|
|
}
|
2021-07-08 04:09:18 +05:30
|
|
|
}
|
2021-09-25 17:47:58 +05:30
|
|
|
.toolbar {
|
2021-09-26 23:10:25 +05:30
|
|
|
#if !os(tvOS)
|
|
|
|
ToolbarItemGroup(placement: toolbarPlacement) {
|
2021-10-21 03:51:50 +05:30
|
|
|
if accounts.app.supportsSearchFilters {
|
|
|
|
Section {
|
|
|
|
#if os(macOS)
|
|
|
|
HStack {
|
|
|
|
Text("Sort:")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
|
|
|
searchSortOrderPicker
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
Menu("Sort: \(searchSortOrder.name)") {
|
|
|
|
searchSortOrderPicker
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
.transaction { t in t.animation = .none }
|
2021-09-26 23:10:25 +05:30
|
|
|
|
2021-10-21 03:51:50 +05:30
|
|
|
Spacer()
|
2021-09-26 23:10:25 +05:30
|
|
|
|
2021-10-21 03:51:50 +05:30
|
|
|
filtersMenu
|
|
|
|
}
|
2021-09-25 17:47:58 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2021-07-08 04:09:18 +05:30
|
|
|
.onAppear {
|
2021-09-19 16:36:54 +05:30
|
|
|
if query != nil {
|
2021-09-25 17:47:58 +05:30
|
|
|
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-29 17:15:00 +05:30
|
|
|
|
|
|
|
if !videos.isEmpty {
|
2021-10-22 04:59:10 +05:30
|
|
|
state.store.replace(ContentItem.array(of: videos))
|
2021-09-29 17:15:00 +05:30
|
|
|
}
|
2021-07-08 04:09:18 +05:30
|
|
|
}
|
2021-09-25 17:47:58 +05:30
|
|
|
.searchable(text: $state.queryText, placement: searchFieldPlacement) {
|
|
|
|
ForEach(state.querySuggestions.collection, id: \.self) { suggestion in
|
|
|
|
Text(suggestion)
|
|
|
|
.searchCompletion(suggestion)
|
|
|
|
}
|
|
|
|
}
|
2021-09-26 23:10:25 +05:30
|
|
|
.onChange(of: state.queryText) { newQuery in
|
|
|
|
if newQuery.isEmpty {
|
|
|
|
state.resetQuery()
|
|
|
|
}
|
|
|
|
|
|
|
|
state.loadSuggestions(newQuery)
|
|
|
|
|
|
|
|
#if os(tvOS)
|
2021-09-27 01:42:43 +05:30
|
|
|
searchDebounce.invalidate()
|
|
|
|
recentsDebounce.invalidate()
|
2021-09-26 23:10:25 +05:30
|
|
|
|
2021-09-27 01:42:43 +05:30
|
|
|
searchDebounce.debouncing(2) {
|
2021-09-26 23:10:25 +05:30
|
|
|
state.changeQuery { query in query.query = newQuery }
|
|
|
|
}
|
|
|
|
|
2021-09-27 01:42:43 +05:30
|
|
|
recentsDebounce.debouncing(10) {
|
2021-09-26 23:10:25 +05:30
|
|
|
recents.addQuery(newQuery)
|
|
|
|
}
|
|
|
|
#endif
|
2021-09-25 17:47:58 +05:30
|
|
|
}
|
|
|
|
.onSubmit(of: .search) {
|
|
|
|
state.changeQuery { query in query.query = state.queryText }
|
|
|
|
recents.addQuery(state.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-11-08 21:59:35 +05:30
|
|
|
.navigationTitle("Search")
|
2021-09-25 17:47:58 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
var searchFieldPlacement: SearchFieldPlacement {
|
|
|
|
#if os(iOS)
|
|
|
|
.navigationBarDrawer(displayMode: .always)
|
|
|
|
#else
|
|
|
|
.automatic
|
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-26 23:10:25 +05:30
|
|
|
var toolbarPlacement: ToolbarItemPlacement {
|
|
|
|
#if os(iOS)
|
|
|
|
.bottomBar
|
|
|
|
#else
|
|
|
|
.automatic
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-09-27 01:42:43 +05:30
|
|
|
fileprivate var showRecentQueries: Bool {
|
|
|
|
navigationStyle == .tab && state.queryText.isEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
fileprivate var filtersActive: Bool {
|
2021-09-26 23:10:25 +05:30
|
|
|
searchDuration != .any || searchDate != .any
|
|
|
|
}
|
|
|
|
|
2021-09-27 01:42:43 +05:30
|
|
|
fileprivate func resetFilters() {
|
|
|
|
searchSortOrder = .relevance
|
|
|
|
searchDate = .any
|
|
|
|
searchDuration = .any
|
|
|
|
}
|
|
|
|
|
|
|
|
fileprivate var noResults: Bool {
|
2021-10-23 04:34:03 +05:30
|
|
|
items.isEmpty && !state.isLoading && !state.query.isEmpty
|
2021-09-27 01:42:43 +05:30
|
|
|
}
|
|
|
|
|
2021-09-19 18:12:47 +05:30
|
|
|
var recentQueries: some View {
|
2021-09-26 23:10:25 +05:30
|
|
|
VStack {
|
|
|
|
List {
|
|
|
|
Section(header: Text("Recents")) {
|
|
|
|
if recentItems.isEmpty {
|
|
|
|
Text("Search history is empty")
|
|
|
|
.foregroundColor(.secondary)
|
2021-09-19 18:12:47 +05:30
|
|
|
}
|
2021-09-26 23:10:25 +05:30
|
|
|
ForEach(recentItems) { item in
|
|
|
|
Button(item.title) {
|
|
|
|
state.queryText = item.title
|
|
|
|
state.changeQuery { query in query.query = item.title }
|
2021-09-19 18:12:47 +05:30
|
|
|
}
|
2021-09-26 23:10:25 +05:30
|
|
|
#if os(iOS)
|
2021-11-08 21:59:35 +05:30
|
|
|
.swipeActions(edge: .trailing) {
|
|
|
|
deleteButton(item)
|
|
|
|
}
|
2021-09-26 23:10:25 +05:30
|
|
|
#elseif os(tvOS)
|
2021-11-08 21:59:35 +05:30
|
|
|
.contextMenu {
|
|
|
|
deleteButton(item)
|
|
|
|
}
|
2021-09-26 23:10:25 +05:30
|
|
|
#endif
|
|
|
|
}
|
2021-09-19 18:12:47 +05:30
|
|
|
}
|
2021-09-26 23:10:25 +05:30
|
|
|
.redrawOn(change: recentsChanged)
|
2021-09-19 18:12:47 +05:30
|
|
|
|
2021-09-26 23:10:25 +05:30
|
|
|
if !recentItems.isEmpty {
|
|
|
|
clearAllButton
|
|
|
|
}
|
|
|
|
}
|
2021-09-19 18:12:47 +05:30
|
|
|
}
|
|
|
|
#if os(iOS)
|
2021-11-08 21:59:35 +05:30
|
|
|
.listStyle(.insetGrouped)
|
2021-09-19 18:12:47 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-09-29 19:59:17 +05:30
|
|
|
#if !os(macOS)
|
|
|
|
func deleteButton(_ item: RecentItem) -> some View {
|
|
|
|
Button(role: .destructive) {
|
|
|
|
recents.close(item)
|
|
|
|
recentsChanged.toggle()
|
|
|
|
} label: {
|
|
|
|
Label("Delete", systemImage: "trash")
|
|
|
|
}
|
2021-09-19 18:12:47 +05:30
|
|
|
}
|
2021-09-29 19:59:17 +05:30
|
|
|
#endif
|
2021-09-19 18:12:47 +05:30
|
|
|
|
|
|
|
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-08 04:09:18 +05:30
|
|
|
var searchFiltersActive: Bool {
|
2021-09-26 23:10:25 +05:30
|
|
|
searchDate != .any || searchDuration != .any
|
2021-07-08 04:09:18 +05:30
|
|
|
}
|
2021-09-19 18:12:47 +05:30
|
|
|
|
|
|
|
var recentItems: [RecentItem] {
|
|
|
|
Defaults[.recentlyOpened].filter { $0.type == .query }.reversed()
|
|
|
|
}
|
2021-09-26 23:10:25 +05:30
|
|
|
|
|
|
|
var searchSortOrderPicker: some View {
|
|
|
|
Picker("Sort", selection: $searchSortOrder) {
|
|
|
|
ForEach(SearchQuery.SortOrder.allCases) { sortOrder in
|
|
|
|
Text(sortOrder.name).tag(sortOrder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if os(tvOS)
|
|
|
|
var searchSortOrderButton: some View {
|
|
|
|
Button(action: { self.searchSortOrder = self.searchSortOrder.next() }) { Text(self.searchSortOrder.name)
|
|
|
|
.font(.system(size: 30))
|
|
|
|
.padding(.horizontal)
|
|
|
|
.padding(.vertical, 2)
|
|
|
|
}
|
|
|
|
.buttonStyle(.card)
|
|
|
|
.contextMenu {
|
|
|
|
ForEach(SearchQuery.SortOrder.allCases) { sortOrder in
|
|
|
|
Button(sortOrder.name) {
|
|
|
|
self.searchSortOrder = sortOrder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var searchDateButton: some View {
|
|
|
|
Button(action: { self.searchDate = self.searchDate.next() }) {
|
|
|
|
Text(self.searchDate.name)
|
|
|
|
.font(.system(size: 30))
|
|
|
|
.padding(.horizontal)
|
|
|
|
.padding(.vertical, 2)
|
|
|
|
}
|
|
|
|
.buttonStyle(.card)
|
|
|
|
.contextMenu {
|
|
|
|
ForEach(SearchQuery.Date.allCases) { searchDate in
|
|
|
|
Button(searchDate.name) {
|
|
|
|
self.searchDate = searchDate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var searchDurationButton: some View {
|
|
|
|
Button(action: { self.searchDuration = self.searchDuration.next() }) {
|
2021-09-29 17:15:00 +05:30
|
|
|
Text(self.searchDuration.name)
|
2021-09-26 23:10:25 +05:30
|
|
|
.font(.system(size: 30))
|
|
|
|
.padding(.horizontal)
|
|
|
|
.padding(.vertical, 2)
|
|
|
|
}
|
|
|
|
.buttonStyle(.card)
|
|
|
|
.contextMenu {
|
|
|
|
ForEach(SearchQuery.Duration.allCases) { searchDuration in
|
|
|
|
Button(searchDuration.name) {
|
|
|
|
self.searchDuration = searchDuration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var filtersHorizontalStack: some View {
|
|
|
|
HStack {
|
|
|
|
HStack(spacing: 30) {
|
|
|
|
Text("Sort")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
searchSortOrderButton
|
|
|
|
}
|
|
|
|
.frame(maxWidth: .infinity, alignment: .trailing)
|
2021-09-27 01:42:43 +05:30
|
|
|
|
2021-09-26 23:10:25 +05:30
|
|
|
HStack(spacing: 30) {
|
|
|
|
Text("Duration")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
searchDurationButton
|
|
|
|
}
|
|
|
|
.frame(maxWidth: .infinity)
|
|
|
|
|
|
|
|
HStack(spacing: 30) {
|
|
|
|
Text("Date")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
searchDateButton
|
|
|
|
}
|
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
}
|
2021-09-29 17:22:09 +05:30
|
|
|
.font(.system(size: 30))
|
2021-09-26 23:10:25 +05:30
|
|
|
}
|
|
|
|
#else
|
|
|
|
var filtersMenu: some View {
|
|
|
|
Menu(filtersActive ? "Filter: active" : "Filter") {
|
|
|
|
Picker(selection: $searchDuration, label: Text("Duration")) {
|
|
|
|
ForEach(SearchQuery.Duration.allCases) { duration in
|
|
|
|
Text(duration.name).tag(duration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Picker("Upload date", selection: $searchDate) {
|
|
|
|
ForEach(SearchQuery.Date.allCases) { date in
|
|
|
|
Text(date.name).tag(date)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.foregroundColor(filtersActive ? .accentColor : .secondary)
|
|
|
|
.transaction { t in t.animation = .none }
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SearchView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
NavigationView {
|
2021-09-29 17:15:00 +05:30
|
|
|
SearchView(SearchQuery(query: "Is Google Evil"), videos: Video.fixtures(30))
|
|
|
|
.injectFixtureEnvironmentObjects()
|
2021-09-26 23:10:25 +05:30
|
|
|
}
|
|
|
|
}
|
2021-06-11 18:06:26 +05:30
|
|
|
}
|