yattee/Apple TV/PlaylistsView.swift
2021-06-27 01:29:55 +02:00

76 lines
1.8 KiB
Swift

import SwiftUI
struct PlaylistsView: View {
@EnvironmentObject private var state: AppState
@ObservedObject private var provider = PlaylistsProvider()
@State private var selectedPlaylist: Playlist?
var body: some View {
Section {
VStack(alignment: .leading, spacing: 2) {
HStack(alignment: .top) {
Spacer()
selectPlaylistButton
Spacer()
}
.padding(.bottom, 5)
VStack {
if selectedPlaylist != nil {
VideosView(videos: selectedPlaylist!.videos)
}
}
}
}.task {
Task {
provider.load { playlists in
selectedPlaylist = playlists.first
}
}
}
}
var playlists: [Playlist] {
if provider.playlists.isEmpty {
provider.load()
}
return provider.playlists
}
var selectPlaylistButton: some View {
Button(selectedPlaylist?.title ?? "Select playlist") {
guard selectedPlaylist != nil else {
return
}
selectedPlaylist = playlists.next(after: selectedPlaylist!)
}
.contextMenu {
ForEach(provider.playlists) { playlist in
Button(playlist.title) {
selectedPlaylist = playlist
}
}
}
}
}
extension Array where Element: Equatable {
func next(after element: Element) -> Element? {
let idx = firstIndex(of: element)
if idx == nil {
return first
}
let next = index(after: idx!)
return self[next == endIndex ? startIndex : next]
}
}