diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt index c23523b3f..0afb79590 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt @@ -350,6 +350,10 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) { get() = prefs.getEnumValue(KEY_HISTORY_ORDER, ListSortOrder.UPDATED) set(value) = prefs.edit { putEnumValue(KEY_HISTORY_ORDER, value) } + var allFavoritesSortOrder: ListSortOrder + get() = prefs.getEnumValue(KEY_FAVORITES_ORDER, ListSortOrder.NEWEST) + set(value) = prefs.edit { putEnumValue(KEY_FAVORITES_ORDER, value) } + val isRelatedMangaEnabled: Boolean get() = prefs.getBoolean(KEY_RELATED_MANGA, true) @@ -530,6 +534,7 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) { const val KEY_READER_OPTIMIZE = "reader_optimize" const val KEY_LOCAL_LIST_ORDER = "local_order" const val KEY_HISTORY_ORDER = "history_order" + const val KEY_FAVORITES_ORDER = "fav_order" const val KEY_WEBTOON_ZOOM = "webtoon_zoom" const val KEY_PREFETCH_CONTENT = "prefetch_content" const val KEY_APP_LOCALE = "app_locale" diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/favourites/data/FavouritesDao.kt b/app/src/main/kotlin/org/koitharu/kotatsu/favourites/data/FavouritesDao.kt index a7c4df2c8..ba4180e1e 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/favourites/data/FavouritesDao.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/favourites/data/FavouritesDao.kt @@ -175,9 +175,9 @@ abstract class FavouritesDao { ListSortOrder.RATING -> "manga.rating DESC" ListSortOrder.NEWEST -> "favourites.created_at DESC" ListSortOrder.ALPHABETIC -> "manga.title ASC" - ListSortOrder.NEW_CHAPTERS -> "(SELECT chapters_new FROM tracks WHERE tracks.manga_id = manga.manga_id) DESC" + ListSortOrder.NEW_CHAPTERS -> "IFNULL((SELECT chapters_new FROM tracks WHERE tracks.manga_id = manga.manga_id), 0) DESC" ListSortOrder.UPDATED, // for legacy support - ListSortOrder.PROGRESS -> "(SELECT percent FROM history WHERE history.manga_id = manga.manga_id) DESC" + ListSortOrder.PROGRESS -> "IFNULL((SELECT percent FROM history WHERE history.manga_id = manga.manga_id), 0) DESC" else -> throw IllegalArgumentException("Sort order $sortOrder is not supported") } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/favourites/ui/list/FavouritesListViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/favourites/ui/list/FavouritesListViewModel.kt index 84ceb10e1..c2e3a277e 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/favourites/ui/list/FavouritesListViewModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/favourites/ui/list/FavouritesListViewModel.kt @@ -9,6 +9,8 @@ import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.filterNotNull +import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.plus @@ -49,16 +51,19 @@ class FavouritesListViewModel @Inject constructor( .stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, settings.favoritesListMode) val sortOrder: StateFlow = if (categoryId == NO_ID) { - MutableStateFlow(null) + settings.observeAsFlow(AppSettings.KEY_FAVORITES_ORDER) { + allFavoritesSortOrder + } } else { repository.observeCategory(categoryId) .map { it?.order } - .stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, null) - } + }.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, null) override val content = combine( if (categoryId == NO_ID) { - repository.observeAll(ListSortOrder.NEWEST) + sortOrder.filterNotNull().flatMapLatest { + repository.observeAll(it) + } } else { repository.observeAll(categoryId) }, diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/config/ListConfigViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/config/ListConfigViewModel.kt index 23d4f59d7..e1a39d634 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/config/ListConfigViewModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/config/ListConfigViewModel.kt @@ -9,6 +9,7 @@ import org.koitharu.kotatsu.core.ui.BaseViewModel import org.koitharu.kotatsu.core.util.ext.require import org.koitharu.kotatsu.core.util.ext.sortedByOrdinal import org.koitharu.kotatsu.favourites.domain.FavouritesRepository +import org.koitharu.kotatsu.favourites.ui.list.FavouritesListFragment.Companion.NO_ID import org.koitharu.kotatsu.list.domain.ListSortOrder import org.koitharu.kotatsu.parsers.util.runCatchingCancellable import javax.inject.Inject @@ -65,7 +66,11 @@ class ListConfigViewModel @Inject constructor( val value = getSortOrders()?.getOrNull(position) ?: return when (section) { is ListConfigSection.Favorites -> launchJob { - favouritesRepository.setCategoryOrder(section.categoryId, value) + if (section.categoryId == NO_ID) { + settings.allFavoritesSortOrder = value + } else { + favouritesRepository.setCategoryOrder(section.categoryId, value) + } } ListConfigSection.General -> Unit @@ -75,9 +80,13 @@ class ListConfigViewModel @Inject constructor( } } - private fun getCategorySortOrder(id: Long): ListSortOrder = runBlocking { + private fun getCategorySortOrder(id: Long): ListSortOrder = if (id == NO_ID) { + settings.allFavoritesSortOrder + } else runBlocking { runCatchingCancellable { favouritesRepository.getCategory(id).order - }.getOrDefault(ListSortOrder.NEWEST) + }.getOrElse { + settings.allFavoritesSortOrder + } } }