From acda3d283b3b0076883932f7ea686b128832af21 Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Thu, 23 Jun 2022 13:42:57 +0200 Subject: [PATCH] add multiselect for poll options Signed-off-by: Marcel Hibbe --- .../talk/polls/repositories/PollRepository.kt | 2 +- .../polls/repositories/PollRepositoryImpl.kt | 4 +- .../talk/polls/ui/PollVoteFragment.kt | 40 +++++++++++++++---- .../polls/viewmodels/PollVoteViewModel.kt | 30 +++++++++----- app/src/main/res/layout/dialog_poll_vote.xml | 34 ++++++++++++---- 5 files changed, 81 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/polls/repositories/PollRepository.kt b/app/src/main/java/com/nextcloud/talk/polls/repositories/PollRepository.kt index b2f9c0cf8..003651af0 100644 --- a/app/src/main/java/com/nextcloud/talk/polls/repositories/PollRepository.kt +++ b/app/src/main/java/com/nextcloud/talk/polls/repositories/PollRepository.kt @@ -15,7 +15,7 @@ interface PollRepository { fun getPoll(roomToken: String, pollId: String): Observable? - fun vote(roomToken: String, pollId: String, option: Int): Observable? + fun vote(roomToken: String, pollId: String, options: List): Observable? fun closePoll(roomToken: String, pollId: String): Observable? } diff --git a/app/src/main/java/com/nextcloud/talk/polls/repositories/PollRepositoryImpl.kt b/app/src/main/java/com/nextcloud/talk/polls/repositories/PollRepositoryImpl.kt index 3c5d73bb4..d5329869a 100644 --- a/app/src/main/java/com/nextcloud/talk/polls/repositories/PollRepositoryImpl.kt +++ b/app/src/main/java/com/nextcloud/talk/polls/repositories/PollRepositoryImpl.kt @@ -89,7 +89,7 @@ class PollRepositoryImpl(private val ncApi: NcApi, private val currentUserProvid // ) } - override fun vote(roomToken: String, pollId: String, option: Int): Observable? { + override fun vote(roomToken: String, pollId: String, options: List): Observable? { return ncApi.votePoll( credentials, @@ -98,7 +98,7 @@ class PollRepositoryImpl(private val ncApi: NcApi, private val currentUserProvid roomToken, pollId ), - arrayOf(option).asList() + options ).map { mapToPoll(it.ocs?.data!!) } } 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 44c2a4098..fef5a0c99 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 @@ -26,6 +26,7 @@ import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.CheckBox import android.widget.RadioButton import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProvider @@ -92,24 +93,47 @@ class PollVoteFragment( } } - binding.radioGroup.setOnCheckedChangeListener { group, checkedId -> + binding.pollVoteRadioGroup.setOnCheckedChangeListener { group, checkedId -> // todo set selected in viewmodel. Log.d("bb", "click") } // todo observe viewmodel checked, set view checked with it binding.pollVoteSubmitButton.setOnClickListener { - viewModel.vote(roomToken, pollId, binding.radioGroup.checkedRadioButtonId) + // viewModel.vote(roomToken, pollId, binding.pollVoteRadioGroup.checkedRadioButtonId) + viewModel.vote(roomToken, pollId) } } private fun initPollOptions(poll: Poll) { - binding.radioGroup.removeAllViews() - poll.options?.map { option -> - RadioButton(context).apply { text = option } - }?.forEachIndexed { index, radioButton -> - radioButton.id = index - binding.radioGroup.addView(radioButton) + poll.votedSelf?.let { viewModel.initSelectedOptions(it as ArrayList) } + + + if (poll.maxVotes == 1) { + binding.pollVoteRadioGroup.removeAllViews() + poll.options?.map { option -> + RadioButton(context).apply { text = option } + }?.forEachIndexed { index, radioButton -> + radioButton.id = index + binding.pollVoteRadioGroup.addView(radioButton) + } + } else { + binding.voteOptionsCheckboxesWrapper.removeAllViews() + poll.options?.map { option -> + CheckBox(context).apply { text = option } + }?.forEachIndexed { index, checkBox -> + checkBox.id = index + binding.voteOptionsCheckboxesWrapper.addView(checkBox) + + checkBox.isChecked = viewModel.selectedOptions.value?.contains(index) == true + checkBox.setOnCheckedChangeListener { buttonView, isChecked -> + if (isChecked) { + viewModel.selectOption(index) + } else { + viewModel.deSelectOption(index) + } + } + } } } 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 7de9f7243..41ffe54df 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,20 +46,30 @@ class PollVoteViewModel @Inject constructor(private val repository: PollReposito private var disposable: Disposable? = null - private val _selectedOptions: MutableLiveData> = MutableLiveData(emptyList()) - val selectedOptions: LiveData> + private val _selectedOptions: MutableLiveData> = MutableLiveData(emptyList()) + val selectedOptions: LiveData> get() = _selectedOptions - fun selectOption(option: String) { - _selectedOptions.value = listOf(option) + fun initSelectedOptions(selectedOptions: List) { + _selectedOptions.value = selectedOptions } - fun vote(roomToken: String, pollId: String, option: Int) { - repository.vote(roomToken, pollId, option) - ?.doOnSubscribe { disposable = it } - ?.subscribeOn(Schedulers.io()) - ?.observeOn(AndroidSchedulers.mainThread()) - ?.subscribe(PollObserver()) + fun selectOption(option: Int) { + _selectedOptions.value = _selectedOptions.value?.plus(option) + } + + fun deSelectOption(option: Int) { + _selectedOptions.value = _selectedOptions.value?.minus(option) + } + + fun vote(roomToken: String, pollId: String) { + if (!_selectedOptions.value.isNullOrEmpty()) { + repository.vote(roomToken, pollId, _selectedOptions.value!!) + ?.doOnSubscribe { disposable = it } + ?.subscribeOn(Schedulers.io()) + ?.observeOn(AndroidSchedulers.mainThread()) + ?.subscribe(PollObserver()) + } } override fun onCleared() { diff --git a/app/src/main/res/layout/dialog_poll_vote.xml b/app/src/main/res/layout/dialog_poll_vote.xml index 004b8db4e..407d9a43f 100644 --- a/app/src/main/res/layout/dialog_poll_vote.xml +++ b/app/src/main/res/layout/dialog_poll_vote.xml @@ -23,14 +23,32 @@ xmlns:tools="http://schemas.android.com/tools" tools:background="@color/white"> - + app:layout_constraintTop_toTopOf="parent" + android:orientation="vertical"> + + + + + + + + + + + app:layout_constraintTop_toBottomOf="@+id/vote_options_wrapper" /> + app:layout_constraintTop_toBottomOf="@+id/vote_options_wrapper" />