Преглед на файлове

fix percentage calculation for multiselect polls

for multiselect polls the total votes must be taken instead amount of voters (one voter can have more than 1 votes..)

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
Marcel Hibbe преди 3 години
родител
ревизия
cedbcaefee

+ 2 - 1
app/src/main/java/com/nextcloud/talk/polls/model/Poll.kt

@@ -33,7 +33,8 @@ data class Poll(
     val maxVotes: Int,
     val votedSelf: List<Int>?,
     val numVoters: Int,
-    val details: List<PollDetails>?
+    val details: List<PollDetails>?,
+    val totalVotes: Int
 ) {
     companion object {
         const val STATUS_OPEN: Int = 0

+ 9 - 0
app/src/main/java/com/nextcloud/talk/polls/repositories/PollRepositoryImpl.kt

@@ -134,6 +134,7 @@ class PollRepositoryImpl(private val ncApi: NcApi, private val currentUserProvid
                 pollResponse.votedSelf,
                 pollResponse.numVoters,
                 pollDetails,
+                getTotalVotes(pollResponse.votes)
             )
         }
 
@@ -153,5 +154,13 @@ class PollRepositoryImpl(private val ncApi: NcApi, private val currentUserProvid
                 pollDetailsResponse.optionId,
             )
         }
+
+        private fun getTotalVotes(votes: Map<String, Int>?): Int {
+            var totalVotes = 0
+            votes?.forEach {
+                totalVotes += it.value
+            }
+            return totalVotes
+        }
     }
 }

+ 3 - 3
app/src/main/java/com/nextcloud/talk/polls/ui/PollMainDialogFragment.kt

@@ -115,7 +115,7 @@ class PollMainDialogFragment : DialogFragment() {
     }
 
     private fun showResultsScreen(poll: Poll) {
-        initVotersAmount(poll.numVoters)
+        initVotesAmount(poll.totalVotes)
 
         val contentFragment = PollResultsFragment.newInstance(
             user
@@ -126,11 +126,11 @@ class PollMainDialogFragment : DialogFragment() {
         transaction.commit()
     }
 
-    private fun initVotersAmount(numVoters: Int) {
+    private fun initVotesAmount(totalVotes: Int) {
         binding.pollDetailsText.visibility = View.VISIBLE
         binding.pollDetailsText.text = String.format(
             resources.getString(R.string.polls_amount_voters),
-            numVoters
+            totalVotes
         )
     }
 

+ 1 - 12
app/src/main/java/com/nextcloud/talk/polls/viewmodels/PollResultsViewModel.kt

@@ -84,8 +84,7 @@ class PollResultsViewModel @Inject constructor() : ViewModel() {
     private fun initPollResults(poll: Poll) {
         _items.value = ArrayList()
 
-        val votersAmount = getVotersAmount(poll)
-        val oneVoteInPercent = HUNDRED / votersAmount
+        val oneVoteInPercent = HUNDRED / poll.totalVotes
 
         poll.options?.forEachIndexed { index, option ->
             val votersAmountForThisOption = getVotersAmountForOption(poll, index)
@@ -115,16 +114,6 @@ class PollResultsViewModel @Inject constructor() : ViewModel() {
         _items.value = tempList
     }
 
-    private fun getVotersAmount(poll: Poll): Int {
-        var votersAmount = 0
-        if (poll.details != null) {
-            votersAmount = poll.details.size
-        } else if (poll.votes != null) {
-            votersAmount = poll.numVoters
-        }
-        return votersAmount
-    }
-
     private fun getVotersAmountForOption(poll: Poll, index: Int): Int {
         var votersAmountForThisOption: Int? = 0
         if (poll.details != null) {