Fix page loading retry
This commit is contained in:
@@ -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 <T> Deferred<T>.getCompletionResultOrNull(): Result<T>? = if (isCompleted) {
|
||||
getCompletionExceptionOrNull()?.let { error ->
|
||||
Result.failure(error)
|
||||
} ?: Result.success(getCompleted())
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
@@ -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<File>.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),
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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">
|
||||
|
||||
<org.koitharu.kotatsu.core.ui.widgets.CoverImageView
|
||||
android:id="@+id/imageView_thumb"
|
||||
|
||||
Reference in New Issue
Block a user