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?
|
|
|
|
|
2021-10-23 10:13:05 +00:00
|
|
|
@EnvironmentObject<AccountsModel> private var accounts
|
|
|
|
@EnvironmentObject<PlayerModel> private var player
|
|
|
|
@EnvironmentObject<PlaylistsModel> private var model
|
|
|
|
|
2021-09-29 12:36:52 +00:00
|
|
|
@Namespace private var focusNamespace
|
|
|
|
|
2021-10-21 23:29:10 +00:00
|
|
|
var items: [ContentItem] {
|
2021-10-24 21:36:24 +00:00
|
|
|
ContentItem.array(of: currentPlaylist?.videos ?? [])
|
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 {
|
2021-10-05 20:20:09 +00:00
|
|
|
PlayerControlsView {
|
|
|
|
SignInRequiredView(title: "Playlists") {
|
|
|
|
VStack {
|
2021-09-29 11:49:37 +00:00
|
|
|
#if os(tvOS)
|
2021-10-05 20:20:09 +00:00
|
|
|
toolbar
|
2021-09-29 11:49:37 +00:00
|
|
|
#endif
|
2021-10-05 20:20:09 +00:00
|
|
|
|
2021-10-24 21:36:24 +00:00
|
|
|
if currentPlaylist != nil, items.isEmpty {
|
2021-10-05 20:20:09 +00:00
|
|
|
hintText("Playlist is empty\n\nTap and hold on a video and then tap \"Add to Playlist\"")
|
|
|
|
} else if model.all.isEmpty {
|
|
|
|
hintText("You have no playlists\n\nTap on \"New Playlist\" to create one")
|
|
|
|
} else {
|
2021-10-24 21:36:24 +00:00
|
|
|
Group {
|
|
|
|
#if os(tvOS)
|
|
|
|
HorizontalCells(items: items)
|
|
|
|
.padding(.top, 40)
|
|
|
|
Spacer()
|
|
|
|
#else
|
|
|
|
VerticalCells(items: items)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
.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
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
.environmentObject(accounts)
|
|
|
|
}
|
|
|
|
.fullScreenCover(isPresented: $showingEditPlaylist, onDismiss: selectEditedPlaylist) {
|
|
|
|
PlaylistFormView(playlist: $editedPlaylist)
|
|
|
|
.environmentObject(accounts)
|
|
|
|
}
|
2021-08-16 15:52:42 +00:00
|
|
|
#else
|
2021-11-30 22:58:11 +00:00
|
|
|
.background(
|
|
|
|
EmptyView()
|
|
|
|
.sheet(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
|
|
|
|
PlaylistFormView(playlist: $createdPlaylist)
|
|
|
|
.environmentObject(accounts)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.background(
|
|
|
|
EmptyView()
|
|
|
|
.sheet(isPresented: $showingEditPlaylist, onDismiss: selectEditedPlaylist) {
|
|
|
|
PlaylistFormView(playlist: $editedPlaylist)
|
|
|
|
.environmentObject(accounts)
|
|
|
|
}
|
|
|
|
)
|
2021-07-11 20:52:49 +00:00
|
|
|
#endif
|
2021-11-08 16:29:35 +00:00
|
|
|
.toolbar {
|
|
|
|
ToolbarItemGroup {
|
|
|
|
#if !os(iOS)
|
|
|
|
if !model.isEmpty {
|
2021-11-28 14:37:55 +00:00
|
|
|
if #available(macOS 12.0, *) {
|
|
|
|
selectPlaylistButton
|
|
|
|
.prefersDefaultFocus(in: focusNamespace)
|
|
|
|
} else {
|
|
|
|
selectPlaylistButton
|
|
|
|
}
|
2021-11-08 16:29:35 +00:00
|
|
|
}
|
2021-08-16 15:52:42 +00:00
|
|
|
|
2021-11-08 16:29:35 +00:00
|
|
|
if currentPlaylist != nil {
|
|
|
|
editPlaylistButton
|
|
|
|
}
|
|
|
|
#endif
|
2021-11-01 21:56:18 +00:00
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
FavoriteButton(item: FavoriteItem(section: .playlist(selectedPlaylistID)))
|
2021-11-08 16:29:35 +00:00
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-11-08 16:29:35 +00:00
|
|
|
#if os(iOS)
|
|
|
|
ToolbarItemGroup(placement: .bottomBar) {
|
|
|
|
Group {
|
|
|
|
if model.isEmpty {
|
|
|
|
Text("No Playlists")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
} else {
|
|
|
|
Text("Current Playlist")
|
|
|
|
.foregroundColor(.secondary)
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-11-08 16:29:35 +00:00
|
|
|
selectPlaylistButton
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-11-08 16:29:35 +00:00
|
|
|
Spacer()
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
newPlaylistButton
|
|
|
|
|
2021-11-08 16:29:35 +00:00
|
|
|
if currentPlaylist != nil {
|
|
|
|
editPlaylistButton
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.transaction { t in t.animation = .none }
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
2021-11-08 16:29:35 +00:00
|
|
|
#endif
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
2021-09-29 14:29:17 +00:00
|
|
|
#if os(tvOS)
|
2021-11-08 16:29:35 +00:00
|
|
|
.focusScope(focusNamespace)
|
2021-09-29 14:29:17 +00:00
|
|
|
#endif
|
2021-11-08 16:29:35 +00:00
|
|
|
.onAppear {
|
|
|
|
model.load()
|
|
|
|
}
|
|
|
|
.onChange(of: accounts.current) { _ in
|
|
|
|
model.load(force: true)
|
|
|
|
}
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
Button {
|
2021-10-21 23:29:10 +00:00
|
|
|
player.playAll(items.compactMap(\.video))
|
2021-12-19 17:17:04 +00:00
|
|
|
player.show()
|
2021-10-05 20:20:09 +00:00
|
|
|
} label: {
|
|
|
|
HStack(spacing: 15) {
|
|
|
|
Image(systemName: "play.fill")
|
|
|
|
Text("Play All")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 21:36:24 +00:00
|
|
|
if currentPlaylist != nil {
|
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
|
|
|
if let playlist = currentPlaylist {
|
|
|
|
FavoriteButton(item: FavoriteItem(section: .playlist(playlist.id)))
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
.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
|
2021-10-24 21:36:24 +00:00
|
|
|
Menu(currentPlaylist?.title ?? "Select playlist") {
|
2021-09-28 18:06:05 +00:00
|
|
|
ForEach(model.all) { playlist in
|
2021-10-24 21:36:24 +00:00
|
|
|
Button(action: { selectedPlaylistID = playlist.id }) {
|
|
|
|
if playlist == currentPlaylist {
|
2021-08-16 15:52:42 +00:00
|
|
|
Label(playlist.title, systemImage: "checkmark")
|
|
|
|
} else {
|
|
|
|
Text(playlist.title)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#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
|
|
|
|
}) {
|
2021-07-09 14:53:53 +00:00
|
|
|
HStack(spacing: 8) {
|
2021-08-16 15:52:42 +00:00
|
|
|
Image(systemName: "slider.horizontal.3")
|
2021-07-09 14:53:53 +00:00
|
|
|
Text("Edit")
|
|
|
|
}
|
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 }) {
|
2021-07-09 14:53:53 +00:00
|
|
|
HStack(spacing: 8) {
|
|
|
|
Image(systemName: "plus")
|
2021-07-11 20:52:49 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
Text("New Playlist")
|
|
|
|
#endif
|
2021-07-09 14:53:53 +00:00
|
|
|
}
|
2021-07-08 15:14:54 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-24 21:36:24 +00:00
|
|
|
|
|
|
|
private var currentPlaylist: Playlist? {
|
|
|
|
model.find(id: selectedPlaylistID) ?? model.all.first
|
|
|
|
}
|
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 {
|
|
|
|
PlaylistsView()
|
2021-09-29 11:45:00 +00:00
|
|
|
.injectFixtureEnvironmentObjects()
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
|
|
|
}
|