From 13cc0a2e8d836bf11a4d44eb9cbdd502066cbf91 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 14 Apr 2022 12:14:09 +0200 Subject: [PATCH] Immutable data class. var -> val. The code should be equivalent. --- .../session/room/model/PollSummaryContent.kt | 12 ++--- .../EventRelationsAggregationProcessor.kt | 48 +++++++++++-------- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/PollSummaryContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/PollSummaryContent.kt index f1e4354314..09458ff12e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/PollSummaryContent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/PollSummaryContent.kt @@ -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? = null, - var votesSummary: Map? = null, - var totalVotes: Int = 0, - var winnerVoteCount: Int = 0 + val votes: List? = null, + val votesSummary: Map? = null, + val totalVotes: Int = 0, + val winnerVoteCount: Int = 0 ) @JsonClass(generateAdapter = true) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/EventRelationsAggregationProcessor.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/EventRelationsAggregationProcessor.kt index 9f2b7d5f60..15ce5810c8 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/EventRelationsAggregationProcessor.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/EventRelationsAggregationProcessor.kt @@ -316,14 +316,16 @@ internal class EventRelationsAggregationProcessor @Inject constructor( ContentMapper .map(eventAnnotationsSummaryEntity.pollResponseSummary?.aggregatedContent) ?.toModel() - ?.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() + val currentModel = ContentMapper.map(existingPollSummary.aggregatedContent).toModel() - 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,