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?
|
|
|
|
|
2022-08-21 14:05:26 +00:00
|
|
|
@StateObject private var channelPlaylist = Store<ChannelPlaylist>()
|
|
|
|
@StateObject private var userPlaylist = Store<Playlist>()
|
2022-05-22 15:53:12 +00:00
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
@ObservedObject private var accounts = AccountsModel.shared
|
|
|
|
private var player = PlayerModel.shared
|
|
|
|
@ObservedObject private var model = PlaylistsModel.shared
|
2022-12-11 17:04:39 +00:00
|
|
|
private var cache = PlaylistsCacheModel.shared
|
2021-10-23 10:13:05 +00:00
|
|
|
|
2021-09-29 12:36:52 +00:00
|
|
|
@Namespace private var focusNamespace
|
|
|
|
|
2021-10-21 23:29:10 +00:00
|
|
|
var items: [ContentItem] {
|
2022-05-22 15:53:12 +00:00
|
|
|
var videos = currentPlaylist?.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-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? {
|
2022-08-21 14:05:26 +00:00
|
|
|
guard let playlist = currentPlaylist else { return nil }
|
2022-05-22 15:53:12 +00:00
|
|
|
|
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-05-22 15:53:12 +00:00
|
|
|
|
|
|
|
return resource
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
2021-07-09 14:53:53 +00:00
|
|
|
|
2021-08-16 15:52:42 +00:00
|
|
|
var body: some View {
|
2022-12-10 21:37:14 +00:00
|
|
|
SignInRequiredView(title: "Playlists".localized()) {
|
|
|
|
Section {
|
|
|
|
VStack {
|
|
|
|
#if os(tvOS)
|
|
|
|
toolbar
|
|
|
|
#endif
|
|
|
|
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())
|
|
|
|
} else {
|
|
|
|
Group {
|
|
|
|
#if os(tvOS)
|
|
|
|
HorizontalCells(items: items)
|
|
|
|
.padding(.top, 40)
|
|
|
|
Spacer()
|
|
|
|
#else
|
2022-12-11 17:04:39 +00:00
|
|
|
VerticalCells(items: items) {
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
CacheStatusHeader(
|
|
|
|
refreshTime: cache.getFormattedPlaylistTime(account: accounts.current),
|
|
|
|
isLoading: model.isLoading
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.environment(\.scrollViewBottomPadding, 70)
|
2022-12-10 21:37:14 +00:00
|
|
|
#endif
|
2021-10-24 21:36:24 +00:00
|
|
|
}
|
2022-12-10 21:37:14 +00:00
|
|
|
.environment(\.currentPlaylistID, currentPlaylist?.id)
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
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-06-26 13:18:10 +00:00
|
|
|
resource?.load()
|
2022-02-04 17:38:29 +00:00
|
|
|
}
|
|
|
|
.onChange(of: accounts.current) { _ in
|
|
|
|
model.load(force: true)
|
2022-08-21 14:05:26 +00:00
|
|
|
resource?.load()
|
2022-02-04 17:38:29 +00:00
|
|
|
}
|
2022-08-21 14:05:26 +00:00
|
|
|
.onChange(of: currentPlaylist) { _ in
|
2022-09-12 15:23:00 +00:00
|
|
|
channelPlaylist.clear()
|
|
|
|
userPlaylist.clear()
|
2022-02-04 17:38:29 +00:00
|
|
|
resource?.load()
|
|
|
|
}
|
|
|
|
.onChange(of: model.reloadPlaylists) { _ in
|
|
|
|
resource?.load()
|
|
|
|
}
|
2022-09-04 15:24:07 +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 {
|
|
|
|
ToolbarItem(placement: .principal) {
|
|
|
|
playlistsMenu
|
|
|
|
}
|
|
|
|
}
|
2022-09-04 15:24:07 +00:00
|
|
|
#endif
|
2021-08-16 15:52:42 +00:00
|
|
|
#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)
|
2021-08-16 15:52:42 +00:00
|
|
|
#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-09-04 15:24:07 +00:00
|
|
|
|
2022-08-28 18:00:43 +00:00
|
|
|
#if !os(macOS)
|
|
|
|
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
|
|
|
|
model.load()
|
|
|
|
resource?.loadIfNeeded()
|
|
|
|
}
|
|
|
|
#endif
|
2022-09-04 15:24:07 +00:00
|
|
|
#if !os(tvOS)
|
|
|
|
.background(
|
|
|
|
Button("Refresh") {
|
|
|
|
resource?.load()
|
|
|
|
}
|
|
|
|
.keyboardShortcut("r")
|
|
|
|
.opacity(0)
|
|
|
|
)
|
|
|
|
#endif
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:47:38 +00:00
|
|
|
#if os(iOS)
|
|
|
|
var playlistsMenu: some View {
|
|
|
|
Menu {
|
|
|
|
selectPlaylistButton
|
|
|
|
|
|
|
|
Section {
|
|
|
|
if let currentPlaylist {
|
|
|
|
playButton
|
|
|
|
|
|
|
|
editPlaylistButton
|
|
|
|
|
|
|
|
FavoriteButton(item: FavoriteItem(section: .playlist(currentPlaylist.id)))
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
}
|
|
|
|
}
|
2022-12-11 15:18:12 +00:00
|
|
|
|
|
|
|
if accounts.signedIn {
|
|
|
|
newPlaylistButton
|
|
|
|
}
|
2022-12-10 20:47:38 +00:00
|
|
|
} label: {
|
|
|
|
HStack(spacing: 12) {
|
|
|
|
Text(currentPlaylist?.title ?? "Playlists")
|
|
|
|
.font(.headline)
|
|
|
|
.foregroundColor(.primary)
|
|
|
|
|
|
|
|
Image(systemName: "chevron.down.circle.fill")
|
|
|
|
.foregroundColor(.accentColor)
|
|
|
|
.imageScale(.small)
|
|
|
|
}
|
2022-12-11 15:18:12 +00:00
|
|
|
.lineLimit(1)
|
|
|
|
.frame(maxWidth: 300)
|
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
|
|
|
|
|
2021-09-29 14:29:17 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
var toolbar: some View {
|
|
|
|
HStack {
|
|
|
|
if model.isEmpty {
|
|
|
|
Text("No Playlists")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
} else {
|
|
|
|
Text("Current Playlist")
|
|
|
|
.foregroundColor(.secondary)
|
2021-08-16 15:52:42 +00:00
|
|
|
|
2021-09-29 14:29:17 +00:00
|
|
|
selectPlaylistButton
|
|
|
|
}
|
2021-08-16 15:52:42 +00:00
|
|
|
|
2022-01-02 18:59:57 +00:00
|
|
|
if let playlist = currentPlaylist {
|
2021-09-29 14:29:17 +00:00
|
|
|
editPlaylistButton
|
2021-08-16 15:52:42 +00:00
|
|
|
|
2021-11-01 21:56:18 +00:00
|
|
|
FavoriteButton(item: FavoriteItem(section: .playlist(playlist.id)))
|
|
|
|
.labelStyle(.iconOnly)
|
2022-01-02 18:59:57 +00:00
|
|
|
|
|
|
|
playButton
|
2021-11-01 21:56:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 14:29:17 +00:00
|
|
|
Spacer()
|
2021-09-29 12:36:52 +00:00
|
|
|
|
2021-09-29 14:29:17 +00:00
|
|
|
newPlaylistButton
|
|
|
|
.padding(.leading, 40)
|
|
|
|
}
|
2022-12-10 20:47:38 +00:00
|
|
|
.labelStyle(.iconOnly)
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
2021-09-29 14:29:17 +00:00
|
|
|
#endif
|
2021-08-16 15:52:42 +00:00
|
|
|
|
|
|
|
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)
|
2021-12-19 23:36:12 +00:00
|
|
|
.background(Color.secondaryBackground)
|
2021-08-16 15:52:42 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-07-08 15:14:54 +00:00
|
|
|
func selectCreatedPlaylist() {
|
|
|
|
guard createdPlaylist != nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-28 18:06:05 +00:00
|
|
|
model.load(force: true) {
|
|
|
|
if let id = createdPlaylist?.id {
|
2021-10-24 21:36:24 +00:00
|
|
|
selectedPlaylistID = id
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
2021-07-08 17:18:36 +00:00
|
|
|
|
|
|
|
self.createdPlaylist = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectEditedPlaylist() {
|
2021-09-26 17:40:25 +00:00
|
|
|
if editedPlaylist.isNil {
|
2021-10-24 21:36:24 +00:00
|
|
|
selectedPlaylistID = ""
|
2021-07-08 17:18:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 18:06:05 +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 {
|
2021-08-16 15:52:42 +00:00
|
|
|
#if os(tvOS)
|
2021-10-24 21:36:24 +00:00
|
|
|
Button(currentPlaylist?.title ?? "Select playlist") {
|
|
|
|
guard currentPlaylist != nil else {
|
2021-08-16 15:52:42 +00:00
|
|
|
return
|
|
|
|
}
|
2021-06-26 09:39:35 +00:00
|
|
|
|
2021-10-24 21:36:24 +00:00
|
|
|
selectedPlaylistID = model.all.next(after: currentPlaylist!)?.id ?? ""
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
2022-08-21 15:40:44 +00:00
|
|
|
.lineLimit(1)
|
2021-08-16 15:52:42 +00:00
|
|
|
.contextMenu {
|
2021-09-28 18:06:05 +00:00
|
|
|
ForEach(model.all) { playlist in
|
2021-08-16 15:52:42 +00:00
|
|
|
Button(playlist.title) {
|
2021-10-24 21:36:24 +00:00
|
|
|
selectedPlaylistID = playlist.id
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
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
|
|
|
}
|
2021-08-16 15:52:42 +00:00
|
|
|
#else
|
2022-12-10 20:47:38 +00:00
|
|
|
Picker("Current Playlist", selection: $selectedPlaylistID) {
|
2021-09-28 18:06:05 +00:00
|
|
|
ForEach(model.all) { playlist in
|
2022-12-10 20:47:38 +00:00
|
|
|
Text(playlist.title).tag(playlist.id)
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#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-01-02 18:59:57 +00:00
|
|
|
private var playButton: some View {
|
|
|
|
Button {
|
|
|
|
player.play(items.compactMap(\.video))
|
|
|
|
} label: {
|
2022-12-10 20:47:38 +00:00
|
|
|
Label("Play", systemImage: "play")
|
2022-01-02 18:59:57 +00:00
|
|
|
}
|
2022-09-04 15:23:02 +00:00
|
|
|
.contextMenu {
|
|
|
|
Button {
|
|
|
|
player.play(items.compactMap(\.video), shuffling: true)
|
|
|
|
} label: {
|
|
|
|
Label("Shuffle", systemImage: "shuffle")
|
|
|
|
}
|
|
|
|
}
|
2022-01-02 18:59:57 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2021-06-26 09:39:35 +00:00
|
|
|
}
|
2021-08-16 15:52:42 +00:00
|
|
|
|
|
|
|
struct PlaylistsView_Provider: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2022-12-10 20:47:38 +00:00
|
|
|
NavigationView {
|
|
|
|
PlaylistsView()
|
|
|
|
}
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
|
|
|
}
|