1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-14 22:30:32 +05:30
yattee/Shared/Playlists/PlaylistsView.swift

273 lines
8.7 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-10-25 03:06:24 +05:30
@State private var selectedPlaylistID: Playlist.ID = ""
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?
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<PlayerModel> private var player
@EnvironmentObject<PlaylistsModel> private var model
2021-09-29 18:06:52 +05:30
@Namespace private var focusNamespace
var items: [ContentItem] {
2021-10-25 03:06:24 +05:30
ContentItem.array(of: currentPlaylist?.videos ?? [])
}
var body: some View {
PlayerControlsView {
SignInRequiredView(title: "Playlists") {
VStack {
#if os(tvOS)
toolbar
#endif
2021-10-25 03:06:24 +05:30
if currentPlaylist != nil, items.isEmpty {
hintText("Playlist is empty\n\nTap and hold on a video and then tap \"Add to Playlist\"")
} else if model.all.isEmpty {
hintText("You have no playlists\n\nTap on \"New Playlist\" to create one")
} else {
2021-10-25 03:06:24 +05:30
Group {
#if os(tvOS)
HorizontalCells(items: items)
.padding(.top, 40)
Spacer()
#else
VerticalCells(items: items)
#endif
}
.environment(\.currentPlaylistID, currentPlaylist?.id)
}
2021-09-25 13:48:22 +05:30
}
2021-06-26 15:09:35 +05:30
}
}
#if os(tvOS)
2021-11-08 21:59:35 +05:30
.fullScreenCover(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
PlaylistFormView(playlist: $createdPlaylist)
.environmentObject(accounts)
}
.fullScreenCover(isPresented: $showingEditPlaylist, onDismiss: selectEditedPlaylist) {
PlaylistFormView(playlist: $editedPlaylist)
.environmentObject(accounts)
}
#else
2021-11-08 21:59:35 +05:30
.sheet(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
PlaylistFormView(playlist: $createdPlaylist)
.environmentObject(accounts)
}
.sheet(isPresented: $showingEditPlaylist, onDismiss: selectEditedPlaylist) {
PlaylistFormView(playlist: $editedPlaylist)
.environmentObject(accounts)
}
2021-07-12 02:22:49 +05:30
#endif
2021-11-08 21:59:35 +05:30
.toolbar {
ToolbarItemGroup {
#if !os(iOS)
if !model.isEmpty {
2021-11-28 20:07:55 +05:30
if #available(macOS 12.0, *) {
selectPlaylistButton
.prefersDefaultFocus(in: focusNamespace)
} else {
selectPlaylistButton
}
2021-11-08 21:59:35 +05:30
}
2021-11-08 21:59:35 +05:30
if currentPlaylist != nil {
editPlaylistButton
}
#endif
2021-11-02 03:26:18 +05:30
2021-11-28 20:07:55 +05:30
FavoriteButton(item: FavoriteItem(section: .playlist(selectedPlaylistID)))
2021-11-08 21:59:35 +05:30
}
2021-09-25 13:48:22 +05:30
2021-11-08 21:59:35 +05:30
#if os(iOS)
ToolbarItemGroup(placement: .bottomBar) {
Group {
if model.isEmpty {
Text("No Playlists")
.foregroundColor(.secondary)
} else {
Text("Current Playlist")
.foregroundColor(.secondary)
2021-09-25 13:48:22 +05:30
2021-11-08 21:59:35 +05:30
selectPlaylistButton
}
2021-09-25 13:48:22 +05:30
2021-11-08 21:59:35 +05:30
Spacer()
2021-09-25 13:48:22 +05:30
2021-11-28 20:07:55 +05:30
newPlaylistButton
2021-11-08 21:59:35 +05:30
if currentPlaylist != nil {
editPlaylistButton
}
}
.transaction { t in t.animation = .none }
2021-09-25 13:48:22 +05:30
}
2021-11-08 21:59:35 +05:30
#endif
2021-09-25 13:48:22 +05:30
}
#if os(tvOS)
2021-11-08 21:59:35 +05:30
.focusScope(focusNamespace)
#endif
2021-11-08 21:59:35 +05:30
.onAppear {
model.load()
}
.onChange(of: accounts.current) { _ in
model.load(force: true)
}
}
#if os(tvOS)
var toolbar: some View {
HStack {
if model.isEmpty {
Text("No Playlists")
.foregroundColor(.secondary)
} else {
Text("Current Playlist")
.foregroundColor(.secondary)
selectPlaylistButton
}
Button {
player.playAll(items.compactMap(\.video))
player.presentPlayer()
} label: {
HStack(spacing: 15) {
Image(systemName: "play.fill")
Text("Play All")
}
}
2021-10-25 03:06:24 +05:30
if currentPlaylist != nil {
editPlaylistButton
}
2021-11-02 03:26:18 +05:30
if let playlist = currentPlaylist {
FavoriteButton(item: FavoriteItem(section: .playlist(playlist.id)))
.labelStyle(.iconOnly)
}
Spacer()
2021-09-29 18:06:52 +05:30
newPlaylistButton
.padding(.leading, 40)
}
}
#endif
func hintText(_ text: String) -> some View {
VStack {
Spacer()
Text(text)
.foregroundColor(.secondary)
.multilineTextAlignment(.center)
Spacer()
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
#if os(macOS)
2021-11-28 20:07:55 +05:30
.background(Color.tertiaryBackground)
#endif
}
2021-07-08 20:44:54 +05:30
func selectCreatedPlaylist() {
guard createdPlaylist != nil else {
return
}
model.load(force: true) {
if let id = createdPlaylist?.id {
2021-10-25 03:06:24 +05:30
selectedPlaylistID = id
}
2021-07-08 22:48:36 +05:30
self.createdPlaylist = nil
}
}
func selectEditedPlaylist() {
if editedPlaylist.isNil {
2021-10-25 03:06:24 +05:30
selectedPlaylistID = ""
2021-07-08 22:48:36 +05:30
}
model.load(force: true) {
2021-10-25 03:06:24 +05:30
self.selectedPlaylistID = editedPlaylist?.id ?? ""
2021-07-08 22:48:36 +05:30
self.editedPlaylist = nil
2021-07-08 20:44:54 +05:30
}
}
2021-06-26 15:09:35 +05:30
var selectPlaylistButton: some View {
#if os(tvOS)
2021-10-25 03:06:24 +05:30
Button(currentPlaylist?.title ?? "Select playlist") {
guard currentPlaylist != nil else {
return
}
2021-06-26 15:09:35 +05:30
2021-10-25 03:06:24 +05:30
selectedPlaylistID = model.all.next(after: currentPlaylist!)?.id ?? ""
}
.contextMenu {
ForEach(model.all) { playlist in
Button(playlist.title) {
2021-10-25 03:06:24 +05:30
selectedPlaylistID = playlist.id
}
2021-06-26 15:09:35 +05:30
}
2021-09-29 18:06:52 +05:30
Button("Cancel", role: .cancel) {}
2021-06-26 15:09:35 +05:30
}
#else
2021-10-25 03:06:24 +05:30
Menu(currentPlaylist?.title ?? "Select playlist") {
ForEach(model.all) { playlist in
2021-10-25 03:06:24 +05:30
Button(action: { selectedPlaylistID = playlist.id }) {
if playlist == currentPlaylist {
Label(playlist.title, systemImage: "checkmark")
} else {
Text(playlist.title)
}
}
}
}
#endif
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: {
2021-10-25 03:06:24 +05:30
self.editedPlaylist = self.currentPlaylist
2021-07-08 22:48:36 +05:30
self.showingEditPlaylist = true
}) {
HStack(spacing: 8) {
Image(systemName: "slider.horizontal.3")
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-10-25 03:06:24 +05:30
private var currentPlaylist: Playlist? {
model.find(id: selectedPlaylistID) ?? model.all.first
}
2021-06-26 15:09:35 +05:30
}
struct PlaylistsView_Provider: PreviewProvider {
static var previews: some View {
PlaylistsView()
2021-09-29 17:15:00 +05:30
.injectFixtureEnvironmentObjects()
}
}