yattee/Model/PlaylistsModel.swift

121 lines
3.5 KiB
Swift
Raw Normal View History

import Defaults
2021-09-25 08:18:22 +00:00
import Foundation
import Siesta
import SwiftUI
final class PlaylistsModel: ObservableObject {
static var shared = PlaylistsModel()
2022-12-11 17:04:39 +00:00
@Published var isLoading = false
2021-09-25 08:18:22 +00:00
@Published var playlists = [Playlist]()
@Published var reloadPlaylists = false
2021-09-25 08:18:22 +00:00
var accounts = AccountsModel.shared
2021-10-16 22:48:58 +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() }
}
var lastUsed: Playlist? {
find(id: Defaults[.lastUsedPlaylistID])
}
func find(id: Playlist.ID?) -> Playlist? {
if id.isNil {
return nil
}
return playlists.first { $0.id == id! }
}
var isEmpty: Bool {
playlists.isEmpty
2021-09-25 08:18:22 +00:00
}
func load(force: Bool = false, onSuccess: @escaping () -> Void = {}) {
2022-12-11 17:04:39 +00:00
guard accounts.app.supportsUserPlaylists, accounts.signedIn, let account = accounts.current else {
playlists = []
return
}
2022-12-11 17:04:39 +00:00
loadCachedPlaylists(account)
2021-09-25 08:18:22 +00:00
2022-12-11 17:04:39 +00:00
DispatchQueue.main.async { [weak self] in
guard let self else { return }
let request = force ? self.resource?.load() : self.resource?.loadIfNeeded()
2022-12-11 17:04:39 +00:00
guard !request.isNil else {
onSuccess()
return
2021-09-25 08:18:22 +00:00
}
2022-12-11 17:04:39 +00:00
self.isLoading = true
request?
.onCompletion { [weak self] _ in
self?.isLoading = false
}
.onSuccess { resource in
if let playlists: [Playlist] = resource.typedContent() {
self.playlists = playlists
PlaylistsCacheModel.shared.storePlaylist(account: account, playlists: playlists)
onSuccess()
}
}
.onFailure { error in
self.playlists = []
NavigationModel.shared.presentAlert(title: "Could not refresh Playlists", message: error.userMessage)
}
}
}
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 08:18:22 +00:00
}
2022-12-11 17:04:39 +00:00
}
2021-09-25 08:18:22 +00:00
}
2021-12-17 19:53:05 +00:00
func addVideo(
playlistID: Playlist.ID,
videoID: Video.ID,
onSuccess: @escaping () -> Void = {},
onFailure: ((RequestError) -> Void)? = nil
2021-12-17 19:53:05 +00:00
) {
accounts.api.addVideoToPlaylist(
videoID,
playlistID,
onFailure: onFailure ?? { requestError in
NavigationModel.shared.presentAlert(
title: "Error when adding to playlist",
message: "(\(requestError.httpStatusCode ?? -1)) \(requestError.userMessage)"
)
}
) {
self.load(force: true) {
self.reloadPlaylists.toggle()
onSuccess()
}
}
}
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()
}
}
}
private var resource: Resource? {
accounts.api.playlists
}
2021-09-25 08:18:22 +00:00
}