Skip to content

Commit

Permalink
Deprecate CoilContentProvider. (#293)
Browse files Browse the repository at this point in the history
* Deprecate CoilContentProvider.

* Revert README.md changes.
  • Loading branch information
colinrtwhite authored Feb 15, 2020
1 parent 1fb653e commit 33c2cda
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 37 deletions.
24 changes: 12 additions & 12 deletions coil-base/src/main/java/coil/ImageLoaderBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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))
}

/**
Expand All @@ -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))
}

/**
Expand All @@ -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))
}

/**
Expand All @@ -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()

Expand All @@ -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,
Expand All @@ -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()
}
}
28 changes: 20 additions & 8 deletions coil-default/src/main/java/coil/Coil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package coil

import android.content.Context
import coil.util.CoilContentProvider

/**
Expand All @@ -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.
Expand All @@ -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
}
}
96 changes: 88 additions & 8 deletions coil-default/src/main/java/coil/api/Coils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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 = {}
Expand All @@ -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 = {}
Expand All @@ -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 = {}
Expand All @@ -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 = {}
Expand All @@ -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 = {}
Expand All @@ -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 = {}
Expand All @@ -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 = {}
Expand Down
16 changes: 8 additions & 8 deletions coil-default/src/main/java/coil/api/ImageViews.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down
2 changes: 1 addition & 1 deletion docs/migrating.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
```

0 comments on commit 33c2cda

Please sign in to comment.