浏览代码

add multiselect for poll options

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
Marcel Hibbe 3 年之前
父节点
当前提交
acda3d283b

+ 1 - 1
app/src/main/java/com/nextcloud/talk/polls/repositories/PollRepository.kt

@@ -15,7 +15,7 @@ interface PollRepository {
 
     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>?
 }

+ 2 - 2
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<Poll>? {
+    override fun vote(roomToken: String, pollId: String, options: List<Int>): Observable<Poll>? {
 
         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!!) }
     }
 

+ 32 - 8
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<Int>) }
+
+
+        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)
+                    }
+                }
+            }
         }
     }
 

+ 20 - 10
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<List<String>> = MutableLiveData(emptyList())
-    val selectedOptions: LiveData<List<String>>
+    private val _selectedOptions: MutableLiveData<List<Int>> = MutableLiveData(emptyList())
+    val selectedOptions: LiveData<List<Int>>
         get() = _selectedOptions
 
-    fun selectOption(option: String) {
-        _selectedOptions.value = listOf(option)
+    fun initSelectedOptions(selectedOptions: List<Int>) {
+        _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() {

+ 26 - 8
app/src/main/res/layout/dialog_poll_vote.xml

@@ -23,14 +23,32 @@
     xmlns:tools="http://schemas.android.com/tools"
     tools:background="@color/white">
 
-    <RadioGroup
-        android:id="@+id/radioGroup"
+    <LinearLayout
+        android:id="@+id/vote_options_wrapper"
+        android:layout_width="match_parent"
         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_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
         android:id="@+id/poll_vote_close_poll_button"
@@ -41,7 +59,7 @@
         android:layout_marginEnd="@dimen/standard_margin"
         app:cornerRadius="@dimen/button_corner_radius"
         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
@@ -52,6 +70,6 @@
         android:text="@string/nc_common_submit"
         android:theme="@style/Button.Primary"
         app:layout_constraintEnd_toEndOf="parent"
-        app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
+        app:layout_constraintTop_toBottomOf="@+id/vote_options_wrapper" />
 
 </androidx.constraintlayout.widget.ConstraintLayout>