yattee/Shared/Views/ChannelVideosView.swift

179 lines
5.3 KiB
Swift
Raw Normal View History

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-11-13 15:45:47 +00:00
@State private var shareURL: URL?
2022-03-26 13:37:55 +00:00
@State private var subscriptionToggleButtonDisabled = false
2021-10-26 22:59:59 +00:00
2021-09-25 08:18:22 +00:00
@StateObject private var store = Store<Channel>()
@Environment(\.colorScheme) private var colorScheme
#if os(iOS)
@Environment(\.inNavigationView) private var inNavigationView
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
@EnvironmentObject<PlayerModel> private var player
#endif
2021-10-22 23:04:03 +00:00
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<SubscriptionsModel> private var subscriptions
@Namespace private var focusNamespace
2021-08-29 21:36:18 +00:00
var videos: [ContentItem] {
ContentItem.array(of: store.item?.videos ?? [])
}
2021-08-29 21:36:18 +00:00
var body: some View {
#if os(iOS)
if inNavigationView {
content
} else {
PlayerControlsView {
content
}
}
#else
PlayerControlsView {
content
}
#endif
}
var content: some View {
2021-11-28 14:37:55 +00:00
let content = 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)
if let subscribers = store.item?.subscriptionsString {
Text("**\(subscribers)** subscribers")
.foregroundColor(.secondary)
}
subscriptionToggleButton
}
.frame(maxWidth: .infinity)
#endif
VerticalCells(items: videos)
.environment(\.inChannelView, true)
#if os(tvOS)
.prefersDefaultFocus(in: focusNamespace)
#endif
}
2021-11-28 14:37:55 +00:00
2021-08-29 21:36:18 +00:00
#if !os(tvOS)
.toolbar {
ToolbarItem(placement: .navigation) {
ShareButton(
contentItem: contentItem,
presentingShareSheet: $presentingShareSheet,
shareURL: $shareURL
)
}
2021-10-26 22:59:59 +00:00
ToolbarItem {
HStack {
HStack(spacing: 3) {
Text("\(store.item?.subscriptionsString ?? "loading")")
.fontWeight(.bold)
Text(" subscribers")
}
.allowsTightening(true)
.foregroundColor(.secondary)
.opacity(store.item?.subscriptionsString != nil ? 1 : 0)
subscriptionToggleButton
FavoriteButton(item: FavoriteItem(section: .channel(channel.id, channel.name)))
}
}
}
#endif
2021-10-26 22:59:59 +00:00
#if os(iOS)
2021-11-08 16:29:35 +00:00
.sheet(isPresented: $presentingShareSheet) {
2021-11-13 15:45:47 +00:00
if let shareURL = shareURL {
ShareSheet(activityItems: [shareURL])
2021-10-26 22:59:59 +00:00
}
2021-11-08 16:29:35 +00:00
}
2021-10-26 22:59:59 +00:00
#endif
2021-11-08 16:29:35 +00:00
.onAppear {
if store.item.isNil {
resource.addObserver(store)
resource.load()
}
2021-11-08 16:29:35 +00:00
}
2021-12-17 19:53:36 +00:00
#if os(iOS)
.navigationBarHidden(player.playerNavigationLinkActive)
#endif
2021-11-08 16:29:35 +00:00
.navigationTitle(navigationTitle)
2021-11-28 14:37:55 +00:00
return Group {
if #available(macOS 12.0, *) {
content
#if os(tvOS)
.background(Color.background(scheme: colorScheme))
#endif
2021-11-28 14:37:55 +00:00
#if !os(iOS)
.focusScope(focusNamespace)
#endif
} else {
content
}
}
}
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-10-26 22:59:59 +00:00
private var subscriptionToggleButton: some View {
Group {
2021-10-20 22:21:50 +00:00
if accounts.app.supportsSubscriptions && accounts.signedIn {
if subscriptions.isSubscribing(channel.id) {
Button("Unsubscribe") {
2022-03-26 13:37:55 +00:00
subscriptionToggleButtonDisabled = true
subscriptions.unsubscribe(channel.id) {
self.subscriptionToggleButtonDisabled = false
}
2021-10-20 22:21:50 +00:00
}
} else {
Button("Subscribe") {
2022-03-26 13:37:55 +00:00
subscriptionToggleButtonDisabled = true
2021-10-20 22:21:50 +00:00
subscriptions.subscribe(channel.id) {
2022-03-26 13:37:55 +00:00
subscriptionToggleButtonDisabled = false
2021-10-20 22:21:50 +00:00
navigation.sidebarSectionChanged.toggle()
}
}
}
}
2021-08-29 21:36:18 +00:00
}
2022-03-26 13:37:55 +00:00
.disabled(subscriptionToggleButtonDisabled)
2021-08-29 21:36:18 +00:00
}
2021-10-26 22:59:59 +00:00
private var contentItem: ContentItem {
ContentItem(channel: channel)
}
private var navigationTitle: String {
store.item?.name ?? channel.name
}
2021-08-29 21:36:18 +00:00
}