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-26 17:40:25 +00:00
|
|
|
@State private var searchSortOrder = SearchQuery.SortOrder.relevance
|
|
|
|
@State private var searchDate = SearchQuery.Date.any
|
|
|
|
@State private var searchDuration = SearchQuery.Duration.any
|
2021-09-19 12:42:47 +00:00
|
|
|
|
|
|
|
@State private var recentsChanged = false
|
|
|
|
|
2021-09-29 14:29:17 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
@State private var searchDebounce = Debounce()
|
|
|
|
@State private var recentsDebounce = Debounce()
|
2022-11-24 20:36:05 +00:00
|
|
|
private var recents = RecentsModel.shared
|
2021-09-29 14:29:17 +00:00
|
|
|
#endif
|
2021-09-26 20:12:43 +00:00
|
|
|
|
2021-11-09 17:43:15 +00:00
|
|
|
@State private var favoriteItem: FavoriteItem?
|
|
|
|
|
2021-09-25 12:17:58 +00:00
|
|
|
@Environment(\.navigationStyle) private var navigationStyle
|
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
@ObservedObject private var accounts = AccountsModel.shared
|
|
|
|
@ObservedObject private var state = SearchModel.shared
|
2021-12-02 20:35:25 +00:00
|
|
|
private var favorites = FavoritesModel.shared
|
2022-12-11 19:58:52 +00:00
|
|
|
private var navigation = NavigationModel.shared
|
2021-09-19 11:06:54 +00:00
|
|
|
|
2022-08-31 19:24:46 +00:00
|
|
|
@Default(.recentlyOpened) private var recentlyOpened
|
2021-12-01 11:22:19 +00:00
|
|
|
@Default(.saveRecents) private var saveRecents
|
2022-11-11 20:28:40 +00:00
|
|
|
@Default(.showHome) private var showHome
|
2022-12-12 00:18:29 +00:00
|
|
|
@Default(.searchListingStyle) private var searchListingStyle
|
2024-09-09 14:05:24 +00:00
|
|
|
@Default(.showSearchSuggestions) private var showSearchSuggestions
|
2021-12-01 11:22:19 +00:00
|
|
|
|
2021-09-29 11:45:00 +00:00
|
|
|
private var videos = [Video]()
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
init(_ query: SearchQuery? = nil, videos: [Video] = []) {
|
2021-09-19 11:06:54 +00:00
|
|
|
self.query = query
|
2021-09-29 11:45:00 +00:00
|
|
|
self.videos = videos
|
2021-09-19 11:06:54 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 14:05:24 +00:00
|
|
|
#if os(iOS)
|
|
|
|
var body: some View {
|
|
|
|
VStack {
|
2021-11-28 14:37:55 +00:00
|
|
|
VStack {
|
2022-12-09 00:15:19 +00:00
|
|
|
if accounts.app.supportsSearchSuggestions, state.query.query != state.queryText {
|
2021-11-28 14:37:55 +00:00
|
|
|
SearchSuggestions()
|
2022-08-17 15:34:25 +00:00
|
|
|
.opacity(state.queryText.isEmpty ? 0 : 1)
|
2021-11-28 14:37:55 +00:00
|
|
|
} else {
|
|
|
|
results
|
|
|
|
}
|
|
|
|
}
|
2022-08-17 15:34:25 +00:00
|
|
|
.backport
|
2023-02-06 20:51:57 +00:00
|
|
|
.scrollDismissesKeyboardInteractively()
|
2024-09-09 14:05:24 +00:00
|
|
|
}
|
|
|
|
.environment(\.listingStyle, searchListingStyle)
|
|
|
|
.toolbar {
|
|
|
|
ToolbarItem(placement: .principal) {
|
|
|
|
if #available(iOS 15, *) {
|
|
|
|
FocusableSearchTextField()
|
|
|
|
} else {
|
|
|
|
SearchTextField()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ToolbarItem(placement: .navigationBarTrailing) {
|
|
|
|
searchMenu
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
|
|
.ignoresSafeArea(.keyboard, edges: .bottom)
|
|
|
|
.navigationTitle("Search")
|
|
|
|
.onAppear {
|
|
|
|
if let query {
|
|
|
|
state.queryText = query.query
|
|
|
|
state.resetQuery(query)
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !videos.isEmpty {
|
|
|
|
state.store.replace(ContentItem.array(of: videos))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onChange(of: accounts.current) { _ in
|
|
|
|
state.reloadQuery()
|
|
|
|
}
|
|
|
|
.onChange(of: state.queryText) { newQuery in
|
|
|
|
if newQuery.isEmpty {
|
|
|
|
favoriteItem = nil
|
|
|
|
state.resetQuery()
|
|
|
|
} else {
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
|
|
|
state.loadSuggestions(newQuery)
|
|
|
|
}
|
|
|
|
.onChange(of: searchSortOrder) { order in
|
|
|
|
state.changeQuery { query in
|
|
|
|
query.sortBy = order
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onChange(of: searchDate) { date in
|
|
|
|
state.changeQuery { query in
|
|
|
|
query.date = date
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onChange(of: searchDuration) { duration in
|
|
|
|
state.changeQuery { query in
|
|
|
|
query.duration = duration
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#elseif os(tvOS)
|
|
|
|
var body: some View {
|
|
|
|
VStack {
|
2021-11-28 14:37:55 +00:00
|
|
|
ZStack {
|
|
|
|
results
|
2024-09-09 14:05:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.environment(\.listingStyle, searchListingStyle)
|
|
|
|
.onAppear {
|
|
|
|
if let query {
|
|
|
|
state.queryText = query.query
|
|
|
|
state.resetQuery(query)
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
2021-11-28 14:37:55 +00:00
|
|
|
|
2024-09-09 14:05:24 +00:00
|
|
|
if !videos.isEmpty {
|
|
|
|
state.store.replace(ContentItem.array(of: videos))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onChange(of: accounts.current) { _ in
|
|
|
|
state.reloadQuery()
|
|
|
|
}
|
|
|
|
.onChange(of: state.queryText) { newQuery in
|
|
|
|
if newQuery.isEmpty {
|
|
|
|
favoriteItem = nil
|
|
|
|
state.resetQuery()
|
|
|
|
} else {
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
|
|
|
if showSearchSuggestions {
|
|
|
|
state.loadSuggestions(newQuery)
|
|
|
|
}
|
|
|
|
searchDebounce.invalidate()
|
|
|
|
recentsDebounce.invalidate()
|
|
|
|
|
|
|
|
searchDebounce.debouncing(2) {
|
|
|
|
state.changeQuery { query in
|
|
|
|
query.query = newQuery
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
recentsDebounce.debouncing(10) {
|
|
|
|
recents.addQuery(newQuery)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onChange(of: searchSortOrder) { order in
|
|
|
|
state.changeQuery { query in
|
|
|
|
query.sortBy = order
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onChange(of: searchDate) { date in
|
|
|
|
state.changeQuery { query in
|
|
|
|
query.date = date
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onChange(of: searchDuration) { duration in
|
|
|
|
state.changeQuery { query in
|
|
|
|
query.duration = duration
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.searchable(text: $state.queryText) {
|
|
|
|
if !state.queryText.isEmpty {
|
|
|
|
ForEach(state.querySuggestions, id: \.self) { suggestion in
|
|
|
|
Text(suggestion)
|
|
|
|
.searchCompletion(suggestion)
|
|
|
|
}
|
2021-09-19 12:42:47 +00:00
|
|
|
}
|
2024-09-09 14:05:24 +00:00
|
|
|
}
|
2021-07-07 22:39:18 +00:00
|
|
|
}
|
2024-09-09 14:05:24 +00:00
|
|
|
|
|
|
|
#elseif os(macOS)
|
|
|
|
var body: some View {
|
|
|
|
ZStack {
|
|
|
|
results
|
|
|
|
if accounts.app.supportsSearchSuggestions, state.query.query != state.queryText, showSearchSuggestions {
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
SearchSuggestions()
|
|
|
|
.borderLeading(width: 1, color: Color("ControlsBorderColor"))
|
|
|
|
.frame(maxWidth: 262)
|
|
|
|
.opacity(state.queryText.isEmpty ? 0 : 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.environment(\.listingStyle, searchListingStyle)
|
|
|
|
.toolbar {
|
2021-09-26 17:40:25 +00:00
|
|
|
ToolbarItemGroup(placement: toolbarPlacement) {
|
2022-12-12 00:18:29 +00:00
|
|
|
ListingStyleButtons(listingStyle: $searchListingStyle)
|
2023-05-23 16:48:39 +00:00
|
|
|
HideWatchedButtons()
|
2023-05-23 16:54:53 +00:00
|
|
|
HideShortsButtons()
|
2022-02-04 17:38:29 +00:00
|
|
|
FavoriteButton(item: favoriteItem)
|
|
|
|
.id(favoriteItem?.id)
|
2021-11-09 17:43:15 +00:00
|
|
|
|
2021-10-20 22:21:50 +00:00
|
|
|
if accounts.app.supportsSearchFilters {
|
|
|
|
Section {
|
2022-02-04 17:38:29 +00:00
|
|
|
HStack {
|
|
|
|
Text("Sort:")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
searchSortOrderPicker
|
|
|
|
}
|
2021-10-20 22:21:50 +00:00
|
|
|
}
|
|
|
|
.transaction { t in t.animation = .none }
|
2021-11-09 17:43:15 +00:00
|
|
|
}
|
2021-09-26 17:40:25 +00:00
|
|
|
|
2021-11-09 17:43:15 +00:00
|
|
|
if accounts.app.supportsSearchFilters {
|
2021-10-20 22:21:50 +00:00
|
|
|
filtersMenu
|
|
|
|
}
|
2021-11-28 14:37:55 +00:00
|
|
|
|
2023-10-15 11:35:23 +00:00
|
|
|
if #available(macOS 12, *) {
|
|
|
|
FocusableSearchTextField()
|
|
|
|
} else {
|
|
|
|
SearchTextField()
|
|
|
|
}
|
2021-09-25 12:17:58 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-09 14:05:24 +00:00
|
|
|
.onAppear {
|
|
|
|
if let query {
|
|
|
|
state.queryText = query.query
|
|
|
|
state.resetQuery(query)
|
|
|
|
updateFavoriteItem()
|
2021-09-26 17:40:25 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 14:05:24 +00:00
|
|
|
if !videos.isEmpty {
|
|
|
|
state.store.replace(ContentItem.array(of: videos))
|
2021-09-26 17:40:25 +00:00
|
|
|
}
|
2021-11-09 17:43:15 +00:00
|
|
|
}
|
2024-09-09 14:05:24 +00:00
|
|
|
.onChange(of: accounts.current) { _ in
|
|
|
|
state.reloadQuery()
|
2021-11-09 17:43:15 +00:00
|
|
|
}
|
2024-09-09 14:05:24 +00:00
|
|
|
.onChange(of: state.queryText) { newQuery in
|
|
|
|
if newQuery.isEmpty {
|
|
|
|
favoriteItem = nil
|
|
|
|
state.resetQuery()
|
|
|
|
} else {
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
|
|
|
state.loadSuggestions(newQuery)
|
2021-11-09 17:43:15 +00:00
|
|
|
}
|
2024-09-09 14:05:24 +00:00
|
|
|
.onChange(of: searchSortOrder) { order in
|
|
|
|
state.changeQuery { query in
|
|
|
|
query.sortBy = order
|
|
|
|
updateFavoriteItem()
|
2022-08-17 15:34:25 +00:00
|
|
|
}
|
2021-12-01 11:22:19 +00:00
|
|
|
}
|
2024-09-09 14:05:24 +00:00
|
|
|
.onChange(of: searchDate) { date in
|
|
|
|
state.changeQuery { query in
|
|
|
|
query.date = date
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onChange(of: searchDuration) { duration in
|
|
|
|
state.changeQuery { query in
|
|
|
|
query.duration = duration
|
|
|
|
updateFavoriteItem()
|
2023-10-15 11:35:23 +00:00
|
|
|
}
|
2022-12-10 01:19:36 +00:00
|
|
|
}
|
2024-09-09 14:05:24 +00:00
|
|
|
.frame(minWidth: Constants.contentViewMinWidth)
|
|
|
|
.navigationTitle("Search")
|
2022-12-10 01:19:36 +00:00
|
|
|
}
|
2024-09-09 14:05:24 +00:00
|
|
|
#endif
|
2021-06-11 21:54:00 +00:00
|
|
|
|
2022-12-10 01:19:36 +00:00
|
|
|
#if os(iOS)
|
|
|
|
var searchMenu: some View {
|
|
|
|
Menu {
|
|
|
|
if accounts.app.supportsSearchFilters {
|
|
|
|
searchSortOrderPicker
|
|
|
|
.pickerStyle(.menu)
|
|
|
|
|
|
|
|
Picker(selection: $searchDuration, label: Text("Duration")) {
|
|
|
|
ForEach(SearchQuery.Duration.allCases) { duration in
|
|
|
|
Text(duration.name).tag(duration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.pickerStyle(.menu)
|
|
|
|
|
|
|
|
Picker("Upload date", selection: $searchDate) {
|
|
|
|
ForEach(SearchQuery.Date.allCases) { date in
|
|
|
|
Text(date.name).tag(date)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.pickerStyle(.menu)
|
|
|
|
}
|
|
|
|
|
2022-12-11 19:58:52 +00:00
|
|
|
if !state.query.isEmpty {
|
|
|
|
Section {
|
|
|
|
FavoriteButton(item: favoriteItem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-12 00:18:29 +00:00
|
|
|
ListingStyleButtons(listingStyle: $searchListingStyle)
|
|
|
|
|
2023-02-25 15:42:18 +00:00
|
|
|
Section {
|
2023-05-23 16:48:39 +00:00
|
|
|
HideWatchedButtons()
|
2023-05-23 16:54:53 +00:00
|
|
|
HideShortsButtons()
|
2023-02-25 15:42:18 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 01:19:36 +00:00
|
|
|
Section {
|
2022-12-11 22:15:56 +00:00
|
|
|
SettingsButtons()
|
2022-12-10 01:19:36 +00:00
|
|
|
}
|
|
|
|
} label: {
|
2022-12-11 13:28:16 +00:00
|
|
|
HStack {
|
|
|
|
Image(systemName: "chevron.down.circle.fill")
|
2024-09-09 14:05:24 +00:00
|
|
|
.foregroundColor(.accentColor)
|
|
|
|
.imageScale(.large)
|
2022-12-11 13:28:16 +00:00
|
|
|
}
|
2022-12-10 01:19:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
private var results: some View {
|
|
|
|
VStack {
|
|
|
|
if showRecentQueries {
|
|
|
|
recentQueries
|
|
|
|
} else {
|
2023-06-07 20:32:54 +00:00
|
|
|
VerticalCells(items: state.store.collection, allowEmpty: state.query.isEmpty) {
|
2023-04-22 19:07:30 +00:00
|
|
|
if shouldDisplayHeader {
|
|
|
|
header
|
2021-11-28 14:37:55 +00:00
|
|
|
}
|
2023-04-22 19:07:30 +00:00
|
|
|
}
|
|
|
|
.environment(\.loadMoreContentHandler) { state.loadNextPage() }
|
2023-06-07 20:32:54 +00:00
|
|
|
|
|
|
|
if noResults {
|
|
|
|
Text("No results")
|
|
|
|
|
|
|
|
if searchFiltersActive {
|
|
|
|
Button("Reset search filters", action: resetFilters)
|
|
|
|
}
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
}
|
2021-11-28 14:37:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var toolbarPlacement: ToolbarItemPlacement {
|
2021-09-26 17:40:25 +00:00
|
|
|
#if os(iOS)
|
2021-12-02 20:35:25 +00:00
|
|
|
accounts.app.supportsSearchFilters || favorites.isEnabled ? .bottomBar : .automatic
|
2021-09-26 17:40:25 +00:00
|
|
|
#else
|
2023-05-07 19:45:18 +00:00
|
|
|
.automatic
|
2021-09-26 17:40:25 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
private var showRecentQueries: Bool {
|
2021-12-01 11:22:19 +00:00
|
|
|
navigationStyle == .tab && saveRecents && state.queryText.isEmpty
|
2021-09-26 20:12:43 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
private var filtersActive: Bool {
|
2021-09-26 17:40:25 +00:00
|
|
|
searchDuration != .any || searchDate != .any
|
|
|
|
}
|
|
|
|
|
2023-06-07 20:32:54 +00:00
|
|
|
private func resetFilters() {
|
|
|
|
searchSortOrder = .relevance
|
|
|
|
searchDate = .any
|
|
|
|
searchDuration = .any
|
|
|
|
}
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
private var noResults: Bool {
|
2022-11-27 10:42:16 +00:00
|
|
|
state.store.collection.isEmpty && !state.isLoading && !state.query.isEmpty
|
2021-09-26 20:12:43 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
private var recentQueries: some View {
|
2021-09-26 17:40:25 +00:00
|
|
|
VStack {
|
|
|
|
List {
|
|
|
|
Section(header: Text("Recents")) {
|
2022-08-31 19:24:46 +00:00
|
|
|
if recentlyOpened.isEmpty {
|
2021-09-26 17:40:25 +00:00
|
|
|
Text("Search history is empty")
|
|
|
|
.foregroundColor(.secondary)
|
2021-09-19 12:42:47 +00:00
|
|
|
}
|
2022-09-02 13:15:54 +00:00
|
|
|
ForEach(recentlyOpened.reversed(), id: \.tag) { item in
|
2022-12-11 19:58:52 +00:00
|
|
|
recentItemControl(item)
|
|
|
|
.lineLimit(1)
|
|
|
|
.truncationMode(.middle)
|
|
|
|
.foregroundColor(.accentColor)
|
2021-09-26 17:40:25 +00:00
|
|
|
}
|
2021-09-19 12:42:47 +00:00
|
|
|
}
|
2021-09-26 17:40:25 +00:00
|
|
|
.redrawOn(change: recentsChanged)
|
2022-08-21 15:39:06 +00:00
|
|
|
|
|
|
|
Section(footer: Color.clear.frame(minHeight: 80)) {
|
|
|
|
clearHistoryButton
|
|
|
|
}
|
2021-09-26 17:40:25 +00:00
|
|
|
}
|
2021-09-19 12:42:47 +00:00
|
|
|
}
|
|
|
|
#if os(iOS)
|
2021-11-08 16:29:35 +00:00
|
|
|
.listStyle(.insetGrouped)
|
2021-09-19 12:42:47 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-12-11 19:58:52 +00:00
|
|
|
@ViewBuilder private func recentItemControl(_ item: RecentItem) -> some View {
|
|
|
|
#if os(tvOS)
|
|
|
|
recentItemButton(item)
|
|
|
|
#else
|
|
|
|
if recentItemIsNavigationLink(item) {
|
|
|
|
recentItemNavigationLink(item)
|
|
|
|
} else {
|
|
|
|
recentItemButton(item)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private func recentItemNavigationLink(_ item: RecentItem) -> some View {
|
|
|
|
NavigationLink(destination: recentItemNavigationLinkDestination(item)) {
|
|
|
|
recentItemLabel(item)
|
|
|
|
}
|
2023-05-26 21:21:07 +00:00
|
|
|
.contextMenu { recentItemContextMenu(item) }
|
2022-12-11 19:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder private func recentItemNavigationLinkDestination(_ item: RecentItem) -> some View {
|
|
|
|
switch item.type {
|
|
|
|
case .channel:
|
|
|
|
if let channel = item.channel {
|
|
|
|
ChannelVideosView(channel: channel)
|
|
|
|
}
|
|
|
|
case .playlist:
|
|
|
|
if let playlist = item.playlist {
|
|
|
|
ChannelPlaylistView(playlist: playlist)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
EmptyView()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func recentItemIsNavigationLink(_ item: RecentItem) -> Bool {
|
|
|
|
switch item.type {
|
|
|
|
case .channel:
|
|
|
|
return navigationStyle == .tab
|
|
|
|
case .playlist:
|
|
|
|
return navigationStyle == .tab
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 16:12:06 +00:00
|
|
|
private func recentItemButton(_ item: RecentItem) -> some View {
|
|
|
|
Button {
|
|
|
|
switch item.type {
|
|
|
|
case .query:
|
2022-08-17 15:34:25 +00:00
|
|
|
state.queryText = item.title
|
2022-06-14 16:12:06 +00:00
|
|
|
state.changeQuery { query in query.query = item.title }
|
2022-11-24 20:36:05 +00:00
|
|
|
NavigationModel.shared.hideKeyboard()
|
2022-06-14 16:12:06 +00:00
|
|
|
|
|
|
|
updateFavoriteItem()
|
2022-11-24 20:36:05 +00:00
|
|
|
RecentsModel.shared.add(item)
|
2022-06-14 16:12:06 +00:00
|
|
|
case .channel:
|
|
|
|
guard let channel = item.channel else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
NavigationModel.shared.openChannel(
|
2022-06-14 16:12:06 +00:00
|
|
|
channel,
|
2022-06-30 08:05:32 +00:00
|
|
|
navigationStyle: navigationStyle
|
2022-06-14 16:12:06 +00:00
|
|
|
)
|
|
|
|
case .playlist:
|
|
|
|
guard let playlist = item.playlist else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
NavigationModel.shared.openChannelPlaylist(
|
2022-06-14 16:12:06 +00:00
|
|
|
playlist,
|
2022-06-30 08:05:32 +00:00
|
|
|
navigationStyle: navigationStyle
|
2022-06-14 16:12:06 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
} label: {
|
2022-12-11 19:58:52 +00:00
|
|
|
recentItemLabel(item)
|
2022-06-14 16:12:06 +00:00
|
|
|
}
|
2023-05-26 21:21:07 +00:00
|
|
|
.contextMenu { recentItemContextMenu(item) }
|
|
|
|
}
|
|
|
|
|
|
|
|
private func recentItemContextMenu(_ item: RecentItem) -> some View {
|
|
|
|
Group {
|
2022-06-14 16:12:06 +00:00
|
|
|
removeButton(item)
|
2022-08-21 15:39:06 +00:00
|
|
|
|
2022-08-14 16:59:21 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
Button("Cancel", role: .cancel) {}
|
|
|
|
#endif
|
2022-06-14 16:12:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-11 19:58:52 +00:00
|
|
|
private func recentItemLabel(_ item: RecentItem) -> some View {
|
|
|
|
let systemImage = item.type == .query ? "magnifyingglass" :
|
|
|
|
item.type == .channel ? RecentsModel.symbolSystemImage(item.title) :
|
|
|
|
"list.and.film"
|
|
|
|
return Label(item.title, systemImage: systemImage)
|
|
|
|
}
|
|
|
|
|
2022-01-06 16:55:56 +00:00
|
|
|
private func removeButton(_ item: RecentItem) -> some View {
|
2021-11-28 14:37:55 +00:00
|
|
|
Button {
|
2022-11-24 20:36:05 +00:00
|
|
|
RecentsModel.shared.close(item)
|
2021-11-28 14:37:55 +00:00
|
|
|
recentsChanged.toggle()
|
|
|
|
} label: {
|
2022-01-06 16:55:56 +00:00
|
|
|
Label("Remove", systemImage: "trash")
|
2021-09-19 12:42:47 +00:00
|
|
|
}
|
2021-11-28 14:37:55 +00:00
|
|
|
}
|
2021-09-19 12:42:47 +00:00
|
|
|
|
2022-08-21 15:39:06 +00:00
|
|
|
private var clearHistoryButton: some View {
|
2021-11-28 14:37:55 +00:00
|
|
|
Button {
|
2022-11-24 20:36:05 +00:00
|
|
|
NavigationModel.shared.presentAlert(
|
2022-08-21 15:39:06 +00:00
|
|
|
Alert(
|
|
|
|
title: Text("Are you sure you want to clear search history?"),
|
|
|
|
message: Text("This cannot be reverted"),
|
|
|
|
primaryButton: .destructive(Text("Clear")) {
|
2022-11-24 20:36:05 +00:00
|
|
|
RecentsModel.shared.clear()
|
2022-08-21 15:39:06 +00:00
|
|
|
recentsChanged.toggle()
|
|
|
|
},
|
|
|
|
secondaryButton: .cancel()
|
|
|
|
)
|
|
|
|
)
|
2021-11-28 14:37:55 +00:00
|
|
|
} label: {
|
2022-08-21 15:39:06 +00:00
|
|
|
Label("Clear Search History...", systemImage: "trash.fill")
|
2021-09-19 12:42:47 +00:00
|
|
|
}
|
2022-08-21 15:39:06 +00:00
|
|
|
.labelStyle(.titleOnly)
|
|
|
|
.foregroundColor(Color("AppRedColor"))
|
2021-09-19 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
private var searchFiltersActive: Bool {
|
2021-09-26 17:40:25 +00:00
|
|
|
searchDate != .any || searchDuration != .any
|
2021-07-07 22:39:18 +00:00
|
|
|
}
|
2021-09-19 12:42:47 +00:00
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
private var searchSortOrderPicker: some View {
|
2021-09-26 17:40:25 +00:00
|
|
|
Picker("Sort", selection: $searchSortOrder) {
|
|
|
|
ForEach(SearchQuery.SortOrder.allCases) { sortOrder in
|
|
|
|
Text(sortOrder.name).tag(sortOrder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if os(tvOS)
|
2021-11-28 14:37:55 +00:00
|
|
|
private var searchSortOrderButton: some View {
|
2021-09-26 17:40:25 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
private var searchDateButton: some View {
|
2021-09-26 17:40:25 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
private var searchDurationButton: some View {
|
2021-09-26 17:40:25 +00:00
|
|
|
Button(action: { self.searchDuration = self.searchDuration.next() }) {
|
2021-09-29 11:45:00 +00:00
|
|
|
Text(self.searchDuration.name)
|
2021-09-26 17:40:25 +00:00
|
|
|
.font(.system(size: 30))
|
|
|
|
.padding(.horizontal)
|
|
|
|
.padding(.vertical, 2)
|
|
|
|
}
|
|
|
|
.buttonStyle(.card)
|
|
|
|
.contextMenu {
|
|
|
|
ForEach(SearchQuery.Duration.allCases) { searchDuration in
|
|
|
|
Button(searchDuration.name) {
|
|
|
|
self.searchDuration = searchDuration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
private var filtersHorizontalStack: some View {
|
2021-09-26 17:40:25 +00:00
|
|
|
HStack {
|
|
|
|
HStack(spacing: 30) {
|
|
|
|
Text("Sort")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
searchSortOrderButton
|
|
|
|
}
|
2021-11-09 17:43:15 +00:00
|
|
|
.frame(maxWidth: 300, alignment: .trailing)
|
2021-09-26 20:12:43 +00:00
|
|
|
|
2021-09-26 17:40:25 +00:00
|
|
|
HStack(spacing: 30) {
|
|
|
|
Text("Duration")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
searchDurationButton
|
|
|
|
}
|
2021-11-09 17:43:15 +00:00
|
|
|
.frame(maxWidth: 300)
|
2021-09-26 17:40:25 +00:00
|
|
|
|
|
|
|
HStack(spacing: 30) {
|
|
|
|
Text("Date")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
searchDateButton
|
|
|
|
}
|
2021-11-09 17:43:15 +00:00
|
|
|
.frame(maxWidth: 300, alignment: .leading)
|
2021-09-26 17:40:25 +00:00
|
|
|
}
|
2021-09-29 11:52:09 +00:00
|
|
|
.font(.system(size: 30))
|
2021-09-26 17:40:25 +00:00
|
|
|
}
|
|
|
|
#else
|
2021-11-28 14:37:55 +00:00
|
|
|
private var filtersMenu: some View {
|
2021-09-26 17:40:25 +00:00
|
|
|
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
|
2021-11-09 17:43:15 +00:00
|
|
|
|
|
|
|
private func updateFavoriteItem() {
|
|
|
|
favoriteItem = FavoriteItem(section: .searchQuery(
|
2022-08-17 15:34:25 +00:00
|
|
|
state.queryText,
|
|
|
|
searchDate.rawValue,
|
|
|
|
searchDuration.rawValue,
|
|
|
|
searchSortOrder.rawValue
|
2021-11-09 17:43:15 +00:00
|
|
|
))
|
|
|
|
}
|
2023-04-22 18:06:30 +00:00
|
|
|
|
|
|
|
var shouldDisplayHeader: Bool {
|
|
|
|
#if os(tvOS)
|
|
|
|
!state.query.isEmpty
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
var header: some View {
|
|
|
|
HStack {
|
|
|
|
clearButton
|
|
|
|
|
|
|
|
#if os(tvOS)
|
|
|
|
if accounts.app.supportsSearchFilters {
|
|
|
|
filtersHorizontalStack
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
FavoriteButton(item: favoriteItem)
|
|
|
|
.id(favoriteItem?.id)
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
.font(.system(size: 25))
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
ListingStyleButtons(listingStyle: $searchListingStyle)
|
2023-05-23 16:48:39 +00:00
|
|
|
HideWatchedButtons()
|
2023-05-23 16:54:53 +00:00
|
|
|
HideShortsButtons()
|
2023-04-22 18:06:30 +00:00
|
|
|
}
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
.padding(.leading, 30)
|
|
|
|
.padding(.bottom, 15)
|
|
|
|
.padding(.trailing, 30)
|
|
|
|
}
|
|
|
|
|
|
|
|
var clearButton: some View {
|
|
|
|
Button {
|
|
|
|
state.queryText = ""
|
|
|
|
} label: {
|
|
|
|
Label("Clear", systemImage: "xmark")
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
}
|
|
|
|
.font(.caption)
|
|
|
|
}
|
2021-09-26 17:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct SearchView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
NavigationView {
|
2021-09-29 11:45:00 +00:00
|
|
|
SearchView(SearchQuery(query: "Is Google Evil"), videos: Video.fixtures(30))
|
|
|
|
.injectFixtureEnvironmentObjects()
|
2021-09-26 17:40:25 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-11 12:36:26 +00:00
|
|
|
}
|