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-06-28 10:43:07 +00:00
|
|
|
@ObservedObject private var store = Store<[Playlist]>()
|
2021-06-26 09:39:35 +00:00
|
|
|
|
2021-07-08 17:18:36 +00:00
|
|
|
@Default(.selectedPlaylistID) private var selectedPlaylistID
|
2021-06-26 09:39:35 +00:00
|
|
|
|
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-06-28 10:43:07 +00:00
|
|
|
var resource: Resource {
|
|
|
|
InvidiousAPI.shared.playlists
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
resource.addObserver(store)
|
|
|
|
}
|
|
|
|
|
2021-06-26 09:39:35 +00:00
|
|
|
var body: some View {
|
|
|
|
Section {
|
2021-07-07 23:01:54 +00:00
|
|
|
VStack(alignment: .center, spacing: 2) {
|
2021-07-08 15:14:54 +00:00
|
|
|
HStack {
|
2021-07-09 14:53:53 +00:00
|
|
|
if store.collection.isEmpty {
|
|
|
|
Text("No Playlists")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
} else {
|
|
|
|
Text("Current Playlist")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
|
|
|
selectPlaylistButton
|
|
|
|
}
|
2021-07-08 15:14:54 +00:00
|
|
|
|
2021-07-08 17:18:36 +00:00
|
|
|
if currentPlaylist != nil {
|
|
|
|
editPlaylistButton
|
|
|
|
}
|
|
|
|
|
2021-07-08 15:14:54 +00:00
|
|
|
newPlaylistButton
|
2021-07-09 14:53:53 +00:00
|
|
|
.padding(.leading, 40)
|
2021-07-08 15:14:54 +00:00
|
|
|
}
|
|
|
|
.scaleEffect(0.85)
|
2021-06-26 09:39:35 +00:00
|
|
|
|
2021-07-07 23:01:54 +00:00
|
|
|
if currentPlaylist != nil {
|
2021-07-09 14:53:53 +00:00
|
|
|
if currentPlaylist!.videos.isEmpty {
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
Text("Playlist is empty\n\nTap and hold on a video and then tap \"Add to Playlist\"")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
.multilineTextAlignment(.center)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
} else {
|
|
|
|
VideosView(videos: currentPlaylist!.videos)
|
|
|
|
}
|
2021-07-07 23:01:54 +00:00
|
|
|
} else {
|
2021-06-26 09:39:35 +00:00
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-08 15:14:54 +00:00
|
|
|
.fullScreenCover(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
|
2021-07-08 17:18:36 +00:00
|
|
|
PlaylistFormView(playlist: $createdPlaylist)
|
|
|
|
}
|
|
|
|
.fullScreenCover(isPresented: $showingEditPlaylist, onDismiss: selectEditedPlaylist) {
|
|
|
|
PlaylistFormView(playlist: $editedPlaylist)
|
2021-07-08 15:14:54 +00:00
|
|
|
}
|
2021-06-28 10:43:07 +00:00
|
|
|
.onAppear {
|
2021-07-08 17:18:36 +00:00
|
|
|
resource.loadIfNeeded()?.onSuccess { _ in
|
|
|
|
selectPlaylist(selectedPlaylistID)
|
|
|
|
}
|
2021-06-26 09:39:35 +00:00
|
|
|
}
|
2021-06-28 10:43:07 +00:00
|
|
|
}
|
2021-06-26 09:39:35 +00:00
|
|
|
|
2021-07-08 17:18:36 +00:00
|
|
|
func selectPlaylist(_ id: String?) {
|
|
|
|
selectedPlaylistID = id
|
|
|
|
}
|
|
|
|
|
2021-07-08 15:14:54 +00:00
|
|
|
func selectCreatedPlaylist() {
|
|
|
|
guard createdPlaylist != nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.load().onSuccess { _ in
|
2021-07-08 17:18:36 +00:00
|
|
|
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 15:14:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 10:43:07 +00:00
|
|
|
var currentPlaylist: Playlist? {
|
2021-07-09 14:53:53 +00:00
|
|
|
store.collection.first { $0.id == selectedPlaylistID } ?? store.collection.first
|
2021-06-26 09:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var selectPlaylistButton: some View {
|
2021-06-28 10:43:07 +00:00
|
|
|
Button(currentPlaylist?.title ?? "Select playlist") {
|
|
|
|
guard currentPlaylist != nil else {
|
2021-06-26 09:39:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-08 17:18:36 +00:00
|
|
|
selectPlaylist(store.collection.next(after: currentPlaylist!)?.id)
|
2021-06-26 09:39:35 +00:00
|
|
|
}
|
|
|
|
.contextMenu {
|
2021-06-28 10:43:07 +00:00
|
|
|
ForEach(store.collection) { playlist in
|
2021-06-26 09:39:35 +00:00
|
|
|
Button(playlist.title) {
|
2021-07-08 17:18:36 +00:00
|
|
|
selectPlaylist(playlist.id)
|
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: {
|
|
|
|
self.editedPlaylist = self.currentPlaylist
|
|
|
|
self.showingEditPlaylist = true
|
|
|
|
}) {
|
2021-07-09 14:53:53 +00:00
|
|
|
HStack(spacing: 8) {
|
|
|
|
Image(systemName: "pencil")
|
|
|
|
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")
|
|
|
|
Text("New Playlist")
|
|
|
|
}
|
2021-07-08 15:14:54 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-26 09:39:35 +00:00
|
|
|
}
|