diff --git a/app/build.gradle b/app/build.gradle index bd31d5785..059544422 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -15,8 +15,8 @@ android { applicationId 'org.koitharu.kotatsu' minSdkVersion 21 targetSdkVersion 33 - versionCode 554 - versionName '5.2.2' + versionCode 555 + versionName '5.2.3' generatedDensities = [] testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/history/data/HistoryRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/history/data/HistoryRepository.kt index 3cd576e87..28f6bf451 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/history/data/HistoryRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/history/data/HistoryRepository.kt @@ -45,6 +45,13 @@ class HistoryRepository @Inject constructor( return entity.manga.toManga(entity.tags.toMangaTags()) } + fun observeLast(): Flow { + return db.historyDao.observeAll(1).map { + val first = it.firstOrNull() + first?.manga?.toManga(first.tags.toMangaTags()) + } + } + fun observeAll(): Flow> { return db.historyDao.observeAll().mapItems { it.manga.toManga(it.tags.toMangaTags()) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/main/domain/ReadingResumeEnabledUseCase.kt b/app/src/main/kotlin/org/koitharu/kotatsu/main/domain/ReadingResumeEnabledUseCase.kt new file mode 100644 index 000000000..26e9d26c3 --- /dev/null +++ b/app/src/main/kotlin/org/koitharu/kotatsu/main/domain/ReadingResumeEnabledUseCase.kt @@ -0,0 +1,31 @@ +package org.koitharu.kotatsu.main.domain + +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.flatMapLatest +import kotlinx.coroutines.flow.flowOf +import org.koitharu.kotatsu.core.os.NetworkState +import org.koitharu.kotatsu.core.prefs.AppSettings +import org.koitharu.kotatsu.core.prefs.observeAsFlow +import org.koitharu.kotatsu.history.data.HistoryRepository +import org.koitharu.kotatsu.parsers.model.MangaSource +import javax.inject.Inject + +class ReadingResumeEnabledUseCase @Inject constructor( + private val networkState: NetworkState, + private val historyRepository: HistoryRepository, + private val settings: AppSettings, +) { + + operator fun invoke(): Flow = settings.observeAsFlow(AppSettings.KEY_INCOGNITO_MODE) { + isIncognitoModeEnabled + }.flatMapLatest { incognito -> + if (incognito) { + flowOf(false) + } else { + combine(networkState, historyRepository.observeLast()) { isOnline, last -> + last != null && (isOnline || last.source == MangaSource.LOCAL) + } + } + } +} diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/main/ui/MainViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/main/ui/MainViewModel.kt index b983b7cbd..cbec456a3 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/main/ui/MainViewModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/main/ui/MainViewModel.kt @@ -13,12 +13,12 @@ import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.exceptions.EmptyHistoryException import org.koitharu.kotatsu.core.github.AppUpdateRepository import org.koitharu.kotatsu.core.prefs.AppSettings -import org.koitharu.kotatsu.core.prefs.observeAsFlow 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.history.data.HistoryRepository +import org.koitharu.kotatsu.main.domain.ReadingResumeEnabledUseCase import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.tracker.domain.TrackingRepository import javax.inject.Inject @@ -29,16 +29,12 @@ class MainViewModel @Inject constructor( private val appUpdateRepository: AppUpdateRepository, trackingRepository: TrackingRepository, settings: AppSettings, + readingResumeEnabledUseCase: ReadingResumeEnabledUseCase, ) : BaseViewModel() { val onOpenReader = MutableEventFlow() - val isResumeEnabled = combine( - historyRepository.observeHasItems(), - settings.observeAsFlow(AppSettings.KEY_INCOGNITO_MODE) { isIncognitoModeEnabled }, - ) { hasItems, incognito -> - hasItems && !incognito - }.stateIn( + val isResumeEnabled = readingResumeEnabledUseCase().stateIn( scope = viewModelScope + Dispatchers.Default, started = SharingStarted.WhileSubscribed(5000), initialValue = false,