Refacto: move displayEmojiReactions to ActionListState.Target.Success

This commit is contained in:
Benoit Marty 2023-12-26 14:45:08 +01:00
parent c6f5c0a556
commit 29d23cbeee
6 changed files with 65 additions and 52 deletions

View file

@ -19,7 +19,6 @@ package io.element.android.features.messages.impl.actionlist
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@ -53,13 +52,6 @@ class ActionListPresenter @Inject constructor(
val isDeveloperModeEnabled by preferencesStore.isDeveloperModeEnabledFlow().collectAsState(initial = false)
val displayEmojiReactions by remember {
derivedStateOf {
val event = (target.value as? ActionListState.Target.Success)?.event
event?.isRemote == true && event.content.canReact()
}
}
fun handleEvents(event: ActionListEvents) {
when (event) {
ActionListEvents.Clear -> target.value = ActionListState.Target.None
@ -75,7 +67,6 @@ class ActionListPresenter @Inject constructor(
return ActionListState(
target = target.value,
displayEmojiReactions = displayEmojiReactions,
eventSink = { handleEvents(it) }
)
}
@ -178,8 +169,13 @@ class ActionListPresenter @Inject constructor(
}
}
}
if (actions.isNotEmpty()) {
target.value = ActionListState.Target.Success(timelineItem, actions.toImmutableList())
val displayEmojiReactions = timelineItem.isRemote && timelineItem.content.canReact()
if (actions.isNotEmpty() || displayEmojiReactions) {
target.value = ActionListState.Target.Success(
event = timelineItem,
displayEmojiReactions = displayEmojiReactions,
actions = actions.toImmutableList()
)
} else {
target.value = ActionListState.Target.None
}

View file

@ -24,7 +24,6 @@ import kotlinx.collections.immutable.ImmutableList
@Immutable
data class ActionListState(
val target: Target,
val displayEmojiReactions: Boolean,
val eventSink: (ActionListEvents) -> Unit,
) {
sealed interface Target {
@ -32,6 +31,7 @@ data class ActionListState(
data class Loading(val event: TimelineItem.Event) : Target
data class Success(
val event: TimelineItem.Event,
val displayEmojiReactions: Boolean,
val actions: ImmutableList<TimelineItemAction>,
) : Target
}

View file

@ -42,6 +42,7 @@ open class ActionListStateProvider : PreviewParameterProvider<ActionListState> {
event = aTimelineItemEvent().copy(
reactionsState = reactionsState
),
displayEmojiReactions = true,
actions = aTimelineItemActionList(),
)
),
@ -50,6 +51,7 @@ open class ActionListStateProvider : PreviewParameterProvider<ActionListState> {
event = aTimelineItemEvent(content = aTimelineItemImageContent()).copy(
reactionsState = reactionsState
),
displayEmojiReactions = true,
actions = aTimelineItemActionList(),
)
),
@ -58,6 +60,7 @@ open class ActionListStateProvider : PreviewParameterProvider<ActionListState> {
event = aTimelineItemEvent(content = aTimelineItemVideoContent()).copy(
reactionsState = reactionsState
),
displayEmojiReactions = true,
actions = aTimelineItemActionList(),
)
),
@ -66,6 +69,7 @@ open class ActionListStateProvider : PreviewParameterProvider<ActionListState> {
event = aTimelineItemEvent(content = aTimelineItemFileContent()).copy(
reactionsState = reactionsState
),
displayEmojiReactions = true,
actions = aTimelineItemActionList(),
)
),
@ -74,6 +78,7 @@ open class ActionListStateProvider : PreviewParameterProvider<ActionListState> {
event = aTimelineItemEvent(content = aTimelineItemAudioContent()).copy(
reactionsState = reactionsState
),
displayEmojiReactions = true,
actions = aTimelineItemActionList(),
)
),
@ -82,6 +87,7 @@ open class ActionListStateProvider : PreviewParameterProvider<ActionListState> {
event = aTimelineItemEvent(content = aTimelineItemVoiceContent()).copy(
reactionsState = reactionsState
),
displayEmojiReactions = true,
actions = aTimelineItemActionList(),
)
),
@ -90,6 +96,7 @@ open class ActionListStateProvider : PreviewParameterProvider<ActionListState> {
event = aTimelineItemEvent(content = aTimelineItemLocationContent()).copy(
reactionsState = reactionsState
),
displayEmojiReactions = true,
actions = aTimelineItemActionList(),
)
),
@ -98,18 +105,18 @@ open class ActionListStateProvider : PreviewParameterProvider<ActionListState> {
event = aTimelineItemEvent(content = aTimelineItemLocationContent()).copy(
reactionsState = reactionsState
),
displayEmojiReactions = false,
actions = aTimelineItemActionList(),
),
displayEmojiReactions = false,
),
anActionListState().copy(
target = ActionListState.Target.Success(
event = aTimelineItemEvent(content = aTimelineItemPollContent()).copy(
reactionsState = reactionsState
),
displayEmojiReactions = false,
actions = aTimelineItemPollActionList(),
),
displayEmojiReactions = false,
),
)
}
@ -117,7 +124,6 @@ open class ActionListStateProvider : PreviewParameterProvider<ActionListState> {
fun anActionListState() = ActionListState(
target = ActionListState.Target.None,
displayEmojiReactions = true,
eventSink = {}
)

View file

@ -179,7 +179,7 @@ private fun SheetContent(
HorizontalDivider()
}
}
if (state.displayEmojiReactions) {
if (target.displayEmojiReactions) {
item {
EmojiReactionsRow(
highlightedEmojis = target.event.reactionsState.highlightedKeys,

View file

@ -47,6 +47,7 @@ fun TimelineItemEventContent.canBeRepliedTo(): Boolean =
/**
* Return true if user can react (i.e. send a reaction) on the event content.
* This does not take into account the power level of the user.
*/
fun TimelineItemEventContent.canReact(): Boolean =
when (this) {