Skip to content

Commit

Permalink
Add ImageLoader option for creating memory cache key using toString()…
Browse files Browse the repository at this point in the history
… by default.
  • Loading branch information
colinrtwhite committed Jan 28, 2025
1 parent 66e5615 commit 6540b67
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package coil3

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.os.Build.VERSION.SDK_INT
import coil3.decode.BitmapFactoryDecoder
import coil3.decode.ExifOrientationStrategy.Companion.RESPECT_ALL
Expand All @@ -11,6 +13,7 @@ import coil3.fetch.ContentUriFetcher
import coil3.fetch.DrawableFetcher
import coil3.fetch.ResourceUriFetcher
import coil3.key.AndroidResourceUriKeyer
import coil3.key.CacheDisabledKeyer
import coil3.map.AndroidUriMapper
import coil3.map.ResourceIntMapper
import coil3.request.Disposable
Expand Down Expand Up @@ -79,6 +82,10 @@ internal actual fun ComponentRegistry.Builder.addAndroidComponents(

// Keyers
add(AndroidResourceUriKeyer())
if (options.defaultKeyerEnabled) {
add(CacheDisabledKeyer<Bitmap>())
add(CacheDisabledKeyer<Drawable>())
}

// Fetchers
add(AssetUriFetcher.Factory())
Expand Down
33 changes: 20 additions & 13 deletions coil-core/src/commonMain/kotlin/coil3/RealImageLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import coil3.fetch.DataUriFetcher
import coil3.fetch.FileUriFetcher
import coil3.intercept.EngineInterceptor
import coil3.intercept.RealInterceptorChain
import coil3.key.CacheEnabledKeyer
import coil3.key.FileUriKeyer
import coil3.key.UriKeyer
import coil3.map.PathMapper
Expand Down Expand Up @@ -56,7 +57,7 @@ internal class RealImageLoader(
.addAndroidComponents(options)
.addJvmComponents(options)
.addAppleComponents(options)
.addCommonComponents()
.addCommonComponents(options)
.add(EngineInterceptor(this, systemCallbacks, requestService, options.logger))
.build()
private val shutdown = atomic(false)
Expand Down Expand Up @@ -288,18 +289,24 @@ internal expect fun ComponentRegistry.Builder.addAppleComponents(
options: RealImageLoader.Options,
): ComponentRegistry.Builder

internal fun ComponentRegistry.Builder.addCommonComponents(): ComponentRegistry.Builder {
return this
// Mappers
.add(StringMapper())
.add(PathMapper())
// Keyers
.add(FileUriKeyer())
.add(UriKeyer())
// Fetchers
.add(FileUriFetcher.Factory())
.add(ByteArrayFetcher.Factory())
.add(DataUriFetcher.Factory())
internal fun ComponentRegistry.Builder.addCommonComponents(
options: RealImageLoader.Options,
): ComponentRegistry.Builder = apply {
// Mappers
add(StringMapper())
add(PathMapper())

// Keyers
add(FileUriKeyer())
add(UriKeyer())
if (options.defaultKeyerEnabled) {
add(CacheEnabledKeyer())
}

// Fetchers
add(FileUriFetcher.Factory())
add(ByteArrayFetcher.Factory())
add(DataUriFetcher.Factory())
}

private const val TAG = "RealImageLoader"
Expand Down
18 changes: 18 additions & 0 deletions coil-core/src/commonMain/kotlin/coil3/imageLoaders.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package coil3

import coil3.key.Keyer

/**
* Create a new [ImageLoader] without configuration.
*/
Expand Down Expand Up @@ -30,3 +32,19 @@ internal val RealImageLoader.Options.serviceLoaderEnabled: Boolean
private val serviceLoaderEnabledKey = Extras.Key(default = true)

// endregion

/**
* Enables the default [Keyer]. This means that data types that don't have a registered [Keyer]
* will have a memory cache key created for them using [Any.toString].
*
* If disabled (the default), images will not be cached if they do not have a registered [Keyer]
* for the associated data type.
*/
fun ImageLoader.Builder.defaultKeyerEnabled(enable: Boolean) = apply {
extras[defaultKeyerEnabledKey] = enable
}

internal val RealImageLoader.Options.defaultKeyerEnabled: Boolean
get() = defaults.extras.getOrDefault(defaultKeyerEnabledKey)

private val defaultKeyerEnabledKey = Extras.Key(default = false)
21 changes: 21 additions & 0 deletions coil-core/src/commonMain/kotlin/coil3/key/AnyKeyer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package coil3.key

import coil3.request.Options

@Suppress("UNCHECKED_CAST")
internal fun <T : Any> CacheEnabledKeyer(): Keyer<T> = CacheEnabledKeyer as Keyer<T>

private object CacheEnabledKeyer : Keyer<Any> {
override fun key(data: Any, options: Options): String {
return data.toString()
}
}

@Suppress("UNCHECKED_CAST")
internal fun <T : Any> CacheDisabledKeyer(): Keyer<T> = CacheDisabledKeyer as Keyer<T>

private object CacheDisabledKeyer : Keyer<Any> {
override fun key(data: Any, options: Options): String? {
return null
}
}

0 comments on commit 6540b67

Please sign in to comment.