2022-12-12 00:18:29 +00:00
|
|
|
import Defaults
|
2021-10-22 23:04:03 +00:00
|
|
|
import Siesta
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ChannelPlaylistView: View {
|
2023-05-29 13:54:11 +00:00
|
|
|
var playlist: ChannelPlaylist
|
2022-12-11 11:38:57 +00:00
|
|
|
var showCloseButton = false
|
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-12-12 00:18:29 +00:00
|
|
|
@Default(.channelPlaylistListingStyle) private var channelPlaylistListingStyle
|
2021-10-22 23:04:03 +00:00
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
@ObservedObject private var accounts = AccountsModel.shared
|
|
|
|
var player = PlayerModel.shared
|
|
|
|
@ObservedObject private var recents = RecentsModel.shared
|
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 resource: Resource? {
|
|
|
|
let resource = accounts.api.channelPlaylist(playlist.id)
|
|
|
|
resource?.addObserver(store)
|
|
|
|
|
|
|
|
return resource
|
2021-10-22 23:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
VStack(alignment: .leading) {
|
|
|
|
#if os(tvOS)
|
2021-11-01 21:56:18 +00:00
|
|
|
HStack {
|
2023-05-29 13:54:11 +00:00
|
|
|
ThumbnailView(url: store.item?.thumbnailURL ?? playlist.thumbnailURL)
|
|
|
|
.frame(width: 140, height: 80)
|
|
|
|
.clipShape(RoundedRectangle(cornerRadius: 2))
|
2022-12-13 10:40:08 +00:00
|
|
|
|
2023-05-29 13:54:11 +00:00
|
|
|
Text(playlist.title)
|
|
|
|
.font(.headline)
|
|
|
|
.frame(alignment: .leading)
|
|
|
|
.lineLimit(1)
|
2021-11-01 21:56:18 +00:00
|
|
|
|
2023-05-29 13:54:11 +00:00
|
|
|
Spacer()
|
2021-11-01 21:56:18 +00:00
|
|
|
|
2023-05-29 13:54:11 +00:00
|
|
|
FavoriteButton(item: FavoriteItem(section: .channelPlaylist(accounts.app.appType.rawValue, playlist.id, playlist.title)))
|
|
|
|
.labelStyle(.iconOnly)
|
2022-01-02 18:59:57 +00:00
|
|
|
|
2022-12-15 22:03:04 +00:00
|
|
|
playButtons
|
2022-01-02 18:59:57 +00:00
|
|
|
.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
|
|
|
}
|
2022-12-12 00:18:29 +00:00
|
|
|
.environment(\.listingStyle, channelPlaylistListingStyle)
|
2021-10-22 23:04:03 +00:00
|
|
|
.onAppear {
|
2023-05-29 13:54:11 +00:00
|
|
|
if let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(playlist) {
|
2022-12-16 11:31:43 +00:00
|
|
|
store.replace(cache)
|
|
|
|
}
|
|
|
|
resource?.loadIfNeeded()?.onSuccess { response in
|
|
|
|
if let playlist: ChannelPlaylist = response.typedContent() {
|
|
|
|
ChannelPlaylistsCacheModel.shared.storePlaylist(playlist: playlist)
|
2022-08-08 17:30:40 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-22 23:04:03 +00:00
|
|
|
}
|
2022-01-02 18:59:57 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
.background(Color.background(scheme: colorScheme))
|
2022-12-12 00:18:29 +00:00
|
|
|
#endif
|
|
|
|
#if os(iOS)
|
|
|
|
.toolbar {
|
|
|
|
ToolbarItem(placement: .principal) {
|
|
|
|
playlistMenu
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2021-11-08 16:29:35 +00:00
|
|
|
.toolbar {
|
2022-12-03 23:50:44 +00:00
|
|
|
ToolbarItem(placement: .cancellationAction) {
|
2022-12-11 11:38:57 +00:00
|
|
|
if showCloseButton {
|
2022-12-03 23:50:44 +00:00
|
|
|
Button {
|
|
|
|
NavigationModel.shared.presentingPlaylist = false
|
|
|
|
} label: {
|
|
|
|
Label("Close", systemImage: "xmark")
|
2022-05-29 19:09:57 +00:00
|
|
|
}
|
2022-12-03 23:50:44 +00:00
|
|
|
.buttonStyle(.plain)
|
2022-05-29 19:09:57 +00:00
|
|
|
}
|
2021-11-08 16:29:35 +00:00
|
|
|
}
|
2023-05-21 12:01:33 +00:00
|
|
|
}
|
|
|
|
#if os(macOS)
|
|
|
|
.toolbar {
|
2022-01-02 18:59:57 +00:00
|
|
|
ToolbarItem(placement: playlistButtonsPlacement) {
|
|
|
|
HStack {
|
2022-12-12 00:18:29 +00:00
|
|
|
ListingStyleButtons(listingStyle: $channelPlaylistListingStyle)
|
2023-05-23 16:48:39 +00:00
|
|
|
HideWatchedButtons()
|
2023-05-23 16:54:53 +00:00
|
|
|
HideShortsButtons()
|
2022-06-26 11:57:02 +00:00
|
|
|
ShareButton(contentItem: contentItem)
|
2022-05-29 19:09:57 +00:00
|
|
|
|
2022-12-12 00:18:29 +00:00
|
|
|
favoriteButton
|
2022-01-02 18:59:57 +00:00
|
|
|
|
2022-12-15 22:03:04 +00:00
|
|
|
playButtons
|
2022-01-02 18:59:57 +00:00
|
|
|
}
|
2021-10-22 23:04:03 +00:00
|
|
|
}
|
2021-11-08 16:29:35 +00:00
|
|
|
}
|
2023-05-29 13:54:11 +00:00
|
|
|
.navigationTitle(playlist.title)
|
2022-01-02 18:59:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-12-12 00:18:29 +00:00
|
|
|
@ViewBuilder private var favoriteButton: some View {
|
2023-05-29 13:54:11 +00:00
|
|
|
FavoriteButton(item: FavoriteItem(section: .channelPlaylist(accounts.app.appType.rawValue, playlist.id, playlist.title)))
|
2022-12-12 00:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
private var playlistMenu: some View {
|
|
|
|
Menu {
|
2022-12-15 22:03:04 +00:00
|
|
|
playButtons
|
|
|
|
|
2022-12-12 00:18:29 +00:00
|
|
|
favoriteButton
|
|
|
|
|
|
|
|
ListingStyleButtons(listingStyle: $channelPlaylistListingStyle)
|
|
|
|
|
2023-02-25 15:42:18 +00:00
|
|
|
Section {
|
2023-05-23 16:48:39 +00:00
|
|
|
HideWatchedButtons()
|
2023-05-23 16:54:53 +00:00
|
|
|
HideShortsButtons()
|
2023-02-25 15:42:18 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 00:18:29 +00:00
|
|
|
Section {
|
|
|
|
SettingsButtons()
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
HStack(spacing: 12) {
|
2023-05-29 13:54:11 +00:00
|
|
|
if let url = store.item?.thumbnailURL ?? playlist.thumbnailURL {
|
2022-12-16 11:32:31 +00:00
|
|
|
ThumbnailView(url: url)
|
|
|
|
.frame(width: 60, height: 30)
|
|
|
|
.clipShape(RoundedRectangle(cornerRadius: 2))
|
|
|
|
}
|
2022-12-13 10:40:08 +00:00
|
|
|
|
2023-05-29 13:54:11 +00:00
|
|
|
Text(playlist.title)
|
2022-12-12 00:18:29 +00:00
|
|
|
.font(.headline)
|
|
|
|
.foregroundColor(.primary)
|
|
|
|
|
|
|
|
Image(systemName: "chevron.down.circle.fill")
|
|
|
|
.foregroundColor(.accentColor)
|
|
|
|
.imageScale(.small)
|
|
|
|
}
|
|
|
|
.frame(maxWidth: 320)
|
|
|
|
.transaction { t in t.animation = nil }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-01-02 18:59:57 +00:00
|
|
|
private var playlistButtonsPlacement: ToolbarItemPlacement {
|
|
|
|
#if os(iOS)
|
|
|
|
.navigationBarTrailing
|
2021-10-22 23:04:03 +00:00
|
|
|
#else
|
2023-05-07 19:45:18 +00:00
|
|
|
.automatic
|
2021-10-22 23:04:03 +00:00
|
|
|
#endif
|
|
|
|
}
|
2021-10-26 22:59:59 +00:00
|
|
|
|
2022-12-15 22:03:04 +00:00
|
|
|
private var playButtons: some View {
|
|
|
|
Group {
|
|
|
|
Button {
|
|
|
|
player.play(videos)
|
|
|
|
} label: {
|
|
|
|
Label("Play All", systemImage: "play")
|
|
|
|
}
|
2022-09-04 15:23:02 +00:00
|
|
|
Button {
|
|
|
|
player.play(videos, shuffling: true)
|
|
|
|
} label: {
|
|
|
|
Label("Shuffle All", systemImage: "shuffle")
|
|
|
|
}
|
|
|
|
}
|
2022-01-02 18:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-12-12 00:18:29 +00:00
|
|
|
NavigationView {
|
|
|
|
ChannelPlaylistView(playlist: ChannelPlaylist.fixture)
|
|
|
|
}
|
2021-10-22 23:04:03 +00:00
|
|
|
}
|
|
|
|
}
|