yattee/Shared/Search/SearchView.swift

464 lines
15 KiB
Swift
Raw Normal View History

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
@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
#if os(tvOS)
@State private var searchDebounce = Debounce()
@State private var recentsDebounce = Debounce()
#endif
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
2021-10-20 22:21:50 +00:00
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<PlayerModel> private var player
2021-09-25 12:17:58 +00:00
@EnvironmentObject<RecentsModel> private var recents
@EnvironmentObject<SearchModel> private var state
2021-12-02 20:35:25 +00:00
private var favorites = FavoritesModel.shared
2021-09-19 11:06:54 +00:00
@Default(.saveRecents) private var saveRecents
2021-09-29 11:45:00 +00:00
private var videos = [Video]()
2021-10-22 23:04:03 +00:00
var items: [ContentItem] {
state.store.collection.sorted { $0 < $1 }
}
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
}
2021-06-11 12:36:26 +00:00
var body: some View {
PlayerControlsView {
2021-11-28 14:37:55 +00:00
#if os(iOS)
VStack {
2021-12-04 19:35:41 +00:00
SearchTextField(favoriteItem: $favoriteItem)
2021-11-28 14:37:55 +00:00
if state.query.query != state.queryText, !state.queryText.isEmpty, !state.querySuggestions.collection.isEmpty {
SearchSuggestions()
} else {
results
}
}
#else
ZStack {
results
2021-12-01 11:22:19 +00:00
#if !os(tvOS)
if state.query.query != state.queryText, !state.queryText.isEmpty, !state.querySuggestions.collection.isEmpty {
HStack {
Spacer()
SearchSuggestions()
.borderLeading(width: 1, color: Color("ControlsBorderColor"))
.frame(maxWidth: 280)
}
}
2021-12-01 11:22:19 +00:00
#endif
2021-09-19 12:42:47 +00:00
}
2021-11-28 14:37:55 +00:00
#endif
2021-07-07 22:39:18 +00:00
}
2021-09-25 12:17:58 +00:00
.toolbar {
#if !os(tvOS)
ToolbarItemGroup(placement: toolbarPlacement) {
2021-11-09 17:43:15 +00:00
#if os(macOS)
2021-12-02 20:35:25 +00:00
FavoriteButton(item: favoriteItem)
.id(favoriteItem?.id)
2021-11-09 17:43:15 +00:00
#endif
2021-10-20 22:21:50 +00:00
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-11-09 17:43:15 +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
#if os(macOS)
SearchTextField()
#endif
2021-09-25 12:17:58 +00:00
}
#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-11-09 17:43:15 +00:00
updateFavoriteItem()
2021-07-07 22:39:18 +00:00
}
2021-09-29 11:45:00 +00:00
if !videos.isEmpty {
state.store.replace(ContentItem.array(of: videos))
2021-09-29 11:45:00 +00:00
}
2021-07-07 22:39:18 +00:00
}
2021-11-28 14:37:55 +00:00
.onChange(of: state.query.query) { newQuery in
if newQuery.isEmpty {
favoriteItem = nil
} else {
updateFavoriteItem()
2021-09-25 12:17:58 +00:00
}
}
.onChange(of: state.queryText) { newQuery in
if newQuery.isEmpty {
state.resetQuery()
}
state.loadSuggestions(newQuery)
#if os(tvOS)
searchDebounce.invalidate()
recentsDebounce.invalidate()
searchDebounce.debouncing(2) {
2021-11-09 17:43:15 +00:00
state.changeQuery { query in
query.query = newQuery
updateFavoriteItem()
}
}
recentsDebounce.debouncing(10) {
recents.addQuery(newQuery)
}
#endif
2021-09-25 12:17:58 +00:00
}
2021-11-28 14:37:55 +00:00
2021-07-07 22:39:18 +00:00
.onChange(of: searchSortOrder) { order in
2021-11-09 17:43:15 +00:00
state.changeQuery { query in
query.sortBy = order
updateFavoriteItem()
}
2021-07-07 22:39:18 +00:00
}
.onChange(of: searchDate) { date in
2021-11-09 17:43:15 +00:00
state.changeQuery { query in
query.date = date
updateFavoriteItem()
}
2021-07-07 22:39:18 +00:00
}
.onChange(of: searchDuration) { duration in
2021-11-09 17:43:15 +00:00
state.changeQuery { query in
query.duration = duration
updateFavoriteItem()
}
2021-07-07 22:39:18 +00:00
}
2021-12-01 11:22:19 +00:00
#if os(tvOS)
.searchable(text: $state.queryText) {
ForEach(state.querySuggestions.collection, id: \.self) { suggestion in
Text(suggestion)
.searchCompletion(suggestion)
}
}
#else
.ignoresSafeArea(.keyboard, edges: .bottom)
.navigationTitle("Search")
2021-09-25 12:17:58 +00:00
#endif
#if os(iOS)
.navigationBarHidden(!Defaults[.visibleSections].isEmpty || navigationStyle == .sidebar)
2021-12-02 20:35:25 +00:00
.navigationBarTitleDisplayMode(.inline)
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-11-28 14:37:55 +00:00
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-02 20:35:25 +00:00
FavoriteButton(item: favoriteItem)
.id(favoriteItem?.id)
.labelStyle(.iconOnly)
.font(.system(size: 25))
2021-11-28 14:37:55 +00:00
}
HorizontalCells(items: items)
.environment(\.loadMoreContentHandler) { state.loadNextPage() }
2021-11-28 14:37:55 +00:00
}
.edgesIgnoringSafeArea(.horizontal)
#else
VerticalCells(items: items)
.environment(\.loadMoreContentHandler) { state.loadNextPage() }
2021-11-28 14:37:55 +00:00
#endif
if noResults {
Text("No results")
if searchFiltersActive {
Button("Reset search filters", action: resetFilters)
}
Spacer()
}
}
}
}
private var toolbarPlacement: ToolbarItemPlacement {
#if os(iOS)
2021-12-02 20:35:25 +00:00
accounts.app.supportsSearchFilters || favorites.isEnabled ? .bottomBar : .automatic
#else
2021-12-02 20:35:25 +00:00
.automatic
#endif
}
2021-11-28 14:37:55 +00:00
private var showRecentQueries: Bool {
navigationStyle == .tab && saveRecents && state.queryText.isEmpty
}
2021-11-28 14:37:55 +00:00
private var filtersActive: Bool {
searchDuration != .any || searchDate != .any
}
2021-11-28 14:37:55 +00:00
private func resetFilters() {
searchSortOrder = .relevance
searchDate = .any
searchDuration = .any
}
2021-11-28 14:37:55 +00:00
private var noResults: Bool {
2021-10-22 23:04:03 +00:00
items.isEmpty && !state.isLoading && !state.query.isEmpty
}
2021-11-28 14:37:55 +00:00
private var recentQueries: some View {
VStack {
List {
Section(header: Text("Recents")) {
if recentItems.isEmpty {
Text("Search history is empty")
.foregroundColor(.secondary)
2021-09-19 12:42:47 +00:00
}
ForEach(recentItems) { item in
Button {
switch item.type {
case .query:
state.queryText = item.title
state.changeQuery { query in query.query = item.title }
updateFavoriteItem()
recents.add(item)
case .channel:
guard let channel = item.channel else {
return
}
NavigationModel.openChannel(
channel,
player: player,
recents: recents,
navigation: navigation,
navigationStyle: navigationStyle,
delay: false
)
case .playlist:
guard let playlist = item.playlist else {
return
}
NavigationModel.openChannelPlaylist(
playlist,
player: player,
recents: recents,
navigation: navigation,
navigationStyle: navigationStyle,
delay: false
)
}
} label: {
let systemImage = item.type == .query ? "magnifyingglass" :
item.type == .channel ? RecentsModel.symbolSystemImage(item.title) :
"list.and.film"
Label(item.title, systemImage: systemImage)
.lineLimit(1)
2021-09-19 12:42:47 +00:00
}
2021-11-08 16:29:35 +00:00
.contextMenu {
removeButton(item)
removeAllButton
2021-11-08 16:29:35 +00:00
}
}
2021-09-19 12:42:47 +00:00
}
.redrawOn(change: recentsChanged)
}
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
}
private func removeButton(_ item: RecentItem) -> some View {
2021-11-28 14:37:55 +00:00
Button {
recents.close(item)
recentsChanged.toggle()
} label: {
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
private var removeAllButton: some View {
2021-11-28 14:37:55 +00:00
Button {
recents.clearQueries()
recentsChanged.toggle()
} label: {
Label("Remove All", systemImage: "trash.fill")
2021-09-19 12:42:47 +00:00
}
}
2021-11-28 14:37:55 +00:00
private var searchFiltersActive: Bool {
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 recentItems: [RecentItem] {
Defaults[.recentlyOpened].reversed()
2021-09-19 12:42:47 +00:00
}
2021-11-28 14:37:55 +00:00
private var searchSortOrderPicker: some View {
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 {
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 {
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 {
Button(action: { self.searchDuration = self.searchDuration.next() }) {
2021-09-29 11:45:00 +00:00
Text(self.searchDuration.name)
.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 {
HStack {
HStack(spacing: 30) {
Text("Sort")
.foregroundColor(.secondary)
searchSortOrderButton
}
2021-11-09 17:43:15 +00:00
.frame(maxWidth: 300, alignment: .trailing)
HStack(spacing: 30) {
Text("Duration")
.foregroundColor(.secondary)
searchDurationButton
}
2021-11-09 17:43:15 +00:00
.frame(maxWidth: 300)
HStack(spacing: 30) {
Text("Date")
.foregroundColor(.secondary)
searchDateButton
}
2021-11-09 17:43:15 +00:00
.frame(maxWidth: 300, alignment: .leading)
}
.font(.system(size: 30))
}
#else
2021-11-28 14:37:55 +00:00
private 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
2021-11-09 17:43:15 +00:00
private func updateFavoriteItem() {
favoriteItem = FavoriteItem(section: .searchQuery(
state.query.query,
state.query.date?.rawValue ?? "",
state.query.duration?.rawValue ?? "",
state.query.sortBy.rawValue
))
}
}
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-06-11 12:36:26 +00:00
}