Skip to content

Commit

Permalink
Rename ImageLoaderProvider to ImageLoaderFactory. (#311)
Browse files Browse the repository at this point in the history
* Rename ImageLoaderProvider to ImageLoaderFactory.

* Emphasize.
  • Loading branch information
colinrtwhite authored Mar 12, 2020
1 parent 7dd9049 commit ee3cc3c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 39 deletions.
39 changes: 20 additions & 19 deletions coil-default/src/main/java/coil/Coil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import coil.util.CoilContentProvider
object Coil {

private var imageLoader: ImageLoader? = null
private var imageLoaderProvider: ImageLoaderProvider? = null
private var imageLoaderFactory: ImageLoaderFactory? = null

/** @see imageLoader */
@Deprecated(
Expand All @@ -26,29 +26,29 @@ object Coil {
* Get the default [ImageLoader]. Creates a new instance if none has been set.
*/
@JvmStatic
fun imageLoader(context: Context): ImageLoader = imageLoader ?: buildImageLoader(context)
fun imageLoader(context: Context): ImageLoader = imageLoader ?: newImageLoader(context)

/**
* Set the default [ImageLoader]. Shutdown the current instance if there is one.
*/
@JvmStatic
fun setImageLoader(loader: ImageLoader) {
setImageLoader(object : ImageLoaderProvider {
override fun getImageLoader() = loader
setImageLoader(object : ImageLoaderFactory {
override fun newImageLoader() = loader
})
}

/**
* Set a lazy callback to create the default [ImageLoader]. Shutdown the current instance if there is one.
* The [provider] is guaranteed to be called at most once.
* Set the [ImageLoaderFactory] that will be used to create the default [ImageLoader].
* Shutdown the current instance if there is one. The [factory] is guaranteed to be called at most once.
*
* Using this method to set an explicit [provider] takes precedence over an [Application] that
* implements [ImageLoaderProvider].
* Using this method to set an explicit [factory] takes precedence over an [Application] that
* implements [ImageLoaderFactory].
*/
@JvmStatic
@Synchronized
fun setImageLoader(provider: ImageLoaderProvider) {
imageLoaderProvider = provider
fun setImageLoader(factory: ImageLoaderFactory) {
imageLoaderFactory = factory

// Shutdown the image loader after clearing the reference.
val loader = imageLoader
Expand All @@ -66,26 +66,27 @@ object Coil {

/** @see setImageLoader */
@Deprecated(
message = "Migrate to setDefaultImageLoader(ImageLoaderProvider).",
replaceWith = ReplaceWith("setImageLoader(object : ImageLoaderProvider { override fun getImageLoader() = provider() })")
message = "Migrate to setDefaultImageLoader(ImageLoaderFactory).",
replaceWith = ReplaceWith("setImageLoader(object : ImageLoaderFactory { override fun getImageLoader() = initializer() })")
)
@JvmStatic
fun setDefaultImageLoader(provider: () -> ImageLoader) {
setImageLoader(object : ImageLoaderProvider {
override fun getImageLoader() = provider()
fun setDefaultImageLoader(initializer: () -> ImageLoader) {
setImageLoader(object : ImageLoaderFactory {
override fun newImageLoader() = initializer()
})
}

/** Create and set the new default [ImageLoader]. */
@Synchronized
private fun buildImageLoader(context: Context): ImageLoader {
private fun newImageLoader(context: Context): ImageLoader {
// Check again in case imageLoader was just set.
imageLoader?.let { return it }

// Create a new ImageLoader.
val loader = imageLoaderProvider?.getImageLoader()
?: (context.applicationContext as? ImageLoaderProvider)?.getImageLoader()
val loader = imageLoaderFactory?.newImageLoader()
?: (context.applicationContext as? ImageLoaderFactory)?.newImageLoader()
?: ImageLoader(context)
imageLoaderProvider = null
imageLoaderFactory = null
setImageLoader(loader)
return loader
}
Expand Down
18 changes: 18 additions & 0 deletions coil-default/src/main/java/coil/ImageLoaderFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package coil

import android.app.Application

/**
* A factory that creates new [ImageLoader] instances.
*
* To configure how the default [ImageLoader] is created **either**:
* - Implement [ImageLoaderFactory] in your [Application].
* - **Or** call [Coil.setImageLoader] with your [ImageLoaderFactory].
*/
interface ImageLoaderFactory {

/**
* Return a new [ImageLoader].
*/
fun newImageLoader(): ImageLoader
}
17 changes: 0 additions & 17 deletions coil-default/src/main/java/coil/ImageLoaderProvider.kt

This file was deleted.

6 changes: 3 additions & 3 deletions coil-sample/src/main/java/coil/sample/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.os.Build.VERSION.SDK_INT
import android.os.Build.VERSION_CODES.P
import androidx.multidex.MultiDexApplication
import coil.ImageLoader
import coil.ImageLoaderProvider
import coil.ImageLoaderFactory
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import coil.decode.SvgDecoder
Expand All @@ -17,14 +17,14 @@ import okhttp3.Cache
import okhttp3.OkHttpClient
import java.io.File

class Application : MultiDexApplication(), ImageLoaderProvider {
class Application : MultiDexApplication(), ImageLoaderFactory {

init {
// Enable logging to the standard Android log if this is a debug build.
CoilLogger.setEnabled(BuildConfig.DEBUG)
}

override fun getImageLoader(): ImageLoader {
override fun newImageLoader(): ImageLoader {
return ImageLoader.Builder(this)
.availableMemoryPercentage(0.5) // Use 50% of the application's available memory.
.crossfade(true) // Show a short crossfade when loading images from network or disk.
Expand Down

0 comments on commit ee3cc3c

Please # to comment.