I am using coil(version 2.1.0) to load images from URL. When there is network connection, the images are loading fine. However, when there is no network connection, the images are not being pulled from the cache as I expected them. Here's a block of code I have.
class App : Application(), ImageLoaderFactory {
override fun newImageLoader(): ImageLoader {
return ImageLoader.Builder(this)
.memoryCache {
MemoryCache.Builder(this)
.maxSizePercent(0.25)
.build()
}
.diskCache {
DiskCache.Builder()
.directory(cacheDir.resolve("image_cache"))
.maxSizeBytes(5 * 1024 * 1024)
.build()
}
.build()
}
}
In Compose:
val context = LocalContext.current
val placeholderImage = R.drawable.ic_placeholder
val imageRequest = ImageRequest.Builder(context)
.data(imageUrl)
.memoryCacheKey(imageUrl)
.diskCacheKey(imageUrl)
.placeholder(placeholderImage)
.error(placeholderImage)
.fallback(placeholderImage)
.diskCachePolicy(CachePolicy.ENABLED)
.memoryCachePolicy(CachePolicy.ENABLED)
.transformations(CircleCropTransformation())
.build()
AsyncImage(
model = imageRequest,
modifier = Modifier.size(64.dp),
contentDescription = null,
imageLoader = context.imageLoader
)
When the device is offline, it only loads the placeholder image instead of the image from cache as expected. What am I missing here?