2021-08-29 21:36:18 +00:00
|
|
|
import Siesta
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ChannelVideosView: View {
|
|
|
|
let channel: Channel
|
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
@StateObject private var store = Store<Channel>()
|
|
|
|
|
|
|
|
@EnvironmentObject<InvidiousAPI> private var api
|
|
|
|
@EnvironmentObject<NavigationModel> private var navigation
|
|
|
|
@EnvironmentObject<SubscriptionsModel> private var subscriptions
|
2021-08-31 21:17:50 +00:00
|
|
|
|
|
|
|
@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-29 21:36:18 +00:00
|
|
|
|
|
|
|
var body: some View {
|
2021-08-31 21:17:50 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
VideosView(videos: store.item?.videos ?? [])
|
|
|
|
|
|
|
|
#if !os(iOS)
|
|
|
|
.prefersDefaultFocus(in: focusNamespace)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#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-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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ToolbarItem(placement: .cancellationAction) {
|
|
|
|
if inNavigationView {
|
|
|
|
Button("Done") {
|
|
|
|
dismiss()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-29 21:36:18 +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)
|
|
|
|
}
|
|
|
|
|
|
|
|
var resource: Resource {
|
2021-09-25 08:18:22 +00:00
|
|
|
let resource = api.channel(channel.id)
|
|
|
|
resource.addObserver(store)
|
|
|
|
|
|
|
|
return resource
|
2021-08-31 21:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if !os(tvOS)
|
|
|
|
var subscriptionToolbarItemPlacement: ToolbarItemPlacement {
|
|
|
|
#if os(iOS)
|
|
|
|
if horizontalSizeClass == .regular {
|
2021-09-13 20:41:16 +00:00
|
|
|
return .primaryAction // swiftlint:disable:this implicit_return
|
2021-08-31 21:17:50 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
return .automatic
|
2021-08-31 21:17:50 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
var subscriptionToggleButton: some View {
|
|
|
|
Group {
|
|
|
|
if subscriptions.isSubscribing(channel.id) {
|
|
|
|
Button("Unsubscribe") {
|
2021-09-25 08:18:22 +00:00
|
|
|
navigation.presentUnsubscribeAlert(channel)
|
2021-08-31 21:17:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Button("Subscribe") {
|
|
|
|
subscriptions.subscribe(channel.id) {
|
2021-09-25 08:18:22 +00:00
|
|
|
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
|
|
|
|
|
|
|
var navigationTitle: String {
|
|
|
|
store.item?.name ?? channel.name
|
|
|
|
}
|
2021-08-29 21:36:18 +00:00
|
|
|
}
|