mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-11-02 21:32:34 +00:00
Show an error when the room to leave cannot be found
showLeaveRoomAlert ran its whole body inside client.getRoom(roomId)?.use { }.
When getRoom returned null, which happens for a room the client can no longer
resolve, the body was skipped without writing any state: leaveAction stayed
Uninitialized, so no confirmation dialog and no error was displayed and
Leave room appeared to do nothing at all.
Handle the missing room explicitly and surface it as a failure, which
AsyncActionView already renders with the generic error dialog. leaveRoom used
a !! for the same lookup, so it reported the miss as a NullPointerException;
it now throws the same named exception.
This commit is contained in:
+11
-2
@@ -60,7 +60,13 @@ class LeaveRoomPresenter(
|
|||||||
roomId: RoomId,
|
roomId: RoomId,
|
||||||
leaveAction: MutableState<AsyncAction<Unit>>,
|
leaveAction: MutableState<AsyncAction<Unit>>,
|
||||||
) = launch(dispatchers.io) {
|
) = launch(dispatchers.io) {
|
||||||
client.getRoom(roomId)?.use { room ->
|
val room = client.getRoom(roomId)
|
||||||
|
if (room == null) {
|
||||||
|
Timber.e("Unable to find room $roomId to leave it")
|
||||||
|
leaveAction.value = AsyncAction.Failure(RoomNotFoundException(roomId))
|
||||||
|
return@launch
|
||||||
|
}
|
||||||
|
room.use {
|
||||||
val roomInfo = room.roomInfoFlow.first()
|
val roomInfo = room.roomInfoFlow.first()
|
||||||
leaveAction.value = when {
|
leaveAction.value = when {
|
||||||
roomInfo.isDm -> Confirmation.Dm(roomId)
|
roomInfo.isDm -> Confirmation.Dm(roomId)
|
||||||
@@ -78,7 +84,8 @@ class LeaveRoomPresenter(
|
|||||||
leaveAction: MutableState<AsyncAction<Unit>>,
|
leaveAction: MutableState<AsyncAction<Unit>>,
|
||||||
) = launch(dispatchers.io) {
|
) = launch(dispatchers.io) {
|
||||||
leaveAction.runCatchingUpdatingState {
|
leaveAction.runCatchingUpdatingState {
|
||||||
client.getRoom(roomId)!!.use { room ->
|
val room = client.getRoom(roomId) ?: throw RoomNotFoundException(roomId)
|
||||||
|
room.use {
|
||||||
room
|
room
|
||||||
.leave()
|
.leave()
|
||||||
.onSuccess { notificationConversationService.onLeftRoom(client.sessionId, roomId) }
|
.onSuccess { notificationConversationService.onLeftRoom(client.sessionId, roomId) }
|
||||||
@@ -100,3 +107,5 @@ class LeaveRoomPresenter(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal class RoomNotFoundException(roomId: RoomId) : IllegalStateException("Room $roomId not found")
|
||||||
|
|||||||
+26
@@ -175,6 +175,32 @@ class LeaveBaseRoomPresenterTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - show error if the room to confirm leaving is not found`() = runTest {
|
||||||
|
val presenter = createLeaveRoomPresenter(client = FakeMatrixClient())
|
||||||
|
presenter.stateFlow().test {
|
||||||
|
val initialState = awaitItem()
|
||||||
|
initialState.eventSink(LeaveRoomEvent.LeaveRoom(A_ROOM_ID, needsConfirmation = true))
|
||||||
|
val errorState = awaitItem()
|
||||||
|
assertThat(errorState.leaveAction).isInstanceOf(AsyncAction.Failure::class.java)
|
||||||
|
cancelAndIgnoreRemainingEvents()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - show error if the room to leave is not found`() = runTest {
|
||||||
|
val presenter = createLeaveRoomPresenter(client = FakeMatrixClient())
|
||||||
|
presenter.stateFlow().test {
|
||||||
|
val initialState = awaitItem()
|
||||||
|
initialState.eventSink(LeaveRoomEvent.LeaveRoom(A_ROOM_ID, needsConfirmation = false))
|
||||||
|
val progressState = awaitItem()
|
||||||
|
assertThat(progressState.leaveAction).isEqualTo(AsyncAction.Loading)
|
||||||
|
val errorState = awaitItem()
|
||||||
|
assertThat(errorState.leaveAction).isInstanceOf(AsyncAction.Failure::class.java)
|
||||||
|
cancelAndIgnoreRemainingEvents()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `present - reset state after error`() = runTest {
|
fun `present - reset state after error`() = runTest {
|
||||||
val presenter = createLeaveRoomPresenter(
|
val presenter = createLeaveRoomPresenter(
|
||||||
|
|||||||
Reference in New Issue
Block a user