yattee/Shared/Playlists/PlaylistsView.swift

431 lines
13 KiB
Swift
Raw Normal View History

2021-07-08 17:18:36 +00:00
import Defaults
2021-06-28 10:43:07 +00:00
import Siesta
2021-06-26 09:39:35 +00:00
import SwiftUI
struct PlaylistsView: View {
2021-10-24 21:36:24 +00:00
@State private var selectedPlaylistID: Playlist.ID = ""
2021-07-08 15:14:54 +00:00
@State private var showingNewPlaylist = false
@State private var createdPlaylist: Playlist?
2021-07-08 17:18:36 +00:00
@State private var showingEditPlaylist = false
@State private var editedPlaylist: Playlist?
@StateObject private var channelPlaylist = Store<ChannelPlaylist>()
@StateObject private var userPlaylist = Store<Playlist>()
2022-05-22 15:53:12 +00:00
@ObservedObject private var accounts = AccountsModel.shared
@ObservedObject private var model = PlaylistsModel.shared
2022-12-16 08:35:10 +00:00
private var player = PlayerModel.shared
2022-12-11 17:04:39 +00:00
private var cache = PlaylistsCacheModel.shared
2021-09-29 12:36:52 +00:00
@Namespace private var focusNamespace
2022-12-12 00:18:29 +00:00
@Default(.playlistListingStyle) private var playlistListingStyle
2022-12-13 11:09:20 +00:00
@Default(.showCacheStatus) private var showCacheStatus
2022-12-12 00:18:29 +00:00
var items: [ContentItem] {
2022-05-22 15:53:12 +00:00
var videos = currentPlaylist?.videos ?? []
if videos.isEmpty {
videos = userPlaylist.item?.videos ?? channelPlaylist.item?.videos ?? []
if !accounts.app.userPlaylistsEndpointIncludesVideos {
2022-05-22 15:53:12 +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)
}
private var resource: Resource? {
guard let playlist = currentPlaylist else { return nil }
2022-05-22 15:53:12 +00:00
let resource = accounts.api.playlist(playlist.id)
if accounts.app.userPlaylistsUseChannelPlaylistEndpoint {
resource?.addObserver(channelPlaylist)
} else {
resource?.addObserver(userPlaylist)
}
2022-05-22 15:53:12 +00:00
return resource
}
var body: some View {
2022-12-10 21:37:14 +00:00
SignInRequiredView(title: "Playlists".localized()) {
VStack {
VerticalCells(items: items, allowEmpty: true) { if shouldDisplayHeader { header } }
.environment(\.currentPlaylistID, currentPlaylist?.id)
.environment(\.listingStyle, playlistListingStyle)
if currentPlaylist != nil, items.isEmpty {
hintText("Playlist is empty\n\nTap and hold on a video and then \n\"Add to Playlist\"".localized())
} else if model.all.isEmpty {
hintText("You have no playlists\n\nTap on \"New Playlist\" to create one".localized())
2021-09-25 08:18:22 +00:00
}
2021-06-26 09:39:35 +00:00
}
}
2022-02-04 17:38:29 +00:00
.onAppear {
model.load()
2022-12-11 17:44:55 +00:00
loadResource()
2022-02-04 17:38:29 +00:00
}
.onChange(of: accounts.current) { _ in
model.load(force: true)
2022-12-11 17:44:55 +00:00
loadResource()
2022-02-04 17:38:29 +00:00
}
.onChange(of: currentPlaylist) { _ in
2022-09-12 15:23:00 +00:00
channelPlaylist.clear()
userPlaylist.clear()
2022-12-11 17:44:55 +00:00
loadResource()
2022-02-04 17:38:29 +00:00
}
.onChange(of: model.reloadPlaylists) { _ in
2022-12-11 17:44:55 +00:00
loadResource()
2022-02-04 17:38:29 +00:00
}
#if os(iOS)
.refreshControl { refreshControl in
model.load(force: true) {
model.reloadPlaylists.toggle()
refreshControl.endRefreshing()
}
}
.backport
.refreshable {
DispatchQueue.main.async {
model.load(force: true) { model.reloadPlaylists.toggle() }
}
}
2022-12-10 20:47:38 +00:00
.navigationBarTitleDisplayMode(.inline)
.toolbar {
2022-12-16 08:35:10 +00:00
ToolbarItem {
RequestErrorButton(error: model.error)
}
2022-12-10 20:47:38 +00:00
ToolbarItem(placement: .principal) {
playlistsMenu
}
}
#endif
#if os(tvOS)
2021-11-08 16:29:35 +00:00
.fullScreenCover(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
PlaylistFormView(playlist: $createdPlaylist)
}
.fullScreenCover(isPresented: $showingEditPlaylist, onDismiss: selectEditedPlaylist) {
PlaylistFormView(playlist: $editedPlaylist)
}
2022-02-04 17:38:29 +00:00
.focusScope(focusNamespace)
#else
2022-02-04 17:38:29 +00:00
.background(
EmptyView()
.sheet(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
PlaylistFormView(playlist: $createdPlaylist)
2021-11-08 16:29:35 +00:00
}
2022-02-04 17:38:29 +00:00
)
.background(
EmptyView()
.sheet(isPresented: $showingEditPlaylist, onDismiss: selectEditedPlaylist) {
PlaylistFormView(playlist: $editedPlaylist)
2022-05-22 15:53:12 +00:00
}
2022-02-04 17:38:29 +00:00
)
#endif
2022-12-12 00:18:29 +00:00
#if os(macOS)
.toolbar {
ToolbarItem {
ListingStyleButtons(listingStyle: $playlistListingStyle)
}
ToolbarItem {
HideWatchedButtons()
}
2023-02-25 15:42:18 +00:00
ToolbarItem {
2023-05-23 16:54:53 +00:00
HideShortsButtons()
2023-02-25 15:42:18 +00:00
}
2022-08-28 18:00:43 +00:00
}
2022-12-12 00:18:29 +00:00
#else
2023-05-07 19:45:18 +00:00
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
2022-12-12 00:18:29 +00:00
model.load()
loadResource()
}
2022-08-28 18:00:43 +00:00
#endif
#if !os(tvOS)
.background(
Button("Refresh") {
resource?.load()
}
.keyboardShortcut("r")
.opacity(0)
)
#endif
}
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() {
if !selectedPlaylistID.isEmpty,
2022-12-16 19:37:05 +00:00
let currentPlaylist,
let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(currentPlaylist.channelPlaylist)
2022-12-11 17:44:55 +00:00
{
DispatchQueue.main.async {
self.channelPlaylist.replace(cache)
}
}
}
2022-12-10 20:47:38 +00:00
#if os(iOS)
var playlistsMenu: some View {
2022-12-15 22:03:04 +00:00
let title = currentPlaylist?.title ?? "Playlists"
return Menu {
Menu {
selectPlaylistButton
} label: {
Label(title, systemImage: "list.and.film")
}
2022-12-10 20:47:38 +00:00
Section {
if let currentPlaylist {
2022-12-15 22:03:04 +00:00
playButtons
2022-12-10 20:47:38 +00:00
editPlaylistButton
2022-12-11 22:30:28 +00:00
if let account = accounts.current {
FavoriteButton(item: FavoriteItem(section: .playlist(account.id, currentPlaylist.id)))
2022-12-16 11:31:22 +00:00
.id(currentPlaylist.id)
2022-12-11 22:30:28 +00:00
}
2022-12-10 20:47:38 +00:00
}
}
2022-12-11 15:18:12 +00:00
if accounts.signedIn {
newPlaylistButton
}
2022-12-11 22:15:56 +00:00
2022-12-12 00:18:29 +00:00
ListingStyleButtons(listingStyle: $playlistListingStyle)
2023-02-25 15:42:18 +00:00
Section {
HideWatchedButtons()
2023-05-23 16:54:53 +00:00
HideShortsButtons()
2023-02-25 15:42:18 +00:00
}
2022-12-11 22:15:56 +00:00
Section {
SettingsButtons()
}
2022-12-10 20:47:38 +00:00
} label: {
HStack(spacing: 12) {
2022-12-12 00:18:29 +00:00
HStack(spacing: 6) {
Image(systemName: "list.and.film")
2022-12-15 22:03:04 +00:00
Text(title)
2022-12-12 00:18:29 +00:00
.font(.headline)
}
.foregroundColor(.primary)
2022-12-10 20:47:38 +00:00
Image(systemName: "chevron.down.circle.fill")
.foregroundColor(.accentColor)
}
2022-12-12 00:18:29 +00:00
.imageScale(.small)
2022-12-11 15:18:12 +00:00
.lineLimit(1)
2022-12-12 00:18:29 +00:00
.frame(maxWidth: 320)
2022-12-10 20:47:38 +00:00
.transaction { t in t.animation = nil }
}
2022-12-11 15:18:12 +00:00
.disabled(!accounts.signedIn)
2022-12-10 20:47:38 +00:00
}
#endif
func hintText(_ text: String) -> some View {
VStack {
Spacer()
Text(text)
.foregroundColor(.secondary)
.multilineTextAlignment(.center)
Spacer()
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
#if os(macOS)
.background(Color.secondaryBackground)
#endif
}
2021-07-08 15:14:54 +00:00
func selectCreatedPlaylist() {
guard createdPlaylist != nil else {
return
}
model.load(force: true) {
if let id = createdPlaylist?.id {
2021-10-24 21:36:24 +00:00
selectedPlaylistID = id
}
2021-07-08 17:18:36 +00:00
self.createdPlaylist = nil
}
}
func selectEditedPlaylist() {
if editedPlaylist.isNil {
2021-10-24 21:36:24 +00:00
selectedPlaylistID = ""
2021-07-08 17:18:36 +00:00
}
model.load(force: true) {
2021-10-24 21:36:24 +00:00
self.selectedPlaylistID = editedPlaylist?.id ?? ""
2021-07-08 17:18:36 +00:00
self.editedPlaylist = nil
2021-07-08 15:14:54 +00:00
}
}
2021-06-26 09:39:35 +00:00
var selectPlaylistButton: some View {
#if os(tvOS)
Button {
2021-10-24 21:36:24 +00:00
guard currentPlaylist != nil else {
return
}
2021-06-26 09:39:35 +00:00
2021-10-24 21:36:24 +00:00
selectedPlaylistID = model.all.next(after: currentPlaylist!)?.id ?? ""
} label: {
Text(currentPlaylist?.title ?? "Select playlist")
.frame(maxWidth: .infinity)
}
2022-08-21 15:40:44 +00:00
.lineLimit(1)
.contextMenu {
ForEach(model.all) { playlist in
Button(playlist.title) {
2021-10-24 21:36:24 +00:00
selectedPlaylistID = playlist.id
}
2021-06-26 09:39:35 +00:00
}
2021-09-29 12:36:52 +00:00
Button("Cancel", role: .cancel) {}
2021-06-26 09:39:35 +00:00
}
#else
2022-12-10 20:47:38 +00:00
Picker("Current Playlist", selection: $selectedPlaylistID) {
ForEach(model.all) { playlist in
2022-12-10 20:47:38 +00:00
Text(playlist.title).tag(playlist.id)
}
}
#endif
2021-06-26 09:39:35 +00:00
}
2021-07-08 15:14:54 +00:00
2021-07-08 17:18:36 +00:00
var editPlaylistButton: some View {
Button(action: {
2021-10-24 21:36:24 +00:00
self.editedPlaylist = self.currentPlaylist
2021-07-08 17:18:36 +00:00
self.showingEditPlaylist = true
}) {
2022-12-10 20:47:38 +00:00
Label("Edit Playlist", systemImage: "rectangle.and.pencil.and.ellipsis")
2021-07-08 17:18:36 +00:00
}
}
2021-07-08 15:14:54 +00:00
var newPlaylistButton: some View {
Button(action: { self.showingNewPlaylist = true }) {
2022-12-10 20:47:38 +00:00
Label("New Playlist", systemImage: "plus")
2021-07-08 15:14:54 +00:00
}
}
2021-10-24 21:36:24 +00:00
2022-12-15 22:03:04 +00:00
private var playButtons: some View {
Group {
Button {
player.play(items.compactMap(\.video))
} label: {
Label("Play", systemImage: "play")
}
2022-09-04 15:23:02 +00:00
Button {
player.play(items.compactMap(\.video), shuffling: true)
} label: {
Label("Shuffle", systemImage: "shuffle")
}
}
}
2021-10-24 21:36:24 +00:00
private var currentPlaylist: Playlist? {
2022-12-10 20:47:38 +00:00
if selectedPlaylistID.isEmpty {
DispatchQueue.main.async {
self.selectedPlaylistID = model.all.first?.id ?? ""
}
}
return model.find(id: selectedPlaylistID) ?? model.all.first
2021-10-24 21:36:24 +00:00
}
var shouldDisplayHeader: Bool {
#if os(tvOS)
true
#else
showCacheStatus
#endif
}
var header: some View {
HStack {
2023-05-22 13:33:13 +00:00
#if os(tvOS)
if model.isEmpty {
Text("No Playlists")
.foregroundColor(.secondary)
} else {
selectPlaylistButton
}
2023-05-22 13:33:13 +00:00
if let playlist = currentPlaylist {
editPlaylistButton
2023-05-22 13:33:13 +00:00
FavoriteButton(item: FavoriteItem(section: .playlist(accounts.current.id, playlist.id)))
.labelStyle(.iconOnly)
2023-05-22 13:33:13 +00:00
playButtons
}
2023-05-22 13:33:13 +00:00
newPlaylistButton
2023-05-22 13:33:13 +00:00
Spacer()
2023-05-22 13:33:13 +00:00
ListingStyleButtons(listingStyle: $playlistListingStyle)
HideWatchedButtons()
2023-05-23 16:54:53 +00:00
HideShortsButtons()
2023-05-22 13:33:13 +00:00
#else
Spacer()
#endif
if let account = accounts.current, showCacheStatus {
CacheStatusHeader(
refreshTime: cache.getFormattedPlaylistTime(account: account),
isLoading: model.isLoading
)
}
2023-05-22 13:33:13 +00:00
#if os(tvOS)
Button {
model.load(force: true)
loadResource()
} label: {
Label("Refresh", systemImage: "arrow.clockwise")
.labelStyle(.iconOnly)
}
#endif
}
.labelStyle(.iconOnly)
.font(.caption)
.imageScale(.small)
#if os(tvOS)
2023-05-22 13:33:13 +00:00
.padding(.leading, 30)
.padding(.bottom, 15)
.padding(.trailing, 30)
#endif
}
2021-06-26 09:39:35 +00:00
}
struct PlaylistsView_Provider: PreviewProvider {
static var previews: some View {
2022-12-10 20:47:38 +00:00
NavigationView {
PlaylistsView()
}
}
}