Add code to Downloader

This commit is contained in:
tzugen 2022-06-16 19:51:45 +02:00
parent 8490f7115d
commit 60dbe70ca5
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
1 changed files with 26 additions and 11 deletions

View File

@ -457,8 +457,10 @@ class Downloader(
)
}
if (downloadFile.track.artistId != null) {
cacheMetadata(downloadFile.track.artistId!!)
try {
downloadFile.track.cacheMetadata()
} catch (ignore: Exception) {
Timber.w(ignore)
}
downloadAndSaveCoverArt()
@ -510,13 +512,13 @@ class Downloader(
return String.format(Locale.ROOT, "DownloadTask (%s)", downloadFile.track)
}
private fun cacheMetadata(artistId: String) {
// TODO: Right now it's caching the track artist.
// Once the albums are cached in db, we should retrieve the album,
// and then cache the album artist.
if (artistId.isEmpty()) return
var artist: Artist? =
activeServerProvider.getActiveMetaDatabase().artistsDao().get(artistId)
private fun Track.cacheMetadata() {
if (artistId.isNullOrEmpty()) return
val onlineDB = activeServerProvider.getActiveMetaDatabase()
val offlineDB = activeServerProvider.offlineMetaDatabase
var artist: Artist? = onlineDB.artistDao().get(artistId!!)
// If we are downloading a new album, and the user has not visited the Artists list
// recently, then the artist won't be in the database.
@ -527,10 +529,23 @@ class Downloader(
}
}
// If we have found an artist, catch it.
// If we have found an artist, cache it.
if (artist != null) {
activeServerProvider.offlineMetaDatabase.artistsDao().insert(artist)
offlineDB.artistDao().insert(artist)
}
// Now cache the album
if (albumId?.isNotEmpty() == true) {
val albums = musicService.getAlbumsOfArtist(artistId!!, null, false)
val album = albums.find { it.id == albumId }
if (album != null) {
offlineDB.albumDao().insert(album)
}
}
// Now cache the track data
offlineDB.trackDao().insert(this)
}
private fun downloadAndSaveCoverArt() {