2021-08-30 03:06:18 +05:30
|
|
|
import Siesta
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ChannelVideosView: View {
|
2022-05-29 23:56:56 +05:30
|
|
|
#if os(iOS)
|
|
|
|
static let hiddenOffset = max(UIScreen.main.bounds.height, UIScreen.main.bounds.width) + 100
|
|
|
|
#endif
|
|
|
|
var channel: Channel?
|
2021-08-30 03:06:18 +05:30
|
|
|
|
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?
|
2022-03-26 19:07:55 +05:30
|
|
|
@State private var subscriptionToggleButtonDisabled = false
|
2021-10-27 04:29:59 +05:30
|
|
|
|
2022-05-29 23:56:56 +05:30
|
|
|
#if os(iOS)
|
|
|
|
@State private var viewVerticalOffset = Self.hiddenOffset
|
|
|
|
#endif
|
|
|
|
|
2021-09-25 13:48:22 +05:30
|
|
|
@StateObject private var store = Store<Channel>()
|
|
|
|
|
2021-12-05 22:39:25 +05:30
|
|
|
@Environment(\.colorScheme) private var colorScheme
|
2022-05-29 23:56:56 +05:30
|
|
|
@Environment(\.navigationStyle) private var navigationStyle
|
2021-09-01 02:47:50 +05:30
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
|
2021-12-20 05:06:12 +05:30
|
|
|
@EnvironmentObject<PlayerModel> private var player
|
2021-09-01 02:47:50 +05:30
|
|
|
#endif
|
|
|
|
|
2021-10-23 04:34:03 +05:30
|
|
|
@EnvironmentObject<AccountsModel> private var accounts
|
|
|
|
@EnvironmentObject<NavigationModel> private var navigation
|
2022-05-29 23:56:56 +05:30
|
|
|
@EnvironmentObject<RecentsModel> private var recents
|
2021-10-23 04:34:03 +05:30
|
|
|
@EnvironmentObject<SubscriptionsModel> private var subscriptions
|
|
|
|
|
2021-09-01 02:47:50 +05:30
|
|
|
@Namespace private var focusNamespace
|
2021-08-30 03:06:18 +05:30
|
|
|
|
2022-05-29 23:56:56 +05:30
|
|
|
var presentedChannel: Channel? {
|
2022-05-30 01:42:59 +05:30
|
|
|
channel ?? recents.presentedChannel
|
2022-05-29 23:56:56 +05:30
|
|
|
}
|
|
|
|
|
2021-10-22 04:59:10 +05:30
|
|
|
var videos: [ContentItem] {
|
|
|
|
ContentItem.array(of: store.item?.videos ?? [])
|
|
|
|
}
|
|
|
|
|
2021-08-30 03:06:18 +05:30
|
|
|
var body: some View {
|
2022-05-29 23:56:56 +05:30
|
|
|
if navigationStyle == .tab {
|
|
|
|
NavigationView {
|
|
|
|
BrowserPlayerControls {
|
|
|
|
content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if os(iOS)
|
|
|
|
.onChange(of: navigation.presentingChannel) { newValue in
|
|
|
|
if newValue {
|
2022-05-30 01:42:59 +05:30
|
|
|
store.clear()
|
2022-05-29 23:56:56 +05:30
|
|
|
viewVerticalOffset = 0
|
2022-05-30 01:42:59 +05:30
|
|
|
resource?.load()
|
2022-05-29 23:56:56 +05:30
|
|
|
} else {
|
|
|
|
viewVerticalOffset = Self.hiddenOffset
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.offset(y: viewVerticalOffset)
|
2022-05-30 01:42:59 +05:30
|
|
|
.opacity(viewVerticalOffset == Self.hiddenOffset ? 0 : 1)
|
2022-05-29 23:56:56 +05:30
|
|
|
.animation(.easeIn(duration: 0.2), value: viewVerticalOffset)
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
BrowserPlayerControls {
|
|
|
|
content
|
|
|
|
}
|
2022-05-29 03:11:23 +05:30
|
|
|
}
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var content: some View {
|
2021-11-28 20:07:55 +05:30
|
|
|
let content = VStack {
|
2021-09-01 02:47:50 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
HStack {
|
|
|
|
Text(navigationTitle)
|
|
|
|
.font(.title2)
|
|
|
|
.frame(alignment: .leading)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
2022-05-29 23:56:56 +05:30
|
|
|
if let channel = presentedChannel {
|
|
|
|
FavoriteButton(item: FavoriteItem(section: .channel(channel.id, channel.name)))
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
}
|
2021-11-02 03:26:18 +05:30
|
|
|
|
2021-09-01 02:47:50 +05:30
|
|
|
if let subscribers = store.item?.subscriptionsString {
|
|
|
|
Text("**\(subscribers)** subscribers")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
|
|
|
|
|
|
|
subscriptionToggleButton
|
|
|
|
}
|
|
|
|
.frame(maxWidth: .infinity)
|
|
|
|
#endif
|
|
|
|
|
2021-12-20 03:57:20 +05:30
|
|
|
VerticalCells(items: videos)
|
|
|
|
.environment(\.inChannelView, true)
|
|
|
|
#if os(tvOS)
|
|
|
|
.prefersDefaultFocus(in: focusNamespace)
|
2021-09-01 02:47:50 +05:30
|
|
|
#endif
|
|
|
|
}
|
2021-11-28 20:07:55 +05:30
|
|
|
|
2021-08-30 03:06:18 +05:30
|
|
|
#if !os(tvOS)
|
2021-12-20 03:57:20 +05:30
|
|
|
.toolbar {
|
|
|
|
ToolbarItem(placement: .navigation) {
|
2022-05-29 23:56:56 +05:30
|
|
|
if navigationStyle == .tab {
|
|
|
|
Button("Done") {
|
|
|
|
navigation.presentingChannel = false
|
|
|
|
}
|
|
|
|
}
|
2021-12-20 03:57:20 +05:30
|
|
|
}
|
2021-10-27 04:29:59 +05:30
|
|
|
|
2021-12-20 03:57:20 +05:30
|
|
|
ToolbarItem {
|
|
|
|
HStack {
|
|
|
|
HStack(spacing: 3) {
|
|
|
|
Text("\(store.item?.subscriptionsString ?? "loading")")
|
|
|
|
.fontWeight(.bold)
|
|
|
|
Text(" subscribers")
|
2022-05-29 23:56:56 +05:30
|
|
|
.allowsTightening(true)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
.opacity(store.item?.subscriptionsString != nil ? 1 : 0)
|
2021-12-20 03:57:20 +05:30
|
|
|
}
|
2022-05-29 23:56:56 +05:30
|
|
|
|
|
|
|
ShareButton(
|
|
|
|
contentItem: contentItem,
|
|
|
|
presentingShareSheet: $presentingShareSheet,
|
|
|
|
shareURL: $shareURL
|
|
|
|
)
|
2021-09-01 02:47:50 +05:30
|
|
|
|
2021-12-20 03:57:20 +05:30
|
|
|
subscriptionToggleButton
|
2021-09-01 02:47:50 +05:30
|
|
|
|
2022-05-29 23:56:56 +05:30
|
|
|
if let channel = presentedChannel {
|
|
|
|
FavoriteButton(item: FavoriteItem(section: .channel(channel.id, channel.name)))
|
|
|
|
}
|
2021-09-01 02:47:50 +05:30
|
|
|
}
|
|
|
|
}
|
2021-12-20 03:57:20 +05:30
|
|
|
}
|
2021-09-28 23:36:05 +05:30
|
|
|
#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
|
2021-11-08 21:59:35 +05:30
|
|
|
.onAppear {
|
2022-05-29 23:56:56 +05:30
|
|
|
resource?.loadIfNeeded()
|
2021-11-08 21:59:35 +05:30
|
|
|
}
|
2022-05-29 23:56:56 +05:30
|
|
|
#if !os(tvOS)
|
2021-11-08 21:59:35 +05:30
|
|
|
.navigationTitle(navigationTitle)
|
2022-05-29 23:56:56 +05:30
|
|
|
#endif
|
2021-11-28 20:07:55 +05:30
|
|
|
|
|
|
|
return Group {
|
|
|
|
if #available(macOS 12.0, *) {
|
|
|
|
content
|
2021-12-05 22:39:25 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
.background(Color.background(scheme: colorScheme))
|
|
|
|
#endif
|
2021-11-28 20:07:55 +05:30
|
|
|
#if !os(iOS)
|
|
|
|
.focusScope(focusNamespace)
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
content
|
|
|
|
}
|
|
|
|
}
|
2021-09-01 02:47:50 +05:30
|
|
|
}
|
|
|
|
|
2022-05-29 23:56:56 +05:30
|
|
|
private var resource: Resource? {
|
|
|
|
guard let channel = presentedChannel else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-09-01 02:47:50 +05:30
|
|
|
}
|
|
|
|
|
2022-05-29 23:56:56 +05:30
|
|
|
@ViewBuilder private var subscriptionToggleButton: some View {
|
|
|
|
if let channel = presentedChannel {
|
|
|
|
Group {
|
|
|
|
if accounts.app.supportsSubscriptions && accounts.signedIn {
|
|
|
|
if subscriptions.isSubscribing(channel.id) {
|
|
|
|
Button("Unsubscribe") {
|
|
|
|
subscriptionToggleButtonDisabled = true
|
|
|
|
|
|
|
|
subscriptions.unsubscribe(channel.id) {
|
|
|
|
subscriptionToggleButtonDisabled = false
|
|
|
|
}
|
2022-03-26 19:07:55 +05:30
|
|
|
}
|
2022-05-29 23:56:56 +05:30
|
|
|
} else {
|
|
|
|
Button("Subscribe") {
|
|
|
|
subscriptionToggleButtonDisabled = true
|
|
|
|
|
|
|
|
subscriptions.subscribe(channel.id) {
|
|
|
|
subscriptionToggleButtonDisabled = false
|
|
|
|
navigation.sidebarSectionChanged.toggle()
|
|
|
|
}
|
2021-10-21 03:51:50 +05:30
|
|
|
}
|
2021-09-01 02:47:50 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-29 23:56:56 +05:30
|
|
|
.disabled(subscriptionToggleButtonDisabled)
|
2021-08-30 03:06:18 +05:30
|
|
|
}
|
|
|
|
}
|
2021-09-01 02:47:50 +05:30
|
|
|
|
2021-10-27 04:29:59 +05:30
|
|
|
private var contentItem: ContentItem {
|
2022-05-29 23:56:56 +05:30
|
|
|
ContentItem(channel: presentedChannel)
|
2021-10-27 04:29:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
private var navigationTitle: String {
|
2022-05-29 23:56:56 +05:30
|
|
|
presentedChannel?.name ?? store.item?.name ?? "No channel"
|
2021-09-01 02:47:50 +05:30
|
|
|
}
|
2021-08-30 03:06:18 +05:30
|
|
|
}
|