BulkSelection: avoid using toggle when Select all

(cherry picked from commit 695f872b381feca1051f0f5133a0288d252691e5)
This commit is contained in:
Cuong Tran 2024-04-25 22:03:45 +07:00 committed by Cuong M. Tran
parent 097ce0e2c3
commit 45553eb096
No known key found for this signature in database
GPG key ID: 733AA7624B9315C2
2 changed files with 16 additions and 6 deletions

View file

@ -56,10 +56,9 @@ fun GlobalSearchScreen(
state.filteredItems.forEach { (_, result) ->
when (result) {
is SearchItemResult.Success -> {
result.result.map{manga ->
result.result.forEach { manga ->
if (!bulkFavoriteState.selection.contains(manga))
bulkFavoriteScreenModel.toggleSelection(manga)
bulkFavoriteScreenModel.select(manga)
}
}
else -> {}

View file

@ -84,12 +84,23 @@ class BulkFavoriteScreenModel(
mutableState.update { it.copy(selection = persistentListOf()) }
}
fun toggleSelection(manga: Manga) {
fun select(manga: Manga) {
toggleSelection(manga, toSelectedState = true)
}
fun unselect(manga: Manga) {
toggleSelection(manga, toSelectedState = false)
}
/**
* @param toSelectedState set to true to only Select, set to false to only Unselect
*/
fun toggleSelection(manga: Manga, toSelectedState: Boolean? = null) {
mutableState.update { state ->
val newSelection = state.selection.mutate { list ->
if (list.fastAny { it.id == manga.id }) {
if (toSelectedState != true && list.fastAny { it.id == manga.id }) {
list.removeAll { it.id == manga.id }
} else {
} else if (toSelectedState != false) {
list.add(manga)
}
}