Get index of selected update list item based on chapterId

Fixes #8442
This commit is contained in:
arkon 2022-11-05 11:56:08 -04:00
parent 8749be518f
commit 54471a014f

View File

@ -274,51 +274,52 @@ class UpdatesPresenter(
fromLongPress: Boolean = false, fromLongPress: Boolean = false,
) { ) {
state.items = items.toMutableList().apply { state.items = items.toMutableList().apply {
val modifiedIndex = indexOfFirst { it == item } val selectedIndex = indexOfFirst { it.update.chapterId == item.update.chapterId }
if (modifiedIndex < 0) return@apply if (selectedIndex < 0) return@apply
val oldItem = get(modifiedIndex) val selectedItem = get(selectedIndex)
if (oldItem.selected == selected) return@apply if (selectedItem.selected == selected) return@apply
val firstSelection = none { it.selected } val firstSelection = none { it.selected }
var newItem = removeAt(modifiedIndex).copy(selected = selected) set(selectedIndex, selectedItem.copy(selected = selected))
add(modifiedIndex, newItem)
if (selected && userSelected && fromLongPress) { if (selected && userSelected && fromLongPress) {
if (firstSelection) { if (firstSelection) {
selectedPositions[0] = modifiedIndex selectedPositions[0] = selectedIndex
selectedPositions[1] = modifiedIndex selectedPositions[1] = selectedIndex
} else { } else {
// Try to select the items in-between when possible // Try to select the items in-between when possible
val range: IntRange val range: IntRange
if (modifiedIndex < selectedPositions[0]) { if (selectedIndex < selectedPositions[0]) {
range = modifiedIndex + 1 until selectedPositions[0] range = selectedIndex + 1 until selectedPositions[0]
selectedPositions[0] = modifiedIndex selectedPositions[0] = selectedIndex
} else if (modifiedIndex > selectedPositions[1]) { } else if (selectedIndex > selectedPositions[1]) {
range = (selectedPositions[1] + 1) until modifiedIndex range = (selectedPositions[1] + 1) until selectedIndex
selectedPositions[1] = modifiedIndex selectedPositions[1] = selectedIndex
} else { } else {
// Just select itself // Just select itself
range = IntRange.EMPTY range = IntRange.EMPTY
} }
range.forEach { range.forEach {
newItem = removeAt(it).copy(selected = true) val inbetweenItem = get(it)
add(it, newItem) if (!inbetweenItem.selected) {
set(it, inbetweenItem.copy(selected = true))
}
} }
} }
} else if (userSelected && !fromLongPress) { } else if (userSelected && !fromLongPress) {
if (!selected) { if (!selected) {
if (modifiedIndex == selectedPositions[0]) { if (selectedIndex == selectedPositions[0]) {
selectedPositions[0] = indexOfFirst { it.selected } selectedPositions[0] = indexOfFirst { it.selected }
} else if (modifiedIndex == selectedPositions[1]) { } else if (selectedIndex == selectedPositions[1]) {
selectedPositions[1] = indexOfLast { it.selected } selectedPositions[1] = indexOfLast { it.selected }
} }
} else { } else {
if (modifiedIndex < selectedPositions[0]) { if (selectedIndex < selectedPositions[0]) {
selectedPositions[0] = modifiedIndex selectedPositions[0] = selectedIndex
} else if (modifiedIndex > selectedPositions[1]) { } else if (selectedIndex > selectedPositions[1]) {
selectedPositions[1] = modifiedIndex selectedPositions[1] = selectedIndex
} }
} }
} }