yattee/Apple TV/PlaylistsView.swift

82 lines
2.0 KiB
Swift
Raw Normal View History

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
@State private var selectedPlaylist: Playlist?
2021-07-08 15:14:54 +00:00
@State private var showingNewPlaylist = false
@State private var createdPlaylist: 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 {
selectPlaylistButton
newPlaylistButton
}
.scaleEffect(0.85)
2021-06-26 09:39:35 +00:00
2021-07-07 23:01:54 +00:00
if currentPlaylist != nil {
VideosView(videos: currentPlaylist!.videos)
} else {
2021-06-26 09:39:35 +00:00
Spacer()
}
}
}
2021-07-08 15:14:54 +00:00
.fullScreenCover(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
NewPlaylistView(createdPlaylist: $createdPlaylist)
}
2021-06-28 10:43:07 +00:00
.onAppear {
resource.loadIfNeeded()
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 15:14:54 +00:00
func selectCreatedPlaylist() {
guard createdPlaylist != nil else {
return
}
resource.load().onSuccess { _ in
self.selectedPlaylist = store.collection.first { $0 == createdPlaylist }
}
}
2021-06-28 10:43:07 +00:00
var currentPlaylist: Playlist? {
selectedPlaylist ?? 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-06-28 10:43:07 +00:00
selectedPlaylist = store.collection.next(after: currentPlaylist!)
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) {
selectedPlaylist = playlist
}
}
}
}
2021-07-08 15:14:54 +00:00
var newPlaylistButton: some View {
Button(action: { self.showingNewPlaylist = true }) {
Image(systemName: "plus")
}
}
2021-06-26 09:39:35 +00:00
}