1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/Shared/Navigation/AppSidebarRecents.swift

87 lines
2.7 KiB
Swift
Raw Normal View History

2021-09-19 16:36:54 +05:30
import Defaults
import SwiftUI
struct AppSidebarRecents: View {
2021-09-25 17:47:58 +05:30
@EnvironmentObject<RecentsModel> private var recents
2021-09-19 16:36:54 +05:30
@Default(.recentlyOpened) private var recentItems
var body: some View {
Group {
if !recentItems.isEmpty {
Section(header: Text("Recents")) {
2021-09-19 18:12:47 +05:30
ForEach(recentItems.reversed()) { recent in
2021-09-19 16:36:54 +05:30
Group {
switch recent.type {
case .channel:
2021-09-29 04:31:49 +05:30
RecentNavigationLink(recent: recent) {
LazyView(ChannelVideosView(channel: recent.channel!))
2021-09-19 16:36:54 +05:30
}
case .query:
2021-09-29 04:31:49 +05:30
RecentNavigationLink(recent: recent, systemImage: "magnifyingglass") {
2021-09-19 16:36:54 +05:30
LazyView(SearchView(recent.query!))
}
}
}
.contextMenu {
Button("Clear All Recents") {
recents.clear()
}
Button("Clear Search History") {
recents.clearQueries()
}
.disabled(!recentItems.contains { $0.type == .query })
}
}
}
}
}
}
}
struct RecentNavigationLink<DestinationContent: View>: View {
2021-09-29 04:31:49 +05:30
@EnvironmentObject<NavigationModel> private var navigation
2021-09-25 17:47:58 +05:30
@EnvironmentObject<RecentsModel> private var recents
2021-09-19 16:36:54 +05:30
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-29 04:31:49 +05:30
NavigationLink(tag: TabSelection.recentlyOpened(recent.tag), selection: $navigation.tabSelection) {
2021-09-19 16:36:54 +05:30
destination
} label: {
HStack {
Label(recent.title, systemImage: labelSystemImage)
Spacer()
Button(action: {
recents.close(recent)
}) {
Image(systemName: "xmark.circle.fill")
}
.foregroundColor(.secondary)
.opacity(0.5)
2021-09-19 16:36:54 +05:30
.buttonStyle(.plain)
}
}
.id(recent.tag)
}
var labelSystemImage: String {
systemImage != nil ? systemImage! : AppSidebarNavigation.symbolSystemImage(recent.title)
}
}