Fix winning answers detection
This commit is contained in:
parent
7bef4fbf1b
commit
df51615d9b
1 changed files with 17 additions and 12 deletions
|
|
@ -36,27 +36,32 @@ class TimelineItemContentPollFactory @Inject constructor(
|
||||||
if (!featureFlagService.isFeatureEnabled(FeatureFlags.Polls)) return TimelineItemUnknownContent
|
if (!featureFlagService.isFeatureEnabled(FeatureFlags.Polls)) return TimelineItemUnknownContent
|
||||||
|
|
||||||
// Todo Move this computation to the matrix rust sdk
|
// Todo Move this computation to the matrix rust sdk
|
||||||
val pollVotesCount = content.votes.flatMap { it.value }.size
|
val totalVoteCount = content.votes.flatMap { it.value }.size
|
||||||
val userVotes = content.votes.filter { matrixClient.sessionId in it.value }.keys
|
val myVotes = content.votes.filter { matrixClient.sessionId in it.value }.keys
|
||||||
val isEndedPoll = content.endTime != null
|
val isEndedPoll = content.endTime != null
|
||||||
val winnerIds = content.answers.map { it.id }
|
val winnerIds = if (!isEndedPoll) {
|
||||||
.groupBy { content.votes[it]?.size ?: 0 } // Group by votes count
|
emptyList()
|
||||||
.maxBy { it.key } // Keep max voted answers
|
} else {
|
||||||
.takeIf { it.key > 0 } // Ignore if no option has been voted
|
content.answers
|
||||||
?.value
|
.map { answer -> answer.id }
|
||||||
.orEmpty()
|
.groupBy { answerId -> content.votes[answerId]?.size ?: 0 } // Group by votes count
|
||||||
|
.maxByOrNull { (votes, _) -> votes } // Keep max voted answers
|
||||||
|
?.takeIf { (votes, _) -> votes > 0 } // Ignore if no option has been voted
|
||||||
|
?.value
|
||||||
|
.orEmpty()
|
||||||
|
}
|
||||||
val answerItems = content.answers.map { answer ->
|
val answerItems = content.answers.map { answer ->
|
||||||
val votesCount = content.votes[answer.id]?.size ?: 0
|
val answerVoteCount = content.votes[answer.id]?.size ?: 0
|
||||||
val isSelected = answer.id in userVotes
|
val isSelected = answer.id in myVotes
|
||||||
val isWinner = answer.id in winnerIds
|
val isWinner = answer.id in winnerIds
|
||||||
val percentage = if (pollVotesCount > 0) votesCount.toFloat() / pollVotesCount.toFloat() else 0f
|
val percentage = if (totalVoteCount > 0) answerVoteCount.toFloat() / totalVoteCount.toFloat() else 0f
|
||||||
PollAnswerItem(
|
PollAnswerItem(
|
||||||
answer = answer,
|
answer = answer,
|
||||||
isSelected = isSelected,
|
isSelected = isSelected,
|
||||||
isEnabled = !isEndedPoll,
|
isEnabled = !isEndedPoll,
|
||||||
isWinner = isWinner,
|
isWinner = isWinner,
|
||||||
isDisclosed = content.kind.isDisclosed || isEndedPoll,
|
isDisclosed = content.kind.isDisclosed || isEndedPoll,
|
||||||
votesCount = votesCount,
|
votesCount = answerVoteCount,
|
||||||
percentage = percentage,
|
percentage = percentage,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue