From f489deb808523a6b587e7e0f96f1eec80fd63166 Mon Sep 17 00:00:00 2001 From: KotlinGeekDev Date: Sat, 6 Jul 2024 20:57:06 +0100 Subject: [PATCH] Implement custom element removal with reordering. Works nicely, from observation. --- .../amethyst/ui/actions/NewPostViewModel.kt | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt index 55dc95a68..9142519bb 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt @@ -1278,10 +1278,29 @@ open class NewPostViewModel : ViewModel() { } fun removePollOption(optionIndex: Int) { - pollOptions.remove(optionIndex) + pollOptions.removeOrdered(optionIndex) saveDraft() } + private fun MutableMap.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,