yattee/Apple TV/PlaylistsView.swift

56 lines
1.3 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-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) {
selectPlaylistButton
.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-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-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
}
}
}
}
}