enable/disable submit button by liveData

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2022-07-19 12:57:08 +02:00 committed by Andy Scherzinger (Rebase PR Action)
parent 3267fc2f47
commit 880c656be2
2 changed files with 19 additions and 10 deletions

View File

@ -110,6 +110,10 @@ class PollVoteFragment : Fragment() {
} }
} }
viewModel.submitButtonEnabled.observe(viewLifecycleOwner) { enabled ->
binding.pollVoteSubmitButton.isEnabled = enabled
}
binding.pollVoteRadioGroup.setOnCheckedChangeListener { _, checkedId -> binding.pollVoteRadioGroup.setOnCheckedChangeListener { _, checkedId ->
viewModel.selectOption(checkedId, true) viewModel.selectOption(checkedId, true)
updateSubmitButton() updateSubmitButton()
@ -174,15 +178,7 @@ class PollVoteFragment : Fragment() {
} }
private fun updateSubmitButton() { private fun updateSubmitButton() {
binding.pollVoteSubmitButton.isEnabled = viewModel.updateSubmitButton()
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) { private fun makeOptionBoldIfSelfVoted(button: CompoundButton, poll: Poll, index: Int) {

View File

@ -45,6 +45,10 @@ class PollVoteViewModel @Inject constructor(private val repository: PollReposito
val viewState: LiveData<ViewState> val viewState: LiveData<ViewState>
get() = _viewState get() = _viewState
private val _submitButtonEnabled: MutableLiveData<Boolean> = MutableLiveData()
val submitButtonEnabled: LiveData<Boolean>
get() = _submitButtonEnabled
private var disposable: Disposable? = null private var disposable: Disposable? = null
private var _votedOptions: List<Int> = emptyList() private var _votedOptions: List<Int> = emptyList()
@ -75,7 +79,7 @@ class PollVoteViewModel @Inject constructor(private val repository: PollReposito
fun vote(roomToken: String, pollId: String) { fun vote(roomToken: String, pollId: String) {
if (_selectedOptions.isNotEmpty()) { if (_selectedOptions.isNotEmpty()) {
repository.vote(roomToken, pollId, _selectedOptions) repository.vote(roomToken, pollId, _selectedOptions)
?.doOnSubscribe { disposable = it } .doOnSubscribe { disposable = it }
?.subscribeOn(Schedulers.io()) ?.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread()) ?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(PollObserver()) ?.subscribe(PollObserver())
@ -87,6 +91,15 @@ class PollVoteViewModel @Inject constructor(private val repository: PollReposito
disposable?.dispose() disposable?.dispose()
} }
fun updateSubmitButton() {
val areSelectedOptionsDifferentToVotedOptions = !(
votedOptions.containsAll(selectedOptions) &&
selectedOptions.containsAll(votedOptions)
)
_submitButtonEnabled.value = areSelectedOptionsDifferentToVotedOptions && selectedOptions.isNotEmpty()
}
inner class PollObserver : Observer<Poll> { inner class PollObserver : Observer<Poll> {
lateinit var poll: Poll lateinit var poll: Poll