1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30
yattee/Apple TV/PlaylistsView.swift

158 lines
4.4 KiB
Swift
Raw Normal View History

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-06-26 15:09:35 +05:30
var body: some View {
Section {
2021-07-08 04:31:54 +05:30
VStack(alignment: .center, spacing: 2) {
2021-07-12 02:22:49 +05:30
#if os(tvOS)
HStack {
if store.collection.isEmpty {
Text("No Playlists")
.foregroundColor(.secondary)
} else {
Text("Current Playlist")
.foregroundColor(.secondary)
selectPlaylistButton
}
if currentPlaylist != nil {
editPlaylistButton
}
newPlaylistButton
.padding(.leading, 40)
2021-07-08 22:48:36 +05:30
}
2021-07-12 02:22:49 +05:30
.scaleEffect(0.85)
#endif
2021-06-26 15:09:35 +05:30
2021-07-08 04:31:54 +05:30
if currentPlaylist != nil {
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-08 04:31:54 +05:30
} else {
2021-06-26 15:09:35 +05:30
Spacer()
}
}
}
2021-07-12 02:22:49 +05:30
#if !os(macOS)
.fullScreenCover(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
PlaylistFormView(playlist: $createdPlaylist)
}
.fullScreenCover(isPresented: $showingEditPlaylist, onDismiss: selectEditedPlaylist) {
PlaylistFormView(playlist: $editedPlaylist)
}
#endif
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-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? {
store.collection.first { $0.id == selectedPlaylistID } ?? store.collection.first
2021-06-26 15:09:35 +05:30
}
var selectPlaylistButton: some View {
2021-06-28 16:13:07 +05:30
Button(currentPlaylist?.title ?? "Select playlist") {
guard currentPlaylist != nil else {
2021-06-26 15:09:35 +05:30
return
}
2021-07-08 22:48:36 +05:30
selectPlaylist(store.collection.next(after: currentPlaylist!)?.id)
2021-06-26 15:09:35 +05:30
}
.contextMenu {
2021-06-28 16:13:07 +05:30
ForEach(store.collection) { playlist in
2021-06-26 15:09:35 +05:30
Button(playlist.title) {
2021-07-08 22:48:36 +05:30
selectPlaylist(playlist.id)
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
}) {
HStack(spacing: 8) {
Image(systemName: "pencil")
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 }) {
HStack(spacing: 8) {
Image(systemName: "plus")
2021-07-12 02:22:49 +05:30
#if os(tvOS)
Text("New Playlist")
#endif
}
2021-07-08 20:44:54 +05:30
}
}
2021-06-26 15:09:35 +05:30
}