2021-08-29 21:36:18 +00:00
|
|
|
import Siesta
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct PlaylistVideosView: View {
|
|
|
|
let playlist: Playlist
|
|
|
|
|
2022-01-02 18:59:57 +00:00
|
|
|
@Environment(\.inNavigationView) private var inNavigationView
|
|
|
|
@EnvironmentObject<PlayerModel> private var player
|
|
|
|
|
2022-04-10 15:07:10 +00:00
|
|
|
@StateObject private var store = Store<ChannelPlaylist>()
|
|
|
|
|
2022-01-02 18:59:57 +00:00
|
|
|
var contentItems: [ContentItem] {
|
2022-04-10 15:07:10 +00:00
|
|
|
ContentItem.array(of: playlist.videos.isEmpty ? (store.item?.videos ?? []) : playlist.videos)
|
|
|
|
}
|
|
|
|
|
|
|
|
private var resource: Resource? {
|
|
|
|
let resource = player.accounts.api.playlist(playlist.id)
|
|
|
|
resource?.addObserver(store)
|
|
|
|
|
|
|
|
return resource
|
2021-10-21 23:29:10 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 18:59:57 +00:00
|
|
|
var videos: [Video] {
|
|
|
|
contentItems.compactMap(\.video)
|
|
|
|
}
|
|
|
|
|
2021-08-29 21:36:18 +00:00
|
|
|
init(_ playlist: Playlist) {
|
|
|
|
self.playlist = playlist
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
2021-10-05 20:20:09 +00:00
|
|
|
PlayerControlsView {
|
2022-01-02 18:59:57 +00:00
|
|
|
VerticalCells(items: contentItems)
|
2022-04-10 15:07:10 +00:00
|
|
|
.onAppear {
|
|
|
|
if !player.accounts.app.userPlaylistsEndpointIncludesVideos {
|
|
|
|
resource?.loadIfNeeded()
|
|
|
|
}
|
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
#if !os(tvOS)
|
|
|
|
.navigationTitle("\(playlist.title) Playlist")
|
|
|
|
#endif
|
|
|
|
}
|
2021-11-01 21:56:18 +00:00
|
|
|
.toolbar {
|
2022-01-02 18:59:57 +00:00
|
|
|
ToolbarItem(placement: playlistButtonsPlacement) {
|
|
|
|
HStack {
|
|
|
|
FavoriteButton(item: FavoriteItem(section: .channelPlaylist(playlist.id, playlist.title)))
|
|
|
|
|
|
|
|
Button {
|
|
|
|
player.play(videos, inNavigationView: inNavigationView)
|
|
|
|
} label: {
|
|
|
|
Label("Play All", systemImage: "play")
|
|
|
|
}
|
|
|
|
|
|
|
|
Button {
|
|
|
|
player.play(videos, shuffling: true, inNavigationView: inNavigationView)
|
|
|
|
} label: {
|
|
|
|
Label("Shuffle", systemImage: "shuffle")
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:56:18 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-29 21:36:18 +00:00
|
|
|
}
|
2022-01-02 18:59:57 +00:00
|
|
|
|
|
|
|
private var playlistButtonsPlacement: ToolbarItemPlacement {
|
|
|
|
#if os(iOS)
|
|
|
|
.navigationBarTrailing
|
|
|
|
#else
|
|
|
|
.automatic
|
|
|
|
#endif
|
|
|
|
}
|
2021-08-29 21:36:18 +00:00
|
|
|
}
|