yattee/Shared/Views/ChannelVideosView.swift

379 lines
12 KiB
Swift
Raw Normal View History

2022-11-27 10:42:16 +00:00
import SDWebImageSwiftUI
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
2022-11-27 10:42:16 +00:00
@State private var contentType = Channel.ContentType.videos
@StateObject private var contentTypeItems = Store<[ContentItem]>()
2021-09-25 08:18:22 +00:00
@StateObject private var store = Store<Channel>()
@Environment(\.colorScheme) private var colorScheme
2022-05-29 18:26:56 +00:00
@Environment(\.navigationStyle) private var navigationStyle
#if os(iOS)
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
#endif
@ObservedObject private var accounts = AccountsModel.shared
@ObservedObject private var navigation = NavigationModel.shared
@ObservedObject private var recents = RecentsModel.shared
@ObservedObject private var subscriptions = SubscriptionsModel.shared
@Namespace private var focusNamespace
2021-08-29 21:36:18 +00:00
2022-05-29 18:26:56 +00:00
var presentedChannel: Channel? {
2022-11-27 10:42:16 +00:00
store.item ?? channel ?? recents.presentedChannel
2022-05-29 18:26:56 +00:00
}
2022-11-27 10:42:16 +00:00
var contentItems: [ContentItem] {
guard contentType != .videos else {
return ContentItem.array(of: presentedChannel?.videos ?? [])
}
return contentTypeItems.collection
}
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
}
}
}
var content: some View {
2021-11-28 14:37:55 +00:00
let content = VStack {
#if os(tvOS)
2022-11-27 10:42:16 +00:00
VStack {
HStack(spacing: 24) {
thumbnail
2022-11-27 10:42:16 +00:00
Text(navigationTitle)
.font(.title2)
.frame(alignment: .leading)
2022-11-27 10:42:16 +00:00
Spacer()
2021-11-01 21:56:18 +00:00
2022-11-27 10:42:16 +00:00
subscriptionsLabel
viewsLabel
2022-11-27 10:42:16 +00:00
subscriptionToggleButton
if let channel = presentedChannel {
FavoriteButton(item: FavoriteItem(section: .channel(channel.id, channel.name)))
.labelStyle(.iconOnly)
}
}
contentTypePicker
.pickerStyle(.automatic)
}
.frame(maxWidth: .infinity)
#endif
2022-11-27 10:42:16 +00:00
VerticalCells(items: contentItems) {
banner
}
.environment(\.inChannelView, true)
#if os(tvOS)
.prefersDefaultFocus(in: focusNamespace)
#endif
}
2021-11-28 14:37:55 +00:00
2021-08-29 21:36:18 +00:00
#if !os(tvOS)
.toolbar {
2022-11-27 10:42:16 +00:00
#if os(iOS)
ToolbarItem(placement: .principal) {
channelMenu
}
#endif
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
}
2022-11-27 10:42:16 +00:00
.buttonStyle(.plain)
2022-05-29 18:26:56 +00:00
}
}
2022-11-27 10:42:16 +00:00
#if !os(iOS)
ToolbarItem(placement: .navigation) {
thumbnail
}
ToolbarItem {
contentTypePicker
}
2021-10-26 22:59:59 +00:00
2022-11-27 10:42:16 +00:00
ToolbarItem {
HStack(spacing: 3) {
2022-11-27 10:42:16 +00:00
subscriptionsLabel
viewsLabel
}
2022-11-27 10:42:16 +00:00
}
2022-05-29 18:26:56 +00:00
2022-11-27 10:42:16 +00:00
ToolbarItem {
if let contentItem = presentedChannel?.contentItem {
ShareButton(contentItem: contentItem)
}
}
2022-11-27 10:42:16 +00:00
ToolbarItem {
subscriptionToggleButton
2022-11-27 10:42:16 +00:00
.layoutPriority(2)
}
2022-11-27 10:42:16 +00:00
ToolbarItem {
if let presentedChannel {
FavoriteButton(item: FavoriteItem(section: .channel(presentedChannel.id, presentedChannel.name)))
2022-05-29 18:26:56 +00:00
}
}
2022-11-27 10:42:16 +00:00
#endif
}
#endif
2021-11-08 16:29:35 +00:00
.onAppear {
if navigationStyle == .tab {
2022-08-29 15:56:58 +00:00
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
resource?.loadIfNeeded()
}
} else {
resource?.loadIfNeeded()
}
2021-11-08 16:29:35 +00:00
}
2022-11-27 10:42:16 +00:00
.onChange(of: contentType) { _ in
resource?.load()
}
#if os(iOS)
.navigationBarTitleDisplayMode(.inline)
#endif
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
#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
}
}
}
2022-11-27 10:42:16 +00:00
var thumbnail: some View {
Group {
if let thumbnail = store.item?.thumbnailURL {
WebImage(url: thumbnail)
.resizable()
} else {
ZStack {
Color(white: 0.6)
.opacity(0.5)
Image(systemName: "play.rectangle")
.foregroundColor(.accentColor)
.imageScale(.small)
.contentShape(Rectangle())
}
}
2022-05-29 18:26:56 +00:00
}
2022-11-27 10:42:16 +00:00
#if os(tvOS)
.frame(width: 80, height: 80, alignment: .trailing)
#else
.frame(width: 30, height: 30, alignment: .trailing)
#endif
.clipShape(Circle())
}
2022-05-29 18:26:56 +00:00
2022-11-27 10:42:16 +00:00
@ViewBuilder var banner: some View {
if let banner = presentedChannel?.bannerURL {
WebImage(url: banner)
.resizable()
.placeholder { Color.clear.frame(height: 0) }
.scaledToFit()
.clipShape(RoundedRectangle(cornerRadius: 3))
}
}
var subscriptionsLabel: some View {
Group {
if let subscribers = store.item?.subscriptionsString {
HStack(spacing: 0) {
Text(subscribers)
Image(systemName: "person.2.fill")
}
} else if store.item.isNil {
HStack(spacing: 0) {
Text("1234")
.redacted(reason: .placeholder)
Image(systemName: "person.2.fill")
}
2022-11-27 10:42:16 +00:00
}
}
.imageScale(.small)
2022-11-27 10:42:16 +00:00
.foregroundColor(.secondary)
}
var viewsLabel: some View {
HStack(spacing: 0) {
if let views = store.item?.totalViewsString {
2022-11-27 10:42:16 +00:00
Text(views)
Image(systemName: "eye.fill")
.imageScale(.small)
}
}
.foregroundColor(.secondary)
}
#if !os(tvOS)
var channelMenu: some View {
Menu {
if let channel = presentedChannel {
contentTypePicker
Section {
subscriptionToggleButton
FavoriteButton(item: FavoriteItem(section: .channel(channel.id, channel.name)))
}
}
} label: {
HStack(spacing: 12) {
thumbnail
VStack(alignment: .leading) {
Text(presentedChannel?.name ?? "Channel")
.font(.headline)
.foregroundColor(.primary)
.layoutPriority(1)
.frame(minWidth: 160, alignment: .leading)
2022-11-27 10:42:16 +00:00
Group {
HStack(spacing: 12) {
subscriptionsLabel
if presentedChannel?.verified ?? false {
Text("Verified")
}
viewsLabel
}
.frame(minWidth: 160, alignment: .leading)
2022-11-27 10:42:16 +00:00
}
.font(.caption2.bold())
.foregroundColor(.secondary)
}
Image(systemName: "chevron.down.circle.fill")
.foregroundColor(.accentColor)
.imageScale(.small)
}
.frame(maxWidth: 320)
2022-11-27 10:42:16 +00:00
}
}
#endif
private var contentTypePicker: some View {
Picker("Content type", selection: $contentType) {
if let channel = presentedChannel {
2022-12-04 12:01:05 +00:00
ForEach(Channel.ContentType.allCases, id: \.self) { type in
if channel.hasData(for: type) {
Label(type.description, systemImage: type.systemImage).tag(type)
}
2022-11-27 10:42:16 +00:00
}
}
}
}
private var resource: Resource? {
guard let channel = presentedChannel else { return nil }
let data = contentType != .videos ? channel.tabs.first(where: { $0.contentType == contentType })?.data : nil
let resource = accounts.api.channel(channel.id, contentType: contentType, data: data)
if contentType == .videos {
resource.addObserver(store)
} else {
resource.addObserver(contentTypeItems)
}
2021-09-25 08:18:22 +00:00
return resource
}
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
}
}
}
}
2022-05-29 18:26:56 +00:00
.disabled(subscriptionToggleButtonDisabled)
2021-08-29 21:36:18 +00:00
}
}
2021-10-26 22:59:59 +00:00
private var navigationTitle: String {
2022-11-27 10:42:16 +00:00
presentedChannel?.name ?? "No channel"
}
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)
2022-11-27 10:42:16 +00:00
.environment(\.navigationStyle, .tab)
2022-08-23 15:10:14 +00:00
.injectFixtureEnvironmentObjects()
2022-11-27 10:42:16 +00:00
NavigationView {
Spacer()
ChannelVideosView(channel: Video.fixture.channel)
.environment(\.navigationStyle, .sidebar)
}
2022-08-23 15:10:14 +00:00
}
}