2021-07-08 22:48:36 +05:30
|
|
|
import Defaults
|
2021-06-28 16:13:07 +05:30
|
|
|
import Siesta
|
2021-06-26 15:09:35 +05:30
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct PlaylistsView: View {
|
2021-07-08 20:44:54 +05:30
|
|
|
@State private var showingNewPlaylist = false
|
|
|
|
@State private var createdPlaylist: Playlist?
|
|
|
|
|
2021-07-08 22:48:36 +05:30
|
|
|
@State private var showingEditPlaylist = false
|
|
|
|
@State private var editedPlaylist: Playlist?
|
|
|
|
|
2021-10-23 15:43:05 +05:30
|
|
|
@EnvironmentObject<AccountsModel> private var accounts
|
|
|
|
@EnvironmentObject<PlayerModel> private var player
|
|
|
|
@EnvironmentObject<PlaylistsModel> private var model
|
|
|
|
|
2021-09-29 18:06:52 +05:30
|
|
|
@Namespace private var focusNamespace
|
|
|
|
|
2021-10-22 04:59:10 +05:30
|
|
|
var items: [ContentItem] {
|
|
|
|
ContentItem.array(of: model.currentPlaylist?.videos ?? [])
|
2021-08-16 21:22:42 +05:30
|
|
|
}
|
2021-07-09 20:23:53 +05:30
|
|
|
|
2021-08-16 21:22:42 +05:30
|
|
|
var body: some View {
|
2021-10-06 01:50:09 +05:30
|
|
|
PlayerControlsView {
|
|
|
|
SignInRequiredView(title: "Playlists") {
|
|
|
|
VStack {
|
2021-09-29 17:19:37 +05:30
|
|
|
#if os(tvOS)
|
2021-10-06 01:50:09 +05:30
|
|
|
toolbar
|
2021-09-29 17:19:37 +05:30
|
|
|
#endif
|
2021-10-06 01:50:09 +05:30
|
|
|
|
2021-10-22 04:59:10 +05:30
|
|
|
if model.currentPlaylist != nil, items.isEmpty {
|
2021-10-06 01:50:09 +05:30
|
|
|
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 {
|
|
|
|
#if os(tvOS)
|
2021-10-22 04:59:10 +05:30
|
|
|
HorizontalCells(items: items)
|
2021-10-06 01:50:09 +05:30
|
|
|
.padding(.top, 40)
|
|
|
|
Spacer()
|
|
|
|
#else
|
2021-10-22 04:59:10 +05:30
|
|
|
VerticalCells(items: items)
|
2021-10-06 01:50:09 +05:30
|
|
|
#endif
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
2021-06-26 15:09:35 +05:30
|
|
|
}
|
|
|
|
}
|
2021-08-16 21:22:42 +05:30
|
|
|
#if os(tvOS)
|
2021-07-12 02:22:49 +05:30
|
|
|
.fullScreenCover(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
|
|
|
|
PlaylistFormView(playlist: $createdPlaylist)
|
|
|
|
}
|
|
|
|
.fullScreenCover(isPresented: $showingEditPlaylist, onDismiss: selectEditedPlaylist) {
|
|
|
|
PlaylistFormView(playlist: $editedPlaylist)
|
|
|
|
}
|
2021-08-16 21:22:42 +05:30
|
|
|
#else
|
|
|
|
.sheet(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
|
|
|
|
PlaylistFormView(playlist: $createdPlaylist)
|
|
|
|
}
|
|
|
|
.sheet(isPresented: $showingEditPlaylist, onDismiss: selectEditedPlaylist) {
|
|
|
|
PlaylistFormView(playlist: $editedPlaylist)
|
|
|
|
}
|
2021-07-12 02:22:49 +05:30
|
|
|
#endif
|
2021-08-16 21:22:42 +05:30
|
|
|
.toolbar {
|
|
|
|
ToolbarItemGroup {
|
|
|
|
#if !os(iOS)
|
2021-09-28 23:36:05 +05:30
|
|
|
if !model.isEmpty {
|
2021-08-16 21:22:42 +05:30
|
|
|
selectPlaylistButton
|
2021-09-29 18:06:52 +05:30
|
|
|
.prefersDefaultFocus(in: focusNamespace)
|
2021-08-16 21:22:42 +05:30
|
|
|
}
|
|
|
|
|
2021-09-28 23:36:05 +05:30
|
|
|
if model.currentPlaylist != nil {
|
2021-08-16 21:22:42 +05:30
|
|
|
editPlaylistButton
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
newPlaylistButton
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
ToolbarItemGroup(placement: .bottomBar) {
|
|
|
|
Group {
|
2021-09-28 23:36:05 +05:30
|
|
|
if model.isEmpty {
|
2021-09-25 13:48:22 +05:30
|
|
|
Text("No Playlists")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
} else {
|
|
|
|
Text("Current Playlist")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
|
|
|
selectPlaylistButton
|
|
|
|
}
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
2021-09-28 23:36:05 +05:30
|
|
|
if model.currentPlaylist != nil {
|
2021-09-25 13:48:22 +05:30
|
|
|
editPlaylistButton
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.transaction { t in t.animation = .none }
|
|
|
|
}
|
|
|
|
#endif
|
2021-08-16 21:22:42 +05:30
|
|
|
}
|
2021-09-29 19:59:17 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
.focusScope(focusNamespace)
|
|
|
|
#endif
|
2021-06-28 16:13:07 +05:30
|
|
|
.onAppear {
|
2021-09-28 23:36:05 +05:30
|
|
|
model.load()
|
2021-06-26 15:09:35 +05:30
|
|
|
}
|
2021-10-23 15:43:05 +05:30
|
|
|
.onChange(of: accounts.current) { _ in
|
|
|
|
model.load(force: true)
|
|
|
|
}
|
2021-08-16 21:22:42 +05:30
|
|
|
}
|
|
|
|
|
2021-09-29 19:59:17 +05:30
|
|
|
#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 21:22:42 +05:30
|
|
|
|
2021-09-29 19:59:17 +05:30
|
|
|
selectPlaylistButton
|
|
|
|
}
|
2021-08-16 21:22:42 +05:30
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
Button {
|
2021-10-22 04:59:10 +05:30
|
|
|
player.playAll(items.compactMap(\.video))
|
2021-10-06 01:50:09 +05:30
|
|
|
player.presentPlayer()
|
|
|
|
} label: {
|
|
|
|
HStack(spacing: 15) {
|
|
|
|
Image(systemName: "play.fill")
|
|
|
|
Text("Play All")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 19:59:17 +05:30
|
|
|
if model.currentPlaylist != nil {
|
|
|
|
editPlaylistButton
|
|
|
|
}
|
2021-08-16 21:22:42 +05:30
|
|
|
|
2021-09-29 19:59:17 +05:30
|
|
|
Spacer()
|
2021-09-29 18:06:52 +05:30
|
|
|
|
2021-09-29 19:59:17 +05:30
|
|
|
newPlaylistButton
|
|
|
|
.padding(.leading, 40)
|
|
|
|
}
|
2021-08-16 21:22:42 +05:30
|
|
|
}
|
2021-09-29 19:59:17 +05:30
|
|
|
#endif
|
2021-08-16 21:22:42 +05:30
|
|
|
|
|
|
|
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()
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-07-08 20:44:54 +05:30
|
|
|
func selectCreatedPlaylist() {
|
|
|
|
guard createdPlaylist != nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-28 23:36:05 +05:30
|
|
|
model.load(force: true) {
|
|
|
|
if let id = createdPlaylist?.id {
|
|
|
|
self.model.selectPlaylist(id)
|
|
|
|
}
|
2021-07-08 22:48:36 +05:30
|
|
|
|
|
|
|
self.createdPlaylist = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectEditedPlaylist() {
|
2021-09-26 23:10:25 +05:30
|
|
|
if editedPlaylist.isNil {
|
2021-09-28 23:36:05 +05:30
|
|
|
model.selectPlaylist(nil)
|
2021-07-08 22:48:36 +05:30
|
|
|
}
|
|
|
|
|
2021-09-28 23:36:05 +05:30
|
|
|
model.load(force: true) {
|
|
|
|
model.selectPlaylist(editedPlaylist?.id)
|
2021-07-08 22:48:36 +05:30
|
|
|
|
|
|
|
self.editedPlaylist = nil
|
2021-07-08 20:44:54 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-26 15:09:35 +05:30
|
|
|
var selectPlaylistButton: some View {
|
2021-08-16 21:22:42 +05:30
|
|
|
#if os(tvOS)
|
2021-09-28 23:36:05 +05:30
|
|
|
Button(model.currentPlaylist?.title ?? "Select playlist") {
|
|
|
|
guard model.currentPlaylist != nil else {
|
2021-08-16 21:22:42 +05:30
|
|
|
return
|
|
|
|
}
|
2021-06-26 15:09:35 +05:30
|
|
|
|
2021-09-28 23:36:05 +05:30
|
|
|
model.selectPlaylist(model.all.next(after: model.currentPlaylist!)?.id)
|
2021-08-16 21:22:42 +05:30
|
|
|
}
|
|
|
|
.contextMenu {
|
2021-09-28 23:36:05 +05:30
|
|
|
ForEach(model.all) { playlist in
|
2021-08-16 21:22:42 +05:30
|
|
|
Button(playlist.title) {
|
2021-09-28 23:36:05 +05:30
|
|
|
model.selectPlaylist(playlist.id)
|
2021-08-16 21:22:42 +05:30
|
|
|
}
|
2021-06-26 15:09:35 +05:30
|
|
|
}
|
2021-09-29 18:06:52 +05:30
|
|
|
|
|
|
|
Button("Cancel", role: .cancel) {}
|
2021-06-26 15:09:35 +05:30
|
|
|
}
|
2021-08-16 21:22:42 +05:30
|
|
|
#else
|
2021-09-28 23:36:05 +05:30
|
|
|
Menu(model.currentPlaylist?.title ?? "Select playlist") {
|
|
|
|
ForEach(model.all) { playlist in
|
|
|
|
Button(action: { model.selectPlaylist(playlist.id) }) {
|
|
|
|
if playlist == model.currentPlaylist {
|
2021-08-16 21:22:42 +05:30
|
|
|
Label(playlist.title, systemImage: "checkmark")
|
|
|
|
} else {
|
|
|
|
Text(playlist.title)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2021-06-26 15:09:35 +05:30
|
|
|
}
|
2021-07-08 20:44:54 +05:30
|
|
|
|
2021-07-08 22:48:36 +05:30
|
|
|
var editPlaylistButton: some View {
|
|
|
|
Button(action: {
|
2021-09-28 23:36:05 +05:30
|
|
|
self.editedPlaylist = self.model.currentPlaylist
|
2021-07-08 22:48:36 +05:30
|
|
|
self.showingEditPlaylist = true
|
|
|
|
}) {
|
2021-07-09 20:23:53 +05:30
|
|
|
HStack(spacing: 8) {
|
2021-08-16 21:22:42 +05:30
|
|
|
Image(systemName: "slider.horizontal.3")
|
2021-07-09 20:23:53 +05:30
|
|
|
Text("Edit")
|
|
|
|
}
|
2021-07-08 22:48:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 20:44:54 +05:30
|
|
|
var newPlaylistButton: some View {
|
|
|
|
Button(action: { self.showingNewPlaylist = true }) {
|
2021-07-09 20:23:53 +05:30
|
|
|
HStack(spacing: 8) {
|
|
|
|
Image(systemName: "plus")
|
2021-07-12 02:22:49 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
Text("New Playlist")
|
|
|
|
#endif
|
2021-07-09 20:23:53 +05:30
|
|
|
}
|
2021-07-08 20:44:54 +05:30
|
|
|
}
|
|
|
|
}
|
2021-06-26 15:09:35 +05:30
|
|
|
}
|
2021-08-16 21:22:42 +05:30
|
|
|
|
|
|
|
struct PlaylistsView_Provider: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
PlaylistsView()
|
2021-09-29 17:15:00 +05:30
|
|
|
.injectFixtureEnvironmentObjects()
|
2021-08-16 21:22:42 +05:30
|
|
|
}
|
|
|
|
}
|