Skip to content

Commit

Permalink
Use own cache for bitmaps instead of coil (#265)
Browse files Browse the repository at this point in the history
* Use own cache for bitmaps instead of coil

* Fix detekt
  • Loading branch information
michaeltroger authored May 24, 2024
1 parent 07de196 commit 41cfaa9
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
1 change: 0 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ dependencies {
implementation(libs.com.github.lisawray.groupie)
implementation(libs.com.github.lisawray.groupie.viewbinding)
implementation(libs.com.google.android.material)
implementation(libs.io.coil)
implementation(libs.io.github.panpf.zoomimage.view)

testImplementation(libs.androidx.test.ext.junit.ktx)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.michaeltroger.gruenerpass

import android.app.Application
import com.michaeltroger.gruenerpass.cache.BitmapCache
import com.michaeltroger.gruenerpass.migration.AppMigrator
import dagger.hilt.android.HiltAndroidApp
import javax.inject.Inject
Expand All @@ -15,4 +16,9 @@ class GreenPassApplication : Application() {
super.onCreate()
appMigrator.performMigration()
}

override fun onLowMemory() {
super.onLowMemory()
BitmapCache.memoryCache.evictAll()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.michaeltroger.gruenerpass.cache

import android.graphics.Bitmap
import android.util.LruCache

object BitmapCache {
@Suppress("MagicNumber")
val memoryCache: LruCache<String, Bitmap> by lazy {
val maxMemory = (Runtime.getRuntime().maxMemory() / 1024).toInt()

// Use 1/8th of the available memory for this memory cache.
val cacheSize = maxMemory / 8

object : LruCache<String, Bitmap>(cacheSize) {

override fun sizeOf(key: String, bitmap: Bitmap): Int {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.byteCount / 1024
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import android.content.Context
import android.graphics.Bitmap
import android.view.View
import androidx.core.view.isVisible
import coil.imageLoader
import coil.memory.MemoryCache
import com.michaeltroger.gruenerpass.R
import com.michaeltroger.gruenerpass.barcode.BarcodeRenderer
import com.michaeltroger.gruenerpass.cache.BitmapCache
import com.michaeltroger.gruenerpass.databinding.ItemCertificatePartialPdfPageBinding
import com.xwray.groupie.Item
import com.xwray.groupie.viewbinding.BindableItem
Expand Down Expand Up @@ -38,8 +37,8 @@ class PdfPageItem(

private var job: Job? = null

private val barcodeCacheKey = MemoryCache.Key("barcode-$fileName-$pageIndex")
private val pdfCacheKey = MemoryCache.Key("pdf-$fileName-$pageIndex")
private val barcodeCacheKey = "barcode-$fileName-$pageIndex"
private val pdfCacheKey = "pdf-$fileName-$pageIndex"

override fun initializeViewBinding(view: View): ItemCertificatePartialPdfPageBinding
= ItemCertificatePartialPdfPageBinding.bind(view)
Expand All @@ -49,11 +48,10 @@ class PdfPageItem(
override fun bind(viewBinding: ItemCertificatePartialPdfPageBinding, position: Int) {
job = scope.launch {
val context = viewBinding.root.context
val imageLoader = context.imageLoader

var pdf: Bitmap? = imageLoader.memoryCache?.get(pdfCacheKey)?.bitmap
var pdf: Bitmap? = BitmapCache.memoryCache.get(pdfCacheKey)
if(!isActive) return@launch
var barcode: Bitmap? = imageLoader.memoryCache?.get(barcodeCacheKey)?.bitmap
var barcode: Bitmap? = BitmapCache.memoryCache.get(barcodeCacheKey)
if(!isActive) return@launch

if (pdf == null) {
Expand All @@ -63,14 +61,14 @@ class PdfPageItem(
if(!isActive) return@launch

if (barcode != null) {
imageLoader.memoryCache?.set(
BitmapCache.memoryCache.put(
barcodeCacheKey,
MemoryCache.Value(barcode)
barcode
)
}
imageLoader.memoryCache?.set(
BitmapCache.memoryCache.put(
pdfCacheKey,
MemoryCache.Value(pdf)
pdf
)
if(!isActive) return@launch
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
import coil.imageLoader
import com.michaeltroger.gruenerpass.R
import com.michaeltroger.gruenerpass.cache.BitmapCache
import com.michaeltroger.gruenerpass.lock.AppLockedRepo
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -49,11 +49,11 @@ class SettingsFragment : PreferenceFragmentCompat() {
if (!preferenceBarcode.isChecked) {
preferenceTryHard.isChecked = false
}
requireContext().imageLoader.memoryCache?.clear()
BitmapCache.memoryCache.evictAll()
true
}
preferenceTryHard.setOnPreferenceClickListener {
requireContext().imageLoader.memoryCache?.clear()
BitmapCache.memoryCache.evictAll()
true
}
}
Expand Down
2 changes: 0 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ com-google-dagger-hilt-android = "2.51.1"
com-google-devtools-ksp = "1.9.23-1.0.20"
com-squareup-leakcanary-android = "2.13"
com-tom-roush-pdfbox-android = "2.0.27.0"
io-coil = "2.6.0"
io-gitlab-arturbosch-detekt = "1.23.6"
io-github-panpf-zoomimage-view = "1.0.2"
io-kotest-assertions-core = "5.8.1"
Expand Down Expand Up @@ -63,7 +62,6 @@ com-google-dagger-hilt-android = { module = "com.google.dagger:hilt-android", ve
com-google-dagger-hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "com-google-dagger-hilt-android" }
com-squareup-leakcanary-android = { module = "com.squareup.leakcanary:leakcanary-android", version.ref = "com-squareup-leakcanary-android" }
com-tom-roush-pdfbox-android = { module = "com.tom-roush:pdfbox-android", version.ref = "com-tom-roush-pdfbox-android" }
io-coil = { module = "io.coil-kt:coil", version.ref = "io-coil" }
io-github-panpf-zoomimage-view = { module = "io.github.panpf.zoomimage:zoomimage-view", version.ref = "io-github-panpf-zoomimage-view" }
io-kotest-assertions-core = { module = "io.kotest:kotest-assertions-core", version.ref = "io-kotest-assertions-core" }
io-mockk = { module = "io.mockk:mockk", version.ref = "io-mockk" }
Expand Down

0 comments on commit 41cfaa9

Please sign in to comment.