Save Artists in Offline database

This commit is contained in:
tzugen 2021-09-12 14:36:33 +02:00
parent 28097bf325
commit ecc7e870f1
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
6 changed files with 65 additions and 7 deletions

View File

@ -43,7 +43,6 @@ class Downloader(
private var executorService: ScheduledExecutorService? = null
private var wifiLock: WifiManager.WifiLock? = null
var playlistUpdateRevision: Long = 0
private set

View File

@ -17,6 +17,8 @@ import timber.log.Timber
/**
* This class can be used to retrieve the properties of the Active Server
* It caches the settings read up from the DB to improve performance.
*
* TODO: There seems to be some confusion whether offline id is 0 or -1. Clean this up (carefully!)
*/
class ActiveServerProvider(
private val repository: ServerSettingDao
@ -29,9 +31,8 @@ class ActiveServerProvider(
* Get the settings of the current Active Server
* @return The Active Server Settings
*/
fun getActiveServer(): ServerSetting {
val serverId = getActiveServerId()
@JvmOverloads
fun getActiveServer(serverId: Int = getActiveServerId()): ServerSetting {
if (serverId > 0) {
if (cachedServer != null && cachedServer!!.id == serverId) return cachedServer!!
@ -94,16 +95,29 @@ class ActiveServerProvider(
return cachedDatabase!!
}
if (activeServer < 1) {
return offlineMetaDatabase
}
Timber.i("Switching to new database, id:$activeServer")
cachedServerId = activeServer
val db = Room.databaseBuilder(
return Room.databaseBuilder(
UApp.applicationContext(),
MetaDatabase::class.java,
METADATA_DB + cachedServerId
)
.fallbackToDestructiveMigrationOnDowngrade()
.build()
return db
}
val offlineMetaDatabase: MetaDatabase by lazy {
Room.databaseBuilder(
UApp.applicationContext(),
MetaDatabase::class.java,
METADATA_DB + 0
)
.fallbackToDestructiveMigrationOnDowngrade()
.build()
}
@Synchronized

View File

@ -17,6 +17,16 @@ interface ArtistsDao {
@JvmSuppressWildcards
fun set(objects: List<Artist>)
/**
* Insert an object in the database.
*
* @param obj the object to be inserted.
* @return The SQLite row id
*/
@Insert(onConflict = OnConflictStrategy.REPLACE)
@JvmSuppressWildcards
fun insert(obj: Artist): Long
/**
* Clear the whole database
*/
@ -28,4 +38,10 @@ interface ArtistsDao {
*/
@Query("SELECT * FROM artists")
fun get(): List<Artist>
/**
* Get artist by id
*/
@Query("SELECT * FROM artists WHERE id LIKE :id")
fun get(id: String): Artist
}

View File

@ -97,6 +97,16 @@ interface GenericDao<T> {
@JvmSuppressWildcards
fun set(objects: List<T>)
/**
* Insert an object in the database.
*
* @param obj the object to be inserted.
* @return The SQLite row id
*/
@Insert(onConflict = OnConflictStrategy.REPLACE)
@JvmSuppressWildcards
fun insert(obj: T): Long
/**
* Insert an object in the database.
*

View File

@ -6,6 +6,10 @@ import org.moire.ultrasonic.domain.Artist
import org.moire.ultrasonic.domain.Index
import org.moire.ultrasonic.domain.MusicFolder
/**
* This database is used to store and cache the ID3 metadata
*/
@Database(
entities = [Artist::class, Index::class, MusicFolder::class],
version = 1

View File

@ -7,7 +7,6 @@
package org.moire.ultrasonic.service
import android.net.wifi.WifiManager.WifiLock
import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import java.io.File
@ -18,6 +17,7 @@ import java.io.OutputStream
import java.io.RandomAccessFile
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.moire.ultrasonic.data.ActiveServerProvider
import org.moire.ultrasonic.domain.MusicDirectory
import org.moire.ultrasonic.service.MusicServiceFactory.getMusicService
import org.moire.ultrasonic.subsonic.ImageLoaderProvider
@ -56,6 +56,7 @@ class DownloadFile(
private val downloader: Downloader by inject()
private val imageLoaderProvider: ImageLoaderProvider by inject()
private val activeServerProvider: ActiveServerProvider by inject()
val progress: MutableLiveData<Int> = MutableLiveData(0)
@ -266,6 +267,11 @@ class DownloadFile(
if (isCancelled) {
throw Exception(String.format("Download of '%s' was cancelled", song))
}
if (song.artistId != null) {
cacheMetadata(song.artistId!!)
}
downloadAndSaveCoverArt()
}
@ -302,6 +308,15 @@ class DownloadFile(
return String.format("DownloadTask (%s)", song)
}
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
val artist = activeServerProvider.getActiveMetaDatabase().artistsDao().get(artistId)
activeServerProvider.offlineMetaDatabase.artistsDao().insert(artist)
}
private fun downloadAndSaveCoverArt() {
try {
if (!TextUtils.isEmpty(song.coverArt)) {