From 6447b829697b8a4d2816d6402cca787f6e0fd5a0 Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Fri, 24 Jun 2022 13:19:43 +0200 Subject: [PATCH] disable vote button when selection is not changed compared to own votes Signed-off-by: Marcel Hibbe --- .../talk/polls/ui/PollVoteFragment.kt | 23 ++++++++++++------- .../polls/viewmodels/PollVoteViewModel.kt | 7 +++++- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/polls/ui/PollVoteFragment.kt b/app/src/main/java/com/nextcloud/talk/polls/ui/PollVoteFragment.kt index ed7b0461b..1c4f98ec3 100644 --- a/app/src/main/java/com/nextcloud/talk/polls/ui/PollVoteFragment.kt +++ b/app/src/main/java/com/nextcloud/talk/polls/ui/PollVoteFragment.kt @@ -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) } - + poll.votedSelf?.let { viewModel.initVotedOptions(it as ArrayList) } 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) diff --git a/app/src/main/java/com/nextcloud/talk/polls/viewmodels/PollVoteViewModel.kt b/app/src/main/java/com/nextcloud/talk/polls/viewmodels/PollVoteViewModel.kt index 329850090..891db9515 100644 --- a/app/src/main/java/com/nextcloud/talk/polls/viewmodels/PollVoteViewModel.kt +++ b/app/src/main/java/com/nextcloud/talk/polls/viewmodels/PollVoteViewModel.kt @@ -46,11 +46,16 @@ class PollVoteViewModel @Inject constructor(private val repository: PollReposito private var disposable: Disposable? = null + private var _votedOptions: List = emptyList() + val votedOptions: List + get() = _votedOptions + private var _selectedOptions: List = emptyList() val selectedOptions: List get() = _selectedOptions - fun initSelectedOptions(selectedOptions: List) { + fun initVotedOptions(selectedOptions: List) { + _votedOptions = selectedOptions _selectedOptions = selectedOptions }