Skip to content

Commit

Permalink
Set memory cache extras for transformations when calling ImageRequest…
Browse files Browse the repository at this point in the history
….Builder.transformations.
  • Loading branch information
colinrtwhite committed Nov 17, 2024
1 parent f5cc579 commit 46f18c5
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
package coil3.memory

import coil3.memory.MemoryCacheService.Companion.EXTRA_TRANSFORMATION_INDEX
import coil3.memory.MemoryCacheService.Companion.EXTRA_TRANSFORMATION_SIZE
import coil3.request.ImageRequest
import coil3.request.Options
import coil3.request.transformations
import coil3.util.forEachIndexedIndices

internal actual fun createComplexMemoryCacheKeyExtras(
request: ImageRequest,
options: Options,
): Map<String, String> {
var memoryCacheKeyExtras = request.memoryCacheKeyExtras
if (request.transformations.isNotEmpty()) {
val mutableMemoryCacheKeyExtras = memoryCacheKeyExtras.toMutableMap()
request.transformations.forEachIndexedIndices { index, transformation ->
mutableMemoryCacheKeyExtras[EXTRA_TRANSFORMATION_INDEX + index] = transformation.cacheKey
}
mutableMemoryCacheKeyExtras[EXTRA_TRANSFORMATION_SIZE] = options.size.toString()
memoryCacheKeyExtras = mutableMemoryCacheKeyExtras
}
return memoryCacheKeyExtras
internal actual fun ImageRequest.needsSizeInCacheKey(): Boolean {
return transformations.isNotEmpty()
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ fun ImageRequest.Builder.transformations(vararg transformations: Transformation)

fun ImageRequest.Builder.transformations(transformations: List<Transformation>) = apply {
extras[transformationsKey] = transformations.toImmutableList()

var index = 0
val memoryCacheKey = transformations.joinToString { "${++index}:${it.cacheKey}" }
memoryCacheKeyExtra("coil#transformations", memoryCacheKey)
}

val ImageRequest.transformations: List<Transformation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import coil3.RealImageLoader
import coil3.asImage
import coil3.key.Keyer
import coil3.memory.MemoryCacheService.Companion.EXTRA_IS_SAMPLED
import coil3.memory.MemoryCacheService.Companion.EXTRA_TRANSFORMATION_INDEX
import coil3.memory.MemoryCacheService.Companion.EXTRA_TRANSFORMATION_SIZE
import coil3.memory.MemoryCacheService.Companion.EXTRA_SIZE
import coil3.request.ImageRequest
import coil3.request.Options
import coil3.request.RequestService
Expand All @@ -25,7 +24,6 @@ import coil3.transform.Transformation
import coil3.util.SystemCallbacks
import coil3.util.createBitmap
import coil3.util.createRequest
import coil3.util.forEachIndexedIndices
import coil3.util.toDrawable
import kotlin.test.assertEquals
import kotlin.test.assertFalse
Expand Down Expand Up @@ -535,10 +533,11 @@ class MemoryCacheServiceTest : RobolectricTest() {
): MemoryCache.Key {
val extras = memoryCacheKeyExtras.toMutableMap()
if (transformations.isNotEmpty()) {
transformations.forEachIndexedIndices { index, transformation ->
extras[EXTRA_TRANSFORMATION_INDEX + index] = transformation.cacheKey
}
extras[EXTRA_TRANSFORMATION_SIZE] = size.toString()
extras += ImageRequest.Builder(context)
.transformations(transformations)
.build()
.memoryCacheKeyExtras
extras[EXTRA_SIZE] = size.toString()
}
return MemoryCache.Key(key, extras)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ internal class MemoryCacheService(
}

// Else, create a memory cache key with all extras.
val extras = createComplexMemoryCacheKeyExtras(request, options)
val extras = request.memoryCacheKeyExtras.toMutableMap()
if (request.needsSizeInCacheKey()) {
extras[EXTRA_SIZE] = options.size.toString()
}
return MemoryCache.Key(key, extras)
}

Expand Down Expand Up @@ -111,7 +114,7 @@ internal class MemoryCacheService(
// The requested dimensions must match the transformation size exactly if it is present.
// Unlike standard, requests we can't assume transformed bitmaps for the same image have
// the same aspect ratio.
val transformationSize = cacheKey.extras[EXTRA_TRANSFORMATION_SIZE]
val transformationSize = cacheKey.extras[EXTRA_SIZE]
if (transformationSize != null) {
// 'Size.toString' is safe to use to determine equality.
return transformationSize == size.toString()
Expand Down Expand Up @@ -219,14 +222,10 @@ internal class MemoryCacheService(

companion object {
private const val TAG = "MemoryCacheService"
internal const val EXTRA_TRANSFORMATION_INDEX = "coil#transformation_"
internal const val EXTRA_TRANSFORMATION_SIZE = "coil#transformation_size"
internal const val EXTRA_SIZE = "coil#size"
internal const val EXTRA_IS_SAMPLED = "coil#is_sampled"
internal const val EXTRA_DISK_CACHE_KEY = "coil#disk_cache_key"
}
}

internal expect fun createComplexMemoryCacheKeyExtras(
request: ImageRequest,
options: Options,
): Map<String, String>
internal expect fun ImageRequest.needsSizeInCacheKey(): Boolean
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package coil3.memory

import coil3.request.ImageRequest
import coil3.request.Options

internal actual fun createComplexMemoryCacheKeyExtras(
request: ImageRequest,
options: Options,
): Map<String, String> {
return request.memoryCacheKeyExtras
}
internal actual fun ImageRequest.needsSizeInCacheKey() = false
5 changes: 5 additions & 0 deletions samples/view/src/main/java/sample/view/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager.VERTICAL
import coil3.load
import coil3.request.transformations
import coil3.transform.CircleCropTransformation
import kotlinx.coroutines.launch
import sample.common.AndroidMainViewModel
import sample.common.AssetType
Expand Down Expand Up @@ -70,6 +72,9 @@ class MainActivity : AppCompatActivity() {
binding.detail.isVisible = true
binding.detail.load(screen.image.uri) {
placeholderMemoryCacheKey(screen.placeholder)
transformations(
CircleCropTransformation()
)
extras(screen.image.extras)
}
}
Expand Down

0 comments on commit 46f18c5

Please sign in to comment.