diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/service/Downloader.kt b/ultrasonic/src/main/java/org/moire/ultrasonic/service/Downloader.kt index 3842be8d..917493f0 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/service/Downloader.kt +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/service/Downloader.kt @@ -43,7 +43,6 @@ class Downloader( private var executorService: ScheduledExecutorService? = null private var wifiLock: WifiManager.WifiLock? = null - var playlistUpdateRevision: Long = 0 private set diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/ActiveServerProvider.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/ActiveServerProvider.kt index d1a5c18b..758fa74d 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/ActiveServerProvider.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/ActiveServerProvider.kt @@ -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 diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/ArtistsDao.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/ArtistsDao.kt index 2a86d496..a039c1f7 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/ArtistsDao.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/ArtistsDao.kt @@ -17,6 +17,16 @@ interface ArtistsDao { @JvmSuppressWildcards fun set(objects: List) + /** + * 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 + + /** + * Get artist by id + */ + @Query("SELECT * FROM artists WHERE id LIKE :id") + fun get(id: String): Artist } diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/BasicDaos.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/BasicDaos.kt index ab69089f..1f3e77f5 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/BasicDaos.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/BasicDaos.kt @@ -97,6 +97,16 @@ interface GenericDao { @JvmSuppressWildcards fun set(objects: List) + /** + * 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. * diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/MetaDatabase.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/MetaDatabase.kt index 6abdc85a..638a591b 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/MetaDatabase.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/MetaDatabase.kt @@ -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 diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/DownloadFile.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/DownloadFile.kt index cccbc498..0c896d14 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/DownloadFile.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/DownloadFile.kt @@ -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 = 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)) {