diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Flow.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Flow.kt index 28e1189b7..0422b48c8 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Flow.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Flow.kt @@ -26,6 +26,17 @@ fun Flow.onFirst(action: suspend (T) -> Unit): Flow { } } +fun Flow.onEachWhile(action: suspend (T) -> Boolean): Flow { + var isCalled = false + return onEach { + if (!isCalled) { + isCalled = action(it) + } + }.onCompletion { + isCalled = false + } +} + inline fun Flow>.mapItems(crossinline transform: (T) -> R): Flow> { return map { list -> list.map(transform) } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/DetailsViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/DetailsViewModel.kt index d8465a55b..74449e15c 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/DetailsViewModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/DetailsViewModel.kt @@ -33,7 +33,7 @@ import org.koitharu.kotatsu.core.ui.BaseViewModel import org.koitharu.kotatsu.core.util.ext.MutableEventFlow import org.koitharu.kotatsu.core.util.ext.call import org.koitharu.kotatsu.core.util.ext.computeSize -import org.koitharu.kotatsu.core.util.ext.onFirst +import org.koitharu.kotatsu.core.util.ext.onEachWhile import org.koitharu.kotatsu.core.util.ext.requireValue import org.koitharu.kotatsu.details.data.MangaDetails import org.koitharu.kotatsu.details.domain.BranchComparator @@ -313,11 +313,15 @@ class DetailsViewModel @Inject constructor( private fun doLoad() = launchLoadingJob(Dispatchers.Default) { detailsLoadUseCase.invoke(intent) - .onFirst { + .onEachWhile { + if (it.allChapters.isEmpty()) { + return@onEachWhile false + } val manga = it.toManga() // find default branch val hist = historyRepository.getOne(manga) selectedBranch.value = manga.getPreferredBranch(hist) + true }.collect { details.value = it }