Fix library range selection crash across categories
This commit is contained in:
@@ -626,7 +626,9 @@ class LibraryViewModel(
|
|||||||
dialog.update { Dialog.SettingsSheet }
|
dialog.update { Dialog.SettingsSheet }
|
||||||
}
|
}
|
||||||
|
|
||||||
private var lastSelectionCategory: Long? = null
|
private data class LastSelection(val categoryId: Long, val mangaId: Long)
|
||||||
|
|
||||||
|
private var lastSelection: LastSelection? = null
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads from [selection] rather than [state], which is derived asynchronously and can still
|
* Reads from [selection] rather than [state], which is derived asynchronously and can still
|
||||||
@@ -639,7 +641,7 @@ class LibraryViewModel(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun clearSelection() {
|
fun clearSelection() {
|
||||||
lastSelectionCategory = null
|
lastSelection = null
|
||||||
selection.update { setOf() }
|
selection.update { setOf() }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -648,7 +650,7 @@ class LibraryViewModel(
|
|||||||
val newSelection = selection.mutate { set ->
|
val newSelection = selection.mutate { set ->
|
||||||
if (!set.remove(manga.id)) set.add(manga.id)
|
if (!set.remove(manga.id)) set.add(manga.id)
|
||||||
}
|
}
|
||||||
lastSelectionCategory = category.id.takeIf { newSelection.isNotEmpty() }
|
lastSelection = LastSelection(category.id, manga.id).takeIf { manga.id in newSelection }
|
||||||
newSelection
|
newSelection
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -661,31 +663,30 @@ class LibraryViewModel(
|
|||||||
val state = state.value
|
val state = state.value
|
||||||
selection.update { selection ->
|
selection.update { selection ->
|
||||||
val newSelection = selection.mutate { list ->
|
val newSelection = selection.mutate { list ->
|
||||||
val lastSelected = list.lastOrNull()
|
val items = state.getItemsForCategoryId(category.id).fastMap { it.id }
|
||||||
if (lastSelectionCategory != category.id) {
|
val lastMangaIndex = lastSelection
|
||||||
|
?.takeIf { it.categoryId == category.id && it.mangaId in list }
|
||||||
|
?.let { items.indexOf(it.mangaId) }
|
||||||
|
?: -1
|
||||||
|
val curMangaIndex = items.indexOf(manga.id)
|
||||||
|
if (lastMangaIndex == -1 || curMangaIndex == -1) {
|
||||||
list.add(manga.id)
|
list.add(manga.id)
|
||||||
return@mutate
|
return@mutate
|
||||||
}
|
}
|
||||||
|
|
||||||
val items = state.getItemsForCategoryId(category.id).fastMap { it.id }
|
|
||||||
val lastMangaIndex = items.indexOf(lastSelected)
|
|
||||||
val curMangaIndex = items.indexOf(manga.id)
|
|
||||||
|
|
||||||
val selectionRange = when {
|
val selectionRange = when {
|
||||||
lastMangaIndex < curMangaIndex -> lastMangaIndex..curMangaIndex
|
lastMangaIndex < curMangaIndex -> lastMangaIndex..curMangaIndex
|
||||||
curMangaIndex < lastMangaIndex -> curMangaIndex..lastMangaIndex
|
else -> curMangaIndex..lastMangaIndex
|
||||||
// We shouldn't reach this point
|
|
||||||
else -> return@mutate
|
|
||||||
}
|
}
|
||||||
selectionRange.mapNotNull { items[it] }.let(list::addAll)
|
list.addAll(items.subList(selectionRange.first, selectionRange.last + 1))
|
||||||
}
|
}
|
||||||
lastSelectionCategory = category.id
|
lastSelection = LastSelection(category.id, manga.id)
|
||||||
newSelection
|
newSelection
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun selectAll() {
|
fun selectAll() {
|
||||||
lastSelectionCategory = null
|
lastSelection = null
|
||||||
val state = state.value
|
val state = state.value
|
||||||
selection.update { selection ->
|
selection.update { selection ->
|
||||||
selection.mutate { list ->
|
selection.mutate { list ->
|
||||||
@@ -695,7 +696,7 @@ class LibraryViewModel(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun invertSelection() {
|
fun invertSelection() {
|
||||||
lastSelectionCategory = null
|
lastSelection = null
|
||||||
val state = state.value
|
val state = state.value
|
||||||
selection.update { selection ->
|
selection.update { selection ->
|
||||||
selection.mutate { list ->
|
selection.mutate { list ->
|
||||||
|
|||||||
Reference in New Issue
Block a user