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-06-28 16:13:07 +05:30
|
|
|
@ObservedObject private var store = Store<[Playlist]>()
|
2021-06-26 15:09:35 +05:30
|
|
|
|
2021-07-08 22:48:36 +05:30
|
|
|
@Default(.selectedPlaylistID) private var selectedPlaylistID
|
2021-06-26 15:09:35 +05:30
|
|
|
|
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-06-28 16:13:07 +05:30
|
|
|
var resource: Resource {
|
|
|
|
InvidiousAPI.shared.playlists
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
resource.addObserver(store)
|
|
|
|
}
|
|
|
|
|
2021-08-16 21:22:42 +05:30
|
|
|
var videos: [Video] {
|
|
|
|
currentPlaylist?.videos ?? []
|
|
|
|
}
|
2021-07-09 20:23:53 +05:30
|
|
|
|
2021-08-16 21:22:42 +05:30
|
|
|
var videosViewMaxHeight: CGFloat {
|
|
|
|
#if os(tvOS)
|
|
|
|
videos.isEmpty ? 150 : .infinity
|
|
|
|
#else
|
|
|
|
videos.isEmpty ? 0 : .infinity
|
|
|
|
#endif
|
|
|
|
}
|
2021-07-09 20:23:53 +05:30
|
|
|
|
2021-08-16 21:22:42 +05:30
|
|
|
var body: some View {
|
|
|
|
VStack {
|
|
|
|
#if os(tvOS)
|
|
|
|
toolbar
|
|
|
|
.font(.system(size: 28))
|
|
|
|
|
|
|
|
#endif
|
|
|
|
if currentPlaylist != nil, videos.isEmpty {
|
|
|
|
hintText("Playlist is empty\n\nTap and hold on a video and then tap \"Add to Playlist\"")
|
|
|
|
} else if store.collection.isEmpty {
|
|
|
|
hintText("You have no playlists\n\nTap on \"New Playlist\" to create one")
|
|
|
|
} else {
|
|
|
|
VideosView(videos: videos)
|
2021-06-26 15:09:35 +05:30
|
|
|
}
|
2021-08-16 21:22:42 +05:30
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
toolbar
|
|
|
|
.font(.system(size: 14))
|
|
|
|
.padding(.horizontal)
|
|
|
|
.padding(.vertical, 10)
|
|
|
|
.overlay(Divider().offset(x: 0, y: -2), alignment: .topTrailing)
|
2021-08-20 04:08:31 +05:30
|
|
|
.transaction { t in t.animation = .none }
|
2021-08-16 21:22:42 +05:30
|
|
|
#endif
|
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)
|
|
|
|
if !store.collection.isEmpty {
|
|
|
|
selectPlaylistButton
|
|
|
|
}
|
|
|
|
|
|
|
|
if currentPlaylist != nil {
|
|
|
|
editPlaylistButton
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
newPlaylistButton
|
|
|
|
}
|
|
|
|
}
|
2021-06-28 16:13:07 +05:30
|
|
|
.onAppear {
|
2021-07-08 22:48:36 +05:30
|
|
|
resource.loadIfNeeded()?.onSuccess { _ in
|
|
|
|
selectPlaylist(selectedPlaylistID)
|
|
|
|
}
|
2021-06-26 15:09:35 +05:30
|
|
|
}
|
2021-07-12 02:22:49 +05:30
|
|
|
#if !os(tvOS)
|
|
|
|
.navigationTitle("Playlists")
|
|
|
|
#elseif os(iOS)
|
|
|
|
.navigationBarItems(trailing: newPlaylistButton)
|
|
|
|
#endif
|
2021-06-28 16:13:07 +05:30
|
|
|
}
|
2021-06-26 15:09:35 +05:30
|
|
|
|
2021-08-16 21:22:42 +05:30
|
|
|
var scaledToolbar: some View {
|
|
|
|
toolbar.scaleEffect(0.85)
|
|
|
|
}
|
|
|
|
|
|
|
|
var toolbar: some View {
|
|
|
|
HStack {
|
|
|
|
if store.collection.isEmpty {
|
|
|
|
Text("No Playlists")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
} else {
|
|
|
|
Text("Current Playlist")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
|
|
|
selectPlaylistButton
|
|
|
|
}
|
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
Spacer()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if currentPlaylist != nil {
|
|
|
|
editPlaylistButton
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !os(iOS)
|
|
|
|
newPlaylistButton
|
|
|
|
.padding(.leading, 40)
|
|
|
|
#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()
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-07-08 22:48:36 +05:30
|
|
|
func selectPlaylist(_ id: String?) {
|
|
|
|
selectedPlaylistID = id
|
|
|
|
}
|
|
|
|
|
2021-07-08 20:44:54 +05:30
|
|
|
func selectCreatedPlaylist() {
|
|
|
|
guard createdPlaylist != nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.load().onSuccess { _ in
|
2021-07-08 22:48:36 +05:30
|
|
|
self.selectPlaylist(createdPlaylist?.id)
|
|
|
|
|
|
|
|
self.createdPlaylist = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectEditedPlaylist() {
|
|
|
|
if editedPlaylist == nil {
|
|
|
|
selectPlaylist(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.load().onSuccess { _ in
|
|
|
|
selectPlaylist(editedPlaylist?.id)
|
|
|
|
|
|
|
|
self.editedPlaylist = nil
|
2021-07-08 20:44:54 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 16:13:07 +05:30
|
|
|
var currentPlaylist: Playlist? {
|
2021-07-09 20:23:53 +05:30
|
|
|
store.collection.first { $0.id == selectedPlaylistID } ?? store.collection.first
|
2021-06-26 15:09:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var selectPlaylistButton: some View {
|
2021-08-16 21:22:42 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
Button(currentPlaylist?.title ?? "Select playlist") {
|
|
|
|
guard currentPlaylist != nil else {
|
|
|
|
return
|
|
|
|
}
|
2021-06-26 15:09:35 +05:30
|
|
|
|
2021-08-16 21:22:42 +05:30
|
|
|
selectPlaylist(store.collection.next(after: currentPlaylist!)?.id)
|
|
|
|
}
|
|
|
|
.contextMenu {
|
|
|
|
ForEach(store.collection) { playlist in
|
|
|
|
Button(playlist.title) {
|
|
|
|
selectPlaylist(playlist.id)
|
|
|
|
}
|
2021-06-26 15:09:35 +05:30
|
|
|
}
|
|
|
|
}
|
2021-08-16 21:22:42 +05:30
|
|
|
#else
|
|
|
|
Menu(currentPlaylist?.title ?? "Select playlist") {
|
|
|
|
ForEach(store.collection) { playlist in
|
|
|
|
Button(action: { selectPlaylist(playlist.id) }) {
|
|
|
|
if playlist == self.currentPlaylist {
|
|
|
|
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: {
|
|
|
|
self.editedPlaylist = self.currentPlaylist
|
|
|
|
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()
|
|
|
|
.environmentObject(NavigationState())
|
|
|
|
}
|
|
|
|
}
|