2021-10-22 23:04:03 +00:00
|
|
|
import Siesta
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ChannelPlaylistView: View {
|
2022-05-29 19:09:57 +00:00
|
|
|
var playlist: ChannelPlaylist?
|
2021-10-22 23:04:03 +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?
|
2021-10-26 22:59:59 +00:00
|
|
|
|
2021-10-22 23:04:03 +00:00
|
|
|
@StateObject private var store = Store<ChannelPlaylist>()
|
|
|
|
|
2021-12-05 17:09:25 +00:00
|
|
|
@Environment(\.colorScheme) private var colorScheme
|
2022-05-29 19:09:57 +00:00
|
|
|
@Environment(\.navigationStyle) private var navigationStyle
|
2021-10-22 23:04:03 +00:00
|
|
|
|
|
|
|
@EnvironmentObject<AccountsModel> private var accounts
|
2022-05-29 19:09:57 +00:00
|
|
|
@EnvironmentObject<NavigationModel> private var navigation
|
2022-01-02 18:59:57 +00:00
|
|
|
@EnvironmentObject<PlayerModel> private var player
|
2022-05-29 19:09:57 +00:00
|
|
|
@EnvironmentObject<RecentsModel> private var recents
|
2021-10-22 23:04:03 +00:00
|
|
|
|
2022-05-29 19:09:57 +00:00
|
|
|
private var items: [ContentItem] {
|
2021-10-22 23:04:03 +00:00
|
|
|
ContentItem.array(of: store.item?.videos ?? [])
|
|
|
|
}
|
|
|
|
|
2022-05-29 19:09:57 +00:00
|
|
|
private var presentedPlaylist: ChannelPlaylist? {
|
2022-05-29 20:12:59 +00:00
|
|
|
playlist ?? recents.presentedPlaylist
|
2022-05-29 19:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private var resource: Resource? {
|
|
|
|
guard let playlist = presentedPlaylist else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let resource = accounts.api.channelPlaylist(playlist.id)
|
|
|
|
resource?.addObserver(store)
|
|
|
|
|
|
|
|
return resource
|
2021-10-22 23:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
2022-05-29 19:09:57 +00:00
|
|
|
if navigationStyle == .tab {
|
|
|
|
NavigationView {
|
|
|
|
BrowserPlayerControls {
|
|
|
|
content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
BrowserPlayerControls {
|
|
|
|
content
|
|
|
|
}
|
2022-05-28 21:41:23 +00:00
|
|
|
}
|
2021-10-22 23:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var content: some View {
|
|
|
|
VStack(alignment: .leading) {
|
|
|
|
#if os(tvOS)
|
2021-11-01 21:56:18 +00:00
|
|
|
HStack {
|
2022-05-29 20:30:00 +00:00
|
|
|
if let playlist = presentedPlaylist {
|
|
|
|
Text(playlist.title)
|
|
|
|
.font(.title2)
|
|
|
|
.frame(alignment: .leading)
|
2021-11-01 21:56:18 +00:00
|
|
|
|
2022-05-29 20:30:00 +00:00
|
|
|
Spacer()
|
2021-11-01 21:56:18 +00:00
|
|
|
|
2022-05-29 20:30:00 +00:00
|
|
|
FavoriteButton(item: FavoriteItem(section: .channelPlaylist(playlist.id, playlist.title)))
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
}
|
2022-01-02 18:59:57 +00:00
|
|
|
|
|
|
|
playButton
|
|
|
|
.labelStyle(.iconOnly)
|
2021-11-01 21:56:18 +00:00
|
|
|
}
|
2021-10-22 23:04:03 +00:00
|
|
|
#endif
|
|
|
|
VerticalCells(items: items)
|
2021-12-19 22:27:20 +00:00
|
|
|
.environment(\.inChannelPlaylistView, true)
|
2021-10-22 23:04:03 +00:00
|
|
|
}
|
|
|
|
.onAppear {
|
2022-08-08 17:30:40 +00:00
|
|
|
if navigationStyle == .tab {
|
2022-08-29 15:56:58 +00:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
2022-08-08 17:30:40 +00:00
|
|
|
resource?.loadIfNeeded()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resource?.loadIfNeeded()
|
|
|
|
}
|
2021-10-22 23:04:03 +00:00
|
|
|
}
|
2022-01-02 18:59:57 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
.background(Color.background(scheme: colorScheme))
|
|
|
|
#else
|
2021-11-08 16:29:35 +00:00
|
|
|
.toolbar {
|
|
|
|
ToolbarItem(placement: .navigation) {
|
2022-05-29 19:09:57 +00:00
|
|
|
if navigationStyle == .tab {
|
|
|
|
Button("Done") {
|
2022-08-25 17:09:55 +00:00
|
|
|
withAnimation(Constants.overlayAnimation) {
|
2022-07-09 00:21:04 +00:00
|
|
|
navigation.presentingPlaylist = false
|
|
|
|
}
|
2022-05-29 19:09:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-08 16:29:35 +00:00
|
|
|
}
|
2021-10-26 22:59:59 +00:00
|
|
|
|
2022-01-02 18:59:57 +00:00
|
|
|
ToolbarItem(placement: playlistButtonsPlacement) {
|
|
|
|
HStack {
|
2022-06-26 11:57:02 +00:00
|
|
|
ShareButton(contentItem: contentItem)
|
2022-05-29 19:09:57 +00:00
|
|
|
|
|
|
|
if let playlist = presentedPlaylist {
|
|
|
|
FavoriteButton(item: FavoriteItem(section: .channelPlaylist(playlist.id, playlist.title)))
|
|
|
|
}
|
2022-01-02 18:59:57 +00:00
|
|
|
|
|
|
|
playButton
|
|
|
|
}
|
2021-10-22 23:04:03 +00:00
|
|
|
}
|
2021-11-08 16:29:35 +00:00
|
|
|
}
|
2022-05-29 19:09:57 +00:00
|
|
|
.navigationTitle(presentedPlaylist?.title ?? "")
|
2022-01-02 18:59:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private var playlistButtonsPlacement: ToolbarItemPlacement {
|
|
|
|
#if os(iOS)
|
|
|
|
.navigationBarTrailing
|
2021-10-22 23:04:03 +00:00
|
|
|
#else
|
2022-01-02 18:59:57 +00:00
|
|
|
.automatic
|
2021-10-22 23:04:03 +00:00
|
|
|
#endif
|
|
|
|
}
|
2021-10-26 22:59:59 +00:00
|
|
|
|
2022-01-02 18:59:57 +00:00
|
|
|
private var playButton: some View {
|
|
|
|
Button {
|
2022-07-10 22:24:56 +00:00
|
|
|
player.playbackMode = .queue
|
2022-05-28 21:41:23 +00:00
|
|
|
player.play(videos)
|
2022-01-02 18:59:57 +00:00
|
|
|
} label: {
|
|
|
|
Label("Play All", systemImage: "play")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var videos: [Video] {
|
|
|
|
items.compactMap(\.video)
|
|
|
|
}
|
|
|
|
|
2021-10-26 22:59:59 +00:00
|
|
|
private var contentItem: ContentItem {
|
|
|
|
ContentItem(playlist: playlist)
|
|
|
|
}
|
2021-10-22 23:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ChannelPlaylistView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
ChannelPlaylistView(playlist: ChannelPlaylist.fixture)
|
|
|
|
.injectFixtureEnvironmentObjects()
|
|
|
|
}
|
|
|
|
}
|