Search navigation improvements

This commit is contained in:
Arkadiusz Fal 2022-12-11 20:58:52 +01:00
parent d4ec360581
commit 4c143f6d88

View File

@ -24,6 +24,7 @@ struct SearchView: View {
@ObservedObject private var accounts = AccountsModel.shared @ObservedObject private var accounts = AccountsModel.shared
@ObservedObject private var state = SearchModel.shared @ObservedObject private var state = SearchModel.shared
private var favorites = FavoritesModel.shared private var favorites = FavoritesModel.shared
private var navigation = NavigationModel.shared
@Default(.recentlyOpened) private var recentlyOpened @Default(.recentlyOpened) private var recentlyOpened
@Default(.saveRecents) private var saveRecents @Default(.saveRecents) private var saveRecents
@ -174,9 +175,7 @@ struct SearchView: View {
} }
ToolbarItem(placement: .principal) { ToolbarItem(placement: .principal) {
HStack(spacing: 0) { HStack(spacing: 0) {
if !state.query.isEmpty { searchMenu
searchMenu
}
SearchTextField() SearchTextField()
} }
} }
@ -207,8 +206,16 @@ struct SearchView: View {
.pickerStyle(.menu) .pickerStyle(.menu)
} }
if !state.query.isEmpty {
Section {
FavoriteButton(item: favoriteItem)
}
}
Section { Section {
FavoriteButton(item: favoriteItem) Button(action: { navigation.presentingSettings = true }) {
Label("Settings", systemImage: "gearshape.2")
}
} }
} label: { } label: {
HStack { HStack {
@ -323,7 +330,10 @@ struct SearchView: View {
.foregroundColor(.secondary) .foregroundColor(.secondary)
} }
ForEach(recentlyOpened.reversed(), id: \.tag) { item in ForEach(recentlyOpened.reversed(), id: \.tag) { item in
recentItemButton(item) recentItemControl(item)
.lineLimit(1)
.truncationMode(.middle)
.foregroundColor(.accentColor)
} }
} }
.redrawOn(change: recentsChanged) .redrawOn(change: recentsChanged)
@ -338,6 +348,50 @@ struct SearchView: View {
#endif #endif
} }
@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)
}
}
@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
}
}
private func recentItemButton(_ item: RecentItem) -> some View { private func recentItemButton(_ item: RecentItem) -> some View {
Button { Button {
switch item.type { switch item.type {
@ -368,10 +422,7 @@ struct SearchView: View {
) )
} }
} label: { } label: {
let systemImage = item.type == .query ? "magnifyingglass" : recentItemLabel(item)
item.type == .channel ? RecentsModel.symbolSystemImage(item.title) :
"list.and.film"
Label(item.title, systemImage: systemImage)
} }
.contextMenu { .contextMenu {
removeButton(item) removeButton(item)
@ -382,6 +433,13 @@ struct SearchView: View {
} }
} }
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)
}
private func removeButton(_ item: RecentItem) -> some View { private func removeButton(_ item: RecentItem) -> some View {
Button { Button {
RecentsModel.shared.close(item) RecentsModel.shared.close(item)