add multiselect for poll options

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2022-06-23 13:42:57 +02:00 committed by Andy Scherzinger (Rebase PR Action)
parent 8541ebbfc0
commit acda3d283b
5 changed files with 81 additions and 29 deletions

View File

@ -15,7 +15,7 @@ interface PollRepository {
fun getPoll(roomToken: String, pollId: String): Observable<Poll>? fun getPoll(roomToken: String, pollId: String): Observable<Poll>?
fun vote(roomToken: String, pollId: String, option: Int): Observable<Poll>? fun vote(roomToken: String, pollId: String, options: List<Int>): Observable<Poll>?
fun closePoll(roomToken: String, pollId: String): Observable<Poll>? fun closePoll(roomToken: String, pollId: String): Observable<Poll>?
} }

View File

@ -89,7 +89,7 @@ class PollRepositoryImpl(private val ncApi: NcApi, private val currentUserProvid
// ) // )
} }
override fun vote(roomToken: String, pollId: String, option: Int): Observable<Poll>? { override fun vote(roomToken: String, pollId: String, options: List<Int>): Observable<Poll>? {
return ncApi.votePoll( return ncApi.votePoll(
credentials, credentials,
@ -98,7 +98,7 @@ class PollRepositoryImpl(private val ncApi: NcApi, private val currentUserProvid
roomToken, roomToken,
pollId pollId
), ),
arrayOf(option).asList() options
).map { mapToPoll(it.ocs?.data!!) } ).map { mapToPoll(it.ocs?.data!!) }
} }

View File

@ -26,6 +26,7 @@ import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.RadioButton import android.widget.RadioButton
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider 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. // todo set selected in viewmodel.
Log.d("bb", "click") Log.d("bb", "click")
} }
// todo observe viewmodel checked, set view checked with it // todo observe viewmodel checked, set view checked with it
binding.pollVoteSubmitButton.setOnClickListener { 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) { private fun initPollOptions(poll: Poll) {
binding.radioGroup.removeAllViews() poll.votedSelf?.let { viewModel.initSelectedOptions(it as ArrayList<Int>) }
poll.options?.map { option ->
RadioButton(context).apply { text = option }
}?.forEachIndexed { index, radioButton -> if (poll.maxVotes == 1) {
radioButton.id = index binding.pollVoteRadioGroup.removeAllViews()
binding.radioGroup.addView(radioButton) 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)
}
}
}
} }
} }

View File

@ -46,20 +46,30 @@ class PollVoteViewModel @Inject constructor(private val repository: PollReposito
private var disposable: Disposable? = null private var disposable: Disposable? = null
private val _selectedOptions: MutableLiveData<List<String>> = MutableLiveData(emptyList()) private val _selectedOptions: MutableLiveData<List<Int>> = MutableLiveData(emptyList())
val selectedOptions: LiveData<List<String>> val selectedOptions: LiveData<List<Int>>
get() = _selectedOptions get() = _selectedOptions
fun selectOption(option: String) { fun initSelectedOptions(selectedOptions: List<Int>) {
_selectedOptions.value = listOf(option) _selectedOptions.value = selectedOptions
} }
fun vote(roomToken: String, pollId: String, option: Int) { fun selectOption(option: Int) {
repository.vote(roomToken, pollId, option) _selectedOptions.value = _selectedOptions.value?.plus(option)
?.doOnSubscribe { disposable = it } }
?.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread()) fun deSelectOption(option: Int) {
?.subscribe(PollObserver()) _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() { override fun onCleared() {

View File

@ -23,14 +23,32 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
tools:background="@color/white"> tools:background="@color/white">
<RadioGroup <LinearLayout
android:id="@+id/radioGroup" android:id="@+id/vote_options_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_width="wrap_content"
tools:layout_height="400dp"
tools:layout_width="match_parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/vote_options_checkboxes_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<RadioGroup
android:id="@+id/poll_vote_radio_group"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
tools:layout_height="400dp"
tools:layout_width="match_parent" />
</LinearLayout>
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/poll_vote_close_poll_button" android:id="@+id/poll_vote_close_poll_button"
@ -41,7 +59,7 @@
android:layout_marginEnd="@dimen/standard_margin" android:layout_marginEnd="@dimen/standard_margin"
app:cornerRadius="@dimen/button_corner_radius" app:cornerRadius="@dimen/button_corner_radius"
app:layout_constraintEnd_toStartOf="@+id/poll_vote_submit_button" app:layout_constraintEnd_toStartOf="@+id/poll_vote_submit_button"
app:layout_constraintTop_toBottomOf="@+id/radioGroup" /> app:layout_constraintTop_toBottomOf="@+id/vote_options_wrapper" />
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
@ -52,6 +70,6 @@
android:text="@string/nc_common_submit" android:text="@string/nc_common_submit"
android:theme="@style/Button.Primary" android:theme="@style/Button.Primary"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioGroup" /> app:layout_constraintTop_toBottomOf="@+id/vote_options_wrapper" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>