1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 14:50:32 +05:30
yattee/Shared/Playlists/PlaylistVideosView.swift

114 lines
3.4 KiB
Swift
Raw Permalink Normal View History

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