2021-08-29 21:36:18 +00:00
|
|
|
import Siesta
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ChannelVideosView: View {
|
|
|
|
let channel: Channel
|
|
|
|
|
2021-10-26 22:59:59 +00:00
|
|
|
@State private var presentingShareSheet = false
|
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
@StateObject private var store = Store<Channel>()
|
|
|
|
|
2021-10-22 23:04:03 +00:00
|
|
|
@Environment(\.dismiss) private var dismiss
|
2021-08-31 21:17:50 +00:00
|
|
|
@Environment(\.inNavigationView) private var inNavigationView
|
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
|
|
|
|
#endif
|
|
|
|
|
2021-10-22 23:04:03 +00:00
|
|
|
@EnvironmentObject<AccountsModel> private var accounts
|
|
|
|
@EnvironmentObject<NavigationModel> private var navigation
|
|
|
|
@EnvironmentObject<SubscriptionsModel> private var subscriptions
|
|
|
|
|
2021-08-31 21:17:50 +00:00
|
|
|
@Namespace private var focusNamespace
|
2021-08-29 21:36:18 +00:00
|
|
|
|
2021-10-21 23:29:10 +00:00
|
|
|
var videos: [ContentItem] {
|
|
|
|
ContentItem.array(of: store.item?.videos ?? [])
|
|
|
|
}
|
|
|
|
|
2021-08-29 21:36:18 +00:00
|
|
|
var body: some View {
|
2021-10-05 20:20:09 +00:00
|
|
|
#if os(iOS)
|
|
|
|
if inNavigationView {
|
|
|
|
content
|
|
|
|
} else {
|
|
|
|
PlayerControlsView {
|
|
|
|
content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
PlayerControlsView {
|
|
|
|
content
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
var content: some View {
|
2021-08-31 21:17:50 +00:00
|
|
|
VStack {
|
|
|
|
#if os(tvOS)
|
|
|
|
HStack {
|
|
|
|
Text(navigationTitle)
|
|
|
|
.font(.title2)
|
|
|
|
.frame(alignment: .leading)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
2021-11-01 21:56:18 +00:00
|
|
|
FavoriteButton(item: FavoriteItem(section: .channel(channel.id, channel.name)))
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
|
2021-08-31 21:17:50 +00:00
|
|
|
if let subscribers = store.item?.subscriptionsString {
|
|
|
|
Text("**\(subscribers)** subscribers")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
|
|
|
|
|
|
|
subscriptionToggleButton
|
|
|
|
}
|
|
|
|
.frame(maxWidth: .infinity)
|
|
|
|
#endif
|
|
|
|
|
2021-10-21 23:29:10 +00:00
|
|
|
VerticalCells(items: videos)
|
2021-08-31 21:17:50 +00:00
|
|
|
|
|
|
|
#if !os(iOS)
|
|
|
|
.prefersDefaultFocus(in: focusNamespace)
|
|
|
|
#endif
|
|
|
|
}
|
2021-10-28 17:14:55 +00:00
|
|
|
.environment(\.inChannelView, true)
|
2021-08-31 21:17:50 +00:00
|
|
|
#if !os(iOS)
|
|
|
|
.focusScope(focusNamespace)
|
|
|
|
#endif
|
2021-08-29 21:36:18 +00:00
|
|
|
#if !os(tvOS)
|
2021-08-31 21:17:50 +00:00
|
|
|
.toolbar {
|
2021-11-02 19:40:49 +00:00
|
|
|
ToolbarItem(placement: .navigation) {
|
2021-10-26 22:59:59 +00:00
|
|
|
ShareButton(
|
|
|
|
contentItem: contentItem,
|
|
|
|
presentingShareSheet: $presentingShareSheet
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
ToolbarItem {
|
2021-08-31 21:17:50 +00:00
|
|
|
HStack {
|
2021-09-25 08:18:22 +00:00
|
|
|
Text("**\(store.item?.subscriptionsString ?? "loading")** subscribers")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
.opacity(store.item?.subscriptionsString != nil ? 1 : 0)
|
2021-08-31 21:17:50 +00:00
|
|
|
|
|
|
|
subscriptionToggleButton
|
|
|
|
|
2021-11-01 21:56:18 +00:00
|
|
|
FavoriteButton(item: FavoriteItem(section: .channel(channel.id, channel.name)))
|
2021-08-31 21:17:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-22 23:04:03 +00:00
|
|
|
#else
|
2021-09-28 18:06:05 +00:00
|
|
|
.background(.thickMaterial)
|
|
|
|
#endif
|
2021-10-26 22:59:59 +00:00
|
|
|
#if os(iOS)
|
|
|
|
.sheet(isPresented: $presentingShareSheet) {
|
2021-10-28 17:14:55 +00:00
|
|
|
if let url = accounts.api.shareURL(contentItem) {
|
|
|
|
ShareSheet(activityItems: [url])
|
|
|
|
}
|
2021-10-26 22:59:59 +00:00
|
|
|
}
|
|
|
|
#endif
|
2021-08-31 21:17:50 +00:00
|
|
|
.modifier(UnsubscribeAlertModifier())
|
|
|
|
.onAppear {
|
2021-09-25 08:18:22 +00:00
|
|
|
if store.item.isNil {
|
|
|
|
resource.addObserver(store)
|
|
|
|
resource.load()
|
|
|
|
}
|
2021-08-31 21:17:50 +00:00
|
|
|
}
|
|
|
|
.navigationTitle(navigationTitle)
|
|
|
|
}
|
|
|
|
|
2021-10-26 22:59:59 +00:00
|
|
|
private var resource: Resource {
|
2021-10-20 22:21:50 +00:00
|
|
|
let resource = accounts.api.channel(channel.id)
|
2021-09-25 08:18:22 +00:00
|
|
|
resource.addObserver(store)
|
|
|
|
|
|
|
|
return resource
|
2021-08-31 21:17:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 22:59:59 +00:00
|
|
|
private var subscriptionToggleButton: some View {
|
2021-08-31 21:17:50 +00:00
|
|
|
Group {
|
2021-10-20 22:21:50 +00:00
|
|
|
if accounts.app.supportsSubscriptions && accounts.signedIn {
|
|
|
|
if subscriptions.isSubscribing(channel.id) {
|
|
|
|
Button("Unsubscribe") {
|
|
|
|
navigation.presentUnsubscribeAlert(channel)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Button("Subscribe") {
|
|
|
|
subscriptions.subscribe(channel.id) {
|
|
|
|
navigation.sidebarSectionChanged.toggle()
|
|
|
|
}
|
2021-08-31 21:17:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-29 21:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-31 21:17:50 +00:00
|
|
|
|
2021-10-26 22:59:59 +00:00
|
|
|
private var contentItem: ContentItem {
|
|
|
|
ContentItem(channel: channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
private var navigationTitle: String {
|
2021-08-31 21:17:50 +00:00
|
|
|
store.item?.name ?? channel.name
|
|
|
|
}
|
2021-08-29 21:36:18 +00:00
|
|
|
}
|