Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Rename ImageLoaderProvider to ImageLoaderFactory. #311

Merged
merged 2 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the method be setImageLoaderFactory instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I get it now. Probably best to keep it like this so the consumer gets a choice of how they want to provide the image loader 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I think it'll make more sense too when we can make ImageLoaderFactory a fun interface in Kotlin 1.4 so it's callable using Coil.setImageLoader { ImageLoader(context) }. Mostly I wanted to give both methods the same name to show they overwrite each other.

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