PushHistoryEvents -> PushHistoryEvent

This commit is contained in:
Benoit Marty
2026-08-25 10:33:58 +02:00
parent cfa63156e2
commit 34a086261f
7 changed files with 39 additions and 39 deletions
@@ -12,9 +12,9 @@ import io.element.android.libraries.matrix.api.core.EventId
import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.matrix.api.core.SessionId
sealed interface PushHistoryEvents {
data class SetShowOnlyErrors(val showOnlyErrors: Boolean) : PushHistoryEvents
data class Reset(val requiresConfirmation: Boolean) : PushHistoryEvents
data class NavigateTo(val sessionId: SessionId, val roomId: RoomId, val eventId: EventId) : PushHistoryEvents
data object ClearDialog : PushHistoryEvents
sealed interface PushHistoryEvent {
data class SetShowOnlyErrors(val showOnlyErrors: Boolean) : PushHistoryEvent
data class Reset(val requiresConfirmation: Boolean) : PushHistoryEvent
data class NavigateTo(val sessionId: SessionId, val roomId: RoomId, val eventId: EventId) : PushHistoryEvent
data object ClearDialog : PushHistoryEvent
}
@@ -62,12 +62,12 @@ class PushHistoryPresenter(
var resetAction: AsyncAction<Unit> by remember { mutableStateOf(AsyncAction.Uninitialized) }
var showNotSameAccountError by remember { mutableStateOf(false) }
fun handleEvent(event: PushHistoryEvents) {
fun handleEvent(event: PushHistoryEvent) {
when (event) {
is PushHistoryEvents.SetShowOnlyErrors -> {
is PushHistoryEvent.SetShowOnlyErrors -> {
showOnlyErrors = event.showOnlyErrors
}
is PushHistoryEvents.Reset -> {
is PushHistoryEvent.Reset -> {
if (event.requiresConfirmation) {
resetAction = AsyncAction.ConfirmingNoParams
} else {
@@ -78,11 +78,11 @@ class PushHistoryPresenter(
}
}
}
PushHistoryEvents.ClearDialog -> {
PushHistoryEvent.ClearDialog -> {
resetAction = AsyncAction.Uninitialized
showNotSameAccountError = false
}
is PushHistoryEvents.NavigateTo -> {
is PushHistoryEvent.NavigateTo -> {
if (event.sessionId != sessionId) {
showNotSameAccountError = true
} else {
@@ -18,5 +18,5 @@ data class PushHistoryState(
val showOnlyErrors: Boolean,
val resetAction: AsyncAction<Unit>,
val showNotSameAccountError: Boolean,
val eventSink: (PushHistoryEvents) -> Unit,
val eventSink: (PushHistoryEvent) -> Unit,
)
@@ -53,7 +53,7 @@ fun aPushHistoryState(
showOnlyErrors: Boolean = false,
resetAction: AsyncAction<Unit> = AsyncAction.Uninitialized,
showNotSameAccountError: Boolean = false,
eventSink: (PushHistoryEvents) -> Unit = {},
eventSink: (PushHistoryEvent) -> Unit = {},
) = PushHistoryState(
pushCounter = pushCounter,
pushHistoryItems = pushHistoryItems.toImmutableList(),
@@ -100,14 +100,14 @@ fun PushHistoryView(
},
onClick = {
showMenu = false
state.eventSink(PushHistoryEvents.SetShowOnlyErrors(state.showOnlyErrors.not()))
state.eventSink(PushHistoryEvent.SetShowOnlyErrors(state.showOnlyErrors.not()))
},
)
DropdownMenuItem(
text = { Text(stringResource(id = CommonStrings.action_reset)) },
onClick = {
showMenu = false
state.eventSink(PushHistoryEvents.Reset(requiresConfirmation = true))
state.eventSink(PushHistoryEvent.Reset(requiresConfirmation = true))
},
)
}
@@ -132,8 +132,8 @@ fun PushHistoryView(
title = stringResource(CommonStrings.dialog_title_confirmation),
submitText = stringResource(CommonStrings.action_reset),
cancelText = stringResource(CommonStrings.action_cancel),
onSubmitClick = { state.eventSink(PushHistoryEvents.Reset(requiresConfirmation = false)) },
onDismiss = { state.eventSink(PushHistoryEvents.ClearDialog) },
onSubmitClick = { state.eventSink(PushHistoryEvent.Reset(requiresConfirmation = false)) },
onDismiss = { state.eventSink(PushHistoryEvent.ClearDialog) },
)
},
onErrorDismiss = {},
@@ -142,7 +142,7 @@ fun PushHistoryView(
if (state.showNotSameAccountError) {
ErrorDialog(
content = "Please switch account first to navigate to the event.",
onSubmit = { state.eventSink(PushHistoryEvents.ClearDialog) }
onSubmit = { state.eventSink(PushHistoryEvent.ClearDialog) }
)
}
}
@@ -175,7 +175,7 @@ private fun PushHistoryContent(
val roomId = pushHistory.roomId
val eventId = pushHistory.eventId
if (sessionId != null && roomId != null && eventId != null) {
state.eventSink(PushHistoryEvents.NavigateTo(sessionId, roomId, eventId))
state.eventSink(PushHistoryEvent.NavigateTo(sessionId, roomId, eventId))
}
}
)
@@ -73,9 +73,9 @@ class PushHistoryPresenterTest {
)
presenter.test {
val initialState = awaitItem()
initialState.eventSink(PushHistoryEvents.Reset(requiresConfirmation = true))
initialState.eventSink(PushHistoryEvent.Reset(requiresConfirmation = true))
assertThat(awaitItem().resetAction).isEqualTo(AsyncAction.ConfirmingNoParams)
initialState.eventSink(PushHistoryEvents.ClearDialog)
initialState.eventSink(PushHistoryEvent.ClearDialog)
assertThat(awaitItem().resetAction).isEqualTo(AsyncAction.Uninitialized)
resetPushHistoryResult.assertions().isNeverCalled()
}
@@ -92,9 +92,9 @@ class PushHistoryPresenterTest {
)
presenter.test {
val initialState = awaitItem()
initialState.eventSink(PushHistoryEvents.Reset(requiresConfirmation = true))
initialState.eventSink(PushHistoryEvent.Reset(requiresConfirmation = true))
assertThat(awaitItem().resetAction).isEqualTo(AsyncAction.ConfirmingNoParams)
initialState.eventSink(PushHistoryEvents.Reset(requiresConfirmation = false))
initialState.eventSink(PushHistoryEvent.Reset(requiresConfirmation = false))
assertThat(awaitItem().resetAction).isEqualTo(AsyncAction.Loading)
assertThat(awaitItem().resetAction).isEqualTo(AsyncAction.Uninitialized)
resetPushHistoryResult.assertions().isCalledOnce()
@@ -115,13 +115,13 @@ class PushHistoryPresenterTest {
pushService.emitPushHistoryItems(listOf(item, itemError))
awaitItem().let { state ->
assertThat(state.pushHistoryItems).containsExactly(item, itemError)
state.eventSink(PushHistoryEvents.SetShowOnlyErrors(showOnlyErrors = true))
state.eventSink(PushHistoryEvent.SetShowOnlyErrors(showOnlyErrors = true))
}
skipItems(1)
awaitItem().let { state ->
assertThat(state.showOnlyErrors).isTrue()
assertThat(state.pushHistoryItems).containsExactly(itemError)
state.eventSink(PushHistoryEvents.SetShowOnlyErrors(showOnlyErrors = false))
state.eventSink(PushHistoryEvent.SetShowOnlyErrors(showOnlyErrors = false))
}
skipItems(1)
awaitItem().let { state ->
@@ -142,7 +142,7 @@ class PushHistoryPresenterTest {
presenter.test {
val initialState = awaitItem()
initialState.eventSink(
PushHistoryEvents.NavigateTo(
PushHistoryEvent.NavigateTo(
sessionId = A_SESSION_ID,
roomId = A_ROOM_ID,
eventId = AN_EVENT_ID,
@@ -160,7 +160,7 @@ class PushHistoryPresenterTest {
presenter.test {
val initialState = awaitItem()
initialState.eventSink(
PushHistoryEvents.NavigateTo(
PushHistoryEvent.NavigateTo(
sessionId = A_SESSION_ID_2,
roomId = A_ROOM_ID,
eventId = AN_EVENT_ID,
@@ -168,7 +168,7 @@ class PushHistoryPresenterTest {
)
assertThat(awaitItem().showNotSameAccountError).isTrue()
// Reset error
initialState.eventSink(PushHistoryEvents.ClearDialog)
initialState.eventSink(PushHistoryEvent.ClearDialog)
assertThat(awaitItem().showNotSameAccountError).isFalse()
}
}
@@ -30,8 +30,8 @@ import org.junit.Test
class PushHistoryViewTest : RobolectricTest() {
@Test
fun `clicking on Reset sends a PushHistoryEvents`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<PushHistoryEvents>()
fun `clicking on Reset sends a PushHistoryEvent`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<PushHistoryEvent>()
setPushHistoryView(
aPushHistoryState(
pushCounter = 123,
@@ -41,14 +41,14 @@ class PushHistoryViewTest : RobolectricTest() {
val menuContentDescription = activity!!.getString(CommonStrings.a11y_user_menu)
onNodeWithContentDescription(menuContentDescription).performClick()
clickOn(CommonStrings.action_reset)
eventsRecorder.assertSingle(PushHistoryEvents.Reset(requiresConfirmation = true))
eventsRecorder.assertSingle(PushHistoryEvent.Reset(requiresConfirmation = true))
// Also check that the push counter is rendered
onNodeWithText("123").assertExists()
}
@Test
fun `clicking on show only errors sends a PushHistoryEvents(true)`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<PushHistoryEvents>()
fun `clicking on show only errors sends a PushHistoryEvent(true)`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<PushHistoryEvent>()
setPushHistoryView(
aPushHistoryState(
showOnlyErrors = false,
@@ -58,12 +58,12 @@ class PushHistoryViewTest : RobolectricTest() {
val menuContentDescription = activity!!.getString(CommonStrings.a11y_user_menu)
onNodeWithContentDescription(menuContentDescription).performClick()
onNodeWithText("Show only errors").performClick()
eventsRecorder.assertSingle(PushHistoryEvents.SetShowOnlyErrors(showOnlyErrors = true))
eventsRecorder.assertSingle(PushHistoryEvent.SetShowOnlyErrors(showOnlyErrors = true))
}
@Test
fun `clicking on show only errors sends a PushHistoryEvents(false)`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<PushHistoryEvents>()
fun `clicking on show only errors sends a PushHistoryEvent(false)`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<PushHistoryEvent>()
setPushHistoryView(
aPushHistoryState(
showOnlyErrors = true,
@@ -73,12 +73,12 @@ class PushHistoryViewTest : RobolectricTest() {
val menuContentDescription = activity!!.getString(CommonStrings.a11y_user_menu)
onNodeWithContentDescription(menuContentDescription).performClick()
onNodeWithText("Show only errors").performClick()
eventsRecorder.assertSingle(PushHistoryEvents.SetShowOnlyErrors(showOnlyErrors = false))
eventsRecorder.assertSingle(PushHistoryEvent.SetShowOnlyErrors(showOnlyErrors = false))
}
@Test
fun `clicking on an invalid event has no effect`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<PushHistoryEvents>(expectEvents = false)
val eventsRecorder = EventsRecorder<PushHistoryEvent>(expectEvents = false)
setPushHistoryView(
aPushHistoryState(
pushHistoryItems = listOf(
@@ -95,7 +95,7 @@ class PushHistoryViewTest : RobolectricTest() {
@Test
fun `clicking on a valid event emits the expected Event`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<PushHistoryEvents>()
val eventsRecorder = EventsRecorder<PushHistoryEvent>()
setPushHistoryView(
aPushHistoryState(
pushHistoryItems = listOf(
@@ -111,7 +111,7 @@ class PushHistoryViewTest : RobolectricTest() {
)
onNodeWithText(A_FORMATTED_DATE).performClick()
eventsRecorder.assertSingle(
PushHistoryEvents.NavigateTo(
PushHistoryEvent.NavigateTo(
sessionId = A_SESSION_ID,
roomId = A_ROOM_ID,
eventId = AN_EVENT_ID,