2021-06-26 15:09:35 +05:30
|
|
|
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 {
|
2021-06-27 04:59:55 +05:30
|
|
|
VideosView(videos: selectedPlaylist!.videos)
|
2021-06-26 15:09:35 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.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? {
|
2021-06-26 17:07:24 +05:30
|
|
|
let idx = firstIndex(of: element)
|
|
|
|
|
|
|
|
if idx == nil {
|
|
|
|
return first
|
|
|
|
}
|
|
|
|
|
|
|
|
let next = index(after: idx!)
|
2021-06-26 15:09:35 +05:30
|
|
|
|
|
|
|
return self[next == endIndex ? startIndex : next]
|
|
|
|
}
|
|
|
|
}
|