2022-06-18 12:39:49 +00:00
|
|
|
import Defaults
|
2021-09-25 08:18:22 +00:00
|
|
|
import Foundation
|
|
|
|
import Siesta
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
final class PlaylistsModel: ObservableObject {
|
2022-11-24 20:36:05 +00:00
|
|
|
static var shared = PlaylistsModel()
|
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
@Published var playlists = [Playlist]()
|
2022-05-21 22:29:51 +00:00
|
|
|
@Published var reloadPlaylists = false
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
var accounts = AccountsModel.shared
|
2021-10-16 22:48:58 +00:00
|
|
|
|
2021-09-28 18:06:05 +00:00
|
|
|
init(_ playlists: [Playlist] = [Playlist]()) {
|
|
|
|
self.playlists = playlists
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var all: [Playlist] {
|
|
|
|
playlists.sorted { $0.title.lowercased() < $1.title.lowercased() }
|
|
|
|
}
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
var lastUsed: Playlist? {
|
|
|
|
find(id: Defaults[.lastUsedPlaylistID])
|
|
|
|
}
|
|
|
|
|
2021-10-25 21:29:06 +00:00
|
|
|
func find(id: Playlist.ID?) -> Playlist? {
|
|
|
|
if id.isNil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return playlists.first { $0.id == id! }
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var isEmpty: Bool {
|
|
|
|
playlists.isEmpty
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 18:06:05 +00:00
|
|
|
func load(force: Bool = false, onSuccess: @escaping () -> Void = {}) {
|
2021-12-19 22:27:20 +00:00
|
|
|
guard accounts.app.supportsUserPlaylists, accounts.signedIn else {
|
2021-11-14 23:06:01 +00:00
|
|
|
playlists = []
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-23 10:13:05 +00:00
|
|
|
let request = force ? resource?.load() : resource?.loadIfNeeded()
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-10-25 21:29:06 +00:00
|
|
|
guard !request.isNil else {
|
|
|
|
onSuccess()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
request?
|
|
|
|
.onSuccess { resource in
|
|
|
|
if let playlists: [Playlist] = resource.typedContent() {
|
|
|
|
self.playlists = playlists
|
2021-09-28 18:06:05 +00:00
|
|
|
onSuccess()
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-04 15:24:07 +00:00
|
|
|
.onFailure { error in
|
2021-09-25 08:18:22 +00:00
|
|
|
self.playlists = []
|
2022-09-04 15:24:07 +00:00
|
|
|
NavigationModel.shared.presentAlert(title: "Could not refresh Playlists", message: error.userMessage)
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
|
2021-12-17 19:53:05 +00:00
|
|
|
func addVideo(
|
|
|
|
playlistID: Playlist.ID,
|
|
|
|
videoID: Video.ID,
|
|
|
|
onSuccess: @escaping () -> Void = {},
|
2022-06-18 12:39:49 +00:00
|
|
|
onFailure: ((RequestError) -> Void)? = nil
|
2021-12-17 19:53:05 +00:00
|
|
|
) {
|
2022-06-18 12:39:49 +00:00
|
|
|
accounts.api.addVideoToPlaylist(
|
|
|
|
videoID,
|
|
|
|
playlistID,
|
|
|
|
onFailure: onFailure ?? { requestError in
|
2022-11-24 20:36:05 +00:00
|
|
|
NavigationModel.shared.presentAlert(
|
2022-06-18 12:39:49 +00:00
|
|
|
title: "Error when adding to playlist",
|
|
|
|
message: "(\(requestError.httpStatusCode ?? -1)) \(requestError.userMessage)"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
) {
|
2022-05-21 22:36:15 +00:00
|
|
|
self.load(force: true) {
|
|
|
|
self.reloadPlaylists.toggle()
|
|
|
|
onSuccess()
|
|
|
|
}
|
2022-05-21 22:29:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
|
2022-05-21 22:29:51 +00:00
|
|
|
func removeVideo(index: String, playlistID: Playlist.ID, onSuccess: @escaping () -> Void = {}) {
|
|
|
|
accounts.api.removeVideoFromPlaylist(index, playlistID, onFailure: { _ in }) {
|
|
|
|
self.load(force: true) {
|
|
|
|
self.reloadPlaylists.toggle()
|
2021-12-17 19:53:05 +00:00
|
|
|
onSuccess()
|
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 10:13:05 +00:00
|
|
|
private var resource: Resource? {
|
|
|
|
accounts.api.playlists
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|