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
|
|
|
@EnvironmentObject<PlayerModel> private var player
|
2022-05-21 22:29:51 +00:00
|
|
|
@EnvironmentObject<PlaylistsModel> private var model
|
2022-01-02 18:59:57 +00:00
|
|
|
|
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-05-21 22:29:51 +00:00
|
|
|
var videos = playlist.videos
|
|
|
|
|
|
|
|
if videos.isEmpty {
|
|
|
|
videos = store.item?.videos ?? []
|
|
|
|
if !player.accounts.app.userPlaylistsEndpointIncludesVideos {
|
|
|
|
var i = 0
|
|
|
|
|
|
|
|
for index in videos.indices {
|
|
|
|
var video = videos[index]
|
|
|
|
video.indexID = "\(i)"
|
|
|
|
i += 1
|
|
|
|
videos[index] = video
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ContentItem.array(of: videos)
|
2022-04-10 15:07:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-02-16 20:23:11 +00:00
|
|
|
BrowserPlayerControls {
|
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 {
|
2022-05-21 22:29:51 +00:00
|
|
|
resource?.load()
|
2022-04-10 15:07:10 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-21 22:29:51 +00:00
|
|
|
.onChange(of: model.reloadPlaylists) { _ in
|
|
|
|
resource?.load()
|
|
|
|
}
|
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 {
|
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")
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|