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

130 lines
3.6 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-09-25 13:48:22 +05:30
@StateObject private var store = Store<Channel>()
2021-10-17 04:18:58 +05:30
@EnvironmentObject<AccountsModel> private var accounts
2021-09-25 13:48:22 +05:30
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<SubscriptionsModel> private var subscriptions
@Environment(\.inNavigationView) private var inNavigationView
@Environment(\.dismiss) private var dismiss
#if os(iOS)
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
#endif
@Namespace private var focusNamespace
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()
if let subscribers = store.item?.subscriptionsString {
Text("**\(subscribers)** subscribers")
.foregroundColor(.secondary)
}
subscriptionToggleButton
}
.frame(maxWidth: .infinity)
#endif
2021-09-27 03:58:42 +05:30
VideosCellsVertical(videos: store.item?.videos ?? [])
#if !os(iOS)
.prefersDefaultFocus(in: focusNamespace)
#endif
}
#if !os(iOS)
.focusScope(focusNamespace)
#endif
2021-08-30 03:06:18 +05:30
#if !os(tvOS)
.toolbar {
2021-09-25 13:48:22 +05:30
ToolbarItem {
HStack {
2021-09-25 13:48:22 +05:30
Text("**\(store.item?.subscriptionsString ?? "loading")** subscribers")
.foregroundColor(.secondary)
.opacity(store.item?.subscriptionsString != nil ? 1 : 0)
subscriptionToggleButton
}
}
ToolbarItem(placement: .cancellationAction) {
if inNavigationView {
Button("Done") {
dismiss()
}
}
}
}
2021-08-30 03:06:18 +05:30
#endif
#if os(tvOS)
.background(.thickMaterial)
#endif
.modifier(UnsubscribeAlertModifier())
.onAppear {
2021-09-25 13:48:22 +05:30
if store.item.isNil {
resource.addObserver(store)
resource.load()
}
}
.navigationTitle(navigationTitle)
}
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
}
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
}
}
var navigationTitle: String {
store.item?.name ?? channel.name
}
2021-08-30 03:06:18 +05:30
}