restrict multiselection to maxVotes

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2022-06-23 15:42:59 +02:00 committed by Andy Scherzinger (Rebase PR Action)
parent a4c48b11d6
commit 782d74bfb9

View File

@ -28,6 +28,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.RadioButton
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import autodagger.AutoInjector
@ -127,7 +128,12 @@ class PollVoteFragment(
checkBox.isChecked = viewModel.selectedOptions.contains(index) == true
checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked) {
viewModel.selectOption(index, false)
if (poll.maxVotes == UNLIMITED_VOTES || viewModel.selectedOptions.size < poll.maxVotes) {
viewModel.selectOption(index, false)
} else {
checkBox.isChecked = false
Toast.makeText(context, "max votes reached", Toast.LENGTH_LONG).show()
}
} else {
viewModel.deSelectOption(index)
}
@ -154,5 +160,6 @@ class PollVoteFragment(
companion object {
private val TAG = PollVoteFragment::class.java.simpleName
private const val UNLIMITED_VOTES = 0
}
}