Compose : add immutability to some Reaction classes

This commit is contained in:
ganfra 2024-07-19 17:13:49 +02:00
parent f2378ca6ea
commit 6ee15e4b6a
6 changed files with 18 additions and 16 deletions

View file

@ -47,7 +47,7 @@ class ReactionSummaryPresenter @Inject constructor(
fun handleEvents(event: ReactionSummaryEvents) { fun handleEvents(event: ReactionSummaryEvents) {
when (event) { when (event) {
is ReactionSummaryEvents.ShowReactionSummary -> target.value = ReactionSummaryState.Summary( is ReactionSummaryEvents.ShowReactionSummary -> target.value = ReactionSummaryState.Summary(
reactions = event.reactions, reactions = event.reactions.toImmutableList(),
selectedKey = event.selectedKey, selectedKey = event.selectedKey,
selectedEventId = event.eventId selectedEventId = event.eventId
) )
@ -73,8 +73,8 @@ class ReactionSummaryPresenter @Inject constructor(
avatarUrl = member?.avatarUrl avatarUrl = member?.avatarUrl
) )
sender.copy(user = user) sender.copy(user = user)
}) }.toImmutableList())
}) }.toImmutableList())
} }
} }
} }

View file

@ -18,13 +18,14 @@ package io.element.android.features.messages.impl.timeline.components.reactionsu
import io.element.android.features.messages.impl.timeline.model.AggregatedReaction import io.element.android.features.messages.impl.timeline.model.AggregatedReaction
import io.element.android.libraries.matrix.api.core.EventId import io.element.android.libraries.matrix.api.core.EventId
import kotlinx.collections.immutable.ImmutableList
data class ReactionSummaryState( data class ReactionSummaryState(
val target: Summary?, val target: Summary?,
val eventSink: (ReactionSummaryEvents) -> Unit val eventSink: (ReactionSummaryEvents) -> Unit
) { ) {
data class Summary( data class Summary(
val reactions: List<AggregatedReaction>, val reactions: ImmutableList<AggregatedReaction>,
val selectedKey: String, val selectedKey: String,
val selectedEventId: EventId val selectedEventId: EventId
) )

View file

@ -117,6 +117,7 @@ class TimelineItemEventFactory @Inject constructor(
sentTime = timeFormatter.format(date), sentTime = timeFormatter.format(date),
) )
} }
.toImmutableList()
) )
} }
// Sort aggregated reactions by count and then timestamp ascending, using // Sort aggregated reactions by count and then timestamp ascending, using
@ -127,7 +128,9 @@ class TimelineItemEventFactory @Inject constructor(
compareByDescending<AggregatedReaction> { it.count } compareByDescending<AggregatedReaction> { it.count }
.thenBy { it.senders[0].timestamp } .thenBy { it.senders[0].timestamp }
) )
return TimelineItemReactions(aggregatedReactions.toImmutableList()) return TimelineItemReactions(
reactions = aggregatedReactions.toImmutableList()
)
} }
private fun MatrixTimelineItem.Event.computeReadReceiptState( private fun MatrixTimelineItem.Event.computeReadReceiptState(

View file

@ -18,6 +18,7 @@ package io.element.android.features.messages.impl.timeline.model
import io.element.android.libraries.core.extensions.ellipsize import io.element.android.libraries.core.extensions.ellipsize
import io.element.android.libraries.matrix.api.core.UserId import io.element.android.libraries.matrix.api.core.UserId
import kotlinx.collections.immutable.ImmutableList
/** /**
* Length at which we ellipsize a reaction key for display * Length at which we ellipsize a reaction key for display
@ -35,28 +36,22 @@ private const val MAX_DISPLAY_CHARS = 16
data class AggregatedReaction( data class AggregatedReaction(
val currentUserId: UserId, val currentUserId: UserId,
val key: String, val key: String,
val senders: List<AggregatedReactionSender> val senders: ImmutableList<AggregatedReactionSender>
) { ) {
/** /**
* The key to be displayed on screen. * The key to be displayed on screen.
* *
* See [MAX_DISPLAY_CHARS]. * See [MAX_DISPLAY_CHARS].
*/ */
val displayKey: String by lazy { val displayKey: String = key.ellipsize(MAX_DISPLAY_CHARS)
key.ellipsize(MAX_DISPLAY_CHARS)
}
/** /**
* The number of users who reacted with this key. * The number of users who reacted with this key.
*/ */
val count: Int by lazy { val count: Int = senders.count()
senders.count()
}
/** /**
* True if the reaction has (also) been sent by the current user. * True if the reaction has (also) been sent by the current user.
*/ */
val isHighlighted: Boolean by lazy { val isHighlighted: Boolean = senders.any { it.senderId == currentUserId }
senders.any { it.senderId.value == currentUserId.value }
}
} }

View file

@ -18,6 +18,7 @@ package io.element.android.features.messages.impl.timeline.model
import androidx.compose.ui.tooling.preview.PreviewParameterProvider import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import io.element.android.libraries.matrix.api.core.UserId import io.element.android.libraries.matrix.api.core.UserId
import kotlinx.collections.immutable.toImmutableList
import java.text.DateFormat import java.text.DateFormat
import java.util.Date import java.util.Date
@ -53,6 +54,6 @@ fun anAggregatedReaction(
return AggregatedReaction( return AggregatedReaction(
currentUserId = userId, currentUserId = userId,
key = key, key = key,
senders = senders senders = senders.toImmutableList()
) )
} }

View file

@ -16,10 +16,12 @@
package io.element.android.features.messages.impl.timeline.model package io.element.android.features.messages.impl.timeline.model
import androidx.compose.runtime.Immutable
import io.element.android.libraries.matrix.api.core.UserId import io.element.android.libraries.matrix.api.core.UserId
import io.element.android.libraries.matrix.api.user.MatrixUser import io.element.android.libraries.matrix.api.user.MatrixUser
import java.util.Date import java.util.Date
@Immutable
data class AggregatedReactionSender( data class AggregatedReactionSender(
val senderId: UserId, val senderId: UserId,
val timestamp: Date, val timestamp: Date,