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,
|
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) {
|
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)
|
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)
|
||||||
// SY -->
|
|
||||||
val isDefaultGroup = preferences.groupLibraryBy().get() == LibraryGroup.BY_DEFAULT
|
if (preferences.categorizedDisplaySettings().get()) {
|
||||||
// SY <--
|
|
||||||
if (preferences.categorizedDisplaySettings().get() /* SY --> */ && isDefaultGroup/* SY <-- */) {
|
|
||||||
categoryRepository.updatePartial(
|
categoryRepository.updatePartial(
|
||||||
CategoryUpdate(
|
CategoryUpdate(
|
||||||
id = category.id,
|
id = category.id,
|
||||||
@ -47,13 +34,7 @@ class SetSortModeForCategory(
|
|||||||
} else {
|
} else {
|
||||||
preferences.librarySortingMode().set(sortModeSetting)
|
preferences.librarySortingMode().set(sortModeSetting)
|
||||||
preferences.librarySortingAscending().set(sortDirectionSetting)
|
preferences.librarySortingAscending().set(sortDirectionSetting)
|
||||||
// SY -->
|
categoryRepository.updateAllFlags(flags)
|
||||||
if (isDefaultGroup) {
|
|
||||||
// SY <--
|
|
||||||
categoryRepository.updateAllFlags(flags)
|
|
||||||
// SY -->
|
|
||||||
}
|
|
||||||
// SY <--
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -478,12 +478,16 @@ class LibraryPresenter(
|
|||||||
}
|
}
|
||||||
// SY <--
|
// SY <--
|
||||||
|
|
||||||
val defaultSortingMode = SortModeSetting.get(preferences, null)
|
// SY -->
|
||||||
|
val defaultSortingMode = preferences.librarySortingMode().get()
|
||||||
|
// SY <--
|
||||||
val sortingModes = categories.associate { category ->
|
val sortingModes = categories.associate { category ->
|
||||||
category.id to SortModeSetting.get(preferences, 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 ->
|
val sortDirections = categories.associate { category ->
|
||||||
category.id to SortDirectionSetting.get(preferences, category)
|
category.id to SortDirectionSetting.get(preferences, category)
|
||||||
}
|
}
|
||||||
@ -493,6 +497,7 @@ class LibraryPresenter(
|
|||||||
strength = Collator.PRIMARY
|
strength = Collator.PRIMARY
|
||||||
}
|
}
|
||||||
val sortFn: (LibraryItem, LibraryItem) -> Int = { i1, i2 ->
|
val sortFn: (LibraryItem, LibraryItem) -> Int = { i1, i2 ->
|
||||||
|
// SY -->
|
||||||
val sortingMode = if (groupType == LibraryGroup.BY_DEFAULT) {
|
val sortingMode = if (groupType == LibraryGroup.BY_DEFAULT) {
|
||||||
sortingModes[i1.manga.category.toLong()] ?: defaultSortingMode
|
sortingModes[i1.manga.category.toLong()] ?: defaultSortingMode
|
||||||
} else {
|
} else {
|
||||||
@ -503,6 +508,7 @@ class LibraryPresenter(
|
|||||||
} else {
|
} else {
|
||||||
defaultSortDirection
|
defaultSortDirection
|
||||||
} == SortDirectionSetting.ASCENDING
|
} == SortDirectionSetting.ASCENDING
|
||||||
|
// SY <--
|
||||||
|
|
||||||
when (sortingMode) {
|
when (sortingMode) {
|
||||||
SortModeSetting.ALPHABETICAL -> {
|
SortModeSetting.ALPHABETICAL -> {
|
||||||
|
@ -278,27 +278,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
|
||||||
@ -312,9 +298,14 @@ class LibrarySettingsSheet(
|
|||||||
// SY <--
|
// SY <--
|
||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,13 +11,13 @@ 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(preferences: PreferencesHelper, category: Category?): SortDirectionSetting {
|
||||||
return if (preferences.categorizedDisplaySettings().get() && category != null && category.id != 0L /* SY --> */ && preferences.groupLibraryBy().get() == LibraryGroup.BY_DEFAULT/* SY <-- */) {
|
return if (/* SY --> */ preferences.groupLibraryBy().get() == LibraryGroup.BY_DEFAULT/* SY <-- */) {
|
||||||
fromFlag(category.sortDirection)
|
fromFlag(category?.sortDirection)
|
||||||
} else {
|
} else {
|
||||||
preferences.librarySortingAscending().get()
|
preferences.librarySortingAscending().get()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user