Merge pull request #550 from PhilKes/fix/keyboard-api-24

Fix ListItem deletion order and repair broken parent check state
This commit is contained in:
Phil 2025-04-18 15:42:58 +02:00 committed by GitHub
commit adb981d76c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -5,6 +5,8 @@ import com.philkes.notallyx.data.model.ListItem
import com.philkes.notallyx.data.model.deepCopy import com.philkes.notallyx.data.model.deepCopy
import com.philkes.notallyx.data.model.findChild import com.philkes.notallyx.data.model.findChild
import com.philkes.notallyx.data.model.plus import com.philkes.notallyx.data.model.plus
import com.philkes.notallyx.data.model.shouldParentBeChecked
import com.philkes.notallyx.data.model.shouldParentBeUnchecked
import com.philkes.notallyx.utils.filter import com.philkes.notallyx.utils.filter
import com.philkes.notallyx.utils.forEach import com.philkes.notallyx.utils.forEach
import com.philkes.notallyx.utils.indices import com.philkes.notallyx.utils.indices
@ -149,9 +151,20 @@ fun <R> List<R>.getOrNull(index: Int) = if (lastIndex >= index) this[index] else
fun Collection<ListItem>.init(resetIds: Boolean = true): List<ListItem> { fun Collection<ListItem>.init(resetIds: Boolean = true): List<ListItem> {
val initializedItems = deepCopy() val initializedItems = deepCopy()
initList(initializedItems, resetIds) initList(initializedItems, resetIds)
checkBrokenList(initializedItems)
return initializedItems return initializedItems
} }
private fun checkBrokenList(list: List<ListItem>) {
list.forEach { listItem ->
if (listItem.shouldParentBeChecked()) {
listItem.checked = true
} else if (listItem.shouldParentBeUnchecked()) {
listItem.checked = false
}
}
}
private fun initList(list: List<ListItem>, resetIds: Boolean) { private fun initList(list: List<ListItem>, resetIds: Boolean) {
if (resetIds) { if (resetIds) {
list.forEachIndexed { index, item -> item.id = index } list.forEachIndexed { index, item -> item.id = index }
@ -250,7 +263,7 @@ fun SortedList<ListItem>.findParentsByChecked(checked: Boolean): List<ListItem>
fun SortedList<ListItem>.deleteCheckedItems() { fun SortedList<ListItem>.deleteCheckedItems() {
mapIndexed { index, listItem -> Pair(index, listItem) } mapIndexed { index, listItem -> Pair(index, listItem) }
.filter { it.second.checked } .filter { it.second.checked }
.sortedBy { it.second.isChild } .sortedBy { !it.second.isChild }
.forEach { remove(it.second) } .forEach { remove(it.second) }
} }