Add a "clear cache" function

This commit is contained in:
2026-04-11 11:30:42 +02:00
parent 12224db55e
commit ae706bc8bc
3 changed files with 136 additions and 1 deletions
@@ -145,6 +145,36 @@ final class MALibraryManager {
return try? JSONDecoder().decode(T.self, from: data)
}
/// Clears all cached library data from memory and disk.
func clearAll() {
artists = []
albumArtists = []
albums = []
playlists = []
podcasts = []
favoriteURIs = []
artistsOffset = 0
albumArtistsOffset = 0
albumsOffset = 0
hasMoreArtists = true
hasMoreAlbumArtists = true
hasMoreAlbums = true
lastArtistsRefresh = nil
lastAlbumArtistsRefresh = nil
lastAlbumsRefresh = nil
lastPlaylistsRefresh = nil
lastPodcastsRefresh = nil
let ud = UserDefaults.standard
for key in ["lib.lastArtistsRefresh", "lib.lastAlbumArtistsRefresh",
"lib.lastAlbumsRefresh", "lib.lastPlaylistsRefresh", "lib.lastPodcastsRefresh"] {
ud.removeObject(forKey: key)
}
try? FileManager.default.removeItem(at: cacheDirectory)
try? FileManager.default.createDirectory(at: cacheDirectory, withIntermediateDirectories: true)
}
private func markRefreshed(_ key: String) -> Date {
let now = Date()
UserDefaults.standard.set(now, forKey: key)