Implement custom element removal with reordering. Works nicely, from observation.

This commit is contained in:
KotlinGeekDev
2024-07-06 20:57:06 +01:00
parent 6efb970794
commit f489deb808
@@ -1278,10 +1278,29 @@ open class NewPostViewModel : ViewModel() {
}
fun removePollOption(optionIndex: Int) {
pollOptions.remove(optionIndex)
pollOptions.removeOrdered(optionIndex)
saveDraft()
}
private fun MutableMap<Int, String>.removeOrdered(index: Int) {
val keyList = keys
val elementList = values.toMutableList()
run stop@{
for (i in index until elementList.size) {
val nextIndex = i + 1
if (nextIndex == elementList.size) return@stop
elementList[i] = elementList[nextIndex].also { elementList[nextIndex] = "null" }
}
}
elementList.removeLast()
val newEntries = keyList.zip(elementList) { key, content -> Pair(key, content) }
this.clear()
this.putAll(newEntries)
println("Keys collection size(after deletion) :${keys.size}")
println("Values collection size(after deletion) :${values.size}")
}
fun updatePollOption(
optionIndex: Int,
text: String,