2022-12-13 11:09:20 +00:00
|
|
|
import Defaults
|
2022-12-11 11:38:57 +00:00
|
|
|
import SDWebImageSwiftUI
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ChannelsView: View {
|
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
|
2022-12-11 11:38:57 +00:00
|
|
|
@ObservedObject private var accounts = AccountsModel.shared
|
|
|
|
|
2022-12-13 11:09:20 +00:00
|
|
|
@Default(.showCacheStatus) private var showCacheStatus
|
|
|
|
|
2022-12-11 11:38:57 +00:00
|
|
|
var body: some View {
|
|
|
|
List {
|
|
|
|
Section(header: header) {
|
|
|
|
ForEach(subscriptions.all) { channel in
|
|
|
|
NavigationLink(destination: ChannelVideosView(channel: channel).modifier(PlayerOverlayModifier())) {
|
|
|
|
HStack {
|
2022-12-13 23:07:32 +00:00
|
|
|
if let url = channel.thumbnailURLOrCached {
|
2022-12-11 11:38:57 +00:00
|
|
|
ThumbnailView(url: url)
|
|
|
|
.frame(width: 35, height: 35)
|
|
|
|
.clipShape(RoundedRectangle(cornerRadius: 35))
|
|
|
|
Text(channel.name)
|
|
|
|
} else {
|
|
|
|
Label(channel.name, systemImage: RecentsModel.symbolSystemImage(channel.name))
|
|
|
|
}
|
|
|
|
}
|
2022-12-13 12:14:20 +00:00
|
|
|
.backport
|
|
|
|
.badge(channelBadge(channel))
|
2022-12-11 11:38:57 +00:00
|
|
|
}
|
2022-12-11 16:06:02 +00:00
|
|
|
.contextMenu {
|
|
|
|
Button {
|
|
|
|
subscriptions.unsubscribe(channel.id)
|
|
|
|
} label: {
|
|
|
|
Label("Unsubscribe", systemImage: "xmark.circle")
|
|
|
|
}
|
|
|
|
}
|
2022-12-11 11:38:57 +00:00
|
|
|
}
|
|
|
|
#if os(tvOS)
|
|
|
|
.padding(.horizontal, 50)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Color.clear.padding(.bottom, 50)
|
|
|
|
.listRowBackground(Color.clear)
|
|
|
|
.backport
|
|
|
|
.listRowSeparator(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onAppear {
|
|
|
|
subscriptions.load()
|
|
|
|
}
|
|
|
|
.onChange(of: accounts.current) { _ in
|
|
|
|
subscriptions.load(force: true)
|
|
|
|
}
|
|
|
|
#if os(iOS)
|
|
|
|
.refreshControl { refreshControl in
|
|
|
|
subscriptions.load(force: true) {
|
|
|
|
refreshControl.endRefreshing()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.backport
|
|
|
|
.refreshable {
|
|
|
|
await subscriptions.load(force: true)
|
|
|
|
}
|
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-12-11 11:38:57 +00:00
|
|
|
var header: some View {
|
|
|
|
HStack {
|
|
|
|
#if os(tvOS)
|
|
|
|
SubscriptionsPageButton()
|
|
|
|
#endif
|
|
|
|
|
2022-12-13 11:09:20 +00:00
|
|
|
if showCacheStatus {
|
|
|
|
Spacer()
|
2022-12-11 11:38:57 +00:00
|
|
|
|
2022-12-13 11:09:20 +00:00
|
|
|
CacheStatusHeader(
|
|
|
|
refreshTime: subscriptions.formattedCacheTime,
|
|
|
|
isLoading: subscriptions.isLoading
|
|
|
|
)
|
|
|
|
}
|
2022-12-11 11:38:57 +00:00
|
|
|
|
|
|
|
#if os(tvOS)
|
2022-12-13 11:09:20 +00:00
|
|
|
if !showCacheStatus {
|
|
|
|
Spacer()
|
|
|
|
}
|
2022-12-11 11:38:57 +00:00
|
|
|
Button {
|
|
|
|
subscriptions.load(force: true)
|
|
|
|
} label: {
|
|
|
|
Label("Refresh", systemImage: "arrow.clockwise")
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
.imageScale(.small)
|
|
|
|
.font(.caption2)
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#if os(tvOS)
|
|
|
|
.padding(.bottom, 15)
|
|
|
|
.padding(.top, 15)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ChannelsView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
NavigationView {
|
|
|
|
ChannelsView()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|