1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-14 22:30:32 +05:30
yattee/Shared/Views/ChannelVideosView.swift

153 lines
4.3 KiB
Swift
Raw Normal View History

2021-08-30 03:06:18 +05:30
import Siesta
import SwiftUI
struct ChannelVideosView: View {
let channel: Channel
2021-10-27 04:29:59 +05:30
@State private var presentingShareSheet = false
2021-11-13 21:15:47 +05:30
@State private var shareURL: URL?
2021-10-27 04:29:59 +05:30
2021-09-25 13:48:22 +05:30
@StateObject private var store = Store<Channel>()
2021-10-23 04:34:03 +05:30
@Environment(\.dismiss) private var dismiss
@Environment(\.inNavigationView) private var inNavigationView
#if os(iOS)
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
#endif
2021-10-23 04:34:03 +05:30
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<SubscriptionsModel> private var subscriptions
@Namespace private var focusNamespace
2021-08-30 03:06:18 +05:30
var videos: [ContentItem] {
ContentItem.array(of: store.item?.videos ?? [])
}
2021-08-30 03:06:18 +05:30
var body: some View {
#if os(iOS)
if inNavigationView {
content
} else {
PlayerControlsView {
content
}
}
#else
PlayerControlsView {
content
}
#endif
}
var content: some View {
VStack {
#if os(tvOS)
HStack {
Text(navigationTitle)
.font(.title2)
.frame(alignment: .leading)
Spacer()
2021-11-02 03:26:18 +05:30
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)
#if !os(iOS)
.prefersDefaultFocus(in: focusNamespace)
#endif
}
2021-10-28 22:44:55 +05:30
.environment(\.inChannelView, true)
#if !os(iOS)
.focusScope(focusNamespace)
#endif
2021-08-30 03:06:18 +05:30
#if !os(tvOS)
2021-11-08 21:59:35 +05:30
.toolbar {
ToolbarItem(placement: .navigation) {
ShareButton(
contentItem: contentItem,
2021-11-13 21:15:47 +05:30
presentingShareSheet: $presentingShareSheet,
shareURL: $shareURL
2021-11-08 21:59:35 +05:30
)
}
2021-10-27 04:29:59 +05:30
2021-11-08 21:59:35 +05:30
ToolbarItem {
HStack {
Text("**\(store.item?.subscriptionsString ?? "loading")** subscribers")
.foregroundColor(.secondary)
.opacity(store.item?.subscriptionsString != nil ? 1 : 0)
2021-11-08 21:59:35 +05:30
subscriptionToggleButton
2021-11-08 21:59:35 +05:30
FavoriteButton(item: FavoriteItem(section: .channel(channel.id, channel.name)))
}
}
2021-11-08 21:59:35 +05:30
}
2021-10-23 04:34:03 +05:30
#else
2021-11-08 21:59:35 +05:30
.background(.thickMaterial)
#endif
2021-10-27 04:29:59 +05:30
#if os(iOS)
2021-11-08 21:59:35 +05:30
.sheet(isPresented: $presentingShareSheet) {
2021-11-13 21:15:47 +05:30
if let shareURL = shareURL {
ShareSheet(activityItems: [shareURL])
2021-10-27 04:29:59 +05:30
}
2021-11-08 21:59:35 +05:30
}
2021-10-27 04:29:59 +05:30
#endif
.modifier(UnsubscribeAlertModifier())
2021-11-08 21:59:35 +05:30
.onAppear {
if store.item.isNil {
resource.addObserver(store)
resource.load()
}
2021-11-08 21:59:35 +05:30
}
.navigationTitle(navigationTitle)
}
2021-10-27 04:29:59 +05:30
private var resource: Resource {
2021-10-21 03:51:50 +05:30
let resource = accounts.api.channel(channel.id)
2021-09-25 13:48:22 +05:30
resource.addObserver(store)
return resource
}
2021-10-27 04:29:59 +05:30
private var subscriptionToggleButton: some View {
Group {
2021-10-21 03:51:50 +05:30
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-30 03:06:18 +05:30
}
}
2021-10-27 04:29:59 +05:30
private var contentItem: ContentItem {
ContentItem(channel: channel)
}
private var navigationTitle: String {
store.item?.name ?? channel.name
}
2021-08-30 03:06:18 +05:30
}