From 6efb970794cd1e772165489a7c1c85beb7b3b190 Mon Sep 17 00:00:00 2001 From: KotlinGeekDev Date: Tue, 2 Jul 2024 09:51:34 +0100 Subject: [PATCH 1/3] Use keys of map entries, as they are more reliable(from testing). Extract pollViewModel.pollOptions into a separate variable. --- .../vitorpamplona/amethyst/ui/actions/NewPostView.kt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt index b8f4019b9..de0529f4a 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt @@ -629,17 +629,21 @@ private fun BottomRowActions(postViewModel: NewPostViewModel) { @Composable private fun PollField(postViewModel: NewPostViewModel) { + val optionsList = postViewModel.pollOptions Column( modifier = Modifier.fillMaxWidth(), ) { - postViewModel.pollOptions.values.forEachIndexed { index, _ -> - NewPollOption(postViewModel, index) + optionsList.forEach { value -> + NewPollOption(postViewModel, value.key) } NewPollVoteValueRange(postViewModel) Button( - onClick = { postViewModel.pollOptions[postViewModel.pollOptions.size] = "" }, + onClick = { + // postViewModel.pollOptions[postViewModel.pollOptions.size] = "" + optionsList[optionsList.size] = "" + }, border = BorderStroke( 1.dp, From f489deb808523a6b587e7e0f96f1eec80fd63166 Mon Sep 17 00:00:00 2001 From: KotlinGeekDev Date: Sat, 6 Jul 2024 20:57:06 +0100 Subject: [PATCH 2/3] 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, From 08e84b056e29d3c7ca72363334e3e003f2f1bff9 Mon Sep 17 00:00:00 2001 From: KotlinGeekDev Date: Sun, 7 Jul 2024 00:06:44 +0100 Subject: [PATCH 3/3] Some cleanup. --- .../com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt | 3 --- 1 file changed, 3 deletions(-) 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 9142519bb..eedf5298e 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 @@ -1296,9 +1296,6 @@ open class NewPostViewModel : ViewModel() { 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(