diff --git a/app/src/main/java/org/koitharu/kotatsu/details/ui/DetailsViewModel.kt b/app/src/main/java/org/koitharu/kotatsu/details/ui/DetailsViewModel.kt index b49b1adf8..8e9ce116b 100644 --- a/app/src/main/java/org/koitharu/kotatsu/details/ui/DetailsViewModel.kt +++ b/app/src/main/java/org/koitharu/kotatsu/details/ui/DetailsViewModel.kt @@ -89,8 +89,14 @@ class DetailsViewModel @AssistedInject constructor( private val favourite = favouritesRepository.observeCategoriesIds(delegate.mangaId).map { it.isNotEmpty() } .stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, false) - private val newChapters = trackingRepository.observeNewChaptersCount(delegate.mangaId) - .stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, 0) + private val newChapters = settings.observeAsFlow(AppSettings.KEY_TRACKER_ENABLED) { isTrackerEnabled } + .flatMapLatest { isEnabled -> + if (isEnabled) { + trackingRepository.observeNewChaptersCount(delegate.mangaId) + } else { + flowOf(0) + } + }.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, 0) private val chaptersQuery = MutableStateFlow("") diff --git a/app/src/main/java/org/koitharu/kotatsu/favourites/ui/list/FavouritesListViewModel.kt b/app/src/main/java/org/koitharu/kotatsu/favourites/ui/list/FavouritesListViewModel.kt index fe57434da..ee5a9d324 100644 --- a/app/src/main/java/org/koitharu/kotatsu/favourites/ui/list/FavouritesListViewModel.kt +++ b/app/src/main/java/org/koitharu/kotatsu/favourites/ui/list/FavouritesListViewModel.kt @@ -117,7 +117,11 @@ class FavouritesListViewModel @AssistedInject constructor( } override suspend fun getCounter(mangaId: Long): Int { - return trackingRepository.getNewChaptersCount(mangaId) + return if (settings.isTrackerEnabled) { + trackingRepository.getNewChaptersCount(mangaId) + } else { + 0 + } } override suspend fun getProgress(mangaId: Long): Float { diff --git a/app/src/main/java/org/koitharu/kotatsu/history/ui/HistoryListViewModel.kt b/app/src/main/java/org/koitharu/kotatsu/history/ui/HistoryListViewModel.kt index c9f851f62..bb7bb0e1d 100644 --- a/app/src/main/java/org/koitharu/kotatsu/history/ui/HistoryListViewModel.kt +++ b/app/src/main/java/org/koitharu/kotatsu/history/ui/HistoryListViewModel.kt @@ -3,9 +3,6 @@ package org.koitharu.kotatsu.history.ui import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel -import java.util.* -import java.util.concurrent.TimeUnit -import javax.inject.Inject import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.combine @@ -21,11 +18,20 @@ import org.koitharu.kotatsu.history.domain.HistoryRepository import org.koitharu.kotatsu.history.domain.MangaWithHistory import org.koitharu.kotatsu.history.domain.PROGRESS_NONE import org.koitharu.kotatsu.list.ui.MangaListViewModel -import org.koitharu.kotatsu.list.ui.model.* +import org.koitharu.kotatsu.list.ui.model.EmptyState +import org.koitharu.kotatsu.list.ui.model.ListModel +import org.koitharu.kotatsu.list.ui.model.LoadingState +import org.koitharu.kotatsu.list.ui.model.toErrorState +import org.koitharu.kotatsu.list.ui.model.toGridModel +import org.koitharu.kotatsu.list.ui.model.toListDetailedModel +import org.koitharu.kotatsu.list.ui.model.toListModel import org.koitharu.kotatsu.tracker.domain.TrackingRepository import org.koitharu.kotatsu.utils.asFlowLiveData import org.koitharu.kotatsu.utils.ext.daysDiff import org.koitharu.kotatsu.utils.ext.onFirst +import java.util.Date +import java.util.concurrent.TimeUnit +import javax.inject.Inject @HiltViewModel class HistoryListViewModel @Inject constructor( @@ -53,6 +59,7 @@ class HistoryListViewModel @Inject constructor( actionStringRes = 0, ), ) + else -> mapList(list, grouped, mode) } }.onStart { @@ -103,7 +110,11 @@ class HistoryListViewModel @Inject constructor( } prevDate = date } - val counter = trackingRepository.getNewChaptersCount(manga.id) + val counter = if (settings.isTrackerEnabled) { + trackingRepository.getNewChaptersCount(manga.id) + } else { + 0 + } val percent = if (showPercent) history.percent else PROGRESS_NONE result += when (mode) { ListMode.LIST -> manga.toListModel(counter, percent) diff --git a/app/src/main/java/org/koitharu/kotatsu/local/ui/LocalListViewModel.kt b/app/src/main/java/org/koitharu/kotatsu/local/ui/LocalListViewModel.kt index 1246ea44d..467f87f1b 100644 --- a/app/src/main/java/org/koitharu/kotatsu/local/ui/LocalListViewModel.kt +++ b/app/src/main/java/org/koitharu/kotatsu/local/ui/LocalListViewModel.kt @@ -4,14 +4,16 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.asFlow import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel -import java.io.IOException -import java.util.* -import javax.inject.Inject -import kotlinx.coroutines.* +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.cancelAndJoin import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.update +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import org.koitharu.kotatsu.R import org.koitharu.kotatsu.base.ui.widgets.ChipsView import org.koitharu.kotatsu.core.prefs.AppSettings @@ -20,7 +22,11 @@ import org.koitharu.kotatsu.history.domain.HistoryRepository import org.koitharu.kotatsu.history.domain.PROGRESS_NONE import org.koitharu.kotatsu.list.domain.ListExtraProvider import org.koitharu.kotatsu.list.ui.MangaListViewModel -import org.koitharu.kotatsu.list.ui.model.* +import org.koitharu.kotatsu.list.ui.model.EmptyState +import org.koitharu.kotatsu.list.ui.model.ListHeader2 +import org.koitharu.kotatsu.list.ui.model.LoadingState +import org.koitharu.kotatsu.list.ui.model.toErrorState +import org.koitharu.kotatsu.list.ui.model.toUi import org.koitharu.kotatsu.local.domain.LocalMangaRepository import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.MangaTag @@ -30,6 +36,9 @@ import org.koitharu.kotatsu.utils.SingleLiveEvent import org.koitharu.kotatsu.utils.ext.asLiveDataDistinct import org.koitharu.kotatsu.utils.ext.printStackTraceDebug import org.koitharu.kotatsu.utils.ext.runCatchingCancellable +import java.io.IOException +import java.util.LinkedList +import javax.inject.Inject @HiltViewModel class LocalListViewModel @Inject constructor( @@ -181,7 +190,11 @@ class LocalListViewModel @Inject constructor( } override suspend fun getCounter(mangaId: Long): Int { - return trackingRepository.getNewChaptersCount(mangaId) + return if (settings.isTrackerEnabled) { + trackingRepository.getNewChaptersCount(mangaId) + } else { + 0 + } } override suspend fun getProgress(mangaId: Long): Float { diff --git a/app/src/main/java/org/koitharu/kotatsu/shelf/ui/ShelfViewModel.kt b/app/src/main/java/org/koitharu/kotatsu/shelf/ui/ShelfViewModel.kt index 78cb8050a..ef806d69c 100644 --- a/app/src/main/java/org/koitharu/kotatsu/shelf/ui/ShelfViewModel.kt +++ b/app/src/main/java/org/koitharu/kotatsu/shelf/ui/ShelfViewModel.kt @@ -53,17 +53,22 @@ class ShelfViewModel @Inject constructor( val content: LiveData> = combine( settings.observeAsFlow(AppSettings.KEY_SHELF_SECTIONS) { shelfSections }, + settings.observeAsFlow(AppSettings.KEY_TRACKER_ENABLED) { isTrackerEnabled }, networkState, repository.observeShelfContent(), - ) { sections, isConnected, content -> - mapList(content, sections, isConnected) + ) { sections, isTrackerEnabled, isConnected, content -> + mapList(content, isTrackerEnabled, sections, isConnected) }.debounce(500) .catch { e -> emit(listOf(e.toErrorState(canRetry = false))) }.asFlowLiveData(viewModelScope.coroutineContext + Dispatchers.Default, listOf(LoadingState)) override suspend fun getCounter(mangaId: Long): Int { - return trackingRepository.getNewChaptersCount(mangaId) + return if (settings.isTrackerEnabled) { + trackingRepository.getNewChaptersCount(mangaId) + } else { + 0 + } } override suspend fun getProgress(mangaId: Long): Float { @@ -135,6 +140,7 @@ class ShelfViewModel @Inject constructor( private suspend fun mapList( content: ShelfContent, + isTrackerEnabled: Boolean, sections: List, isNetworkAvailable: Boolean, ): List { @@ -144,7 +150,10 @@ class ShelfViewModel @Inject constructor( when (section) { ShelfSection.HISTORY -> mapHistory(result, content.history) ShelfSection.LOCAL -> mapLocal(result, content.local) - ShelfSection.UPDATED -> mapUpdated(result, content.updated) + ShelfSection.UPDATED -> if (isTrackerEnabled) { + mapUpdated(result, content.updated) + } + ShelfSection.FAVORITES -> mapFavourites(result, content.favourites) } } @@ -194,7 +203,7 @@ class ShelfViewModel @Inject constructor( val showPercent = settings.isReadingIndicatorsEnabled destination += ShelfSectionModel.History( items = list.map { (manga, history) -> - val counter = trackingRepository.getNewChaptersCount(manga.id) + val counter = getCounter(manga.id) val percent = if (showPercent) history.percent else PROGRESS_NONE manga.toGridModel(counter, percent) }, diff --git a/app/src/main/java/org/koitharu/kotatsu/shelf/ui/config/ShelfSettingsViewModel.kt b/app/src/main/java/org/koitharu/kotatsu/shelf/ui/config/ShelfSettingsViewModel.kt index 15323d9b8..fb09af8b1 100644 --- a/app/src/main/java/org/koitharu/kotatsu/shelf/ui/config/ShelfSettingsViewModel.kt +++ b/app/src/main/java/org/koitharu/kotatsu/shelf/ui/config/ShelfSettingsViewModel.kt @@ -23,9 +23,10 @@ class ShelfSettingsViewModel @Inject constructor( val content = combine( settings.observeAsFlow(AppSettings.KEY_SHELF_SECTIONS) { shelfSections }, + settings.observeAsFlow(AppSettings.KEY_TRACKER_ENABLED) { isTrackerEnabled }, favouritesRepository.observeCategories(), - ) { sections, categories -> - buildList(sections, categories) + ) { sections, isTrackerEnabled, categories -> + buildList(sections, isTrackerEnabled, categories) }.asFlowLiveData(viewModelScope.coroutineContext + Dispatchers.Default, emptyList()) private var updateJob: Job? = null @@ -64,13 +65,18 @@ class ShelfSettingsViewModel @Inject constructor( private fun buildList( sections: List, + isTrackerEnabled: Boolean, categories: List ): List { val result = ArrayList() val sectionsList = ShelfSection.values().toMutableList() + if (!isTrackerEnabled) { + sectionsList.remove(ShelfSection.UPDATED) + } for (section in sections) { - sectionsList.remove(section) - result.addSection(section, true, categories) + if (sectionsList.remove(section)) { + result.addSection(section, true, categories) + } } for (section in sectionsList) { result.addSection(section, false, categories)