2021-08-29 21:36:18 +00:00
|
|
|
import Siesta
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct PlaylistVideosView: View {
|
2022-12-11 17:44:55 +00:00
|
|
|
var playlist: Playlist
|
2021-08-29 21:36:18 +00:00
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
@ObservedObject private var accounts = AccountsModel.shared
|
|
|
|
var player = PlayerModel.shared
|
|
|
|
@ObservedObject private var model = PlaylistsModel.shared
|
2022-01-02 18:59:57 +00:00
|
|
|
|
2022-08-21 14:05:26 +00:00
|
|
|
@StateObject private var channelPlaylist = Store<ChannelPlaylist>()
|
|
|
|
@StateObject private var userPlaylist = Store<Playlist>()
|
2022-04-10 15:07:10 +00:00
|
|
|
|
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 {
|
2022-08-21 14:05:26 +00:00
|
|
|
videos = userPlaylist.item?.videos ?? channelPlaylist.item?.videos ?? []
|
2022-11-24 20:36:05 +00:00
|
|
|
if !accounts.app.userPlaylistsEndpointIncludesVideos {
|
2022-05-21 22:29:51 +00:00
|
|
|
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? {
|
2022-11-24 20:36:05 +00:00
|
|
|
let resource = accounts.api.playlist(playlist.id)
|
2022-08-21 14:05:26 +00:00
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
if accounts.app.userPlaylistsUseChannelPlaylistEndpoint {
|
2022-08-21 14:05:26 +00:00
|
|
|
resource?.addObserver(channelPlaylist)
|
|
|
|
} else {
|
|
|
|
resource?.addObserver(userPlaylist)
|
|
|
|
}
|
2022-04-10 15:07:10 +00:00
|
|
|
|
|
|
|
return resource
|
2021-10-21 23:29:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-11 17:44:55 +00:00
|
|
|
func loadResource() {
|
|
|
|
loadCachedResource()
|
|
|
|
resource?.load()
|
|
|
|
.onSuccess { response in
|
|
|
|
if let playlist: Playlist = response.typedContent() {
|
|
|
|
ChannelPlaylistsCacheModel.shared.storePlaylist(playlist: playlist.channelPlaylist)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadCachedResource() {
|
2022-12-16 19:37:05 +00:00
|
|
|
if let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(playlist.channelPlaylist) {
|
2022-12-11 17:44:55 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.channelPlaylist.replace(cache)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-12-10 21:37:14 +00:00
|
|
|
VerticalCells(items: contentItems)
|
|
|
|
.onAppear {
|
|
|
|
guard contentItems.isEmpty else { return }
|
2022-12-11 17:44:55 +00:00
|
|
|
loadResource()
|
2022-12-10 21:37:14 +00:00
|
|
|
}
|
|
|
|
.onChange(of: model.reloadPlaylists) { _ in
|
2022-12-11 17:44:55 +00:00
|
|
|
loadResource()
|
2022-12-10 21:37:14 +00:00
|
|
|
}
|
|
|
|
#if !os(tvOS)
|
|
|
|
.navigationTitle("\(playlist.title) Playlist")
|
|
|
|
#endif
|
|
|
|
.toolbar {
|
|
|
|
ToolbarItem(placement: playlistButtonsPlacement) {
|
|
|
|
HStack {
|
2022-12-11 15:00:20 +00:00
|
|
|
FavoriteButton(item: FavoriteItem(section: .channelPlaylist(accounts.app.appType.rawValue, playlist.id, playlist.title)))
|
2022-01-02 18:59:57 +00:00
|
|
|
|
2022-09-04 15:23:02 +00:00
|
|
|
Button {
|
2022-12-10 21:37:14 +00:00
|
|
|
player.play(videos)
|
2022-09-04 15:23:02 +00:00
|
|
|
} label: {
|
2022-12-10 21:37:14 +00:00
|
|
|
Label("Play All", systemImage: "play")
|
|
|
|
}
|
|
|
|
.contextMenu {
|
|
|
|
Button {
|
|
|
|
player.play(videos, shuffling: true)
|
|
|
|
} label: {
|
|
|
|
Label("Shuffle All", systemImage: "shuffle")
|
|
|
|
}
|
2022-09-04 15:23:02 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-02 18:59:57 +00:00
|
|
|
}
|
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
|
2023-04-22 08:56:18 +00:00
|
|
|
.automatic
|
2022-01-02 18:59:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
2021-08-29 21:36:18 +00:00
|
|
|
}
|