From f9221b3d75d32f2e732aa9ebcdddbe7107b05cda Mon Sep 17 00:00:00 2001 From: Christophe Beyls Date: Sun, 12 May 2024 11:25:49 +0200 Subject: [PATCH] Improve RequestBuilder.submitAsync() to ensure the coroutine continuation is resumed only once (#4436) `RequestListener.onResourceReady()` may also be called to provide the placeholder image when the request is starting or when it is cleared. Check if the current request is complete (its status set to COMPLETE) to determine if the Glide resource is for a thumbnail/placeholder or the final image, and resume the coroutine only for the final image (or in case of error). This logic is [borrowed from the Glide Flow API](https://github.com/bumptech/glide/blob/a7351b0ecf6656ad937fbc52fe9e90d3b289c265/integration/ktx/src/main/java/com/bumptech/glide/integration/ktx/Flows.kt#L378). --- .../java/com/keylesspalace/tusky/util/GlideExtensions.kt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/util/GlideExtensions.kt b/app/src/main/java/com/keylesspalace/tusky/util/GlideExtensions.kt index bc5ad2b3a..01629c731 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/GlideExtensions.kt +++ b/app/src/main/java/com/keylesspalace/tusky/util/GlideExtensions.kt @@ -13,8 +13,8 @@ import kotlinx.coroutines.suspendCancellableCoroutine * Allows waiting for a Glide request to complete without blocking a background thread. */ suspend fun RequestBuilder.submitAsync( - width: Int = Int.MIN_VALUE, - height: Int = Int.MIN_VALUE + width: Int = Target.SIZE_ORIGINAL, + height: Int = Target.SIZE_ORIGINAL ): R { return suspendCancellableCoroutine { continuation -> val target = addListener( @@ -36,7 +36,9 @@ suspend fun RequestBuilder.submitAsync( dataSource: DataSource, isFirstResource: Boolean ): Boolean { - continuation.resume(resource) + if (target?.request?.isComplete == true) { + continuation.resume(resource) + } return false } }