2021-08-29 21:36:18 +00:00
|
|
|
import Siesta
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ChannelVideosView: View {
|
2022-05-29 18:26:56 +00:00
|
|
|
var channel: Channel?
|
2021-08-29 21:36:18 +00:00
|
|
|
|
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>()
|
|
|
|
|
2021-12-05 17:09:25 +00:00
|
|
|
@Environment(\.colorScheme) private var colorScheme
|
2022-05-29 18:26:56 +00:00
|
|
|
@Environment(\.navigationStyle) private var navigationStyle
|
2021-08-31 21:17:50 +00:00
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
|
2021-12-19 23:36:12 +00:00
|
|
|
@EnvironmentObject<PlayerModel> private var player
|
2021-08-31 21:17:50 +00:00
|
|
|
#endif
|
|
|
|
|
2021-10-22 23:04:03 +00:00
|
|
|
@EnvironmentObject<AccountsModel> private var accounts
|
|
|
|
@EnvironmentObject<NavigationModel> private var navigation
|
2022-05-29 18:26:56 +00:00
|
|
|
@EnvironmentObject<RecentsModel> private var recents
|
2021-10-22 23:04:03 +00:00
|
|
|
@EnvironmentObject<SubscriptionsModel> private var subscriptions
|
|
|
|
|
2021-08-31 21:17:50 +00:00
|
|
|
@Namespace private var focusNamespace
|
2021-08-29 21:36:18 +00:00
|
|
|
|
2022-05-29 18:26:56 +00:00
|
|
|
var presentedChannel: Channel? {
|
2022-05-29 20:12:59 +00:00
|
|
|
channel ?? recents.presentedChannel
|
2022-05-29 18:26:56 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 23:29:10 +00:00
|
|
|
var videos: [ContentItem] {
|
|
|
|
ContentItem.array(of: store.item?.videos ?? [])
|
|
|
|
}
|
|
|
|
|
2021-08-29 21:36:18 +00:00
|
|
|
var body: some View {
|
2022-05-29 18:26:56 +00:00
|
|
|
if navigationStyle == .tab {
|
|
|
|
NavigationView {
|
|
|
|
BrowserPlayerControls {
|
|
|
|
content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
BrowserPlayerControls {
|
|
|
|
content
|
|
|
|
}
|
2022-05-28 21:41:23 +00:00
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var content: some View {
|
2021-11-28 14:37:55 +00:00
|
|
|
let content = VStack {
|
2021-08-31 21:17:50 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
HStack {
|
|
|
|
Text(navigationTitle)
|
|
|
|
.font(.title2)
|
|
|
|
.frame(alignment: .leading)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
2022-05-29 18:26:56 +00:00
|
|
|
if let channel = presentedChannel {
|
|
|
|
FavoriteButton(item: FavoriteItem(section: .channel(channel.id, channel.name)))
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
}
|
2021-11-01 21:56:18 +00:00
|
|
|
|
2021-08-31 21:17:50 +00:00
|
|
|
if let subscribers = store.item?.subscriptionsString {
|
|
|
|
Text("**\(subscribers)** subscribers")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
|
|
|
|
|
|
|
subscriptionToggleButton
|
|
|
|
}
|
|
|
|
.frame(maxWidth: .infinity)
|
|
|
|
#endif
|
|
|
|
|
2021-12-19 22:27:20 +00:00
|
|
|
VerticalCells(items: videos)
|
|
|
|
.environment(\.inChannelView, true)
|
|
|
|
#if os(tvOS)
|
|
|
|
.prefersDefaultFocus(in: focusNamespace)
|
2021-08-31 21:17:50 +00:00
|
|
|
#endif
|
|
|
|
}
|
2021-11-28 14:37:55 +00:00
|
|
|
|
2021-08-29 21:36:18 +00:00
|
|
|
#if !os(tvOS)
|
2021-12-19 22:27:20 +00:00
|
|
|
.toolbar {
|
2022-08-26 07:58:08 +00:00
|
|
|
ToolbarItem(placement: .cancellationAction) {
|
2022-05-29 18:26:56 +00:00
|
|
|
if navigationStyle == .tab {
|
2022-08-26 07:58:08 +00:00
|
|
|
Button {
|
2022-08-25 17:09:55 +00:00
|
|
|
withAnimation(Constants.overlayAnimation) {
|
2022-07-09 00:21:04 +00:00
|
|
|
navigation.presentingChannel = false
|
|
|
|
}
|
2022-08-26 07:58:08 +00:00
|
|
|
} label: {
|
|
|
|
Label("Close", systemImage: "xmark")
|
2022-05-29 18:26:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-19 22:27:20 +00:00
|
|
|
}
|
2021-10-26 22:59:59 +00:00
|
|
|
|
2021-12-19 22:27:20 +00:00
|
|
|
ToolbarItem {
|
|
|
|
HStack {
|
|
|
|
HStack(spacing: 3) {
|
2022-05-29 20:13:21 +00:00
|
|
|
Text("\(store.item?.subscriptionsString ?? "")")
|
2021-12-19 22:27:20 +00:00
|
|
|
.fontWeight(.bold)
|
2022-08-21 22:37:52 +00:00
|
|
|
|
|
|
|
let subscribers = Text(" subscribers")
|
2022-05-29 18:26:56 +00:00
|
|
|
.allowsTightening(true)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
.opacity(store.item?.subscriptionsString != nil ? 1 : 0)
|
2022-08-21 22:37:52 +00:00
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
if navigationStyle == .sidebar {
|
|
|
|
subscribers
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
subscribers
|
|
|
|
#endif
|
2021-12-19 22:27:20 +00:00
|
|
|
}
|
2022-05-29 18:26:56 +00:00
|
|
|
|
2022-06-26 11:57:02 +00:00
|
|
|
ShareButton(contentItem: contentItem)
|
2021-08-31 21:17:50 +00:00
|
|
|
|
2021-12-19 22:27:20 +00:00
|
|
|
subscriptionToggleButton
|
2021-08-31 21:17:50 +00:00
|
|
|
|
2022-05-29 18:26:56 +00:00
|
|
|
if let channel = presentedChannel {
|
|
|
|
FavoriteButton(item: FavoriteItem(section: .channel(channel.id, channel.name)))
|
|
|
|
}
|
2021-08-31 21:17:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-19 22:27:20 +00:00
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
#endif
|
2021-11-08 16:29:35 +00:00
|
|
|
.onAppear {
|
2022-08-08 17:30:40 +00:00
|
|
|
if navigationStyle == .tab {
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
|
|
|
resource?.loadIfNeeded()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resource?.loadIfNeeded()
|
|
|
|
}
|
2021-11-08 16:29:35 +00:00
|
|
|
}
|
2022-05-29 18:26:56 +00:00
|
|
|
#if !os(tvOS)
|
2021-11-08 16:29:35 +00:00
|
|
|
.navigationTitle(navigationTitle)
|
2022-05-29 18:26:56 +00:00
|
|
|
#endif
|
2021-11-28 14:37:55 +00:00
|
|
|
|
|
|
|
return Group {
|
|
|
|
if #available(macOS 12.0, *) {
|
|
|
|
content
|
2021-12-05 17:09:25 +00:00
|
|
|
#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-08-31 21:17:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-29 18:26:56 +00:00
|
|
|
private var resource: Resource? {
|
|
|
|
guard let channel = presentedChannel else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-08-31 21:17:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-29 18:26:56 +00:00
|
|
|
@ViewBuilder private var subscriptionToggleButton: some View {
|
|
|
|
if let channel = presentedChannel {
|
|
|
|
Group {
|
|
|
|
if accounts.app.supportsSubscriptions && accounts.signedIn {
|
|
|
|
if subscriptions.isSubscribing(channel.id) {
|
2022-08-21 22:37:52 +00:00
|
|
|
Button {
|
2022-05-29 18:26:56 +00:00
|
|
|
subscriptionToggleButtonDisabled = true
|
|
|
|
|
|
|
|
subscriptions.unsubscribe(channel.id) {
|
|
|
|
subscriptionToggleButtonDisabled = false
|
|
|
|
}
|
2022-08-21 22:37:52 +00:00
|
|
|
} label: {
|
2022-08-23 15:10:14 +00:00
|
|
|
Label("Unsubscribe", systemImage: "star.circle")
|
2022-08-21 22:37:52 +00:00
|
|
|
#if os(iOS)
|
|
|
|
.labelStyle(.automatic)
|
|
|
|
#else
|
|
|
|
.labelStyle(.titleOnly)
|
|
|
|
#endif
|
2022-03-26 13:37:55 +00:00
|
|
|
}
|
2022-05-29 18:26:56 +00:00
|
|
|
} else {
|
2022-08-21 22:37:52 +00:00
|
|
|
Button {
|
2022-05-29 18:26:56 +00:00
|
|
|
subscriptionToggleButtonDisabled = true
|
|
|
|
|
|
|
|
subscriptions.subscribe(channel.id) {
|
|
|
|
subscriptionToggleButtonDisabled = false
|
|
|
|
navigation.sidebarSectionChanged.toggle()
|
|
|
|
}
|
2022-08-21 22:37:52 +00:00
|
|
|
} label: {
|
2022-08-23 15:10:14 +00:00
|
|
|
Label("Subscribe", systemImage: "circle")
|
2022-08-21 22:37:52 +00:00
|
|
|
#if os(iOS)
|
|
|
|
.labelStyle(.automatic)
|
|
|
|
#else
|
|
|
|
.labelStyle(.titleOnly)
|
|
|
|
#endif
|
2021-10-20 22:21:50 +00:00
|
|
|
}
|
2021-08-31 21:17:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-29 18:26:56 +00:00
|
|
|
.disabled(subscriptionToggleButtonDisabled)
|
2021-08-29 21:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-31 21:17:50 +00:00
|
|
|
|
2021-10-26 22:59:59 +00:00
|
|
|
private var contentItem: ContentItem {
|
2022-05-29 18:26:56 +00:00
|
|
|
ContentItem(channel: presentedChannel)
|
2021-10-26 22:59:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private var navigationTitle: String {
|
2022-05-29 18:26:56 +00:00
|
|
|
presentedChannel?.name ?? store.item?.name ?? "No channel"
|
2021-08-31 21:17:50 +00:00
|
|
|
}
|
2021-08-29 21:36:18 +00:00
|
|
|
}
|
2022-08-23 15:10:14 +00:00
|
|
|
|
|
|
|
struct ChannelVideosView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
ChannelVideosView(channel: Video.fixture.channel)
|
|
|
|
.injectFixtureEnvironmentObjects()
|
|
|
|
}
|
|
|
|
}
|