|
|
|
|
@ -29,6 +29,7 @@ import kotlinx.coroutines.plus
|
|
|
|
|
import org.koitharu.kotatsu.R
|
|
|
|
|
import org.koitharu.kotatsu.bookmarks.domain.Bookmark
|
|
|
|
|
import org.koitharu.kotatsu.bookmarks.domain.BookmarksRepository
|
|
|
|
|
import org.koitharu.kotatsu.core.exceptions.EmptyMangaException
|
|
|
|
|
import org.koitharu.kotatsu.core.model.getPreferredBranch
|
|
|
|
|
import org.koitharu.kotatsu.core.nav.MangaIntent
|
|
|
|
|
import org.koitharu.kotatsu.core.nav.ReaderIntent
|
|
|
|
|
@ -47,6 +48,7 @@ import org.koitharu.kotatsu.details.data.MangaDetails
|
|
|
|
|
import org.koitharu.kotatsu.details.domain.DetailsInteractor
|
|
|
|
|
import org.koitharu.kotatsu.details.domain.DetailsLoadUseCase
|
|
|
|
|
import org.koitharu.kotatsu.details.ui.pager.ChaptersPagesViewModel
|
|
|
|
|
import org.koitharu.kotatsu.details.ui.pager.EmptyMangaReason
|
|
|
|
|
import org.koitharu.kotatsu.download.ui.worker.DownloadWorker
|
|
|
|
|
import org.koitharu.kotatsu.history.data.HistoryRepository
|
|
|
|
|
import org.koitharu.kotatsu.history.domain.HistoryUpdateUseCase
|
|
|
|
|
@ -405,9 +407,11 @@ class ReaderViewModel @Inject constructor(
|
|
|
|
|
private fun loadImpl() {
|
|
|
|
|
loadingJob = launchLoadingJob(Dispatchers.Default + EventExceptionHandler(onLoadingError)) {
|
|
|
|
|
var exception: Exception? = null
|
|
|
|
|
var loadedDetails: MangaDetails? = null
|
|
|
|
|
try {
|
|
|
|
|
detailsLoadUseCase(intent, force = false)
|
|
|
|
|
.collect { details ->
|
|
|
|
|
loadedDetails = details
|
|
|
|
|
if (mangaDetails.value == null) {
|
|
|
|
|
mangaDetails.value = details
|
|
|
|
|
}
|
|
|
|
|
@ -452,9 +456,28 @@ class ReaderViewModel @Inject constructor(
|
|
|
|
|
exception = e.mergeWith(exception)
|
|
|
|
|
}
|
|
|
|
|
if (readingState.value == null) {
|
|
|
|
|
onLoadingError.call(
|
|
|
|
|
exception ?: IllegalStateException("Unable to load manga. This should never happen. Please report"),
|
|
|
|
|
val loadedManga = loadedDetails // for smart cast
|
|
|
|
|
if (loadedManga != null) {
|
|
|
|
|
mangaDetails.value = loadedManga.filterChapters(selectedBranch.value)
|
|
|
|
|
}
|
|
|
|
|
val loadingError = when {
|
|
|
|
|
exception != null -> exception
|
|
|
|
|
loadedManga == null || !loadedManga.isLoaded -> null
|
|
|
|
|
loadedManga.isRestricted -> EmptyMangaException(
|
|
|
|
|
EmptyMangaReason.RESTRICTED,
|
|
|
|
|
loadedManga.toManga(),
|
|
|
|
|
null,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
loadedManga.allChapters.isEmpty() -> EmptyMangaException(
|
|
|
|
|
EmptyMangaReason.NO_CHAPTERS,
|
|
|
|
|
loadedManga.toManga(),
|
|
|
|
|
null,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
else -> null
|
|
|
|
|
} ?: IllegalStateException("Unable to load manga. This should never happen. Please report")
|
|
|
|
|
onLoadingError.call(loadingError)
|
|
|
|
|
} else exception?.let { e ->
|
|
|
|
|
// manga has been loaded but error occurred
|
|
|
|
|
errorEvent.call(e)
|
|
|
|
|
|