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) { try {
cacheMetadata(downloadFile.track.artistId!!) downloadFile.track.cacheMetadata()
} catch (ignore: Exception) {
Timber.w(ignore)
} }
downloadAndSaveCoverArt() downloadAndSaveCoverArt()
@ -510,13 +512,13 @@ class Downloader(
return String.format(Locale.ROOT, "DownloadTask (%s)", downloadFile.track) return String.format(Locale.ROOT, "DownloadTask (%s)", downloadFile.track)
} }
private fun cacheMetadata(artistId: String) { private fun Track.cacheMetadata() {
// TODO: Right now it's caching the track artist. if (artistId.isNullOrEmpty()) return
// Once the albums are cached in db, we should retrieve the album,
// and then cache the album artist. val onlineDB = activeServerProvider.getActiveMetaDatabase()
if (artistId.isEmpty()) return val offlineDB = activeServerProvider.offlineMetaDatabase
var artist: Artist? =
activeServerProvider.getActiveMetaDatabase().artistsDao().get(artistId) var artist: Artist? = onlineDB.artistDao().get(artistId!!)
// If we are downloading a new album, and the user has not visited the Artists list // 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. // 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) { 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() { private fun downloadAndSaveCoverArt() {