mirror of
https://github.com/nextcloud/talk-android
synced 2025-06-19 19:49:33 +01:00
enable/disable submit button by liveData
Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
parent
3267fc2f47
commit
880c656be2
@ -110,6 +110,10 @@ class PollVoteFragment : Fragment() {
|
||||
}
|
||||
}
|
||||
|
||||
viewModel.submitButtonEnabled.observe(viewLifecycleOwner) { enabled ->
|
||||
binding.pollVoteSubmitButton.isEnabled = enabled
|
||||
}
|
||||
|
||||
binding.pollVoteRadioGroup.setOnCheckedChangeListener { _, checkedId ->
|
||||
viewModel.selectOption(checkedId, true)
|
||||
updateSubmitButton()
|
||||
@ -174,15 +178,7 @@ class PollVoteFragment : Fragment() {
|
||||
}
|
||||
|
||||
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)
|
||||
)
|
||||
viewModel.updateSubmitButton()
|
||||
}
|
||||
|
||||
private fun makeOptionBoldIfSelfVoted(button: CompoundButton, poll: Poll, index: Int) {
|
||||
|
@ -45,6 +45,10 @@ class PollVoteViewModel @Inject constructor(private val repository: PollReposito
|
||||
val viewState: LiveData<ViewState>
|
||||
get() = _viewState
|
||||
|
||||
private val _submitButtonEnabled: MutableLiveData<Boolean> = MutableLiveData()
|
||||
val submitButtonEnabled: LiveData<Boolean>
|
||||
get() = _submitButtonEnabled
|
||||
|
||||
private var disposable: Disposable? = null
|
||||
|
||||
private var _votedOptions: List<Int> = emptyList()
|
||||
@ -75,7 +79,7 @@ class PollVoteViewModel @Inject constructor(private val repository: PollReposito
|
||||
fun vote(roomToken: String, pollId: String) {
|
||||
if (_selectedOptions.isNotEmpty()) {
|
||||
repository.vote(roomToken, pollId, _selectedOptions)
|
||||
?.doOnSubscribe { disposable = it }
|
||||
.doOnSubscribe { disposable = it }
|
||||
?.subscribeOn(Schedulers.io())
|
||||
?.observeOn(AndroidSchedulers.mainThread())
|
||||
?.subscribe(PollObserver())
|
||||
@ -87,6 +91,15 @@ class PollVoteViewModel @Inject constructor(private val repository: PollReposito
|
||||
disposable?.dispose()
|
||||
}
|
||||
|
||||
fun updateSubmitButton() {
|
||||
val areSelectedOptionsDifferentToVotedOptions = !(
|
||||
votedOptions.containsAll(selectedOptions) &&
|
||||
selectedOptions.containsAll(votedOptions)
|
||||
)
|
||||
|
||||
_submitButtonEnabled.value = areSelectedOptionsDifferentToVotedOptions && selectedOptions.isNotEmpty()
|
||||
}
|
||||
|
||||
inner class PollObserver : Observer<Poll> {
|
||||
|
||||
lateinit var poll: Poll
|
||||
|
Loading…
Reference in New Issue
Block a user