Browse Source

Increase memory cache size

pull/532/head
tzugen 4 months ago
parent
commit
2da4f79098
No known key found for this signature in database GPG Key ID: 61E9C34BC10EC930
  1. 1
      detekt-config.yml
  2. 17
      ultrasonic/src/main/kotlin/org/moire/ultrasonic/imageloader/ImageLoader.kt

1
detekt-config.yml

@ -45,6 +45,7 @@ complexity:
thresholdInFiles: 20
thresholdInClasses: 20
thresholdInInterfaces: 20
thresholdInObjects: 30
LabeledExpression:
active: false

17
ultrasonic/src/main/kotlin/org/moire/ultrasonic/imageloader/ImageLoader.kt

@ -1,9 +1,13 @@
package org.moire.ultrasonic.imageloader
import android.app.ActivityManager
import android.content.Context
import android.content.pm.ApplicationInfo
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import androidx.core.content.ContextCompat
import com.squareup.picasso.LruCache
import com.squareup.picasso.Picasso
import com.squareup.picasso.RequestCreator
import java.io.File
@ -35,6 +39,7 @@ class ImageLoader(
private val picasso = Picasso.Builder(context)
.addRequestHandler(CoverArtRequestHandler(apiClient))
.addRequestHandler(AvatarRequestHandler(apiClient))
.memoryCache(LruCache(calculateMemoryCacheSize(context)))
.build().apply {
setIndicatorsEnabled(BuildConfig.DEBUG)
}
@ -179,6 +184,18 @@ class ImageLoader(
return requested
}
}
private fun calculateMemoryCacheSize(context: Context): Int {
val am = ContextCompat.getSystemService(
context,
ActivityManager::class.java
)
val largeHeap = context.applicationInfo.flags and ApplicationInfo.FLAG_LARGE_HEAP != 0
val memoryClass = if (largeHeap) am!!.largeMemoryClass else am!!.memoryClass
// Target 25% of the available heap.
@Suppress("MagicNumber")
return (1024L * 1024L * memoryClass / 4).toInt()
}
}
/**

Loading…
Cancel
Save