Created
February 6, 2022 15:49
-
-
Save kasem-sm/2801c0c2b2472b6765243ac9b39f7b3b to your computer and use it in GitHub Desktop.
Small workaround to show thumbnail before loading actual image with Coil.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var isOriginalImageLoaded = false | |
// Thumbnail Request | |
val thumbRequest = ImageRequest.Builder(ctx) | |
.data(thumbUrl) | |
.target( | |
onSuccess = { result -> | |
// If highRes image is not loaded yet, | |
// show the thumbnail | |
if (!isOriginalImageLoaded) { | |
binding.headerImage.setImageDrawable(result) | |
} | |
} | |
) | |
.build() | |
imageLoader.enqueue(thumbRequest) | |
val highResRequest = ImageRequest.Builder(ctx) | |
.data(highResUrl) | |
.target( | |
onSuccess = { result -> | |
isOriginalImageLoaded = true | |
binding.headerImage.setImageDrawable(result) | |
}, | |
onError = { onFailed() } | |
) | |
.build() | |
imageLoader.enqueue(highResRequest) |
Thanks for checking and improving it :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Made some modifications using an Extension Function