yattee/Shared/Navigation/AppSidebarRecents.swift

105 lines
3.6 KiB
Swift
Raw Normal View History

2021-09-19 11:06:54 +00:00
import Defaults
import SwiftUI
struct AppSidebarRecents: View {
@ObservedObject private var navigation = NavigationModel.shared
var recents = RecentsModel.shared
2021-09-19 11:06:54 +00:00
@Default(.recentlyOpened) private var recentItems
var body: some View {
Group {
if !recentItems.isEmpty {
Section(header: Text("Recents")) {
2021-09-19 12:42:47 +00:00
ForEach(recentItems.reversed()) { recent in
2021-09-19 11:06:54 +00:00
Group {
switch recent.type {
case .channel:
2021-09-28 23:01:49 +00:00
RecentNavigationLink(recent: recent) {
2022-12-10 21:37:14 +00:00
LazyView(ChannelVideosView(channel: recent.channel!).modifier(PlayerOverlayModifier()))
2021-09-19 11:06:54 +00:00
}
2021-10-22 23:04:03 +00:00
case .playlist:
RecentNavigationLink(recent: recent, systemImage: "list.and.film") {
2022-12-10 21:37:14 +00:00
LazyView(ChannelPlaylistView(playlist: recent.playlist!).modifier(PlayerOverlayModifier()))
2021-10-22 23:04:03 +00:00
}
2021-09-19 11:06:54 +00:00
case .query:
2021-09-28 23:01:49 +00:00
RecentNavigationLink(recent: recent, systemImage: "magnifyingglass") {
2022-12-10 21:37:14 +00:00
LazyView(SearchView(recent.query!).modifier(PlayerOverlayModifier()))
2021-09-19 11:06:54 +00:00
}
}
}
.contextMenu {
Button("Clear All Recents") {
recents.clear()
}
Button("Clear Search History") {
recents.clearQueries()
}
.disabled(!recentItems.contains { $0.type == .query })
}
}
}
}
}
}
}
struct RecentNavigationLink<DestinationContent: View>: View {
var recents = RecentsModel.shared
@ObservedObject private var navigation = NavigationModel.shared
2021-09-19 11:06:54 +00:00
var recent: RecentItem
var systemImage: String?
let destination: DestinationContent
init(
recent: RecentItem,
systemImage: String? = nil,
@ViewBuilder destination: () -> DestinationContent
) {
self.recent = recent
self.systemImage = systemImage
self.destination = destination()
}
var body: some View {
2021-09-28 23:01:49 +00:00
NavigationLink(tag: TabSelection.recentlyOpened(recent.tag), selection: $navigation.tabSelection) {
2021-09-19 11:06:54 +00:00
destination
} label: {
HStack {
2022-12-14 17:10:50 +00:00
if recent.type == .channel,
let channel = recent.channel,
channel.thumbnailURLOrCached != nil
{
ChannelAvatarView(channel: channel, subscribedBadge: false)
2022-12-15 21:41:42 +00:00
.frame(width: Constants.sidebarChannelThumbnailSize, height: Constants.sidebarChannelThumbnailSize)
2022-12-14 17:10:50 +00:00
Text(channel.name)
} else {
Label(recent.title, systemImage: labelSystemImage)
.lineLimit(1)
}
2021-09-19 11:06:54 +00:00
Spacer()
Button(action: {
recents.close(recent)
}) {
Image(systemName: "xmark.circle.fill")
}
.foregroundColor(.secondary)
.opacity(0.5)
2021-09-19 11:06:54 +00:00
.buttonStyle(.plain)
}
}
.id(recent.tag)
}
var labelSystemImage: String {
systemImage != nil ? systemImage! : RecentsModel.symbolSystemImage(recent.title)
2021-09-19 11:06:54 +00:00
}
}