diff --git a/coil-base/src/main/java/coil/ImageLoaderBuilder.kt b/coil-base/src/main/java/coil/ImageLoaderBuilder.kt index 082915a104..1dc63c7f70 100644 --- a/coil-base/src/main/java/coil/ImageLoaderBuilder.kt +++ b/coil-base/src/main/java/coil/ImageLoaderBuilder.kt @@ -29,17 +29,17 @@ import okhttp3.OkHttpClient /** Builder for an [ImageLoader]. */ @BuilderMarker -class ImageLoaderBuilder(private val context: Context) { +class ImageLoaderBuilder(context: Context) { - private var callFactory: Call.Factory? = null + private val applicationContext = context.applicationContext + private var callFactory: Call.Factory? = null private var registry: ComponentRegistry? = null - - private var availableMemoryPercentage: Double = Utils.getDefaultAvailableMemoryPercentage(context) - private var bitmapPoolPercentage: Double = Utils.getDefaultBitmapPoolPercentage() - private var defaults = DefaultRequestOptions() + private var availableMemoryPercentage = Utils.getDefaultAvailableMemoryPercentage(applicationContext) + private var bitmapPoolPercentage = Utils.getDefaultBitmapPoolPercentage() + /** * Set the [OkHttpClient] used for network requests. * @@ -205,7 +205,7 @@ class ImageLoaderBuilder(private val context: Context) { * Set the default placeholder drawable to use when a request starts. */ fun placeholder(@DrawableRes drawableResId: Int) = apply { - this.defaults = this.defaults.copy(placeholder = context.getDrawableCompat(drawableResId)) + this.defaults = this.defaults.copy(placeholder = applicationContext.getDrawableCompat(drawableResId)) } /** @@ -219,7 +219,7 @@ class ImageLoaderBuilder(private val context: Context) { * Set the default error drawable to use when a request fails. */ fun error(@DrawableRes drawableResId: Int) = apply { - this.defaults = this.defaults.copy(error = context.getDrawableCompat(drawableResId)) + this.defaults = this.defaults.copy(error = applicationContext.getDrawableCompat(drawableResId)) } /** @@ -233,7 +233,7 @@ class ImageLoaderBuilder(private val context: Context) { * Set the default fallback drawable to use if [Request.data] is null. */ fun fallback(@DrawableRes drawableResId: Int) = apply { - this.defaults = this.defaults.copy(error = context.getDrawableCompat(drawableResId)) + this.defaults = this.defaults.copy(error = applicationContext.getDrawableCompat(drawableResId)) } /** @@ -247,7 +247,7 @@ class ImageLoaderBuilder(private val context: Context) { * Create a new [ImageLoader] instance. */ fun build(): ImageLoader { - val availableMemorySize = Utils.calculateAvailableMemorySize(context, availableMemoryPercentage) + val availableMemorySize = Utils.calculateAvailableMemorySize(applicationContext, availableMemoryPercentage) val bitmapPoolSize = (bitmapPoolPercentage * availableMemorySize).toLong() val memoryCacheSize = (availableMemorySize - bitmapPoolSize).toInt() @@ -256,7 +256,7 @@ class ImageLoaderBuilder(private val context: Context) { val memoryCache = MemoryCache(referenceCounter, memoryCacheSize) return RealImageLoader( - context = context, + context = applicationContext, defaults = defaults, bitmapPool = bitmapPool, referenceCounter = referenceCounter, @@ -268,7 +268,7 @@ class ImageLoaderBuilder(private val context: Context) { private fun buildDefaultCallFactory() = lazyCallFactory { OkHttpClient.Builder() - .cache(CoilUtils.createDefaultCache(context)) + .cache(CoilUtils.createDefaultCache(applicationContext)) .build() } } diff --git a/coil-default/src/main/java/coil/Coil.kt b/coil-default/src/main/java/coil/Coil.kt index 3286f47bca..389a555cbf 100644 --- a/coil-default/src/main/java/coil/Coil.kt +++ b/coil-default/src/main/java/coil/Coil.kt @@ -2,6 +2,7 @@ package coil +import android.content.Context import coil.util.CoilContentProvider /** @@ -15,8 +16,18 @@ object Coil { /** * Get the default [ImageLoader] instance. Creates a new instance if none has been set. */ + @Deprecated( + message = "Migrate to loader(context).", + replaceWith = ReplaceWith("loader(context)") + ) @JvmStatic - fun loader(): ImageLoader = imageLoader ?: buildDefaultImageLoader() + fun loader(): ImageLoader = imageLoader ?: buildDefaultImageLoader(CoilContentProvider.context) + + /** + * Get the default [ImageLoader] instance. Creates a new instance if none has been set. + */ + @JvmStatic + fun loader(context: Context): ImageLoader = imageLoader ?: buildDefaultImageLoader(context) /** * Set the default [ImageLoader] instance. Shutdown the current instance. @@ -41,13 +52,14 @@ object Coil { } @Synchronized - private fun buildDefaultImageLoader(): ImageLoader { + private fun buildDefaultImageLoader(context: Context): ImageLoader { // Check again in case imageLoader was just set. - return imageLoader ?: run { - val loader = imageLoaderInitializer?.invoke() ?: ImageLoader(CoilContentProvider.context) - imageLoaderInitializer = null - setDefaultImageLoader(loader) - loader - } + imageLoader?.let { return it } + + // Create a new ImageLoader. + val loader = imageLoaderInitializer?.invoke() ?: ImageLoader(context) + imageLoaderInitializer = null + setDefaultImageLoader(loader) + return loader } } diff --git a/coil-default/src/main/java/coil/api/Coils.kt b/coil-default/src/main/java/coil/api/Coils.kt index f220e310d8..2a1ca22592 100644 --- a/coil-default/src/main/java/coil/api/Coils.kt +++ b/coil-default/src/main/java/coil/api/Coils.kt @@ -32,8 +32,18 @@ inline fun Coil.load( context: Context, uri: String?, builder: LoadRequestBuilder.() -> Unit = {} -): RequestDisposable = loader().load(context, uri, builder) +): RequestDisposable = loader(context).load(context, uri, builder) +suspend inline fun Coil.get( + context: Context, + uri: String, + builder: GetRequestBuilder.() -> Unit = {} +): Drawable = loader(context).get(uri, builder) + +@Deprecated( + message = "Migrate to Coil.get(context, uri).", + replaceWith = ReplaceWith("get(context, uri, builder)") +) suspend inline fun Coil.get( uri: String, builder: GetRequestBuilder.() -> Unit = {} @@ -46,8 +56,18 @@ inline fun Coil.load( context: Context, url: HttpUrl?, builder: LoadRequestBuilder.() -> Unit = {} -): RequestDisposable = loader().load(context, url, builder) +): RequestDisposable = loader(context).load(context, url, builder) + +suspend inline fun Coil.get( + context: Context, + url: HttpUrl, + builder: GetRequestBuilder.() -> Unit = {} +): Drawable = loader(context).get(url, builder) +@Deprecated( + message = "Migrate to Coil.get(context, url).", + replaceWith = ReplaceWith("get(context, url, builder)") +) suspend inline fun Coil.get( url: HttpUrl, builder: GetRequestBuilder.() -> Unit = {} @@ -60,8 +80,18 @@ inline fun Coil.load( context: Context, uri: Uri?, builder: LoadRequestBuilder.() -> Unit = {} -): RequestDisposable = loader().load(context, uri, builder) +): RequestDisposable = loader(context).load(context, uri, builder) +suspend inline fun Coil.get( + context: Context, + uri: Uri, + builder: GetRequestBuilder.() -> Unit = {} +): Drawable = loader(context).get(uri, builder) + +@Deprecated( + message = "Migrate to Coil.get(context, uri).", + replaceWith = ReplaceWith("get(context, uri, builder)") +) suspend inline fun Coil.get( uri: Uri, builder: GetRequestBuilder.() -> Unit = {} @@ -74,8 +104,18 @@ inline fun Coil.load( context: Context, file: File?, builder: LoadRequestBuilder.() -> Unit = {} -): RequestDisposable = loader().load(context, file, builder) +): RequestDisposable = loader(context).load(context, file, builder) + +suspend inline fun Coil.get( + context: Context, + file: File, + builder: GetRequestBuilder.() -> Unit = {} +): Drawable = loader(context).get(file, builder) +@Deprecated( + message = "Migrate to Coil.get(context, file).", + replaceWith = ReplaceWith("get(context, file, builder)") +) suspend inline fun Coil.get( file: File, builder: GetRequestBuilder.() -> Unit = {} @@ -88,8 +128,18 @@ inline fun Coil.load( context: Context, @DrawableRes drawableRes: Int, builder: LoadRequestBuilder.() -> Unit = {} -): RequestDisposable = loader().load(context, drawableRes, builder) +): RequestDisposable = loader(context).load(context, drawableRes, builder) +suspend inline fun Coil.get( + context: Context, + @DrawableRes drawableRes: Int, + builder: GetRequestBuilder.() -> Unit = {} +): Drawable = loader(context).get(drawableRes, builder) + +@Deprecated( + message = "Migrate to Coil.get(context, drawableRes).", + replaceWith = ReplaceWith("get(context, drawableRes, builder)") +) suspend inline fun Coil.get( @DrawableRes drawableRes: Int, builder: GetRequestBuilder.() -> Unit = {} @@ -102,8 +152,18 @@ inline fun Coil.load( context: Context, drawable: Drawable?, builder: LoadRequestBuilder.() -> Unit = {} -): RequestDisposable = loader().load(context, drawable, builder) +): RequestDisposable = loader(context).load(context, drawable, builder) + +suspend inline fun Coil.get( + context: Context, + drawable: Drawable, + builder: GetRequestBuilder.() -> Unit = {} +): Drawable = loader(context).get(drawable, builder) +@Deprecated( + message = "Migrate to Coil.get(context, drawable).", + replaceWith = ReplaceWith("get(context, drawable, builder)") +) suspend inline fun Coil.get( drawable: Drawable, builder: GetRequestBuilder.() -> Unit = {} @@ -116,8 +176,18 @@ inline fun Coil.load( context: Context, bitmap: Bitmap?, builder: LoadRequestBuilder.() -> Unit = {} -): RequestDisposable = loader().load(context, bitmap, builder) +): RequestDisposable = loader(context).load(context, bitmap, builder) +suspend inline fun Coil.get( + context: Context, + bitmap: Bitmap, + builder: GetRequestBuilder.() -> Unit = {} +): Drawable = loader(context).get(bitmap, builder) + +@Deprecated( + message = "Migrate to Coil.get(context, bitmap).", + replaceWith = ReplaceWith("get(context, bitmap, builder)") +) suspend inline fun Coil.get( bitmap: Bitmap, builder: GetRequestBuilder.() -> Unit = {} @@ -130,8 +200,18 @@ inline fun Coil.loadAny( context: Context, data: Any?, builder: LoadRequestBuilder.() -> Unit = {} -): RequestDisposable = loader().loadAny(context, data, builder) +): RequestDisposable = loader(context).loadAny(context, data, builder) + +suspend inline fun Coil.getAny( + context: Context, + data: Any, + builder: GetRequestBuilder.() -> Unit = {} +): Drawable = loader(context).getAny(data, builder) +@Deprecated( + message = "Migrate to Coil.getAny(context, data).", + replaceWith = ReplaceWith("getAny(context, data, builder)") +) suspend inline fun Coil.getAny( data: Any, builder: GetRequestBuilder.() -> Unit = {} diff --git a/coil-default/src/main/java/coil/api/ImageViews.kt b/coil-default/src/main/java/coil/api/ImageViews.kt index 94f31e39ca..c056679f94 100644 --- a/coil-default/src/main/java/coil/api/ImageViews.kt +++ b/coil-default/src/main/java/coil/api/ImageViews.kt @@ -32,7 +32,7 @@ import java.io.File inline fun ImageView.load( uri: String?, - imageLoader: ImageLoader = Coil.loader(), + imageLoader: ImageLoader = Coil.loader(context), builder: LoadRequestBuilder.() -> Unit = {} ): RequestDisposable { return imageLoader.load(context, uri) { @@ -46,7 +46,7 @@ inline fun ImageView.load( inline fun ImageView.load( url: HttpUrl?, - imageLoader: ImageLoader = Coil.loader(), + imageLoader: ImageLoader = Coil.loader(context), builder: LoadRequestBuilder.() -> Unit = {} ): RequestDisposable { return imageLoader.load(context, url) { @@ -60,7 +60,7 @@ inline fun ImageView.load( inline fun ImageView.load( uri: Uri?, - imageLoader: ImageLoader = Coil.loader(), + imageLoader: ImageLoader = Coil.loader(context), builder: LoadRequestBuilder.() -> Unit = {} ): RequestDisposable { return imageLoader.load(context, uri) { @@ -74,7 +74,7 @@ inline fun ImageView.load( inline fun ImageView.load( file: File?, - imageLoader: ImageLoader = Coil.loader(), + imageLoader: ImageLoader = Coil.loader(context), builder: LoadRequestBuilder.() -> Unit = {} ): RequestDisposable { return imageLoader.load(context, file) { @@ -88,7 +88,7 @@ inline fun ImageView.load( inline fun ImageView.load( @DrawableRes drawableRes: Int, - imageLoader: ImageLoader = Coil.loader(), + imageLoader: ImageLoader = Coil.loader(context), builder: LoadRequestBuilder.() -> Unit = {} ): RequestDisposable { return imageLoader.load(context, drawableRes) { @@ -102,7 +102,7 @@ inline fun ImageView.load( inline fun ImageView.load( drawable: Drawable?, - imageLoader: ImageLoader = Coil.loader(), + imageLoader: ImageLoader = Coil.loader(context), builder: LoadRequestBuilder.() -> Unit = {} ): RequestDisposable { return imageLoader.load(context, drawable) { @@ -116,7 +116,7 @@ inline fun ImageView.load( inline fun ImageView.load( bitmap: Bitmap?, - imageLoader: ImageLoader = Coil.loader(), + imageLoader: ImageLoader = Coil.loader(context), builder: LoadRequestBuilder.() -> Unit = {} ): RequestDisposable { return imageLoader.load(context, bitmap) { @@ -130,7 +130,7 @@ inline fun ImageView.load( inline fun ImageView.loadAny( data: Any?, - imageLoader: ImageLoader = Coil.loader(), + imageLoader: ImageLoader = Coil.loader(context), builder: LoadRequestBuilder.() -> Unit = {} ): RequestDisposable { return imageLoader.loadAny(context, data) { diff --git a/coil-default/src/main/java/coil/util/CoilContentProvider.kt b/coil-default/src/main/java/coil/util/CoilContentProvider.kt index dc2449f833..97bbdf8134 100644 --- a/coil-default/src/main/java/coil/util/CoilContentProvider.kt +++ b/coil-default/src/main/java/coil/util/CoilContentProvider.kt @@ -13,6 +13,7 @@ import coil.Coil /** * A [ContentProvider] whose [Context] is used to initialize the [Coil] singleton. */ +@Deprecated("CoilContentProvider will be removed in a future release in favor of deferred initialization through Coil.loader(context).") @VisibleForTesting(otherwise = PACKAGE_PRIVATE) class CoilContentProvider : ContentProvider() { diff --git a/docs/migrating.md b/docs/migrating.md index 98abc22c24..23698b2694 100644 --- a/docs/migrating.md +++ b/docs/migrating.md @@ -101,7 +101,7 @@ val drawable = Picasso.get() .get() // Coil (suspends the current context; thread safe) -val drawable = Coil.get(url) { +val drawable = Coil.get(context, url) { size(width, height) } ```