From 841ac0cb74a74a65c08bbcbf23dfe0263d8106ab Mon Sep 17 00:00:00 2001 From: tarsin Date: Mon, 27 Feb 2023 23:01:41 +0800 Subject: [PATCH] fixup! Implement image request merging in standalone Interceptor --- .../hippo/ehviewer/coil/MergeInterceptor.kt | 32 +++++++++---------- .../java/com/hippo/widget/LoadImageView.kt | 6 +--- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/coil/MergeInterceptor.kt b/app/src/main/java/com/hippo/ehviewer/coil/MergeInterceptor.kt index a67330581..0d23dbea5 100644 --- a/app/src/main/java/com/hippo/ehviewer/coil/MergeInterceptor.kt +++ b/app/src/main/java/com/hippo/ehviewer/coil/MergeInterceptor.kt @@ -3,6 +3,7 @@ package com.hippo.ehviewer.coil import coil.intercept.Interceptor import coil.request.ImageRequest import coil.request.ImageResult +import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob @@ -15,8 +16,7 @@ import kotlin.coroutines.Continuation import kotlin.coroutines.resume object MergeInterceptor : Interceptor { - private val pendingContinuationMap: HashMap>> = - hashMapOf() + private val pendingContinuationMap: HashMap>> = hashMapOf() private val pendingContinuationMapLock = Mutex() private val notifyScope = CoroutineScope(Dispatchers.IO + SupervisorJob()) override suspend fun intercept(chain: Interceptor.Chain): ImageResult { @@ -24,8 +24,8 @@ object MergeInterceptor : Interceptor { val originListener = originRequest.listener val key = originRequest.data as String val newRequest = ImageRequest.Builder(originRequest).listener( - {}, - {}, // Only original request's onStart and onCancel will get called + { originListener?.onStart(it) }, + { originListener?.onCancel(it) }, { request, result -> originListener?.onError(request, result) // Wake all pending continuations since this request is to be failed @@ -61,18 +61,18 @@ object MergeInterceptor : Interceptor { } } - return withContext(originRequest.interceptorDispatcher) { chain.proceed(newRequest) } - } - - fun onCancel(key: String) { - notifyScope.launch { - pendingContinuationMapLock.withLock { - // Wake up a pending continuation to continue executing task - val successor = - pendingContinuationMap[key]?.removeFirstOrNull()?.apply { resume(Unit) } - // If no successor, delete this entry from hashmap - successor ?: pendingContinuationMap.remove(key) + try { + return withContext(originRequest.interceptorDispatcher) { chain.proceed(newRequest) } + } catch (e: CancellationException) { + notifyScope.launch { + pendingContinuationMapLock.withLock { + // Wake up a pending continuation to continue executing task + val successor = pendingContinuationMap[key]?.removeFirstOrNull()?.apply { resume(Unit) } + // If no successor, delete this entry from hashmap + successor ?: pendingContinuationMap.remove(key) + } } + throw e } } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/hippo/widget/LoadImageView.kt b/app/src/main/java/com/hippo/widget/LoadImageView.kt index e56f257f4..5939a60a9 100644 --- a/app/src/main/java/com/hippo/widget/LoadImageView.kt +++ b/app/src/main/java/com/hippo/widget/LoadImageView.kt @@ -27,7 +27,6 @@ import coil.size.Size import com.hippo.drawable.PreciselyClipDrawable import com.hippo.ehviewer.EhApplication import com.hippo.ehviewer.R -import com.hippo.ehviewer.coil.MergeInterceptor private val application = EhApplication.application private val errorDrawable = AppCompatResources.getDrawable(application, R.drawable.image_failed) @@ -85,10 +84,7 @@ open class LoadImageView @JvmOverloads constructor( if (!crossfade) crossfade(false) listener( { setRetry(false) }, - { - MergeInterceptor.onCancel(url) - setRetry(true) - }, + { setRetry(true) }, { _, _ -> super.setImageDrawable(errorDrawable) setRetry(true)