Thumbnails: avoid loading many Bitmaps, increase cache size

This commit is contained in:
Pierre-Yves Nicolas
2026-04-02 17:48:12 +02:00
parent 7a9e4203ed
commit 1e99520473
5 changed files with 9 additions and 12 deletions

View File

@@ -63,7 +63,7 @@ class MainViewModel(val imageRepository: ImageRepository, launchMode: LaunchMode
_pages.map { pages ->
pages.map {
val jpeg = imageRepository.getThumbnail(it.key())
PageThumbnail(it.key(), jpeg?.toBitmap())
PageThumbnail(it.key(), jpeg)
}.toImmutableList()
}
.flowOn(Dispatchers.IO)

View File

@@ -67,7 +67,7 @@ class ImageRepository(
private val processingJobs = synchronizedMap(mutableMapOf<PageViewKey, Deferred<Unit>>())
private val imageCache = createLruCache<PageViewKey, Deferred<Jpeg?>>(maxEntries = 50)
private val thumbnailCache = createLruCache<PageViewKey, Deferred<Jpeg?>>(maxEntries = 200)
private val thumbnailCache = createLruCache<PageViewKey, Deferred<Jpeg?>>(maxEntries = 1000)
private fun <K, V> createLruCache(maxEntries: Int): MutableMap<K, V> =
synchronizedMap(object : LinkedHashMap<K, V>(16, 0.75f, true) {

View File

@@ -15,11 +15,10 @@
package org.fairscan.app.ui
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableList
import org.fairscan.app.domain.Jpeg
import org.fairscan.app.domain.PageViewKey
import org.fairscan.app.domain.Rotation
import org.fairscan.app.ui.state.DocumentUiModel
@@ -41,8 +40,5 @@ fun fakeDocument(pageIds: ImmutableList<String>, context: Context): DocumentUiMo
return DocumentUiModel(pageKeys)
}
fun fakeImage(id: String, context: Context): Bitmap =
context.assets.open("${id}.jpg").use { input ->
BitmapFactory.decodeStream(input)
}
fun fakeImage(id: String, context: Context): Jpeg =
Jpeg(context.assets.open("${id}.jpg").readBytes())

View File

@@ -339,7 +339,7 @@ private fun BottomBar(
@Preview(name = "Landscape", showBackground = true, widthDp = 640, heightDp = 320)
fun DocumentScreenPreview() {
FairScanTheme {
val image = fakeImage("gallica.bnf.fr-bpt6k5530456s-1", LocalContext.current)
val image = fakeImage("gallica.bnf.fr-bpt6k5530456s-1", LocalContext.current).toBitmap()
val document = fakeDocument(
listOf(1, 2).map { "gallica.bnf.fr-bpt6k5530456s-$it" }.toImmutableList(),
LocalContext.current

View File

@@ -17,6 +17,7 @@ package org.fairscan.app.ui.state
import android.graphics.Bitmap
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import org.fairscan.app.domain.Jpeg
import org.fairscan.app.domain.PageViewKey
data class DocumentUiModel(
@@ -32,11 +33,11 @@ data class DocumentUiModel(
return pages.lastIndex
}
fun thumbnail(index: Int): Bitmap? {
return pages[index].thumbnail
return pages[index].thumbnail?.toBitmap()
}
}
data class PageThumbnail(
val key: PageViewKey,
val thumbnail: Bitmap?,
val thumbnail: Jpeg?,
)