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>
This commit is contained in:
Marcel Hibbe 2022-07-01 14:07:57 +02:00 committed by Andy Scherzinger (Rebase PR Action)
parent 9e8dbb70f3
commit cedbcaefee
4 changed files with 15 additions and 16 deletions

View File

@ -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

View File

@ -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
}
}
}

View File

@ -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
)
}

View File

@ -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) {