musikr: compose dbcache

This commit is contained in:
Alexander Capehart 2025-03-17 12:54:22 -06:00
parent e64b30f00f
commit f213c21225
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47

View file

@ -27,7 +27,7 @@ import org.oxycblt.musikr.fs.device.DeviceFile
import org.oxycblt.musikr.metadata.Properties import org.oxycblt.musikr.metadata.Properties
import org.oxycblt.musikr.tag.parse.ParsedTags import org.oxycblt.musikr.tag.parse.ParsedTags
open class DBCache internal constructor(private val readDao: CacheReadDao) : Cache { class DBCache private constructor(private val readDao: CacheReadDao) : Cache {
override suspend fun read(file: DeviceFile): CacheResult { override suspend fun read(file: DeviceFile): CacheResult {
val dbSong = readDao.selectSong(file.uri.toString()) ?: return CacheResult.Miss(file) val dbSong = readDao.selectSong(file.uri.toString()) ?: return CacheResult.Miss(file)
if (dbSong.modifiedMs != file.modifiedMs) { if (dbSong.modifiedMs != file.modifiedMs) {
@ -66,13 +66,17 @@ open class DBCache internal constructor(private val readDao: CacheReadDao) : Cac
} }
companion object { companion object {
fun from(context: Context) = DBCache(CacheDatabase.from(context).readDao()) fun from(context: Context) = from(CacheDatabase.from(context))
internal fun from(db: CacheDatabase) = DBCache(db.readDao())
} }
} }
class MutableDBCache class MutableDBCache
private constructor(readDao: CacheReadDao, private val writeDao: CacheWriteDao) : private constructor(private val inner: DBCache, private val writeDao: CacheWriteDao) :
MutableCache, DBCache(readDao) { MutableCache {
override suspend fun read(file: DeviceFile) = inner.read(file)
override suspend fun write(cachedSong: CachedSong) { override suspend fun write(cachedSong: CachedSong) {
val dbSong = val dbSong =
CachedSongData( CachedSongData(
@ -114,7 +118,7 @@ private constructor(readDao: CacheReadDao, private val writeDao: CacheWriteDao)
companion object { companion object {
fun from(context: Context): MutableDBCache { fun from(context: Context): MutableDBCache {
val db = CacheDatabase.from(context) val db = CacheDatabase.from(context)
return MutableDBCache(db.readDao(), db.writeDao()) return MutableDBCache(DBCache.from(db), db.writeDao())
} }
} }
} }