Merge pull request #974 from KotlinGeekDev/fix-poll-options-behavior

Fix poll options behavior
This commit is contained in:
Vitor Pamplona 2024-07-09 09:29:27 -04:00 committed by GitHub
commit 25ba353488
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 4 deletions

View File

@ -630,17 +630,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,

View File

@ -1278,10 +1278,26 @@ 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)
}
fun updatePollOption(
optionIndex: Int,
text: String,