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?
|
|
|
|
|
2022-08-21 19:35:26 +05:30
|
|
|
@StateObject private var channelPlaylist = Store<ChannelPlaylist>()
|
|
|
|
@StateObject private var userPlaylist = Store<Playlist>()
|
2022-05-22 21:23:12 +05:30
|
|
|
|
2021-10-23 15:43:05 +05:30
|
|
|
@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
|
|
|
|
|
2021-10-22 04:59:10 +05:30
|
|
|
var items: [ContentItem] {
|
2022-05-22 21:23:12 +05:30
|
|
|
var videos = currentPlaylist?.videos ?? []
|
|
|
|
|
|
|
|
if videos.isEmpty {
|
2022-08-21 19:35:26 +05:30
|
|
|
videos = userPlaylist.item?.videos ?? channelPlaylist.item?.videos ?? []
|
|
|
|
|
2022-05-22 21:23:12 +05:30
|
|
|
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? {
|
2022-08-21 19:35:26 +05:30
|
|
|
guard let playlist = currentPlaylist else { return nil }
|
2022-05-22 21:23:12 +05:30
|
|
|
|
|
|
|
let resource = player.accounts.api.playlist(playlist.id)
|
2022-08-21 19:35:26 +05:30
|
|
|
|
|
|
|
if player.accounts.app.userPlaylistsUseChannelPlaylistEndpoint {
|
|
|
|
resource?.addObserver(channelPlaylist)
|
|
|
|
} else {
|
|
|
|
resource?.addObserver(userPlaylist)
|
|
|
|
}
|
2022-05-22 21:23:12 +05:30
|
|
|
|
|
|
|
return resource
|
2021-08-16 21:22:42 +05:30
|
|
|
}
|
2021-07-09 20:23:53 +05:30
|
|
|
|
2021-08-16 21:22:42 +05:30
|
|
|
var body: some View {
|
2022-02-17 01:53:11 +05:30
|
|
|
BrowserPlayerControls(toolbar: {
|
2022-02-04 23:08:29 +05:30
|
|
|
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-11 03:54:56 +05:30
|
|
|
playButton
|
|
|
|
.offset(x: 10)
|
2022-02-04 23:08:29 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
.padding(.horizontal)
|
|
|
|
}) {
|
2021-10-06 01:50:09 +05:30
|
|
|
SignInRequiredView(title: "Playlists") {
|
|
|
|
VStack {
|
2021-09-29 17:19:37 +05:30
|
|
|
#if os(tvOS)
|
2021-10-06 01:50:09 +05:30
|
|
|
toolbar
|
2021-09-29 17:19:37 +05:30
|
|
|
#endif
|
2021-10-06 01:50:09 +05:30
|
|
|
|
2021-10-25 03:06:24 +05:30
|
|
|
if currentPlaylist != nil, items.isEmpty {
|
2021-10-06 01:50:09 +05:30
|
|
|
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)
|
2022-02-04 23:08:29 +05:30
|
|
|
.environment(\.scrollViewBottomPadding, 70)
|
2021-10-25 03:06:24 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
.environment(\.currentPlaylistID, currentPlaylist?.id)
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
2021-06-26 15:09:35 +05:30
|
|
|
}
|
|
|
|
}
|
2022-02-04 23:08:29 +05:30
|
|
|
.onAppear {
|
|
|
|
model.load()
|
2022-06-26 18:48:10 +05:30
|
|
|
resource?.load()
|
2022-02-04 23:08:29 +05:30
|
|
|
}
|
|
|
|
.onChange(of: accounts.current) { _ in
|
|
|
|
model.load(force: true)
|
2022-08-21 19:35:26 +05:30
|
|
|
resource?.load()
|
2022-02-04 23:08:29 +05:30
|
|
|
}
|
2022-08-21 19:35:26 +05:30
|
|
|
.onChange(of: currentPlaylist) { _ in
|
2022-02-04 23:08:29 +05:30
|
|
|
resource?.load()
|
|
|
|
}
|
|
|
|
.onChange(of: model.reloadPlaylists) { _ in
|
|
|
|
resource?.load()
|
|
|
|
}
|
2021-08-16 21:22:42 +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)
|
|
|
|
}
|
2022-02-04 23:08:29 +05:30
|
|
|
.focusScope(focusNamespace)
|
2021-08-16 21:22:42 +05:30
|
|
|
#else
|
2022-02-04 23:08:29 +05:30
|
|
|
.background(
|
|
|
|
EmptyView()
|
|
|
|
.sheet(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
|
|
|
|
PlaylistFormView(playlist: $createdPlaylist)
|
|
|
|
.environmentObject(accounts)
|
2021-11-08 21:59:35 +05:30
|
|
|
}
|
2022-02-04 23:08:29 +05:30
|
|
|
)
|
|
|
|
.background(
|
|
|
|
EmptyView()
|
|
|
|
.sheet(isPresented: $showingEditPlaylist, onDismiss: selectEditedPlaylist) {
|
|
|
|
PlaylistFormView(playlist: $editedPlaylist)
|
|
|
|
.environmentObject(accounts)
|
2022-05-22 21:23:12 +05:30
|
|
|
}
|
2022-02-04 23:08:29 +05:30
|
|
|
)
|
|
|
|
#endif
|
2022-01-05 21:55:57 +05:30
|
|
|
#if os(iOS)
|
2022-02-04 23:08:29 +05:30
|
|
|
.navigationBarTitleDisplayMode(RefreshControl.navigationBarTitleDisplayMode)
|
2022-01-05 21:55:57 +05:30
|
|
|
#endif
|
2022-08-28 23:30:43 +05:30
|
|
|
#if !os(macOS)
|
|
|
|
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
|
|
|
|
model.load()
|
|
|
|
resource?.loadIfNeeded()
|
|
|
|
}
|
|
|
|
#endif
|
2021-08-16 21:22:42 +05:30
|
|
|
}
|
|
|
|
|
2021-09-29 19:59:17 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
var toolbar: some View {
|
|
|
|
HStack {
|
|
|
|
if model.isEmpty {
|
|
|
|
Text("No Playlists")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
} else {
|
|
|
|
Text("Current Playlist")
|
|
|
|
.foregroundColor(.secondary)
|
2021-08-16 21:22:42 +05:30
|
|
|
|
2021-09-29 19:59:17 +05:30
|
|
|
selectPlaylistButton
|
|
|
|
}
|
2021-08-16 21:22:42 +05:30
|
|
|
|
2022-01-03 00:29:57 +05:30
|
|
|
if let playlist = currentPlaylist {
|
2021-09-29 19:59:17 +05:30
|
|
|
editPlaylistButton
|
2021-08-16 21:22:42 +05:30
|
|
|
|
2021-11-02 03:26:18 +05:30
|
|
|
FavoriteButton(item: FavoriteItem(section: .playlist(playlist.id)))
|
|
|
|
.labelStyle(.iconOnly)
|
2022-01-03 00:29:57 +05:30
|
|
|
|
|
|
|
playButton
|
2021-11-02 03:26:18 +05:30
|
|
|
}
|
|
|
|
|
2021-09-29 19:59:17 +05:30
|
|
|
Spacer()
|
2021-09-29 18:06:52 +05:30
|
|
|
|
2021-09-29 19:59:17 +05:30
|
|
|
newPlaylistButton
|
|
|
|
.padding(.leading, 40)
|
|
|
|
}
|
2021-08-16 21:22:42 +05:30
|
|
|
}
|
2021-09-29 19:59:17 +05:30
|
|
|
#endif
|
2021-08-16 21:22:42 +05:30
|
|
|
|
|
|
|
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-12-20 05:06:12 +05:30
|
|
|
.background(Color.secondaryBackground)
|
2021-08-16 21:22:42 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-07-08 20:44:54 +05:30
|
|
|
func selectCreatedPlaylist() {
|
|
|
|
guard createdPlaylist != nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-28 23:36:05 +05:30
|
|
|
model.load(force: true) {
|
|
|
|
if let id = createdPlaylist?.id {
|
2021-10-25 03:06:24 +05:30
|
|
|
selectedPlaylistID = id
|
2021-09-28 23:36:05 +05:30
|
|
|
}
|
2021-07-08 22:48:36 +05:30
|
|
|
|
|
|
|
self.createdPlaylist = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectEditedPlaylist() {
|
2021-09-26 23:10:25 +05:30
|
|
|
if editedPlaylist.isNil {
|
2021-10-25 03:06:24 +05:30
|
|
|
selectedPlaylistID = ""
|
2021-07-08 22:48:36 +05:30
|
|
|
}
|
|
|
|
|
2021-09-28 23:36:05 +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 {
|
2021-08-16 21:22:42 +05:30
|
|
|
#if os(tvOS)
|
2021-10-25 03:06:24 +05:30
|
|
|
Button(currentPlaylist?.title ?? "Select playlist") {
|
|
|
|
guard currentPlaylist != nil else {
|
2021-08-16 21:22:42 +05:30
|
|
|
return
|
|
|
|
}
|
2021-06-26 15:09:35 +05:30
|
|
|
|
2021-10-25 03:06:24 +05:30
|
|
|
selectedPlaylistID = model.all.next(after: currentPlaylist!)?.id ?? ""
|
2021-08-16 21:22:42 +05:30
|
|
|
}
|
2022-08-21 21:10:44 +05:30
|
|
|
.lineLimit(1)
|
2021-08-16 21:22:42 +05:30
|
|
|
.contextMenu {
|
2021-09-28 23:36:05 +05:30
|
|
|
ForEach(model.all) { playlist in
|
2021-08-16 21:22:42 +05:30
|
|
|
Button(playlist.title) {
|
2021-10-25 03:06:24 +05:30
|
|
|
selectedPlaylistID = playlist.id
|
2021-08-16 21:22:42 +05:30
|
|
|
}
|
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
|
|
|
}
|
2021-08-16 21:22:42 +05:30
|
|
|
#else
|
2022-01-05 23:21:19 +05:30
|
|
|
Menu {
|
2021-09-28 23:36:05 +05:30
|
|
|
ForEach(model.all) { playlist in
|
2021-10-25 03:06:24 +05:30
|
|
|
Button(action: { selectedPlaylistID = playlist.id }) {
|
|
|
|
if playlist == currentPlaylist {
|
2021-08-16 21:22:42 +05:30
|
|
|
Label(playlist.title, systemImage: "checkmark")
|
|
|
|
} else {
|
|
|
|
Text(playlist.title)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-05 23:21:19 +05:30
|
|
|
} label: {
|
|
|
|
Text(currentPlaylist?.title ?? "Select playlist")
|
2022-02-04 23:08:29 +05:30
|
|
|
.frame(maxWidth: 140, alignment: .center)
|
2021-08-16 21:22:42 +05:30
|
|
|
}
|
|
|
|
#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
|
|
|
|
}) {
|
2021-07-09 20:23:53 +05:30
|
|
|
HStack(spacing: 8) {
|
2022-02-04 23:08:29 +05:30
|
|
|
Image(systemName: "rectangle.and.pencil.and.ellipsis")
|
2021-07-09 20:23:53 +05:30
|
|
|
}
|
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 }) {
|
2022-02-04 23:08:29 +05:30
|
|
|
HStack(spacing: 0) {
|
2021-07-09 20:23:53 +05:30
|
|
|
Image(systemName: "plus")
|
2022-02-04 23:08:29 +05:30
|
|
|
.padding(8)
|
|
|
|
.contentShape(Rectangle())
|
2021-07-12 02:22:49 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
Text("New Playlist")
|
|
|
|
#endif
|
2021-07-09 20:23:53 +05:30
|
|
|
}
|
2021-07-08 20:44:54 +05:30
|
|
|
}
|
|
|
|
}
|
2021-10-25 03:06:24 +05:30
|
|
|
|
2022-01-03 00:29:57 +05:30
|
|
|
private var playButton: some View {
|
|
|
|
Button {
|
2022-07-11 03:54:56 +05:30
|
|
|
player.playbackMode = .queue
|
2022-01-03 00:29:57 +05:30
|
|
|
player.play(items.compactMap(\.video))
|
|
|
|
} label: {
|
|
|
|
Image(systemName: "play")
|
2022-02-04 23:08:29 +05:30
|
|
|
.padding(8)
|
|
|
|
.contentShape(Rectangle())
|
2022-01-03 00:29:57 +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
|
|
|
}
|
2021-08-16 21:22:42 +05:30
|
|
|
|
|
|
|
struct PlaylistsView_Provider: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
PlaylistsView()
|
2021-09-29 17:15:00 +05:30
|
|
|
.injectFixtureEnvironmentObjects()
|
2021-08-16 21:22:42 +05:30
|
|
|
}
|
|
|
|
}
|