2022-06-18 18:09:49 +05:30
|
|
|
import Defaults
|
2021-09-25 13:48:22 +05:30
|
|
|
import Foundation
|
|
|
|
import Siesta
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
final class PlaylistsModel: ObservableObject {
|
2022-11-25 02:06:05 +05:30
|
|
|
static var shared = PlaylistsModel()
|
|
|
|
|
2022-12-11 22:34:39 +05:30
|
|
|
@Published var isLoading = false
|
2021-09-25 13:48:22 +05:30
|
|
|
@Published var playlists = [Playlist]()
|
2022-05-22 03:59:51 +05:30
|
|
|
@Published var reloadPlaylists = false
|
2022-12-16 14:05:10 +05:30
|
|
|
@Published var error: RequestError?
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2022-11-25 02:06:05 +05:30
|
|
|
var accounts = AccountsModel.shared
|
2021-10-17 04:18:58 +05:30
|
|
|
|
2021-09-28 23:36:05 +05:30
|
|
|
init(_ playlists: [Playlist] = [Playlist]()) {
|
|
|
|
self.playlists = playlists
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var all: [Playlist] {
|
|
|
|
playlists.sorted { $0.title.lowercased() < $1.title.lowercased() }
|
|
|
|
}
|
|
|
|
|
2022-12-17 00:04:12 +05:30
|
|
|
var editable: [Playlist] {
|
|
|
|
all.filter(\.editable)
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:09:49 +05:30
|
|
|
var lastUsed: Playlist? {
|
|
|
|
find(id: Defaults[.lastUsedPlaylistID])
|
|
|
|
}
|
|
|
|
|
2021-10-26 02:59:06 +05:30
|
|
|
func find(id: Playlist.ID?) -> Playlist? {
|
|
|
|
if id.isNil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return playlists.first { $0.id == id! }
|
2021-09-28 23:36:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var isEmpty: Bool {
|
|
|
|
playlists.isEmpty
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
|
2021-09-28 23:36:05 +05:30
|
|
|
func load(force: Bool = false, onSuccess: @escaping () -> Void = {}) {
|
2022-12-16 17:01:43 +05:30
|
|
|
guard accounts.app.supportsUserPlaylists, let account = accounts.current else {
|
2021-11-15 04:36:01 +05:30
|
|
|
playlists = []
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-11 22:34:39 +05:30
|
|
|
loadCachedPlaylists(account)
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2022-12-16 17:01:43 +05:30
|
|
|
guard accounts.signedIn else { return }
|
|
|
|
|
2022-12-11 22:34:39 +05:30
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
guard let self else { return }
|
|
|
|
let request = force ? self.resource?.load() : self.resource?.loadIfNeeded()
|
2021-10-26 02:59:06 +05:30
|
|
|
|
2022-12-11 22:34:39 +05:30
|
|
|
guard !request.isNil else {
|
|
|
|
onSuccess()
|
|
|
|
return
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
2022-12-11 22:34:39 +05:30
|
|
|
|
|
|
|
self.isLoading = true
|
|
|
|
|
|
|
|
request?
|
|
|
|
.onCompletion { [weak self] _ in
|
|
|
|
self?.isLoading = false
|
|
|
|
}
|
|
|
|
.onSuccess { resource in
|
2022-12-16 14:05:10 +05:30
|
|
|
self.error = nil
|
2022-12-11 22:34:39 +05:30
|
|
|
if let playlists: [Playlist] = resource.typedContent() {
|
2024-08-24 21:22:35 +05:30
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
guard let self else { return }
|
|
|
|
self.playlists = playlists
|
|
|
|
}
|
2022-12-11 22:34:39 +05:30
|
|
|
PlaylistsCacheModel.shared.storePlaylist(account: account, playlists: playlists)
|
|
|
|
onSuccess()
|
|
|
|
}
|
|
|
|
}
|
2022-12-16 14:05:10 +05:30
|
|
|
.onFailure { self.error = $0 }
|
2022-12-11 22:34:39 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func loadCachedPlaylists(_ account: Account) {
|
|
|
|
let cache = PlaylistsCacheModel.shared.retrievePlaylists(account: account)
|
|
|
|
if !cache.isEmpty {
|
|
|
|
DispatchQueue.main.async(qos: .userInteractive) {
|
|
|
|
self.playlists = cache
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
2022-12-11 22:34:39 +05:30
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
2021-09-28 23:36:05 +05:30
|
|
|
|
2021-12-18 01:23:05 +05:30
|
|
|
func addVideo(
|
|
|
|
playlistID: Playlist.ID,
|
|
|
|
videoID: Video.ID,
|
|
|
|
onSuccess: @escaping () -> Void = {},
|
2022-06-18 18:09:49 +05:30
|
|
|
onFailure: ((RequestError) -> Void)? = nil
|
2021-12-18 01:23:05 +05:30
|
|
|
) {
|
2022-06-18 18:09:49 +05:30
|
|
|
accounts.api.addVideoToPlaylist(
|
|
|
|
videoID,
|
|
|
|
playlistID,
|
|
|
|
onFailure: onFailure ?? { requestError in
|
2022-11-25 02:06:05 +05:30
|
|
|
NavigationModel.shared.presentAlert(
|
2022-06-18 18:09:49 +05:30
|
|
|
title: "Error when adding to playlist",
|
|
|
|
message: "(\(requestError.httpStatusCode ?? -1)) \(requestError.userMessage)"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
) {
|
2022-05-22 04:06:15 +05:30
|
|
|
self.load(force: true) {
|
2022-12-17 00:04:12 +05:30
|
|
|
Defaults[.lastUsedPlaylistID] = playlistID
|
2022-05-22 04:06:15 +05:30
|
|
|
self.reloadPlaylists.toggle()
|
|
|
|
onSuccess()
|
|
|
|
}
|
2022-05-22 03:59:51 +05:30
|
|
|
}
|
|
|
|
}
|
2021-09-28 23:36:05 +05:30
|
|
|
|
2022-05-22 03:59:51 +05:30
|
|
|
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-18 01:23:05 +05:30
|
|
|
onSuccess()
|
|
|
|
}
|
2021-09-28 23:36:05 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 15:43:05 +05:30
|
|
|
private var resource: Resource? {
|
|
|
|
accounts.api.playlists
|
2021-09-28 23:36:05 +05:30
|
|
|
}
|
2022-12-21 04:21:04 +05:30
|
|
|
|
|
|
|
func onAccountChange() {
|
|
|
|
error = nil
|
|
|
|
playlists = []
|
|
|
|
load()
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|