mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-02-17 20:40:07 +03:00
Limit maximum number of poll options.
This commit is contained in:
parent
40aa0175d0
commit
19216aec63
3 changed files with 38 additions and 20 deletions
|
@ -82,14 +82,16 @@ class CreatePollController @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
genericButtonItem {
|
||||
id("add_option")
|
||||
text(host.stringProvider.getString(R.string.create_poll_add_option))
|
||||
textColor(host.colorProvider.getColor(R.color.palette_element_green))
|
||||
gravity(Gravity.START)
|
||||
bold(true)
|
||||
buttonClickAction {
|
||||
host.callback?.onAddOption()
|
||||
if (currentState.canAddMoreOptions) {
|
||||
genericButtonItem {
|
||||
id("add_option")
|
||||
text(host.stringProvider.getString(R.string.create_poll_add_option))
|
||||
textColor(host.colorProvider.getColor(R.color.palette_element_green))
|
||||
gravity(Gravity.START)
|
||||
bold(true)
|
||||
buttonClickAction {
|
||||
host.callback?.onAddOption()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,6 +40,25 @@ class CreatePollViewModel @AssistedInject constructor(
|
|||
companion object : MavericksViewModelFactory<CreatePollViewModel, CreatePollViewState> by hiltMavericksViewModelFactory() {
|
||||
|
||||
const val MIN_OPTIONS_COUNT = 2
|
||||
private const val MAX_OPTIONS_COUNT = 20
|
||||
}
|
||||
|
||||
init {
|
||||
observeState()
|
||||
}
|
||||
|
||||
private fun observeState() {
|
||||
onEach(
|
||||
CreatePollViewState::question,
|
||||
CreatePollViewState::options
|
||||
) { question, options ->
|
||||
setState {
|
||||
copy(
|
||||
canCreatePoll = canCreatePoll(question, options),
|
||||
canAddMoreOptions = options.size < MAX_OPTIONS_COUNT
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun handle(action: CreatePollAction) {
|
||||
|
@ -72,8 +91,7 @@ class CreatePollViewModel @AssistedInject constructor(
|
|||
setState {
|
||||
val extendedOptions = options + ""
|
||||
copy(
|
||||
options = extendedOptions,
|
||||
canCreatePoll = canCreatePoll(this.copy(options = extendedOptions))
|
||||
options = extendedOptions
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -82,8 +100,7 @@ class CreatePollViewModel @AssistedInject constructor(
|
|||
setState {
|
||||
val filteredOptions = options.filterIndexed { ind, _ -> ind != index }
|
||||
copy(
|
||||
options = filteredOptions,
|
||||
canCreatePoll = canCreatePoll(this.copy(options = filteredOptions))
|
||||
options = filteredOptions
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -92,8 +109,7 @@ class CreatePollViewModel @AssistedInject constructor(
|
|||
setState {
|
||||
val changedOptions = options.mapIndexed { ind, s -> if (ind == index) option else s }
|
||||
copy(
|
||||
options = changedOptions,
|
||||
canCreatePoll = canCreatePoll(this.copy(options = changedOptions))
|
||||
options = changedOptions
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -101,14 +117,13 @@ class CreatePollViewModel @AssistedInject constructor(
|
|||
private fun handleOnQuestionChanged(question: String) {
|
||||
setState {
|
||||
copy(
|
||||
question = question,
|
||||
canCreatePoll = canCreatePoll(this.copy(question = question))
|
||||
question = question
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun canCreatePoll(state: CreatePollViewState): Boolean {
|
||||
return state.question.isNotEmpty() &&
|
||||
state.options.filter { it.isNotEmpty() }.size >= MIN_OPTIONS_COUNT
|
||||
private fun canCreatePoll(question: String, options: List<String>): Boolean {
|
||||
return question.isNotEmpty() &&
|
||||
options.filter { it.isNotEmpty() }.size >= MIN_OPTIONS_COUNT
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,8 @@ data class CreatePollViewState(
|
|||
val roomId: String,
|
||||
val question: String = "",
|
||||
val options: List<String> = emptyList(),
|
||||
val canCreatePoll: Boolean = false
|
||||
val canCreatePoll: Boolean = false,
|
||||
val canAddMoreOptions: Boolean = true
|
||||
) : MavericksState {
|
||||
|
||||
constructor(args: CreatePollArgs) : this(
|
||||
|
|
Loading…
Add table
Reference in a new issue