2022-12-13 16:39:20 +05:30
|
|
|
import Defaults
|
2022-12-11 17:08:57 +05:30
|
|
|
import SDWebImageSwiftUI
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ChannelsView: View {
|
2022-12-13 17:44:20 +05:30
|
|
|
@ObservedObject private var feed = FeedModel.shared
|
2022-12-11 20:45:42 +05:30
|
|
|
@ObservedObject private var subscriptions = SubscribedChannelsModel.shared
|
2022-12-11 17:08:57 +05:30
|
|
|
@ObservedObject private var accounts = AccountsModel.shared
|
2022-12-17 02:56:14 +05:30
|
|
|
@ObservedObject private var feedCount = UnwatchedFeedCountModel.shared
|
2023-04-23 01:09:27 +05:30
|
|
|
private var navigation = NavigationModel.shared
|
2022-12-11 17:08:57 +05:30
|
|
|
|
2022-12-13 16:39:20 +05:30
|
|
|
@Default(.showCacheStatus) private var showCacheStatus
|
2023-03-01 01:47:12 +05:30
|
|
|
@Default(.showUnwatchedFeedBadges) private var showUnwatchedFeedBadges
|
2023-05-27 02:08:32 +05:30
|
|
|
@Default(.keepChannelsWithUnwatchedFeedOnTop) private var keepChannelsWithUnwatchedFeedOnTop
|
2023-07-22 22:32:59 +05:30
|
|
|
@Default(.showChannelAvatarInChannelsLists) private var showChannelAvatarInChannelsLists
|
2023-05-27 02:08:32 +05:30
|
|
|
|
|
|
|
@State private var channelLinkActive = false
|
|
|
|
@State private var channelForLink: Channel?
|
2022-12-13 16:39:20 +05:30
|
|
|
|
2022-12-11 17:08:57 +05:30
|
|
|
var body: some View {
|
|
|
|
List {
|
|
|
|
Section(header: header) {
|
2023-05-27 02:08:32 +05:30
|
|
|
ForEach(channels) { channel in
|
2023-04-23 01:09:27 +05:30
|
|
|
let label = HStack {
|
2023-07-22 22:32:59 +05:30
|
|
|
if showChannelAvatarInChannelsLists {
|
|
|
|
ChannelAvatarView(channel: channel, subscribedBadge: false)
|
2023-04-23 01:09:27 +05:30
|
|
|
.frame(width: 35, height: 35)
|
|
|
|
} else {
|
2023-05-27 02:08:32 +05:30
|
|
|
Image(systemName: RecentsModel.symbolSystemImage(channel.name))
|
|
|
|
.imageScale(.large)
|
|
|
|
.foregroundColor(.accentColor)
|
|
|
|
.frame(width: 35, height: 35)
|
2022-12-11 17:08:57 +05:30
|
|
|
}
|
2023-05-27 02:08:32 +05:30
|
|
|
Text(channel.name)
|
|
|
|
.lineLimit(1)
|
2023-04-23 01:09:27 +05:30
|
|
|
}
|
2023-07-01 22:08:11 +05:30
|
|
|
#if !os(tvOS)
|
2023-04-23 01:09:27 +05:30
|
|
|
.badge(showUnwatchedFeedBadges ? feedCount.unwatchedByChannelText(channel) : nil)
|
2023-07-01 22:08:11 +05:30
|
|
|
#endif
|
2023-04-23 01:09:27 +05:30
|
|
|
|
|
|
|
Group {
|
|
|
|
#if os(tvOS)
|
|
|
|
Button {
|
|
|
|
navigation.openChannel(channel, navigationStyle: .tab)
|
|
|
|
} label: {
|
|
|
|
label
|
|
|
|
}
|
|
|
|
#else
|
2023-05-27 02:08:32 +05:30
|
|
|
Button {
|
|
|
|
channelForLink = channel
|
|
|
|
channelLinkActive = channelForLink != nil
|
|
|
|
} label: {
|
2023-04-23 01:09:27 +05:30
|
|
|
label
|
2023-05-27 02:08:32 +05:30
|
|
|
.contentShape(Rectangle())
|
|
|
|
.foregroundColor(.primary)
|
2023-04-23 01:09:27 +05:30
|
|
|
}
|
2023-05-27 02:08:32 +05:30
|
|
|
.buttonStyle(.plain)
|
2023-04-23 01:09:27 +05:30
|
|
|
#endif
|
2022-12-11 17:08:57 +05:30
|
|
|
}
|
2022-12-11 21:36:02 +05:30
|
|
|
.contextMenu {
|
2022-12-14 22:40:01 +05:30
|
|
|
if subscriptions.isSubscribing(channel.id) {
|
|
|
|
toggleWatchedButton(channel)
|
|
|
|
}
|
2022-12-11 21:36:02 +05:30
|
|
|
Button {
|
|
|
|
subscriptions.unsubscribe(channel.id)
|
|
|
|
} label: {
|
|
|
|
Label("Unsubscribe", systemImage: "xmark.circle")
|
|
|
|
}
|
|
|
|
}
|
2022-12-11 17:08:57 +05:30
|
|
|
}
|
|
|
|
#if os(tvOS)
|
|
|
|
.padding(.horizontal, 50)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Color.clear.padding(.bottom, 50)
|
|
|
|
.listRowBackground(Color.clear)
|
2023-07-01 22:08:11 +05:30
|
|
|
#if os(iOS)
|
|
|
|
.listRowSeparator(.hidden)
|
|
|
|
#endif
|
2022-12-11 17:08:57 +05:30
|
|
|
}
|
|
|
|
}
|
2023-06-08 15:47:16 +05:30
|
|
|
#if !os(tvOS)
|
2023-05-27 02:08:32 +05:30
|
|
|
.background(
|
2023-05-29 20:01:01 +05:30
|
|
|
NavigationLink(destination: ChannelVideosView(channel: channelForLink ?? Video.fixture.channel), isActive: $channelLinkActive, label: EmptyView.init)
|
2023-05-27 02:08:32 +05:30
|
|
|
)
|
2023-06-08 15:47:16 +05:30
|
|
|
#endif
|
2022-12-11 17:08:57 +05:30
|
|
|
.onAppear {
|
|
|
|
subscriptions.load()
|
|
|
|
}
|
|
|
|
.onChange(of: accounts.current) { _ in
|
|
|
|
subscriptions.load(force: true)
|
|
|
|
}
|
|
|
|
#if os(iOS)
|
|
|
|
.refreshable {
|
2023-09-23 18:45:21 +05:30
|
|
|
subscriptions.load(force: true)
|
2022-12-11 17:08:57 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if !os(tvOS)
|
|
|
|
.background(
|
|
|
|
Button("Refresh") {
|
|
|
|
subscriptions.load(force: true)
|
|
|
|
}
|
|
|
|
.keyboardShortcut("r")
|
|
|
|
.opacity(0)
|
|
|
|
)
|
|
|
|
#endif
|
|
|
|
#if !os(macOS)
|
|
|
|
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
|
|
|
|
subscriptions.load()
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if os(tvOS)
|
|
|
|
.padding(.horizontal, 30)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-05-27 02:08:32 +05:30
|
|
|
var channels: [Channel] {
|
|
|
|
keepChannelsWithUnwatchedFeedOnTop ? subscriptions.allByUnwatchedCount : subscriptions.all
|
|
|
|
}
|
|
|
|
|
2022-12-11 17:08:57 +05:30
|
|
|
var header: some View {
|
|
|
|
HStack {
|
|
|
|
#if os(tvOS)
|
|
|
|
SubscriptionsPageButton()
|
|
|
|
#endif
|
|
|
|
|
2022-12-13 16:39:20 +05:30
|
|
|
if showCacheStatus {
|
|
|
|
Spacer()
|
2022-12-11 17:08:57 +05:30
|
|
|
|
2022-12-13 16:39:20 +05:30
|
|
|
CacheStatusHeader(
|
|
|
|
refreshTime: subscriptions.formattedCacheTime,
|
|
|
|
isLoading: subscriptions.isLoading
|
|
|
|
)
|
|
|
|
}
|
2022-12-11 17:08:57 +05:30
|
|
|
|
|
|
|
#if os(tvOS)
|
2022-12-13 16:39:20 +05:30
|
|
|
if !showCacheStatus {
|
|
|
|
Spacer()
|
|
|
|
}
|
2022-12-11 17:08:57 +05:30
|
|
|
Button {
|
|
|
|
subscriptions.load(force: true)
|
|
|
|
} label: {
|
|
|
|
Label("Refresh", systemImage: "arrow.clockwise")
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
.imageScale(.small)
|
2023-04-23 00:37:30 +05:30
|
|
|
.font(.caption)
|
2022-12-11 17:08:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#if os(tvOS)
|
|
|
|
.padding(.bottom, 15)
|
|
|
|
.padding(.top, 15)
|
|
|
|
#endif
|
|
|
|
}
|
2022-12-14 22:40:01 +05:30
|
|
|
|
|
|
|
@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")
|
|
|
|
}
|
|
|
|
}
|
2022-12-11 17:08:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
struct ChannelsView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
NavigationView {
|
|
|
|
ChannelsView()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|