From 02c9a933d2b9a4ee1b28a266a0de38de2f8ff1b4 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Tue, 20 Jun 2023 17:06:18 +0300 Subject: [PATCH] Fix offline manga details --- .../koitharu/kotatsu/core/util/ext/Flow.kt | 21 +++++++++++++++++++ .../kotatsu/details/ui/DetailsViewModel.kt | 8 +++++-- 2 files changed, 27 insertions(+), 2 deletions(-) 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 2aa0c1e62..5312142c8 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 @@ -4,6 +4,7 @@ import android.os.SystemClock import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onCompletion @@ -62,3 +63,23 @@ fun Flow.zipWithPrevious(): Flow> = flow { emit(result) } } + +@Suppress("UNCHECKED_CAST") +fun combine( + flow: Flow, + flow2: Flow, + flow3: Flow, + flow4: Flow, + flow5: Flow, + flow6: Flow, + transform: suspend (T1, T2, T3, T4, T5, T6) -> R +): Flow = combine(flow, flow2, flow3, flow4, flow5, flow6) { args: Array<*> -> + transform( + args[0] as T1, + args[1] as T2, + args[2] as T3, + args[3] as T4, + args[4] as T5, + args[5] as T6, + ) +} 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 2a7f0ae5d..ea7e8eed0 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 @@ -31,12 +31,14 @@ import org.koitharu.kotatsu.R import org.koitharu.kotatsu.bookmarks.domain.Bookmark import org.koitharu.kotatsu.bookmarks.domain.BookmarksRepository import org.koitharu.kotatsu.core.model.getPreferredBranch +import org.koitharu.kotatsu.core.os.NetworkState import org.koitharu.kotatsu.core.parser.MangaIntent import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.observeAsStateFlow 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.combine import org.koitharu.kotatsu.core.util.ext.computeSize import org.koitharu.kotatsu.core.util.ext.requireValue import org.koitharu.kotatsu.core.util.ext.sanitize @@ -72,6 +74,7 @@ class DetailsViewModel @Inject constructor( savedStateHandle: SavedStateHandle, private val deleteLocalMangaUseCase: DeleteLocalMangaUseCase, private val doubleMangaLoadUseCase: DoubleMangaLoadUseCase, + networkState: NetworkState, ) : BaseViewModel() { private val intent = MangaIntent(savedStateHandle) @@ -176,8 +179,9 @@ class DetailsViewModel @Inject constructor( selectedBranch, newChaptersCount, bookmarks, - ) { manga, history, branch, news, bookmarks -> - mapChapters(manga?.remote, manga?.local, history, news, branch, bookmarks) + networkState, + ) { manga, history, branch, news, bookmarks, isOnline -> + mapChapters(manga?.remote?.takeIf { isOnline }, manga?.local, history, news, branch, bookmarks) }, isChaptersReversed, chaptersQuery,