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