yattee/Shared/Navigation/AppSidebarSubscriptions.swift

83 lines
2.8 KiB
Swift
Raw Normal View History

2021-09-25 08:18:22 +00:00
import Defaults
2021-08-29 21:36:18 +00:00
import SwiftUI
struct AppSidebarSubscriptions: View {
@ObservedObject private var navigation = NavigationModel.shared
2022-12-13 12:14:20 +00:00
@ObservedObject private var feed = FeedModel.shared
2022-12-11 15:15:42 +00:00
@ObservedObject private var subscriptions = SubscribedChannelsModel.shared
2021-08-29 21:36:18 +00:00
2022-12-13 12:14:20 +00:00
@ObservedObject private var accounts = AccountsModel.shared
2021-08-29 21:36:18 +00:00
var body: some View {
Section(header: Text("Subscriptions")) {
ForEach(subscriptions.all) { channel in
2021-09-28 23:01:49 +00:00
NavigationLink(tag: TabSelection.channel(channel.id), selection: $navigation.tabSelection) {
2022-12-10 21:37:14 +00:00
LazyView(ChannelVideosView(channel: channel).modifier(PlayerOverlayModifier()))
2021-08-29 21:36:18 +00:00
} label: {
2022-12-13 12:14:20 +00:00
HStack {
if channel.thumbnailURL != nil {
2022-12-11 17:11:56 +00:00
ChannelAvatarView(channel: channel, subscribedBadge: false)
.frame(width: 20, height: 20)
Text(channel.name)
2022-12-13 12:14:20 +00:00
} else {
Label(channel.name, systemImage: RecentsModel.symbolSystemImage(channel.name))
2022-12-11 17:11:56 +00:00
}
}
2022-12-13 12:14:20 +00:00
.backport
.badge(channelBadge(channel))
2021-08-29 21:36:18 +00:00
}
.contextMenu {
if subscriptions.isSubscribing(channel.id) {
toggleWatchedButton(channel)
}
2021-08-29 21:36:18 +00:00
Button("Unsubscribe") {
2022-06-24 23:21:05 +00:00
navigation.presentUnsubscribeAlert(channel, subscriptions: subscriptions)
2021-08-29 21:36:18 +00:00
}
}
2021-09-28 23:01:49 +00:00
.id("channel\(channel.id)")
2021-08-29 21:36:18 +00:00
}
}
}
2022-12-13 12:14:20 +00:00
func channelBadge(_ channel: Channel) -> Text? {
if let count = feed.unwatchedByChannel[accounts.current]?[channel.id] {
return Text(String(count))
}
return nil
}
@ViewBuilder func toggleWatchedButton(_ channel: Channel) -> some View {
if feed.canMarkChannelAsWatched(channel.id) {
markChannelAsWatchedButton(channel)
} else {
markChannelAsUnwatchedButton(channel)
}
}
func markChannelAsWatchedButton(_ channel: Channel) -> some View {
Button {
feed.markChannelAsWatched(channel.id)
} label: {
Label("Mark channel feed as watched", systemImage: "checkmark.circle.fill")
}
.disabled(!feed.canMarkAllFeedAsWatched)
}
func markChannelAsUnwatchedButton(_ channel: Channel) -> some View {
Button {
feed.markChannelAsUnwatched(channel.id)
} label: {
Label("Mark channel feed as unwatched", systemImage: "checkmark.circle")
}
}
2021-08-29 21:36:18 +00:00
}
struct AppSidebarSubscriptions_Previews: PreviewProvider {
static var previews: some View {
AppSidebarSubscriptions()
}
}