From 14469638b385d7f66d8cba17dac4de26bd012f67 Mon Sep 17 00:00:00 2001 From: tarsin Date: Sun, 19 Feb 2023 18:14:25 +0800 Subject: [PATCH] Delegate all image request listener In case request being cancelled --- .../hippo/ehviewer/coil/MergeInterceptor.kt | 65 +++++++++---------- 1 file changed, 32 insertions(+), 33 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 e7075a340..b19548515 100644 --- a/app/src/main/java/com/hippo/ehviewer/coil/MergeInterceptor.kt +++ b/app/src/main/java/com/hippo/ehviewer/coil/MergeInterceptor.kt @@ -22,44 +22,43 @@ object MergeInterceptor : Interceptor { val originRequest = chain.request val originListener = originRequest.listener val key = originRequest.data as String - var newRequest = originRequest + val newRequest = ImageRequest.Builder(originRequest).listener( + { originListener?.onStart(it) }, + { + originListener?.onCancel(it) + 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) + } + } + }, + { request, result -> + originListener?.onError(request, result) + // Wake all pending continuations since this request is to be failed + notifyScope.launch { + pendingContinuationMapLock.withLock { + pendingContinuationMap.remove(key)?.forEach { it.resume(Unit) } + } + } + }, + { request, result -> + originListener?.onSuccess(request, result) + // Wake all pending continuations shared with the same memory key since we have written it to memory cache + notifyScope.launch { + pendingContinuationMapLock.withLock { + pendingContinuationMap.remove(key)?.forEach { it.resume(Unit) } + } + } + } + ).build() pendingContinuationMapLock.lock() val existPendingContinuations = pendingContinuationMap[key] if (existPendingContinuations == null) { pendingContinuationMap[key] = mutableListOf() - newRequest = ImageRequest.Builder(originRequest).listener( - { originListener?.onStart(it) }, - { - originListener?.onCancel(it) - 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) - } - } - }, - { request, result -> - originListener?.onError(request, result) - // Wake all pending continuations since this request is to be failed - notifyScope.launch { - pendingContinuationMapLock.withLock { - pendingContinuationMap.remove(key)?.forEach { it.resume(Unit) } - } - } - }, - { request, result -> - originListener?.onSuccess(request, result) - // Wake all pending continuations shared with the same memory key since we have written it to memory cache - notifyScope.launch { - pendingContinuationMapLock.withLock { - pendingContinuationMap.remove(key)?.forEach { it.resume(Unit) } - } - } - } - ).build() pendingContinuationMapLock.unlock() } else { existPendingContinuations.apply {