From 763288ab137505813f951bb02ca3ba9c1590dbc4 Mon Sep 17 00:00:00 2001 From: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Date: Sat, 20 Aug 2022 21:15:53 +0700 Subject: [PATCH] LibrarySettingsSheet: Fix race condition when applying sort mode (#7805) Also fix sort direction can't be changed for default category --- .../interactor/SetSortModeForCategory.kt | 18 ------------ .../tachiyomi/ui/library/LibraryPresenter.kt | 2 +- .../ui/library/LibrarySettingsSheet.kt | 29 +++++++------------ .../library/setting/SortDirectionSetting.kt | 11 ++----- 4 files changed, 14 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/eu/kanade/domain/category/interactor/SetSortModeForCategory.kt b/app/src/main/java/eu/kanade/domain/category/interactor/SetSortModeForCategory.kt index 94a9bf620..a122f2157 100644 --- a/app/src/main/java/eu/kanade/domain/category/interactor/SetSortModeForCategory.kt +++ b/app/src/main/java/eu/kanade/domain/category/interactor/SetSortModeForCategory.kt @@ -12,24 +12,6 @@ class SetSortModeForCategory( private val categoryRepository: CategoryRepository, ) { - suspend fun await(category: Category, sortDirectionSetting: SortDirectionSetting) { - val sort = if (preferences.categorizedDisplaySettings().get()) { - SortModeSetting.fromFlag(category.flags) - } else { - preferences.librarySortingMode().get() - } - await(category, sort, sortDirectionSetting) - } - - suspend fun await(category: Category, sortModeSetting: SortModeSetting) { - val direction = if (preferences.categorizedDisplaySettings().get()) { - SortDirectionSetting.fromFlag(category.flags) - } else { - preferences.librarySortingAscending().get() - } - await(category, sortModeSetting, direction) - } - suspend fun await(category: Category, sortModeSetting: SortModeSetting, sortDirectionSetting: SortDirectionSetting) { var flags = category.flags and SortModeSetting.MASK.inv() or (sortModeSetting.flag and SortModeSetting.MASK) flags = flags and SortDirectionSetting.MASK.inv() or (sortDirectionSetting.flag and SortDirectionSetting.MASK) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt index d748b9eeb..463d84cde 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt @@ -338,7 +338,7 @@ class LibraryPresenter( } val sortDirections = categories.associate { category -> - category.id to SortDirectionSetting.get(preferences, category) + category.id to SortDirectionSetting.get(category) } val locale = Locale.getDefault() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt index 998a7b4fd..04bfeea5c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt @@ -203,7 +203,7 @@ class LibrarySettingsSheet( override fun initModels() { val sorting = SortModeSetting.get(preferences, currentCategory) - val order = if (SortDirectionSetting.get(preferences, currentCategory) == SortDirectionSetting.ASCENDING) { + val order = if (SortDirectionSetting.get(currentCategory) == SortDirectionSetting.ASCENDING) { Item.MultiSort.SORT_ASC } else { Item.MultiSort.SORT_DESC @@ -242,27 +242,13 @@ class LibrarySettingsSheet( else -> throw Exception("Unknown state") } - setSortModePreference(item) - - setSortDirectionPreference(item) + setSortPreference(item) item.group.items.forEach { adapter.notifyItemChanged(it) } } - private fun setSortDirectionPreference(item: Item.MultiStateGroup) { - val flag = if (item.state == Item.MultiSort.SORT_ASC) { - SortDirectionSetting.ASCENDING - } else { - SortDirectionSetting.DESCENDING - } - - sheetScope.launchIO { - setSortModeForCategory.await(currentCategory!!, flag) - } - } - - private fun setSortModePreference(item: Item) { - val flag = when (item) { + private fun setSortPreference(item: Item.MultiStateGroup) { + val mode = when (item) { alphabetically -> SortModeSetting.ALPHABETICAL lastRead -> SortModeSetting.LAST_READ lastChecked -> SortModeSetting.LAST_MANGA_UPDATE @@ -273,9 +259,14 @@ class LibrarySettingsSheet( dateAdded -> SortModeSetting.DATE_ADDED else -> throw NotImplementedError("Unknown display mode") } + val direction = if (item.state == Item.MultiSort.SORT_ASC) { + SortDirectionSetting.ASCENDING + } else { + SortDirectionSetting.DESCENDING + } sheetScope.launchIO { - setSortModeForCategory.await(currentCategory!!, flag) + setSortModeForCategory.await(currentCategory!!, mode, direction) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/setting/SortDirectionSetting.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/setting/SortDirectionSetting.kt index e90030727..081f1c8f7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/setting/SortDirectionSetting.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/setting/SortDirectionSetting.kt @@ -1,7 +1,6 @@ package eu.kanade.tachiyomi.ui.library.setting import eu.kanade.domain.category.model.Category -import eu.kanade.tachiyomi.data.preference.PreferencesHelper enum class SortDirectionSetting(val flag: Long) { ASCENDING(0b01000000), @@ -10,16 +9,12 @@ enum class SortDirectionSetting(val flag: Long) { companion object { const val MASK = 0b01000000L - fun fromFlag(flag: Long?): SortDirectionSetting { + private fun fromFlag(flag: Long?): SortDirectionSetting { return values().find { mode -> mode.flag == flag } ?: ASCENDING } - fun get(preferences: PreferencesHelper, category: Category?): SortDirectionSetting { - return if (preferences.categorizedDisplaySettings().get() && category != null && category.id != 0L) { - fromFlag(category.sortDirection) - } else { - preferences.librarySortingAscending().get() - } + fun get(category: Category?): SortDirectionSetting { + return fromFlag(category?.sortDirection) } } }