yattee/Model/PlaylistsModel.swift
2021-09-25 11:34:46 +02:00

36 lines
836 B
Swift

import Foundation
import Siesta
import SwiftUI
final class PlaylistsModel: ObservableObject {
@Published var playlists = [Playlist]()
@Published var api: InvidiousAPI!
var resource: Resource {
api.playlists
}
var all: [Playlist] {
playlists.sorted { $0.title.lowercased() < $1.title.lowercased() }
}
func find(id: Playlist.ID) -> Playlist? {
all.first { $0.id == id }
}
func load(force: Bool = false) {
let request = force ? resource.load() : resource.loadIfNeeded()
request?
.onSuccess { resource in
if let playlists: [Playlist] = resource.typedContent() {
self.playlists = playlists
}
}
.onFailure { _ in
self.playlists = []
}
}
}