disable vote button when selection is not changed compared to own votes

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2022-06-24 13:19:43 +02:00 committed by Andy Scherzinger (Rebase PR Action)
parent 167aa43ca0
commit 6447b82969
2 changed files with 21 additions and 9 deletions

View file

@ -78,9 +78,11 @@ class PollVoteFragment(
if (state is PollMainViewModel.PollVoteState) {
initPollOptions(state.poll)
initCloseButton(state.showCloseButton)
updateSubmitButton()
} else if (state is PollMainViewModel.PollVoteHiddenState) {
initPollOptions(state.poll)
initCloseButton(state.showCloseButton)
updateSubmitButton()
}
}
@ -98,6 +100,7 @@ class PollVoteFragment(
binding.pollVoteRadioGroup.setOnCheckedChangeListener { group, checkedId ->
viewModel.selectOption(checkedId, true)
updateSubmitButton()
}
binding.pollVoteSubmitButton.setOnClickListener {
@ -106,8 +109,7 @@ class PollVoteFragment(
}
private fun initPollOptions(poll: Poll) {
poll.votedSelf?.let { viewModel.initSelectedOptions(it as ArrayList<Int>) }
poll.votedSelf?.let { viewModel.initVotedOptions(it as ArrayList<Int>) }
if (poll.maxVotes == 1) {
binding.pollVoteRadioGroup.removeAllViews()
@ -115,9 +117,6 @@ class PollVoteFragment(
RadioButton(context).apply { text = option }
}?.forEachIndexed { index, radioButton ->
radioButton.id = index
// if (poll.votedSelf?.contains(index) == true) {
// radioButton.setTypeface(null, Typeface.BOLD)
// }
makeOptionBoldIfSelfVoted(radioButton, poll, index)
binding.pollVoteRadioGroup.addView(radioButton)
@ -129,9 +128,6 @@ class PollVoteFragment(
CheckBox(context).apply { text = option }
}?.forEachIndexed { index, checkBox ->
checkBox.id = index
// if (poll.votedSelf?.contains(index) == true) {
// checkBox.setTypeface(null, Typeface.BOLD)
// }
makeOptionBoldIfSelfVoted(checkBox, poll, index)
binding.voteOptionsCheckboxesWrapper.addView(checkBox)
@ -147,11 +143,22 @@ class PollVoteFragment(
} else {
viewModel.deSelectOption(index)
}
updateSubmitButton()
}
}
}
}
private fun updateSubmitButton() {
binding.pollVoteSubmitButton.isEnabled =
areSelectedOptionsDifferentToVotedOptions() && viewModel.selectedOptions.isNotEmpty()
}
private fun areSelectedOptionsDifferentToVotedOptions(): Boolean {
return !(viewModel.votedOptions.containsAll(viewModel.selectedOptions) &&
viewModel.selectedOptions.containsAll(viewModel.votedOptions))
}
private fun makeOptionBoldIfSelfVoted(button: CompoundButton, poll: Poll, index: Int) {
if (poll.votedSelf?.contains(index) == true) {
button.setTypeface(null, Typeface.BOLD)

View file

@ -46,11 +46,16 @@ class PollVoteViewModel @Inject constructor(private val repository: PollReposito
private var disposable: Disposable? = null
private var _votedOptions: List<Int> = emptyList()
val votedOptions: List<Int>
get() = _votedOptions
private var _selectedOptions: List<Int> = emptyList()
val selectedOptions: List<Int>
get() = _selectedOptions
fun initSelectedOptions(selectedOptions: List<Int>) {
fun initVotedOptions(selectedOptions: List<Int>) {
_votedOptions = selectedOptions
_selectedOptions = selectedOptions
}