Repository: element-hq/element-x-android (language: kotlin)

Issue:
Leave space: DMs should not be displayed in the list
In the first iteration, we consider DMs important rooms we do not want to leave.


Discussion hints:
nan

Pull request: Filter out direct room in the leave space screen.
<!-- Please read [CONTRIBUTING.md](https://github.com/element-hq/element-x-android/blob/develop/CONTRIBUTING.md) before submitting your pull request -->
 
## Content

<!-- Describe shortly what has been changed -->
Filter out direct room in the leave space screen.

## Motivation and context

<!-- Provide link to the corresponding issue if applicable or explain the context -->
Closes #5496

## Screenshots / GIFs

<!--
We have screenshot tests in the project, so attaching screenshots to a PR is not mandatory, as far as there
is a Composable Preview covering the changes. In this case, the change will appear in the file diff.
Note that all the UI composables should be covered by a Composable Preview.

Providing a video of the change is still very useful for the reviewer and for the history of the project.

You can use a table like this to show screenshots comparison.
Uncomment this markdown table below and edit the last line `|||`:
|copy screenshot of before here|copy screenshot of after here|

|Before|After|
|-|-|
|||
 -->
No expected change

## Tests

<!-- Explain how you tested your development -->

- Add a DM to a space (using a client capable of doing this)
- Attempt to leave the space
- Observe that the DM is not listed in the room that can be left
- leave the space
- observe that the DM has not been left.

## Tested devices

- [ ] Physical
- [x] Emulator
- OS version(s):

## Checklist

<!-- Depending on the Pull Request content, it can be acceptable if some of the following checkboxes stay unchecked. -->

- [ ] Changes have been tested on an Android device or Android emulator with API 24
- [ ] UI change has been tested on both light and dark themes
- [ ] Accessibility has been taken into account. See https://github.com/element-hq/element-x-android/blob/develop/CONTRIBUTING.md#accessibility
- [ ] Pull request is based on the develop branch
- [ ] Pull request title will be used in the release note, it clearly define what will change for the user
- [ ] Pull request includes screenshots or videos if containing UI changes
- [ ] You've made a self review of your PR


Test command: ./gradlew :features:space:impl:testDebugUnitTest --tests "io.element.android.features.space.impl.leave.LeaveSpacePresenterTest"

Fix patch:
diff --git a/features/space/impl/src/main/kotlin/io/element/android/features/space/impl/leave/LeaveSpacePresenter.kt b/features/space/impl/src/main/kotlin/io/element/android/features/space/impl/leave/LeaveSpacePresenter.kt
index 31271104a31..19e593bf7c0 100644
--- a/features/space/impl/src/main/kotlin/io/element/android/features/space/impl/leave/LeaveSpacePresenter.kt
+++ b/features/space/impl/src/main/kotlin/io/element/android/features/space/impl/leave/LeaveSpacePresenter.kt
@@ -66,7 +66,8 @@ class LeaveSpacePresenter(
                 .orEmpty()
                 .partition { it.spaceRoom.roomId == leaveSpaceHandle.id }
             // By default select all rooms that can be left
-            selectedRoomIds = otherRooms
+            val otherRoomsExcludingDm = otherRooms.filter { it.spaceRoom.isDirect != true }
+            selectedRoomIds = otherRoomsExcludingDm
                 .filter { it.isLastAdmin.not() }
                 .map { it.spaceRoom.roomId }
             leaveSpaceRooms = rooms.fold(
@@ -74,7 +75,7 @@ class LeaveSpacePresenter(
                     AsyncData.Success(
                         LeaveSpaceRooms(
                             current = currentRoom.firstOrNull(),
-                            others = otherRooms.toImmutableList(),
+                            others = otherRoomsExcludingDm.toImmutableList(),
                         )
                     )
                 },


Test patch:
diff --git a/features/space/impl/src/test/kotlin/io/element/android/features/space/impl/leave/LeaveSpacePresenterTest.kt b/features/space/impl/src/test/kotlin/io/element/android/features/space/impl/leave/LeaveSpacePresenterTest.kt
index 81920dd0f22..3d123f3f41c 100644
--- a/features/space/impl/src/test/kotlin/io/element/android/features/space/impl/leave/LeaveSpacePresenterTest.kt
+++ b/features/space/impl/src/test/kotlin/io/element/android/features/space/impl/leave/LeaveSpacePresenterTest.kt
@@ -16,6 +16,7 @@ import io.element.android.libraries.matrix.api.spaces.SpaceRoom
 import io.element.android.libraries.matrix.test.AN_EXCEPTION
 import io.element.android.libraries.matrix.test.A_ROOM_ID
 import io.element.android.libraries.matrix.test.A_ROOM_ID_2
+import io.element.android.libraries.matrix.test.A_ROOM_ID_3
 import io.element.android.libraries.matrix.test.A_SPACE_ID
 import io.element.android.libraries.matrix.test.A_SPACE_NAME
 import io.element.android.libraries.matrix.test.spaces.FakeLeaveSpaceHandle
@@ -91,6 +92,50 @@ class LeaveSpacePresenterTest {
         }
     }
 
+    @Test
+    fun `present - direct rooms are filtered out`() = runTest {
+        val leaveResult = lambdaRecorder<List<RoomId>, Result<Unit>> { Result.success(Unit) }
+        val presenter = createLeaveSpacePresenter(
+            leaveSpaceHandle = FakeLeaveSpaceHandle(
+                roomsResult = {
+                    Result.success(
+                        listOf(
+                            aLeaveSpaceRoom(spaceRoom = aSpace),
+                            aLeaveSpaceRoom(
+                                spaceRoom = aSpaceRoom(roomId = A_ROOM_ID, isDirect = false)
+                            ),
+                            aLeaveSpaceRoom(
+                                spaceRoom = aSpaceRoom(roomId = A_ROOM_ID_2, isDirect = true)
+                            ),
+                            aLeaveSpaceRoom(
+                                spaceRoom = aSpaceRoom(roomId = A_ROOM_ID_3, isDirect = null)
+                            ),
+                        )
+                    )
+                },
+                leaveResult = leaveResult,
+            )
+        )
+        presenter.test {
+            val state = awaitItem()
+            assertThat(state.spaceName).isNull()
+            skipItems(3)
+            val finalState = awaitItem()
+            // The current state is not in the sub room list
+            assertThat(finalState.selectableSpaceRooms.dataOrNull()!!.map { it.spaceRoom.roomId }).containsExactly(A_ROOM_ID, A_ROOM_ID_3)
+            assertThat(finalState.selectedRoomsCount).isEqualTo(2)
+            // Leaving the space will not include the DM
+            finalState.eventSink(LeaveSpaceEvents.LeaveSpace)
+            val stateLeaving = awaitItem()
+            assertThat(stateLeaving.leaveSpaceAction).isEqualTo(AsyncAction.Loading)
+            val stateLeft = awaitItem()
+            assertThat(stateLeft.leaveSpaceAction.isSuccess()).isTrue()
+            leaveResult.assertions().isCalledOnce().with(
+                value(listOf(A_ROOM_ID, A_ROOM_ID_3))
+            )
+        }
+    }
+
     @Test
     fun `present - leave space and sub rooms`() = runTest {
         val leaveResult = lambdaRecorder<List<RoomId>, Result<Unit>> { Result.success(Unit) }