LibrarySettingsSheet: Fix race condition when applying sort mode (#7805)

Also fix sort direction can't be changed for default category
This commit is contained in:
Ivan Iskandar 2022-08-20 21:15:53 +07:00 committed by GitHub
parent 58e6479438
commit 763288ab13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 46 deletions

View File

@ -12,24 +12,6 @@ class SetSortModeForCategory(
private val categoryRepository: CategoryRepository, 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) { suspend fun await(category: Category, sortModeSetting: SortModeSetting, sortDirectionSetting: SortDirectionSetting) {
var flags = category.flags and SortModeSetting.MASK.inv() or (sortModeSetting.flag and SortModeSetting.MASK) 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) flags = flags and SortDirectionSetting.MASK.inv() or (sortDirectionSetting.flag and SortDirectionSetting.MASK)

View File

@ -338,7 +338,7 @@ class LibraryPresenter(
} }
val sortDirections = categories.associate { category -> val sortDirections = categories.associate { category ->
category.id to SortDirectionSetting.get(preferences, category) category.id to SortDirectionSetting.get(category)
} }
val locale = Locale.getDefault() val locale = Locale.getDefault()

View File

@ -203,7 +203,7 @@ class LibrarySettingsSheet(
override fun initModels() { override fun initModels() {
val sorting = SortModeSetting.get(preferences, currentCategory) 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 Item.MultiSort.SORT_ASC
} else { } else {
Item.MultiSort.SORT_DESC Item.MultiSort.SORT_DESC
@ -242,27 +242,13 @@ class LibrarySettingsSheet(
else -> throw Exception("Unknown state") else -> throw Exception("Unknown state")
} }
setSortModePreference(item) setSortPreference(item)
setSortDirectionPreference(item)
item.group.items.forEach { adapter.notifyItemChanged(it) } item.group.items.forEach { adapter.notifyItemChanged(it) }
} }
private fun setSortDirectionPreference(item: Item.MultiStateGroup) { private fun setSortPreference(item: Item.MultiStateGroup) {
val flag = if (item.state == Item.MultiSort.SORT_ASC) { val mode = when (item) {
SortDirectionSetting.ASCENDING
} else {
SortDirectionSetting.DESCENDING
}
sheetScope.launchIO {
setSortModeForCategory.await(currentCategory!!, flag)
}
}
private fun setSortModePreference(item: Item) {
val flag = when (item) {
alphabetically -> SortModeSetting.ALPHABETICAL alphabetically -> SortModeSetting.ALPHABETICAL
lastRead -> SortModeSetting.LAST_READ lastRead -> SortModeSetting.LAST_READ
lastChecked -> SortModeSetting.LAST_MANGA_UPDATE lastChecked -> SortModeSetting.LAST_MANGA_UPDATE
@ -273,9 +259,14 @@ class LibrarySettingsSheet(
dateAdded -> SortModeSetting.DATE_ADDED dateAdded -> SortModeSetting.DATE_ADDED
else -> throw NotImplementedError("Unknown display mode") else -> throw NotImplementedError("Unknown display mode")
} }
val direction = if (item.state == Item.MultiSort.SORT_ASC) {
SortDirectionSetting.ASCENDING
} else {
SortDirectionSetting.DESCENDING
}
sheetScope.launchIO { sheetScope.launchIO {
setSortModeForCategory.await(currentCategory!!, flag) setSortModeForCategory.await(currentCategory!!, mode, direction)
} }
} }
} }

View File

@ -1,7 +1,6 @@
package eu.kanade.tachiyomi.ui.library.setting package eu.kanade.tachiyomi.ui.library.setting
import eu.kanade.domain.category.model.Category import eu.kanade.domain.category.model.Category
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
enum class SortDirectionSetting(val flag: Long) { enum class SortDirectionSetting(val flag: Long) {
ASCENDING(0b01000000), ASCENDING(0b01000000),
@ -10,16 +9,12 @@ enum class SortDirectionSetting(val flag: Long) {
companion object { companion object {
const val MASK = 0b01000000L const val MASK = 0b01000000L
fun fromFlag(flag: Long?): SortDirectionSetting { private fun fromFlag(flag: Long?): SortDirectionSetting {
return values().find { mode -> mode.flag == flag } ?: ASCENDING return values().find { mode -> mode.flag == flag } ?: ASCENDING
} }
fun get(preferences: PreferencesHelper, category: Category?): SortDirectionSetting { fun get(category: Category?): SortDirectionSetting {
return if (preferences.categorizedDisplaySettings().get() && category != null && category.id != 0L) { return fromFlag(category?.sortDirection)
fromFlag(category.sortDirection)
} else {
preferences.librarySortingAscending().get()
}
} }
} }
} }