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 executorService: ScheduledExecutorService? = null
private var wifiLock: WifiManager.WifiLock? = null private var wifiLock: WifiManager.WifiLock? = null
var playlistUpdateRevision: Long = 0 var playlistUpdateRevision: Long = 0
private set 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 * 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. * 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( class ActiveServerProvider(
private val repository: ServerSettingDao private val repository: ServerSettingDao
@ -29,9 +31,8 @@ class ActiveServerProvider(
* Get the settings of the current Active Server * Get the settings of the current Active Server
* @return The Active Server Settings * @return The Active Server Settings
*/ */
fun getActiveServer(): ServerSetting { @JvmOverloads
val serverId = getActiveServerId() fun getActiveServer(serverId: Int = getActiveServerId()): ServerSetting {
if (serverId > 0) { if (serverId > 0) {
if (cachedServer != null && cachedServer!!.id == serverId) return cachedServer!! if (cachedServer != null && cachedServer!!.id == serverId) return cachedServer!!
@ -94,16 +95,29 @@ class ActiveServerProvider(
return cachedDatabase!! return cachedDatabase!!
} }
if (activeServer < 1) {
return offlineMetaDatabase
}
Timber.i("Switching to new database, id:$activeServer") Timber.i("Switching to new database, id:$activeServer")
cachedServerId = activeServer cachedServerId = activeServer
val db = Room.databaseBuilder( return Room.databaseBuilder(
UApp.applicationContext(), UApp.applicationContext(),
MetaDatabase::class.java, MetaDatabase::class.java,
METADATA_DB + cachedServerId METADATA_DB + cachedServerId
) )
.fallbackToDestructiveMigrationOnDowngrade() .fallbackToDestructiveMigrationOnDowngrade()
.build() .build()
return db }
val offlineMetaDatabase: MetaDatabase by lazy {
Room.databaseBuilder(
UApp.applicationContext(),
MetaDatabase::class.java,
METADATA_DB + 0
)
.fallbackToDestructiveMigrationOnDowngrade()
.build()
} }
@Synchronized @Synchronized

View File

@ -17,6 +17,16 @@ interface ArtistsDao {
@JvmSuppressWildcards @JvmSuppressWildcards
fun set(objects: List<Artist>) 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 * Clear the whole database
*/ */
@ -28,4 +38,10 @@ interface ArtistsDao {
*/ */
@Query("SELECT * FROM artists") @Query("SELECT * FROM artists")
fun get(): List<Artist> 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 @JvmSuppressWildcards
fun set(objects: List<T>) 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. * 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.Index
import org.moire.ultrasonic.domain.MusicFolder import org.moire.ultrasonic.domain.MusicFolder
/**
* This database is used to store and cache the ID3 metadata
*/
@Database( @Database(
entities = [Artist::class, Index::class, MusicFolder::class], entities = [Artist::class, Index::class, MusicFolder::class],
version = 1 version = 1

View File

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