Immutable data class. var -> val. The code should be equivalent.

This commit is contained in:
Benoit Marty 2022-04-14 12:14:09 +02:00
parent 015438289e
commit 13cc0a2e8d
2 changed files with 35 additions and 25 deletions

View File

@ -24,13 +24,13 @@ import com.squareup.moshi.JsonClass
*/
@JsonClass(generateAdapter = true)
data class PollSummaryContent(
var myVote: String? = null,
// Array of VoteInfo, list is constructed so that there is only one vote by user
val myVote: String? = null,
// List of VoteInfo, list is constructed so that there is only one vote by user
// And that optionIndex is valid
var votes: List<VoteInfo>? = null,
var votesSummary: Map<String, VoteSummary>? = null,
var totalVotes: Int = 0,
var winnerVoteCount: Int = 0
val votes: List<VoteInfo>? = null,
val votesSummary: Map<String, VoteSummary>? = null,
val totalVotes: Int = 0,
val winnerVoteCount: Int = 0
)
@JsonClass(generateAdapter = true)

View File

@ -316,14 +316,16 @@ internal class EventRelationsAggregationProcessor @Inject constructor(
ContentMapper
.map(eventAnnotationsSummaryEntity.pollResponseSummary?.aggregatedContent)
?.toModel<PollSummaryContent>()
?.apply {
totalVotes = 0
winnerVoteCount = 0
votes = emptyList()
votesSummary = emptyMap()
}
?.apply {
eventAnnotationsSummaryEntity.pollResponseSummary?.aggregatedContent = ContentMapper.map(toContent())
?.let { existingPollSummaryContent ->
eventAnnotationsSummaryEntity.pollResponseSummary?.aggregatedContent = ContentMapper.map(
PollSummaryContent(
myVote = existingPollSummaryContent.myVote,
votes = emptyList(),
votesSummary = emptyMap(),
totalVotes = 0,
winnerVoteCount = 0,
)
.toContent())
}
val txId = event.unsignedData?.transactionId
@ -410,15 +412,15 @@ internal class EventRelationsAggregationProcessor @Inject constructor(
existing.pollResponseSummary = it
}
val closedTime = existingPollSummary?.closedTime
val closedTime = existingPollSummary.closedTime
if (closedTime != null && eventTimestamp > closedTime) {
Timber.v("## POLL is closed ignore event poll:$targetEventId, event :${event.eventId}")
return
}
val sumModel = ContentMapper.map(existingPollSummary?.aggregatedContent).toModel<PollSummaryContent>() ?: PollSummaryContent()
val currentModel = ContentMapper.map(existingPollSummary.aggregatedContent).toModel<PollSummaryContent>()
if (existingPollSummary!!.sourceEvents.contains(eventId)) {
if (existingPollSummary.sourceEvents.contains(eventId)) {
// ignore this event, we already know it (??)
Timber.v("## POLL ignoring event for summary, it's known eventId:$eventId")
return
@ -443,7 +445,9 @@ internal class EventRelationsAggregationProcessor @Inject constructor(
return
}
val votes = sumModel.votes?.toMutableList() ?: ArrayList()
val votes = currentModel?.votes.orEmpty().toMutableList()
var myVote: String? = null
val existingVoteIndex = votes.indexOfFirst { it.userId == senderId }
if (existingVoteIndex != -1) {
// Is the vote newer?
@ -452,7 +456,7 @@ internal class EventRelationsAggregationProcessor @Inject constructor(
// Take the new one
votes[existingVoteIndex] = VoteInfo(senderId, option, eventTimestamp)
if (userId == senderId) {
sumModel.myVote = option
myVote = option
}
Timber.v("## POLL adding vote $option for user $senderId in poll :$targetEventId ")
} else {
@ -461,16 +465,14 @@ internal class EventRelationsAggregationProcessor @Inject constructor(
} else {
votes.add(VoteInfo(senderId, option, eventTimestamp))
if (userId == senderId) {
sumModel.myVote = option
myVote = option
}
Timber.v("## POLL adding vote $option for user $senderId in poll :$targetEventId ")
}
sumModel.votes = votes
// Precompute the percentage of votes for all options
val totalVotes = votes.size
sumModel.totalVotes = totalVotes
sumModel.votesSummary = votes
val newVotesSummary = votes
.groupBy({ it.option }, { it.userId })
.mapValues {
VoteSummary(
@ -478,7 +480,7 @@ internal class EventRelationsAggregationProcessor @Inject constructor(
percentage = if (totalVotes == 0 && it.value.isEmpty()) 0.0 else it.value.size.toDouble() / totalVotes
)
}
sumModel.winnerVoteCount = sumModel.votesSummary?.maxOf { it.value.total } ?: 0
val newWinnerVoteCount = newVotesSummary.maxOf { it.value.total }
if (isLocalEcho) {
existingPollSummary.sourceLocalEchoEvents.add(eventId)
@ -486,7 +488,15 @@ internal class EventRelationsAggregationProcessor @Inject constructor(
existingPollSummary.sourceEvents.add(eventId)
}
existingPollSummary.aggregatedContent = ContentMapper.map(sumModel.toContent())
val newSumModel = PollSummaryContent(
myVote = myVote,
votes = votes,
votesSummary = newVotesSummary,
totalVotes = totalVotes,
winnerVoteCount = newWinnerVoteCount
)
existingPollSummary.aggregatedContent = ContentMapper.map(newSumModel.toContent())
}
private fun handleEndPoll(realm: Realm,