yattee/Shared/Playlists/PlaylistsView.swift

355 lines
11 KiB
Swift
Raw Normal View History

2021-07-08 17:18:36 +00:00
import Defaults
2021-06-28 10:43:07 +00:00
import Siesta
2021-06-26 09:39:35 +00:00
import SwiftUI
struct PlaylistsView: View {
2021-10-24 21:36:24 +00:00
@State private var selectedPlaylistID: Playlist.ID = ""
2021-07-08 15:14:54 +00:00
@State private var showingNewPlaylist = false
@State private var createdPlaylist: Playlist?
2021-07-08 17:18:36 +00:00
@State private var showingEditPlaylist = false
@State private var editedPlaylist: Playlist?
@StateObject private var channelPlaylist = Store<ChannelPlaylist>()
@StateObject private var userPlaylist = Store<Playlist>()
2022-05-22 15:53:12 +00:00
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<PlayerModel> private var player
@EnvironmentObject<PlaylistsModel> private var model
2021-09-29 12:36:52 +00:00
@Namespace private var focusNamespace
var items: [ContentItem] {
2022-05-22 15:53:12 +00:00
var videos = currentPlaylist?.videos ?? []
if videos.isEmpty {
videos = userPlaylist.item?.videos ?? channelPlaylist.item?.videos ?? []
2022-05-22 15:53:12 +00:00
if !player.accounts.app.userPlaylistsEndpointIncludesVideos {
var i = 0
for index in videos.indices {
var video = videos[index]
video.indexID = "\(i)"
i += 1
videos[index] = video
}
}
}
return ContentItem.array(of: videos)
}
private var resource: Resource? {
guard let playlist = currentPlaylist else { return nil }
2022-05-22 15:53:12 +00:00
let resource = player.accounts.api.playlist(playlist.id)
if player.accounts.app.userPlaylistsUseChannelPlaylistEndpoint {
resource?.addObserver(channelPlaylist)
} else {
resource?.addObserver(userPlaylist)
}
2022-05-22 15:53:12 +00:00
return resource
}
var body: some View {
2022-02-16 20:23:11 +00:00
BrowserPlayerControls(toolbar: {
2022-02-04 17:38:29 +00:00
HStack {
HStack {
newPlaylistButton
.offset(x: -10)
if currentPlaylist != nil {
editPlaylistButton
}
}
if !model.isEmpty {
Spacer()
}
HStack {
if model.isEmpty {
Text("No Playlists")
.foregroundColor(.secondary)
} else {
selectPlaylistButton
.transaction { t in t.animation = .none }
}
}
Spacer()
if currentPlaylist != nil {
2022-07-10 22:24:56 +00:00
playButton
.offset(x: 10)
2022-02-04 17:38:29 +00:00
}
}
.padding(.horizontal)
}) {
2022-09-04 15:28:30 +00:00
SignInRequiredView(title: "Playlists".localized()) {
ScrollView {
VStack {
#if os(tvOS)
toolbar
#endif
if currentPlaylist != nil, items.isEmpty {
hintText("Playlist is empty\n\nTap and hold on a video and then \n\"Add to Playlist\"".localized())
} else if model.all.isEmpty {
hintText("You have no playlists\n\nTap on \"New Playlist\" to create one".localized())
} else {
Group {
#if os(tvOS)
HorizontalCells(items: items)
.padding(.top, 40)
Spacer()
#else
VerticalCells(items: items)
.environment(\.scrollViewBottomPadding, 70)
#endif
}
.environment(\.currentPlaylistID, currentPlaylist?.id)
2021-10-24 21:36:24 +00:00
}
}
2021-09-25 08:18:22 +00:00
}
2021-06-26 09:39:35 +00:00
}
}
2022-02-04 17:38:29 +00:00
.onAppear {
model.load()
2022-06-26 13:18:10 +00:00
resource?.load()
2022-02-04 17:38:29 +00:00
}
.onChange(of: accounts.current) { _ in
model.load(force: true)
resource?.load()
2022-02-04 17:38:29 +00:00
}
.onChange(of: currentPlaylist) { _ in
2022-09-12 15:23:00 +00:00
channelPlaylist.clear()
userPlaylist.clear()
2022-02-04 17:38:29 +00:00
resource?.load()
}
.onChange(of: model.reloadPlaylists) { _ in
resource?.load()
}
#if os(iOS)
.refreshControl { refreshControl in
model.load(force: true) {
model.reloadPlaylists.toggle()
refreshControl.endRefreshing()
}
}
.backport
.refreshable {
DispatchQueue.main.async {
model.load(force: true) { model.reloadPlaylists.toggle() }
}
}
.navigationBarTitleDisplayMode(RefreshControl.navigationBarTitleDisplayMode)
#endif
#if os(tvOS)
2021-11-08 16:29:35 +00:00
.fullScreenCover(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
PlaylistFormView(playlist: $createdPlaylist)
.environmentObject(accounts)
}
.fullScreenCover(isPresented: $showingEditPlaylist, onDismiss: selectEditedPlaylist) {
PlaylistFormView(playlist: $editedPlaylist)
.environmentObject(accounts)
}
2022-02-04 17:38:29 +00:00
.focusScope(focusNamespace)
#else
2022-02-04 17:38:29 +00:00
.background(
EmptyView()
.sheet(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
PlaylistFormView(playlist: $createdPlaylist)
.environmentObject(accounts)
2021-11-08 16:29:35 +00:00
}
2022-02-04 17:38:29 +00:00
)
.background(
EmptyView()
.sheet(isPresented: $showingEditPlaylist, onDismiss: selectEditedPlaylist) {
PlaylistFormView(playlist: $editedPlaylist)
.environmentObject(accounts)
2022-05-22 15:53:12 +00:00
}
2022-02-04 17:38:29 +00:00
)
#endif
2022-08-28 18:00:43 +00:00
#if !os(macOS)
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
model.load()
resource?.loadIfNeeded()
}
#endif
#if !os(tvOS)
.background(
Button("Refresh") {
resource?.load()
}
.keyboardShortcut("r")
.opacity(0)
)
#endif
}
#if os(tvOS)
var toolbar: some View {
HStack {
if model.isEmpty {
Text("No Playlists")
.foregroundColor(.secondary)
} else {
Text("Current Playlist")
.foregroundColor(.secondary)
selectPlaylistButton
}
if let playlist = currentPlaylist {
editPlaylistButton
2021-11-01 21:56:18 +00:00
FavoriteButton(item: FavoriteItem(section: .playlist(playlist.id)))
.labelStyle(.iconOnly)
playButton
2021-11-01 21:56:18 +00:00
}
Spacer()
2021-09-29 12:36:52 +00:00
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)
.background(Color.secondaryBackground)
#endif
}
2021-07-08 15:14:54 +00:00
func selectCreatedPlaylist() {
guard createdPlaylist != nil else {
return
}
model.load(force: true) {
if let id = createdPlaylist?.id {
2021-10-24 21:36:24 +00:00
selectedPlaylistID = id
}
2021-07-08 17:18:36 +00:00
self.createdPlaylist = nil
}
}
func selectEditedPlaylist() {
if editedPlaylist.isNil {
2021-10-24 21:36:24 +00:00
selectedPlaylistID = ""
2021-07-08 17:18:36 +00:00
}
model.load(force: true) {
2021-10-24 21:36:24 +00:00
self.selectedPlaylistID = editedPlaylist?.id ?? ""
2021-07-08 17:18:36 +00:00
self.editedPlaylist = nil
2021-07-08 15:14:54 +00:00
}
}
2021-06-26 09:39:35 +00:00
var selectPlaylistButton: some View {
#if os(tvOS)
2021-10-24 21:36:24 +00:00
Button(currentPlaylist?.title ?? "Select playlist") {
guard currentPlaylist != nil else {
return
}
2021-06-26 09:39:35 +00:00
2021-10-24 21:36:24 +00:00
selectedPlaylistID = model.all.next(after: currentPlaylist!)?.id ?? ""
}
2022-08-21 15:40:44 +00:00
.lineLimit(1)
.contextMenu {
ForEach(model.all) { playlist in
Button(playlist.title) {
2021-10-24 21:36:24 +00:00
selectedPlaylistID = playlist.id
}
2021-06-26 09:39:35 +00:00
}
2021-09-29 12:36:52 +00:00
Button("Cancel", role: .cancel) {}
2021-06-26 09:39:35 +00:00
}
#else
2022-01-05 17:51:19 +00:00
Menu {
ForEach(model.all) { playlist in
2021-10-24 21:36:24 +00:00
Button(action: { selectedPlaylistID = playlist.id }) {
if playlist == currentPlaylist {
Label(playlist.title, systemImage: "checkmark")
} else {
Text(playlist.title)
}
}
}
2022-01-05 17:51:19 +00:00
} label: {
Text(currentPlaylist?.title ?? "Select playlist")
2022-02-04 17:38:29 +00:00
.frame(maxWidth: 140, alignment: .center)
}
#endif
2021-06-26 09:39:35 +00:00
}
2021-07-08 15:14:54 +00:00
2021-07-08 17:18:36 +00:00
var editPlaylistButton: some View {
Button(action: {
2021-10-24 21:36:24 +00:00
self.editedPlaylist = self.currentPlaylist
2021-07-08 17:18:36 +00:00
self.showingEditPlaylist = true
}) {
HStack(spacing: 8) {
2022-02-04 17:38:29 +00:00
Image(systemName: "rectangle.and.pencil.and.ellipsis")
}
2021-07-08 17:18:36 +00:00
}
}
2021-07-08 15:14:54 +00:00
var newPlaylistButton: some View {
Button(action: { self.showingNewPlaylist = true }) {
2022-02-04 17:38:29 +00:00
HStack(spacing: 0) {
Image(systemName: "plus")
2022-02-04 17:38:29 +00:00
.padding(8)
.contentShape(Rectangle())
2021-07-11 20:52:49 +00:00
#if os(tvOS)
Text("New Playlist")
#endif
}
2021-07-08 15:14:54 +00:00
}
}
2021-10-24 21:36:24 +00:00
private var playButton: some View {
Button {
player.play(items.compactMap(\.video))
} label: {
Image(systemName: "play")
2022-02-04 17:38:29 +00:00
.padding(8)
.contentShape(Rectangle())
}
2022-09-04 15:23:02 +00:00
.contextMenu {
Button {
player.play(items.compactMap(\.video), shuffling: true)
} label: {
Label("Shuffle", systemImage: "shuffle")
.padding(8)
.contentShape(Rectangle())
}
}
}
2021-10-24 21:36:24 +00:00
private var currentPlaylist: Playlist? {
model.find(id: selectedPlaylistID) ?? model.all.first
}
2021-06-26 09:39:35 +00:00
}
struct PlaylistsView_Provider: PreviewProvider {
static var previews: some View {
PlaylistsView()
2021-09-29 11:45:00 +00:00
.injectFixtureEnvironmentObjects()
}
}