From ab753787b0ee3025e7bfa0cfb62479c8f6bceb39 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Mon, 21 Aug 2023 17:33:43 +0300 Subject: [PATCH] Fix page loading retry --- .../org/koitharu/kotatsu/core/util/ext/Coroutines.kt | 9 +++++++++ .../org/koitharu/kotatsu/reader/domain/PageLoader.kt | 8 +++----- .../kotatsu/reader/ui/pager/PageHolderDelegate.kt | 4 +++- .../kotatsu/reader/ui/pager/standard/PageHolder.kt | 2 +- app/src/main/res/layout/item_page_thumb.xml | 4 ++-- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coroutines.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coroutines.kt index 35e2a1e56..a7c16d706 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coroutines.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coroutines.kt @@ -10,6 +10,7 @@ import androidx.lifecycle.ProcessLifecycleOwner import androidx.lifecycle.lifecycleScope import dagger.hilt.android.lifecycle.RetainedLifecycle import kotlinx.coroutines.CancellableContinuation +import kotlinx.coroutines.Deferred import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.suspendCancellableCoroutine import org.koitharu.kotatsu.core.util.RetainedLifecycleCoroutineScope @@ -70,3 +71,11 @@ private fun Lifecycle.removeObserverFromAnyThread(observer: LifecycleObserver) { removeObserver(observer) } } + +fun Deferred.getCompletionResultOrNull(): Result? = if (isCompleted) { + getCompletionExceptionOrNull()?.let { error -> + Result.failure(error) + } ?: Result.success(getCompleted()) +} else { + null +} diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/domain/PageLoader.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/domain/PageLoader.kt index aa49d0786..0381a40df 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/domain/PageLoader.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/domain/PageLoader.kt @@ -34,6 +34,7 @@ import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.util.FileSize import org.koitharu.kotatsu.core.util.RetainedLifecycleCoroutineScope import org.koitharu.kotatsu.core.util.ext.ensureSuccess +import org.koitharu.kotatsu.core.util.ext.getCompletionResultOrNull import org.koitharu.kotatsu.core.util.ext.isNotEmpty import org.koitharu.kotatsu.core.util.ext.isPowerSaveMode import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug @@ -222,12 +223,9 @@ class PageLoader @Inject constructor( } private fun Deferred.isValid(): Boolean { - return if (isCompleted) { - val file = getCompleted() + return getCompletionResultOrNull()?.map { file -> file.exists() && file.isNotEmpty() - } else { - true - } + }?.getOrDefault(false) ?: true } private class InternalErrorHandler : AbstractCoroutineContextElement(CoroutineExceptionHandler), diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/PageHolderDelegate.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/PageHolderDelegate.kt index 1935d104a..d874a12b1 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/PageHolderDelegate.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/PageHolderDelegate.kt @@ -16,6 +16,7 @@ import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import kotlinx.coroutines.plus +import kotlinx.coroutines.yield import org.koitharu.kotatsu.core.exceptions.resolve.ExceptionResolver import org.koitharu.kotatsu.core.os.NetworkState import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug @@ -137,12 +138,13 @@ class PageHolderDelegate( state = State.LOADING error = null callback.onLoadingStarted() + yield() try { val task = loader.loadPageAsync(data, force) file = coroutineScope { val progressObserver = observeProgress(this, task.progressAsFlow()) val file = task.await() - progressObserver.cancel() + progressObserver.cancelAndJoin() file } state = State.LOADED diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/standard/PageHolder.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/standard/PageHolder.kt index 2cacd4eea..9c568fad6 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/standard/PageHolder.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/standard/PageHolder.kt @@ -116,7 +116,7 @@ open class PageHolder( bindingInfo.progressBar.hide() } - override fun onClick(v: View) { + final override fun onClick(v: View) { when (v.id) { R.id.button_retry -> delegate.retry(boundData?.toMangaPage() ?: return) R.id.button_error_details -> delegate.showErrorDetails(boundData?.url) diff --git a/app/src/main/res/layout/item_page_thumb.xml b/app/src/main/res/layout/item_page_thumb.xml index 5d3152303..0b2a8050f 100644 --- a/app/src/main/res/layout/item_page_thumb.xml +++ b/app/src/main/res/layout/item_page_thumb.xml @@ -3,10 +3,10 @@ xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" - style="@style/Widget.Material3.CardView.Outlined" + style="?materialCardViewOutlinedStyle" android:layout_width="match_parent" android:layout_height="wrap_content" - app:cardBackgroundColor="?scrimBackground"> + app:cardBackgroundColor="?colorBackgroundFloating">