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 01629c731..a328a8e7c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/GlideExtensions.kt +++ b/app/src/main/java/com/keylesspalace/tusky/util/GlideExtensions.kt @@ -5,6 +5,7 @@ import com.bumptech.glide.load.DataSource import com.bumptech.glide.load.engine.GlideException import com.bumptech.glide.request.RequestListener import com.bumptech.glide.request.target.Target +import kotlin.coroutines.Continuation import kotlin.coroutines.resume import kotlin.coroutines.resumeWithException import kotlinx.coroutines.suspendCancellableCoroutine @@ -17,32 +18,41 @@ suspend fun RequestBuilder.submitAsync( height: Int = Target.SIZE_ORIGINAL ): R { return suspendCancellableCoroutine { continuation -> - val target = addListener( - object : RequestListener { - override fun onLoadFailed( - e: GlideException?, - model: Any?, - target: Target, - isFirstResource: Boolean - ): Boolean { - continuation.resumeWithException(e ?: GlideException("Image loading failed")) - return false - } - - override fun onResourceReady( - resource: R & Any, - model: Any, - target: Target?, - dataSource: DataSource, - isFirstResource: Boolean - ): Boolean { - if (target?.request?.isComplete == true) { - continuation.resume(resource) - } - return false - } - } - ).submit(width, height) + val target = addListener(ContinuationRequestListener(continuation)) + .submit(width, height) continuation.invokeOnCancellation { target.cancel(true) } } } + +private class ContinuationRequestListener(continuation: Continuation) : RequestListener { + private var continuation: Continuation? = continuation + + override fun onLoadFailed( + e: GlideException?, + model: Any?, + target: Target, + isFirstResource: Boolean + ): Boolean { + continuation?.let { + continuation = null + it.resumeWithException(e ?: GlideException("Image loading failed")) + } + return false + } + + override fun onResourceReady( + resource: R & Any, + model: Any, + target: Target?, + dataSource: DataSource, + isFirstResource: Boolean + ): Boolean { + continuation?.let { + if (target?.request?.isComplete == true) { + continuation = null + it.resume(resource) + } + } + return false + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 60e5da0f3..53a0b10e8 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -49,7 +49,7 @@ retrofit = "2.11.0" robolectric = "4.13" sparkbutton = "4.2.0" touchimageview = "3.6" -truth = "1.4.3" +truth = "1.4.4" turbine = "1.1.0" unified-push = "2.4.0" xmlwriter = "1.0.4" diff --git a/gradle/verification-metadata.xml b/gradle/verification-metadata.xml index 55379f147..3731b5751 100644 --- a/gradle/verification-metadata.xml +++ b/gradle/verification-metadata.xml @@ -4772,6 +4772,14 @@ + + + + + + + + @@ -4782,6 +4790,11 @@ + + + + +