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 recentsChanged = false
|
|
|
|
|
2021-09-29 19:59:17 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
@State private var searchDebounce = Debounce()
|
|
|
|
@State private var recentsDebounce = Debounce()
|
2022-11-25 02:06:05 +05:30
|
|
|
private var recents = RecentsModel.shared
|
2021-09-29 19:59:17 +05:30
|
|
|
#endif
|
2021-09-27 01:42:43 +05:30
|
|
|
|
2021-11-09 23:13:15 +05:30
|
|
|
@State private var favoriteItem: FavoriteItem?
|
|
|
|
|
2021-09-25 17:47:58 +05:30
|
|
|
@Environment(\.navigationStyle) private var navigationStyle
|
|
|
|
|
2022-11-25 02:06:05 +05:30
|
|
|
@ObservedObject private var accounts = AccountsModel.shared
|
|
|
|
@ObservedObject private var state = SearchModel.shared
|
2021-12-03 02:05:25 +05:30
|
|
|
private var favorites = FavoritesModel.shared
|
2021-09-19 16:36:54 +05:30
|
|
|
|
2022-09-01 00:54:46 +05:30
|
|
|
@Default(.recentlyOpened) private var recentlyOpened
|
2021-12-01 16:52:19 +05:30
|
|
|
@Default(.saveRecents) private var saveRecents
|
2022-11-12 01:58:40 +05:30
|
|
|
@Default(.showHome) private var showHome
|
2021-12-01 16:52:19 +05:30
|
|
|
|
2021-09-29 17:15:00 +05:30
|
|
|
private var videos = [Video]()
|
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
init(_ query: SearchQuery? = nil, videos: [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 {
|
2022-02-17 01:53:11 +05:30
|
|
|
BrowserPlayerControls(toolbar: {
|
2022-02-04 23:08:29 +05:30
|
|
|
#if os(iOS)
|
|
|
|
if accounts.app.supportsSearchFilters {
|
|
|
|
HStack(spacing: 0) {
|
|
|
|
Menu("Sort: \(searchSortOrder.name)") {
|
|
|
|
searchSortOrderPicker
|
|
|
|
}
|
|
|
|
.transaction { t in t.animation = .none }
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
filtersMenu
|
|
|
|
}
|
|
|
|
.padding()
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}) {
|
2021-11-28 20:07:55 +05:30
|
|
|
#if os(iOS)
|
|
|
|
VStack {
|
2022-08-17 21:04:25 +05:30
|
|
|
SearchTextField(favoriteItem: $favoriteItem)
|
2021-11-28 20:07:55 +05:30
|
|
|
|
2022-12-09 05:45:19 +05:30
|
|
|
if accounts.app.supportsSearchSuggestions, state.query.query != state.queryText {
|
2021-11-28 20:07:55 +05:30
|
|
|
SearchSuggestions()
|
2022-08-17 21:04:25 +05:30
|
|
|
.opacity(state.queryText.isEmpty ? 0 : 1)
|
2021-11-28 20:07:55 +05:30
|
|
|
} else {
|
|
|
|
results
|
|
|
|
}
|
|
|
|
}
|
2022-08-17 21:04:25 +05:30
|
|
|
.backport
|
|
|
|
.scrollDismissesKeyboard()
|
2021-11-28 20:07:55 +05:30
|
|
|
#else
|
|
|
|
ZStack {
|
|
|
|
results
|
|
|
|
|
2022-08-05 04:00:09 +05:30
|
|
|
#if os(macOS)
|
2022-12-09 05:45:19 +05:30
|
|
|
if accounts.app.supportsSearchSuggestions, state.query.query != state.queryText {
|
2021-12-01 16:52:19 +05:30
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
SearchSuggestions()
|
|
|
|
.borderLeading(width: 1, color: Color("ControlsBorderColor"))
|
|
|
|
.frame(maxWidth: 280)
|
2022-08-17 21:04:25 +05:30
|
|
|
.opacity(state.queryText.isEmpty ? 0 : 1)
|
2021-12-01 16:52:19 +05:30
|
|
|
}
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
2021-12-01 16:52:19 +05:30
|
|
|
#endif
|
2021-09-19 18:12:47 +05:30
|
|
|
}
|
2021-11-28 20:07:55 +05:30
|
|
|
#endif
|
2021-07-08 04:09:18 +05:30
|
|
|
}
|
2021-09-25 17:47:58 +05:30
|
|
|
.toolbar {
|
2022-02-04 23:08:29 +05:30
|
|
|
#if os(macOS)
|
2021-09-26 23:10:25 +05:30
|
|
|
ToolbarItemGroup(placement: toolbarPlacement) {
|
2022-02-04 23:08:29 +05:30
|
|
|
FavoriteButton(item: favoriteItem)
|
|
|
|
.id(favoriteItem?.id)
|
2021-11-09 23:13:15 +05:30
|
|
|
|
2021-10-21 03:51:50 +05:30
|
|
|
if accounts.app.supportsSearchFilters {
|
|
|
|
Section {
|
2022-02-04 23:08:29 +05:30
|
|
|
HStack {
|
|
|
|
Text("Sort:")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
|
|
|
searchSortOrderPicker
|
|
|
|
}
|
2021-10-21 03:51:50 +05:30
|
|
|
}
|
|
|
|
.transaction { t in t.animation = .none }
|
2021-11-09 23:13:15 +05:30
|
|
|
}
|
2021-09-26 23:10:25 +05:30
|
|
|
|
2021-11-09 23:13:15 +05:30
|
|
|
if accounts.app.supportsSearchFilters {
|
2021-10-21 03:51:50 +05:30
|
|
|
filtersMenu
|
|
|
|
}
|
2021-11-28 20:07:55 +05:30
|
|
|
|
2022-08-17 21:04:25 +05:30
|
|
|
SearchTextField(favoriteItem: $favoriteItem)
|
2021-09-25 17:47:58 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2021-07-08 04:09:18 +05:30
|
|
|
.onAppear {
|
2022-09-28 19:57:01 +05:30
|
|
|
if let query {
|
2022-08-05 04:00:09 +05:30
|
|
|
state.queryText = query.query
|
|
|
|
state.resetQuery(query)
|
2021-11-09 23:13:15 +05:30
|
|
|
updateFavoriteItem()
|
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
|
|
|
}
|
2022-12-09 05:45:19 +05:30
|
|
|
.onChange(of: accounts.current) { _ in
|
|
|
|
state.reloadQuery()
|
|
|
|
}
|
2022-08-05 04:00:09 +05:30
|
|
|
.onChange(of: state.queryText) { newQuery in
|
2021-11-28 20:07:55 +05:30
|
|
|
if newQuery.isEmpty {
|
|
|
|
favoriteItem = nil
|
2022-08-05 04:00:09 +05:30
|
|
|
state.resetQuery()
|
2021-11-28 20:07:55 +05:30
|
|
|
} else {
|
|
|
|
updateFavoriteItem()
|
2021-09-25 17:47:58 +05:30
|
|
|
}
|
2021-09-26 23:10:25 +05:30
|
|
|
|
2022-08-17 21:04:25 +05:30
|
|
|
state.loadSuggestions(newQuery)
|
2021-09-26 23:10:25 +05:30
|
|
|
|
|
|
|
#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-11-09 23:13:15 +05:30
|
|
|
state.changeQuery { query in
|
|
|
|
query.query = newQuery
|
|
|
|
}
|
2021-09-26 23:10:25 +05:30
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2021-07-08 04:09:18 +05:30
|
|
|
.onChange(of: searchSortOrder) { order in
|
2021-11-09 23:13:15 +05:30
|
|
|
state.changeQuery { query in
|
|
|
|
query.sortBy = order
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
2021-07-08 04:09:18 +05:30
|
|
|
}
|
|
|
|
.onChange(of: searchDate) { date in
|
2021-11-09 23:13:15 +05:30
|
|
|
state.changeQuery { query in
|
|
|
|
query.date = date
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
2021-07-08 04:09:18 +05:30
|
|
|
}
|
|
|
|
.onChange(of: searchDuration) { duration in
|
2021-11-09 23:13:15 +05:30
|
|
|
state.changeQuery { query in
|
|
|
|
query.duration = duration
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
2021-07-08 04:09:18 +05:30
|
|
|
}
|
2021-12-01 16:52:19 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
.searchable(text: $state.queryText) {
|
2022-08-17 21:04:25 +05:30
|
|
|
if !state.queryText.isEmpty {
|
|
|
|
ForEach(state.querySuggestions, id: \.self) { suggestion in
|
|
|
|
Text(suggestion)
|
|
|
|
.searchCompletion(suggestion)
|
|
|
|
}
|
2021-12-01 16:52:19 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
.ignoresSafeArea(.keyboard, edges: .bottom)
|
|
|
|
.navigationTitle("Search")
|
2021-09-25 17:47:58 +05:30
|
|
|
#endif
|
|
|
|
#if os(iOS)
|
2022-01-25 02:52:47 +05:30
|
|
|
.navigationBarHidden(navigationBarHidden)
|
2021-12-03 02:05:25 +05:30
|
|
|
.navigationBarTitleDisplayMode(.inline)
|
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
|
|
|
|
2022-01-25 02:52:47 +05:30
|
|
|
private var navigationBarHidden: Bool {
|
|
|
|
if navigationStyle == .sidebar {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
let preferred = Defaults[.visibleSections]
|
|
|
|
var visibleSections = [VisibleSection]()
|
|
|
|
|
|
|
|
if accounts.app.supportsPopular && preferred.contains(.popular) {
|
|
|
|
visibleSections.append(.popular)
|
|
|
|
}
|
|
|
|
|
|
|
|
if accounts.app.supportsSubscriptions && accounts.signedIn && preferred.contains(.subscriptions) {
|
|
|
|
visibleSections.append(.subscriptions)
|
|
|
|
}
|
|
|
|
|
2022-05-23 13:36:27 +05:30
|
|
|
if accounts.app.supportsUserPlaylists && accounts.signedIn && preferred.contains(.playlists) {
|
2022-01-25 02:52:47 +05:30
|
|
|
visibleSections.append(.playlists)
|
|
|
|
}
|
|
|
|
|
2022-11-12 01:58:40 +05:30
|
|
|
if preferred.contains(.trending) {
|
|
|
|
visibleSections.append(.trending)
|
2022-01-25 02:52:47 +05:30
|
|
|
}
|
|
|
|
|
2022-11-12 01:58:40 +05:30
|
|
|
return !visibleSections.isEmpty || showHome
|
2022-01-25 02:52:47 +05:30
|
|
|
}
|
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
private var results: some View {
|
|
|
|
VStack {
|
|
|
|
if showRecentQueries {
|
|
|
|
recentQueries
|
|
|
|
} else {
|
|
|
|
#if os(tvOS)
|
|
|
|
ScrollView(.vertical, showsIndicators: false) {
|
|
|
|
HStack(spacing: 0) {
|
|
|
|
if accounts.app.supportsSearchFilters {
|
|
|
|
filtersHorizontalStack
|
|
|
|
}
|
|
|
|
|
2021-12-03 02:05:25 +05:30
|
|
|
FavoriteButton(item: favoriteItem)
|
|
|
|
.id(favoriteItem?.id)
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
.font(.system(size: 25))
|
2021-11-28 20:07:55 +05:30
|
|
|
}
|
|
|
|
|
2022-11-27 16:12:16 +05:30
|
|
|
HorizontalCells(items: state.store.collection)
|
2022-01-05 04:48:01 +05:30
|
|
|
.environment(\.loadMoreContentHandler) { state.loadNextPage() }
|
2021-11-28 20:07:55 +05:30
|
|
|
}
|
|
|
|
.edgesIgnoringSafeArea(.horizontal)
|
|
|
|
#else
|
2022-11-27 16:12:16 +05:30
|
|
|
VerticalCells(items: state.store.collection, allowEmpty: state.query.isEmpty)
|
2022-01-05 04:48:01 +05:30
|
|
|
.environment(\.loadMoreContentHandler) { state.loadNextPage() }
|
2021-11-28 20:07:55 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
if noResults {
|
|
|
|
Text("No results")
|
|
|
|
|
|
|
|
if searchFiltersActive {
|
|
|
|
Button("Reset search filters", action: resetFilters)
|
|
|
|
}
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var toolbarPlacement: ToolbarItemPlacement {
|
2021-09-26 23:10:25 +05:30
|
|
|
#if os(iOS)
|
2021-12-03 02:05:25 +05:30
|
|
|
accounts.app.supportsSearchFilters || favorites.isEnabled ? .bottomBar : .automatic
|
2021-09-26 23:10:25 +05:30
|
|
|
#else
|
2021-12-03 02:05:25 +05:30
|
|
|
.automatic
|
2021-09-26 23:10:25 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
private var showRecentQueries: Bool {
|
2021-12-01 16:52:19 +05:30
|
|
|
navigationStyle == .tab && saveRecents && state.queryText.isEmpty
|
2021-09-27 01:42:43 +05:30
|
|
|
}
|
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
private var filtersActive: Bool {
|
2021-09-26 23:10:25 +05:30
|
|
|
searchDuration != .any || searchDate != .any
|
|
|
|
}
|
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
private func resetFilters() {
|
2021-09-27 01:42:43 +05:30
|
|
|
searchSortOrder = .relevance
|
|
|
|
searchDate = .any
|
|
|
|
searchDuration = .any
|
|
|
|
}
|
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
private var noResults: Bool {
|
2022-11-27 16:12:16 +05:30
|
|
|
state.store.collection.isEmpty && !state.isLoading && !state.query.isEmpty
|
2021-09-27 01:42:43 +05:30
|
|
|
}
|
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
private var recentQueries: some View {
|
2021-09-26 23:10:25 +05:30
|
|
|
VStack {
|
|
|
|
List {
|
|
|
|
Section(header: Text("Recents")) {
|
2022-09-01 00:54:46 +05:30
|
|
|
if recentlyOpened.isEmpty {
|
2021-09-26 23:10:25 +05:30
|
|
|
Text("Search history is empty")
|
|
|
|
.foregroundColor(.secondary)
|
2021-09-19 18:12:47 +05:30
|
|
|
}
|
2022-09-02 18:45:54 +05:30
|
|
|
ForEach(recentlyOpened.reversed(), id: \.tag) { item in
|
2022-06-14 21:42:06 +05:30
|
|
|
recentItemButton(item)
|
2021-09-26 23:10:25 +05:30
|
|
|
}
|
2021-09-19 18:12:47 +05:30
|
|
|
}
|
2021-09-26 23:10:25 +05:30
|
|
|
.redrawOn(change: recentsChanged)
|
2022-08-21 21:09:06 +05:30
|
|
|
|
|
|
|
Section(footer: Color.clear.frame(minHeight: 80)) {
|
|
|
|
clearHistoryButton
|
|
|
|
}
|
2021-09-26 23:10:25 +05:30
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2022-06-14 21:42:06 +05:30
|
|
|
private func recentItemButton(_ item: RecentItem) -> some View {
|
|
|
|
Button {
|
|
|
|
switch item.type {
|
|
|
|
case .query:
|
2022-08-17 21:04:25 +05:30
|
|
|
state.queryText = item.title
|
2022-06-14 21:42:06 +05:30
|
|
|
state.changeQuery { query in query.query = item.title }
|
2022-11-25 02:06:05 +05:30
|
|
|
NavigationModel.shared.hideKeyboard()
|
2022-06-14 21:42:06 +05:30
|
|
|
|
|
|
|
updateFavoriteItem()
|
2022-11-25 02:06:05 +05:30
|
|
|
RecentsModel.shared.add(item)
|
2022-06-14 21:42:06 +05:30
|
|
|
case .channel:
|
|
|
|
guard let channel = item.channel else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-25 02:06:05 +05:30
|
|
|
NavigationModel.shared.openChannel(
|
2022-06-14 21:42:06 +05:30
|
|
|
channel,
|
2022-06-30 13:35:32 +05:30
|
|
|
navigationStyle: navigationStyle
|
2022-06-14 21:42:06 +05:30
|
|
|
)
|
|
|
|
case .playlist:
|
|
|
|
guard let playlist = item.playlist else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-25 02:06:05 +05:30
|
|
|
NavigationModel.shared.openChannelPlaylist(
|
2022-06-14 21:42:06 +05:30
|
|
|
playlist,
|
2022-06-30 13:35:32 +05:30
|
|
|
navigationStyle: navigationStyle
|
2022-06-14 21:42:06 +05:30
|
|
|
)
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
let systemImage = item.type == .query ? "magnifyingglass" :
|
|
|
|
item.type == .channel ? RecentsModel.symbolSystemImage(item.title) :
|
|
|
|
"list.and.film"
|
|
|
|
Label(item.title, systemImage: systemImage)
|
|
|
|
}
|
|
|
|
.contextMenu {
|
|
|
|
removeButton(item)
|
2022-08-21 21:09:06 +05:30
|
|
|
|
2022-08-14 22:29:21 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
Button("Cancel", role: .cancel) {}
|
|
|
|
#endif
|
2022-06-14 21:42:06 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 22:25:56 +05:30
|
|
|
private func removeButton(_ item: RecentItem) -> some View {
|
2021-11-28 20:07:55 +05:30
|
|
|
Button {
|
2022-11-25 02:06:05 +05:30
|
|
|
RecentsModel.shared.close(item)
|
2021-11-28 20:07:55 +05:30
|
|
|
recentsChanged.toggle()
|
|
|
|
} label: {
|
2022-01-06 22:25:56 +05:30
|
|
|
Label("Remove", systemImage: "trash")
|
2021-09-19 18:12:47 +05:30
|
|
|
}
|
2021-11-28 20:07:55 +05:30
|
|
|
}
|
2021-09-19 18:12:47 +05:30
|
|
|
|
2022-08-21 21:09:06 +05:30
|
|
|
private var clearHistoryButton: some View {
|
2021-11-28 20:07:55 +05:30
|
|
|
Button {
|
2022-11-25 02:06:05 +05:30
|
|
|
NavigationModel.shared.presentAlert(
|
2022-08-21 21:09:06 +05:30
|
|
|
Alert(
|
|
|
|
title: Text("Are you sure you want to clear search history?"),
|
|
|
|
message: Text("This cannot be reverted"),
|
|
|
|
primaryButton: .destructive(Text("Clear")) {
|
2022-11-25 02:06:05 +05:30
|
|
|
RecentsModel.shared.clear()
|
2022-08-21 21:09:06 +05:30
|
|
|
recentsChanged.toggle()
|
|
|
|
},
|
|
|
|
secondaryButton: .cancel()
|
|
|
|
)
|
|
|
|
)
|
2021-11-28 20:07:55 +05:30
|
|
|
} label: {
|
2022-08-21 21:09:06 +05:30
|
|
|
Label("Clear Search History...", systemImage: "trash.fill")
|
2021-09-19 18:12:47 +05:30
|
|
|
}
|
2022-08-21 21:09:06 +05:30
|
|
|
.labelStyle(.titleOnly)
|
|
|
|
.foregroundColor(Color("AppRedColor"))
|
2021-09-19 18:12:47 +05:30
|
|
|
}
|
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
private 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
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
private var searchSortOrderPicker: some View {
|
2021-09-26 23:10:25 +05:30
|
|
|
Picker("Sort", selection: $searchSortOrder) {
|
|
|
|
ForEach(SearchQuery.SortOrder.allCases) { sortOrder in
|
|
|
|
Text(sortOrder.name).tag(sortOrder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if os(tvOS)
|
2021-11-28 20:07:55 +05:30
|
|
|
private var searchSortOrderButton: some View {
|
2021-09-26 23:10:25 +05:30
|
|
|
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 20:07:55 +05:30
|
|
|
private var searchDateButton: some View {
|
2021-09-26 23:10:25 +05:30
|
|
|
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 20:07:55 +05:30
|
|
|
private var searchDurationButton: some View {
|
2021-09-26 23:10:25 +05:30
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
private var filtersHorizontalStack: some View {
|
2021-09-26 23:10:25 +05:30
|
|
|
HStack {
|
|
|
|
HStack(spacing: 30) {
|
|
|
|
Text("Sort")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
searchSortOrderButton
|
|
|
|
}
|
2021-11-09 23:13:15 +05:30
|
|
|
.frame(maxWidth: 300, 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
|
|
|
|
}
|
2021-11-09 23:13:15 +05:30
|
|
|
.frame(maxWidth: 300)
|
2021-09-26 23:10:25 +05:30
|
|
|
|
|
|
|
HStack(spacing: 30) {
|
|
|
|
Text("Date")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
searchDateButton
|
|
|
|
}
|
2021-11-09 23:13:15 +05:30
|
|
|
.frame(maxWidth: 300, alignment: .leading)
|
2021-09-26 23:10:25 +05:30
|
|
|
}
|
2021-09-29 17:22:09 +05:30
|
|
|
.font(.system(size: 30))
|
2021-09-26 23:10:25 +05:30
|
|
|
}
|
|
|
|
#else
|
2021-11-28 20:07:55 +05:30
|
|
|
private var filtersMenu: some View {
|
2021-09-26 23:10:25 +05:30
|
|
|
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 23:13:15 +05:30
|
|
|
|
|
|
|
private func updateFavoriteItem() {
|
|
|
|
favoriteItem = FavoriteItem(section: .searchQuery(
|
2022-08-17 21:04:25 +05:30
|
|
|
state.queryText,
|
|
|
|
searchDate.rawValue,
|
|
|
|
searchDuration.rawValue,
|
|
|
|
searchSortOrder.rawValue
|
2021-11-09 23:13:15 +05:30
|
|
|
))
|
|
|
|
}
|
2021-09-26 23:10:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|