When using Coil, and passing a ImageLoader down th...
# compose
When using Coil, and passing a ImageLoader down the composable tree, how do you usually handle this in your @Previews? I basically need a no-op ImageLoader object, I guess I simply need to make my own custom one and just call
on each of those previews, but I must imagine someone else has had this before, so I wonder if there’s something better I could do.
Right now I simply made a fake impl which I guess since it’s only gonna be used by @Preview composables I can rely on minification to get rid of it in production since I won’t be calling it from any non-preview code.
Copy code
class PreviewImageLoader(private val context: Context) : ImageLoader {
  override val components: ComponentRegistry = ComponentRegistry()
  override val defaults: DefaultRequestOptions = DefaultRequestOptions()
  override val diskCache: DiskCache? = null
  override val memoryCache: MemoryCache? = null
  override fun enqueue(request: ImageRequest): Disposable {
    return object : Disposable {
      override val isDisposed: Boolean
        get() = true
      override val job: Deferred<ImageResult>
        get() = CompletableDeferred()

      override fun dispose() {}

  override suspend fun execute(request: ImageRequest): ImageResult {
    return ErrorResult(null, request, Throwable())

  override fun newBuilder(): ImageLoader.Builder {
    return ImageLoader.Builder(context)

  override fun shutdown() {}

fun rememberPreviewImageLoader(): PreviewImageLoader {
  val context = LocalContext.current
  return remember { PreviewImageLoader(context) }