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

431 lines
13 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?
@StateObject private var channelPlaylist = Store<ChannelPlaylist>()
@StateObject private var userPlaylist = Store<Playlist>()
2022-05-22 21:23:12 +05:30
@ObservedObject private var accounts = AccountsModel.shared
@ObservedObject private var model = PlaylistsModel.shared
2022-12-16 14:05:10 +05:30
private var player = PlayerModel.shared
2022-12-11 22:34:39 +05:30
private var cache = PlaylistsCacheModel.shared
2021-09-29 18:06:52 +05:30
@Namespace private var focusNamespace
2022-12-12 05:48:29 +05:30
@Default(.playlistListingStyle) private var playlistListingStyle
2022-12-13 16:39:20 +05:30
@Default(.showCacheStatus) private var showCacheStatus
2022-12-12 05:48:29 +05:30
var items: [ContentItem] {
2022-05-22 21:23:12 +05:30
var videos = currentPlaylist?.videos ?? []
if videos.isEmpty {
videos = userPlaylist.item?.videos ?? channelPlaylist.item?.videos ?? []
if !accounts.app.userPlaylistsEndpointIncludesVideos {
2022-05-22 21:23:12 +05:30
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 21:23:12 +05:30
let resource = accounts.api.playlist(playlist.id)
if accounts.app.userPlaylistsUseChannelPlaylistEndpoint {
resource?.addObserver(channelPlaylist)
} else {
resource?.addObserver(userPlaylist)
}
2022-05-22 21:23:12 +05:30
return resource
}
var body: some View {
2022-12-11 03:07:14 +05:30
SignInRequiredView(title: "Playlists".localized()) {
VStack {
VerticalCells(items: items, allowEmpty: true) { if shouldDisplayHeader { header } }
.environment(\.currentPlaylistID, currentPlaylist?.id)
.environment(\.listingStyle, playlistListingStyle)
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())
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-12-11 23:14:55 +05:30
loadResource()
2022-02-04 23:08:29 +05:30
}
.onChange(of: accounts.current) { _ in
model.load(force: true)
2022-12-11 23:14:55 +05:30
loadResource()
2022-02-04 23:08:29 +05:30
}
.onChange(of: currentPlaylist) { _ in
2022-09-12 20:53:00 +05:30
channelPlaylist.clear()
userPlaylist.clear()
2022-12-11 23:14:55 +05:30
loadResource()
2022-02-04 23:08:29 +05:30
}
.onChange(of: model.reloadPlaylists) { _ in
2022-12-11 23:14:55 +05:30
loadResource()
2022-02-04 23:08:29 +05:30
}
#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() }
}
}
2022-12-11 02:17:38 +05:30
.navigationBarTitleDisplayMode(.inline)
.toolbar {
2022-12-16 14:05:10 +05:30
ToolbarItem {
RequestErrorButton(error: model.error)
}
2022-12-11 02:17:38 +05:30
ToolbarItem(placement: .principal) {
playlistsMenu
}
}
#endif
#if os(tvOS)
2021-11-08 21:59:35 +05:30
.fullScreenCover(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
PlaylistFormView(playlist: $createdPlaylist)
}
.fullScreenCover(isPresented: $showingEditPlaylist, onDismiss: selectEditedPlaylist) {
PlaylistFormView(playlist: $editedPlaylist)
}
2022-02-04 23:08:29 +05:30
.focusScope(focusNamespace)
#else
2022-02-04 23:08:29 +05:30
.background(
EmptyView()
.sheet(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
PlaylistFormView(playlist: $createdPlaylist)
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)
2022-05-22 21:23:12 +05:30
}
2022-02-04 23:08:29 +05:30
)
#endif
2022-12-12 05:48:29 +05:30
#if os(macOS)
.toolbar {
ToolbarItem {
ListingStyleButtons(listingStyle: $playlistListingStyle)
}
ToolbarItem {
HideWatchedButtons()
}
2023-02-25 21:12:18 +05:30
ToolbarItem {
2023-05-23 22:24:53 +05:30
HideShortsButtons()
2023-02-25 21:12:18 +05:30
}
2022-08-28 23:30:43 +05:30
}
2022-12-12 05:48:29 +05:30
#else
2023-05-08 01:15:18 +05:30
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
2022-12-12 05:48:29 +05:30
model.load()
loadResource()
}
2022-08-28 23:30:43 +05:30
#endif
#if !os(tvOS)
.background(
Button("Refresh") {
resource?.load()
}
.keyboardShortcut("r")
.opacity(0)
)
#endif
}
2022-12-11 23:14:55 +05:30
func loadResource() {
loadCachedResource()
resource?.load()
.onSuccess { response in
if let playlist: Playlist = response.typedContent() {
ChannelPlaylistsCacheModel.shared.storePlaylist(playlist: playlist.channelPlaylist)
}
}
}
func loadCachedResource() {
if !selectedPlaylistID.isEmpty,
2022-12-17 01:07:05 +05:30
let currentPlaylist,
let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(currentPlaylist.channelPlaylist)
2022-12-11 23:14:55 +05:30
{
DispatchQueue.main.async {
self.channelPlaylist.replace(cache)
}
}
}
2022-12-11 02:17:38 +05:30
#if os(iOS)
var playlistsMenu: some View {
2022-12-16 03:33:04 +05:30
let title = currentPlaylist?.title ?? "Playlists"
return Menu {
Menu {
selectPlaylistButton
} label: {
Label(title, systemImage: "list.and.film")
}
2022-12-11 02:17:38 +05:30
Section {
if let currentPlaylist {
2022-12-16 03:33:04 +05:30
playButtons
2022-12-11 02:17:38 +05:30
editPlaylistButton
2022-12-12 04:00:28 +05:30
if let account = accounts.current {
FavoriteButton(item: FavoriteItem(section: .playlist(account.id, currentPlaylist.id)))
2022-12-16 17:01:22 +05:30
.id(currentPlaylist.id)
2022-12-12 04:00:28 +05:30
}
2022-12-11 02:17:38 +05:30
}
}
2022-12-11 20:48:12 +05:30
if accounts.signedIn {
newPlaylistButton
}
2022-12-12 03:45:56 +05:30
2022-12-12 05:48:29 +05:30
ListingStyleButtons(listingStyle: $playlistListingStyle)
2023-02-25 21:12:18 +05:30
Section {
HideWatchedButtons()
2023-05-23 22:24:53 +05:30
HideShortsButtons()
2023-02-25 21:12:18 +05:30
}
2022-12-12 03:45:56 +05:30
Section {
SettingsButtons()
}
2022-12-11 02:17:38 +05:30
} label: {
HStack(spacing: 12) {
2022-12-12 05:48:29 +05:30
HStack(spacing: 6) {
Image(systemName: "list.and.film")
2022-12-16 03:33:04 +05:30
Text(title)
2022-12-12 05:48:29 +05:30
.font(.headline)
}
.foregroundColor(.primary)
2022-12-11 02:17:38 +05:30
Image(systemName: "chevron.down.circle.fill")
.foregroundColor(.accentColor)
}
2022-12-12 05:48:29 +05:30
.imageScale(.small)
2022-12-11 20:48:12 +05:30
.lineLimit(1)
2022-12-12 05:48:29 +05:30
.frame(maxWidth: 320)
2022-12-11 02:17:38 +05:30
.transaction { t in t.animation = nil }
}
2022-12-11 20:48:12 +05:30
.disabled(!accounts.signedIn)
2022-12-11 02:17:38 +05:30
}
#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 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)
Button {
2021-10-25 03:06:24 +05:30
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 ?? ""
} label: {
Text(currentPlaylist?.title ?? "Select playlist")
.frame(maxWidth: .infinity)
}
2022-08-21 21:10:44 +05:30
.lineLimit(1)
.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
2022-12-11 02:17:38 +05:30
Picker("Current Playlist", selection: $selectedPlaylistID) {
ForEach(model.all) { playlist in
2022-12-11 02:17:38 +05:30
Text(playlist.title).tag(playlist.id)
}
}
#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
}) {
2022-12-11 02:17:38 +05:30
Label("Edit Playlist", systemImage: "rectangle.and.pencil.and.ellipsis")
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-12-11 02:17:38 +05:30
Label("New Playlist", systemImage: "plus")
2021-07-08 20:44:54 +05:30
}
}
2021-10-25 03:06:24 +05:30
2022-12-16 03:33:04 +05:30
private var playButtons: some View {
Group {
Button {
player.play(items.compactMap(\.video))
} label: {
Label("Play", systemImage: "play")
}
2022-09-04 20:53:02 +05:30
Button {
player.play(items.compactMap(\.video), shuffling: true)
} label: {
Label("Shuffle", systemImage: "shuffle")
}
}
}
2021-10-25 03:06:24 +05:30
private var currentPlaylist: Playlist? {
2022-12-11 02:17:38 +05:30
if selectedPlaylistID.isEmpty {
DispatchQueue.main.async {
self.selectedPlaylistID = model.all.first?.id ?? ""
}
}
return model.find(id: selectedPlaylistID) ?? model.all.first
2021-10-25 03:06:24 +05:30
}
var shouldDisplayHeader: Bool {
#if os(tvOS)
true
#else
showCacheStatus
#endif
}
var header: some View {
HStack {
2023-05-22 19:03:13 +05:30
#if os(tvOS)
if model.isEmpty {
Text("No Playlists")
.foregroundColor(.secondary)
} else {
selectPlaylistButton
}
2023-05-22 19:03:13 +05:30
if let playlist = currentPlaylist {
editPlaylistButton
2023-05-22 19:03:13 +05:30
FavoriteButton(item: FavoriteItem(section: .playlist(accounts.current.id, playlist.id)))
.labelStyle(.iconOnly)
2023-05-22 19:03:13 +05:30
playButtons
}
2023-05-22 19:03:13 +05:30
newPlaylistButton
2023-05-22 19:03:13 +05:30
Spacer()
2023-05-22 19:03:13 +05:30
ListingStyleButtons(listingStyle: $playlistListingStyle)
HideWatchedButtons()
2023-05-23 22:24:53 +05:30
HideShortsButtons()
2023-05-22 19:03:13 +05:30
#else
Spacer()
#endif
if let account = accounts.current, showCacheStatus {
CacheStatusHeader(
refreshTime: cache.getFormattedPlaylistTime(account: account),
isLoading: model.isLoading
)
}
2023-05-22 19:03:13 +05:30
#if os(tvOS)
Button {
model.load(force: true)
loadResource()
} label: {
Label("Refresh", systemImage: "arrow.clockwise")
.labelStyle(.iconOnly)
}
#endif
}
.labelStyle(.iconOnly)
.font(.caption)
.imageScale(.small)
#if os(tvOS)
2023-05-22 19:03:13 +05:30
.padding(.leading, 30)
.padding(.bottom, 15)
.padding(.trailing, 30)
#endif
}
2021-06-26 15:09:35 +05:30
}
struct PlaylistsView_Provider: PreviewProvider {
static var previews: some View {
2022-12-11 02:17:38 +05:30
NavigationView {
PlaylistsView()
}
}
}