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

223 lines
7.0 KiB
Swift
Raw Normal View History

2021-08-30 03:06:18 +05:30
import Siesta
import SwiftUI
struct ChannelVideosView: View {
2022-05-29 23:56:56 +05:30
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
2021-09-25 13:48:22 +05:30
@StateObject private var store = Store<Channel>()
@Environment(\.colorScheme) private var colorScheme
2022-05-29 23:56:56 +05:30
@Environment(\.navigationStyle) private var navigationStyle
#if os(iOS)
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
@EnvironmentObject<PlayerModel> private var player
#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
@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
}
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
}
}
} else {
BrowserPlayerControls {
content
}
}
}
var content: some View {
2021-11-28 20:07:55 +05:30
let content = VStack {
#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
if let subscribers = store.item?.subscriptionsString {
Text("**\(subscribers)** subscribers")
.foregroundColor(.secondary)
}
subscriptionToggleButton
}
.frame(maxWidth: .infinity)
#endif
VerticalCells(items: videos)
.environment(\.inChannelView, true)
#if os(tvOS)
.prefersDefaultFocus(in: focusNamespace)
#endif
}
2021-11-28 20:07:55 +05:30
2021-08-30 03:06:18 +05:30
#if !os(tvOS)
.toolbar {
2022-08-26 13:28:08 +05:30
ToolbarItem(placement: .cancellationAction) {
2022-05-29 23:56:56 +05:30
if navigationStyle == .tab {
2022-08-26 13:28:08 +05:30
Button {
2022-08-25 22:39:55 +05:30
withAnimation(Constants.overlayAnimation) {
2022-07-09 05:51:04 +05:30
navigation.presentingChannel = false
}
2022-08-26 13:28:08 +05:30
} label: {
Label("Close", systemImage: "xmark")
2022-05-29 23:56:56 +05:30
}
}
}
2021-10-27 04:29:59 +05:30
ToolbarItem {
HStack {
HStack(spacing: 3) {
2022-05-30 01:43:21 +05:30
Text("\(store.item?.subscriptionsString ?? "")")
.fontWeight(.bold)
2022-08-22 04:07:52 +05:30
let subscribers = Text(" subscribers")
2022-05-29 23:56:56 +05:30
.allowsTightening(true)
.foregroundColor(.secondary)
.opacity(store.item?.subscriptionsString != nil ? 1 : 0)
2022-08-22 04:07:52 +05:30
#if os(iOS)
if navigationStyle == .sidebar {
subscribers
}
#else
subscribers
#endif
}
2022-05-29 23:56:56 +05:30
ShareButton(contentItem: contentItem)
subscriptionToggleButton
2022-05-29 23:56:56 +05:30
if let channel = presentedChannel {
FavoriteButton(item: FavoriteItem(section: .channel(channel.id, channel.name)))
}
}
}
}
#endif
2021-11-08 21:59:35 +05:30
.onAppear {
if navigationStyle == .tab {
2022-08-29 21:26:58 +05:30
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
resource?.loadIfNeeded()
}
} else {
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
#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
}
}
}
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
}
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) {
2022-08-22 04:07:52 +05:30
Button {
2022-05-29 23:56:56 +05:30
subscriptionToggleButtonDisabled = true
subscriptions.unsubscribe(channel.id) {
subscriptionToggleButtonDisabled = false
}
2022-08-22 04:07:52 +05:30
} label: {
2022-08-23 20:40:14 +05:30
Label("Unsubscribe", systemImage: "star.circle")
2022-08-22 04:07:52 +05:30
#if os(iOS)
.labelStyle(.automatic)
#else
.labelStyle(.titleOnly)
#endif
2022-03-26 19:07:55 +05:30
}
2022-05-29 23:56:56 +05:30
} else {
2022-08-22 04:07:52 +05:30
Button {
2022-05-29 23:56:56 +05:30
subscriptionToggleButtonDisabled = true
subscriptions.subscribe(channel.id) {
subscriptionToggleButtonDisabled = false
navigation.sidebarSectionChanged.toggle()
}
2022-08-22 04:07:52 +05:30
} label: {
2022-08-23 20:40:14 +05:30
Label("Subscribe", systemImage: "circle")
2022-08-22 04:07:52 +05:30
#if os(iOS)
.labelStyle(.automatic)
#else
.labelStyle(.titleOnly)
#endif
2021-10-21 03:51:50 +05:30
}
}
}
}
2022-05-29 23:56:56 +05:30
.disabled(subscriptionToggleButtonDisabled)
2021-08-30 03:06:18 +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-08-30 03:06:18 +05:30
}
2022-08-23 20:40:14 +05:30
struct ChannelVideosView_Previews: PreviewProvider {
static var previews: some View {
ChannelVideosView(channel: Video.fixture.channel)
.injectFixtureEnvironmentObjects()
}
}