image: use cover retriever in app
This commit is contained in:
parent
ddfe10b869
commit
c3f9f0d80e
4 changed files with 20 additions and 11 deletions
|
@ -43,7 +43,7 @@ import kotlinx.coroutines.withContext
|
|||
import okio.FileSystem
|
||||
import okio.buffer
|
||||
import okio.source
|
||||
import org.oxycblt.auxio.image.stack.extractor.CoverExtractor
|
||||
import org.oxycblt.auxio.image.stack.CoverRetriever
|
||||
|
||||
class CoverKeyer @Inject constructor() : Keyer<Cover> {
|
||||
override fun key(data: Cover, options: Options) = "${data.key}&${options.size}"
|
||||
|
@ -54,16 +54,16 @@ private constructor(
|
|||
private val context: Context,
|
||||
private val cover: Cover,
|
||||
private val size: Size,
|
||||
private val coverExtractor: CoverExtractor,
|
||||
private val coverRetriever: CoverRetriever,
|
||||
) : Fetcher {
|
||||
override suspend fun fetch(): FetchResult? {
|
||||
val streams =
|
||||
when (val cover = cover) {
|
||||
is Cover.Single -> listOfNotNull(coverExtractor.extract(cover))
|
||||
is Cover.Single -> listOfNotNull(coverRetriever.retrieve(cover))
|
||||
is Cover.Multi ->
|
||||
buildList {
|
||||
for (single in cover.all) {
|
||||
coverExtractor.extract(single)?.let { add(it) }
|
||||
coverRetriever.retrieve(single)?.let { add(it) }
|
||||
if (size == 4) {
|
||||
break
|
||||
}
|
||||
|
@ -147,9 +147,9 @@ private constructor(
|
|||
return if (size.mod(2) > 0) size + 1 else size
|
||||
}
|
||||
|
||||
class Factory @Inject constructor(private val coverExtractor: CoverExtractor) :
|
||||
class Factory @Inject constructor(private val coverRetriever: CoverRetriever) :
|
||||
Fetcher.Factory<Cover> {
|
||||
override fun create(data: Cover, options: Options, imageLoader: ImageLoader) =
|
||||
CoverFetcher(options.context, data, options.size, coverExtractor)
|
||||
CoverFetcher(options.context, data, options.size, coverRetriever)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,7 +26,9 @@ sealed interface Cover {
|
|||
|
||||
class Single(song: Song) : Cover {
|
||||
override val key = "${song.uid}@${song.lastModified}"
|
||||
val uid = song.uid
|
||||
val uri = song.uri
|
||||
val lastModified = song.lastModified
|
||||
}
|
||||
|
||||
class Multi(val all: List<Single>) : Cover {
|
||||
|
|
|
@ -20,17 +20,22 @@ package org.oxycblt.auxio.image.stack
|
|||
|
||||
import java.io.InputStream
|
||||
import javax.inject.Inject
|
||||
import org.oxycblt.auxio.image.extractor.Cover
|
||||
import org.oxycblt.auxio.image.stack.cache.CoverCache
|
||||
import org.oxycblt.auxio.music.Song
|
||||
import org.oxycblt.auxio.image.stack.extractor.CoverExtractor
|
||||
|
||||
interface CoverRetriever {
|
||||
suspend fun retrieve(song: Song): InputStream?
|
||||
suspend fun retrieve(cover: Cover.Single): InputStream?
|
||||
}
|
||||
|
||||
class CoverRetrieverImpl
|
||||
@Inject
|
||||
constructor(private val coverCache: CoverCache, private val coverRetriever: CoverRetriever) :
|
||||
constructor(private val coverCache: CoverCache, private val coverExtractor: CoverExtractor) :
|
||||
CoverRetriever {
|
||||
override suspend fun retrieve(song: Song) =
|
||||
coverCache.read(song) ?: coverRetriever.retrieve(song)?.also { coverCache.write(song, it) }
|
||||
override suspend fun retrieve(cover: Cover.Single) =
|
||||
coverCache.read(cover)
|
||||
?: coverExtractor.extract(cover)?.also {
|
||||
coverCache.write(cover, it)
|
||||
it.reset()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,6 +41,8 @@ interface StackModule {
|
|||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
class StoredCoversDatabaseModule {
|
||||
@Provides fun storedCoversDao(database: StoredCoversDatabase) = database.storedCoversDao()
|
||||
|
||||
@Singleton
|
||||
@Provides
|
||||
fun database(@ApplicationContext context: Context) =
|
||||
|
|
Loading…
Reference in a new issue