LibrarySettingsSheet: Fix race condition when applying sort mode (#7805)
Also fix sort direction can't be changed for default category (cherry picked from commit 763288ab137505813f951bb02ca3ba9c1590dbc4) # Conflicts: # app/src/main/java/eu/kanade/domain/category/interactor/SetSortModeForCategory.kt # app/src/main/java/eu/kanade/tachiyomi/ui/library/setting/SortDirectionSetting.kt
This commit is contained in:
parent
bf0ba54a6d
commit
595380f81d
@ -13,31 +13,18 @@ class SetSortModeForCategory(
|
||||
private val categoryRepository: CategoryRepository,
|
||||
) {
|
||||
|
||||
suspend fun await(category: Category, sortDirectionSetting: SortDirectionSetting) {
|
||||
val sort = if (preferences.categorizedDisplaySettings().get() /* SY --> */ && preferences.groupLibraryBy().get() == LibraryGroup.BY_DEFAULT/* SY <-- */) {
|
||||
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() /* SY --> */ && preferences.groupLibraryBy().get() == LibraryGroup.BY_DEFAULT/* SY <-- */) {
|
||||
SortDirectionSetting.fromFlag(category.flags)
|
||||
} else {
|
||||
preferences.librarySortingAscending().get()
|
||||
}
|
||||
await(category, sortModeSetting, direction)
|
||||
}
|
||||
|
||||
suspend fun await(category: Category, sortModeSetting: SortModeSetting, sortDirectionSetting: SortDirectionSetting) {
|
||||
// SY -->
|
||||
if (preferences.groupLibraryBy().get() != LibraryGroup.BY_DEFAULT) {
|
||||
preferences.librarySortingMode().set(sortModeSetting)
|
||||
preferences.librarySortingAscending().set(sortDirectionSetting)
|
||||
return
|
||||
}
|
||||
// SY <--
|
||||
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)
|
||||
// SY -->
|
||||
val isDefaultGroup = preferences.groupLibraryBy().get() == LibraryGroup.BY_DEFAULT
|
||||
// SY <--
|
||||
if (preferences.categorizedDisplaySettings().get() /* SY --> */ && isDefaultGroup/* SY <-- */) {
|
||||
|
||||
if (preferences.categorizedDisplaySettings().get()) {
|
||||
categoryRepository.updatePartial(
|
||||
CategoryUpdate(
|
||||
id = category.id,
|
||||
@ -47,13 +34,7 @@ class SetSortModeForCategory(
|
||||
} else {
|
||||
preferences.librarySortingMode().set(sortModeSetting)
|
||||
preferences.librarySortingAscending().set(sortDirectionSetting)
|
||||
// SY -->
|
||||
if (isDefaultGroup) {
|
||||
// SY <--
|
||||
categoryRepository.updateAllFlags(flags)
|
||||
// SY -->
|
||||
}
|
||||
// SY <--
|
||||
categoryRepository.updateAllFlags(flags)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -478,12 +478,16 @@ class LibraryPresenter(
|
||||
}
|
||||
// SY <--
|
||||
|
||||
val defaultSortingMode = SortModeSetting.get(preferences, null)
|
||||
// SY -->
|
||||
val defaultSortingMode = preferences.librarySortingMode().get()
|
||||
// SY <--
|
||||
val sortingModes = categories.associate { category ->
|
||||
category.id to SortModeSetting.get(preferences, category)
|
||||
}
|
||||
|
||||
val defaultSortDirection = SortDirectionSetting.get(preferences, null)
|
||||
// SY -->
|
||||
val defaultSortDirection = preferences.librarySortingAscending().get()
|
||||
// SY <--
|
||||
val sortDirections = categories.associate { category ->
|
||||
category.id to SortDirectionSetting.get(preferences, category)
|
||||
}
|
||||
@ -493,6 +497,7 @@ class LibraryPresenter(
|
||||
strength = Collator.PRIMARY
|
||||
}
|
||||
val sortFn: (LibraryItem, LibraryItem) -> Int = { i1, i2 ->
|
||||
// SY -->
|
||||
val sortingMode = if (groupType == LibraryGroup.BY_DEFAULT) {
|
||||
sortingModes[i1.manga.category.toLong()] ?: defaultSortingMode
|
||||
} else {
|
||||
@ -503,6 +508,7 @@ class LibraryPresenter(
|
||||
} else {
|
||||
defaultSortDirection
|
||||
} == SortDirectionSetting.ASCENDING
|
||||
// SY <--
|
||||
|
||||
when (sortingMode) {
|
||||
SortModeSetting.ALPHABETICAL -> {
|
||||
|
@ -278,27 +278,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
|
||||
@ -312,9 +298,14 @@ class LibrarySettingsSheet(
|
||||
// SY <--
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,13 +11,13 @@ 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 /* SY --> */ && preferences.groupLibraryBy().get() == LibraryGroup.BY_DEFAULT/* SY <-- */) {
|
||||
fromFlag(category.sortDirection)
|
||||
return if (/* SY --> */ preferences.groupLibraryBy().get() == LibraryGroup.BY_DEFAULT/* SY <-- */) {
|
||||
fromFlag(category?.sortDirection)
|
||||
} else {
|
||||
preferences.librarySortingAscending().get()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user