Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@

## stream-chat-android-ui-components
### 🐞 Fixed
- Filter out Poll Options differing only on whitespaces. [#5913](https://github.com/GetStream/stream-chat-android/pull/5913)

### ⬆️ Improved

Expand All @@ -71,6 +72,7 @@

## stream-chat-android-compose
### 🐞 Fixed
- Filter out Poll Options differing only on whitespaces. [#5913](https://github.com/GetStream/stream-chat-android/pull/5913)

### ⬆️ Improved

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,27 @@ public fun PollOptionList(
PaddingValues(horizontal = 16.dp, vertical = 8.dp)
},
onValueChange = { newTitle ->
optionItemList.toMutableList().apply {
val duplicated = this.any { it.title == newTitle }
if (duplicated) {
this[index] = item.copy(
val duplicated = optionItemList
.withIndex()
.any {
it.index != index &&
it.value.title.trim() == newTitle.trim()
}
optionItemList = optionItemList.mapIndexed { i, item ->
when (i) {
index -> item.copy(
title = newTitle,
pollOptionError = PollOptionDuplicated(
context.getString(R.string.stream_compose_poll_option_error_duplicated),
),
pollOptionError = when (duplicated) {
false -> null
true -> PollOptionDuplicated(
context.getString(R.string.stream_compose_poll_option_error_duplicated),
)
},
)
} else {
this[index] =
item.copy(title = newTitle, pollOptionError = null)
else -> item
}

optionItemList = this
onQuestionsChanged.invoke(this)
}
onQuestionsChanged.invoke(optionItemList)
},
decorationBox = { innerTextField ->
if (item.pollOptionError == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,17 @@ public class CreatePollViewModel : ViewModel() {
_options.value.let { options ->
val previousOptions = options.values
.filterNot { it.id == id }
.map { it.text }
.map { it.text.trim() }
.filter { it.isNotEmpty() }
options[id]?.let { option ->
_options.value = LinkedHashMap(options).apply {
put(id, option.copy(text = text, duplicateError = previousOptions.contains(text)))
put(
id,
option.copy(
text = text,
duplicateError = previousOptions.any { it == text.trim() },
),
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class OptionsAdapter(
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { /* no-op */ }
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { /* no-op */ }
override fun afterTextChanged(s: Editable?) {
pollAnswer.updateErrorState()
onOptionChange(pollAnswer.id, s.toString())
}
}
Expand All @@ -71,9 +72,13 @@ public class OptionsAdapter(
binding.option.removeTextChangedListener(textWatcher)
binding.option.setText(pollAnswer.text)
binding.option.setSelection(pollAnswer.text.length)
pollAnswer.updateErrorState()
binding.option.addTextChangedListener(textWatcher)
}

private fun PollAnswer.updateErrorState() {
binding.option.error = when {
pollAnswer.duplicateError ->
duplicateError ->
binding.root.context.getString(R.string.stream_ui_poll_this_is_already_an_option)
else -> null
}
Expand Down
Loading