Skip to content

Commit ef36922

Browse files
committed
Address review comments.
1 parent c78aa7c commit ef36922

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

features/space/impl/src/main/kotlin/io/element/android/features/space/impl/leave/LeaveSpacePresenter.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ import io.element.android.libraries.matrix.api.MatrixClient
2727
import io.element.android.libraries.matrix.api.core.RoomId
2828
import io.element.android.libraries.matrix.api.spaces.SpaceRoom
2929
import kotlinx.collections.immutable.ImmutableList
30+
import kotlinx.collections.immutable.ImmutableSet
31+
import kotlinx.collections.immutable.persistentSetOf
3032
import kotlinx.collections.immutable.toPersistentList
33+
import kotlinx.collections.immutable.toPersistentSet
3134
import kotlinx.coroutines.CoroutineScope
3235
import kotlinx.coroutines.launch
3336
import kotlin.jvm.optionals.getOrNull
@@ -52,13 +55,13 @@ class LeaveSpacePresenter(
5255
mutableStateOf<AsyncAction<Unit>>(AsyncAction.Uninitialized)
5356
}
5457
val selectedRoomIds = remember {
55-
mutableStateOf<Set<RoomId>>(emptySet())
58+
mutableStateOf<ImmutableSet<RoomId>>(persistentSetOf())
5659
}
5760
val joinedSpaceRooms by produceState(emptyList()) {
58-
// TODO Get the joined room from the SDK, should also have the
61+
// TODO Get the joined room from the SDK, should also have the isLastAdmin boolean
5962
val rooms = emptyList<SpaceRoom>()
6063
// By default select all rooms
61-
selectedRoomIds.value = rooms.map { it.roomId }.toSet()
64+
selectedRoomIds.value = rooms.map { it.roomId }.toPersistentSet()
6265
value = rooms
6366
}
6467
val selectableSpaceRooms by produceState<AsyncData<ImmutableList<SelectableSpaceRoom>>>(
@@ -81,14 +84,14 @@ class LeaveSpacePresenter(
8184
fun handleEvents(event: LeaveSpaceEvents) {
8285
when (event) {
8386
LeaveSpaceEvents.DeselectAllRooms -> {
84-
selectedRoomIds.value = emptySet()
87+
selectedRoomIds.value = persistentSetOf()
8588
}
8689
LeaveSpaceEvents.SelectAllRooms -> {
8790
selectedRoomIds.value = selectableSpaceRooms.dataOrNull()
8891
.orEmpty()
8992
.filter { it.isLastAdmin.not() }
9093
.map { it.spaceRoom.roomId }
91-
.toSet()
94+
.toPersistentSet()
9295
}
9396
is LeaveSpaceEvents.ToggleRoomSelection -> {
9497
val currentSet = selectedRoomIds.value
@@ -97,6 +100,7 @@ class LeaveSpacePresenter(
97100
} else {
98101
currentSet + event.roomId
99102
}
103+
.toPersistentSet()
100104
}
101105
LeaveSpaceEvents.LeaveSpace -> coroutineScope.leaveSpace(
102106
leaveSpaceAction = leaveSpaceAction,

features/space/impl/src/main/kotlin/io/element/android/features/space/impl/leave/LeaveSpaceState.kt

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,27 @@ data class LeaveSpaceState(
1818
val eventSink: (LeaveSpaceEvents) -> Unit,
1919
) {
2020
private val rooms = selectableSpaceRooms.dataOrNull().orEmpty()
21+
private val partition = rooms.partition { it.isLastAdmin }
22+
private val lastAdminRooms = partition.first
23+
private val selectableRooms = partition.second
2124

2225
/**
2326
* True if we should show the quick action to select/deselect all rooms.
2427
*/
25-
val showQuickAction = rooms
26-
.any { !it.isLastAdmin }
28+
val showQuickAction = selectableRooms.isNotEmpty()
2729

2830
/**
29-
* True if there are rooms and they are all selected.
31+
* True if there all the selectable rooms are selected.
3032
*/
31-
val areAllSelected = rooms
32-
.filter { !it.isLastAdmin }
33-
.let { rooms ->
34-
rooms.isNotEmpty() && rooms.all { it.isSelected }
35-
}
33+
val areAllSelected = selectableRooms.all { it.isSelected }
3634

3735
/**
3836
* True if there are rooms but the user is the last admin in all of them.
3937
*/
40-
val hasOnlyLastAdminRoom = rooms
41-
.let { rooms ->
42-
rooms.isNotEmpty() && rooms.all { it.isLastAdmin }
43-
}
38+
val hasOnlyLastAdminRoom = lastAdminRooms.isNotEmpty() && selectableRooms.isEmpty()
4439

4540
/**
4641
* Number of selected rooms.
4742
*/
48-
val numberOfSelectRooms = rooms
49-
.count { it.isSelected }
43+
val selectedRoomsCount = selectableRooms.count { it.isSelected }
5044
}

features/space/impl/src/main/kotlin/io/element/android/features/space/impl/leave/LeaveSpaceView.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ fun LeaveSpaceView(
118118
}
119119
LeaveSpaceButtons(
120120
showLeaveButton = state.selectableSpaceRooms is AsyncData.Success,
121-
nbOfSelectedRooms = state.numberOfSelectRooms,
121+
selectedRoomsCount = state.selectedRoomsCount,
122122
onLeaveSpace = {
123123
state.eventSink(LeaveSpaceEvents.LeaveSpace)
124124
},
@@ -198,16 +198,16 @@ private fun LeaveSpaceHeader(
198198
@Composable
199199
private fun LeaveSpaceButtons(
200200
showLeaveButton: Boolean,
201-
nbOfSelectedRooms: Int,
201+
selectedRoomsCount: Int,
202202
onLeaveSpace: () -> Unit,
203203
onCancel: () -> Unit,
204204
) {
205205
ButtonColumnMolecule(
206206
modifier = Modifier.padding(top = 16.dp)
207207
) {
208208
if (showLeaveButton) {
209-
val text = if (nbOfSelectedRooms > 0) {
210-
pluralStringResource(R.plurals.screen_leave_space_submit, nbOfSelectedRooms, nbOfSelectedRooms)
209+
val text = if (selectedRoomsCount > 0) {
210+
pluralStringResource(R.plurals.screen_leave_space_submit, selectedRoomsCount, selectedRoomsCount)
211211
} else {
212212
stringResource(CommonStrings.action_leave_space)
213213
}

features/space/impl/src/test/kotlin/io/element/android/features/space/impl/leave/LeaveSpaceStateTest.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class LeaveSpaceStateTest {
2020
selectableSpaceRooms = AsyncData.Loading()
2121
)
2222
assertThat(sut.showQuickAction).isFalse()
23-
assertThat(sut.areAllSelected).isFalse()
23+
assertThat(sut.areAllSelected).isTrue()
2424
assertThat(sut.hasOnlyLastAdminRoom).isFalse()
25-
assertThat(sut.numberOfSelectRooms).isEqualTo(0)
25+
assertThat(sut.selectedRoomsCount).isEqualTo(0)
2626
}
2727

2828
@Test
@@ -33,9 +33,9 @@ class LeaveSpaceStateTest {
3333
)
3434
)
3535
assertThat(sut.showQuickAction).isFalse()
36-
assertThat(sut.areAllSelected).isFalse()
36+
assertThat(sut.areAllSelected).isTrue()
3737
assertThat(sut.hasOnlyLastAdminRoom).isFalse()
38-
assertThat(sut.numberOfSelectRooms).isEqualTo(0)
38+
assertThat(sut.selectedRoomsCount).isEqualTo(0)
3939
}
4040

4141
@Test
@@ -51,7 +51,7 @@ class LeaveSpaceStateTest {
5151
assertThat(sut.showQuickAction).isTrue()
5252
assertThat(sut.areAllSelected).isFalse()
5353
assertThat(sut.hasOnlyLastAdminRoom).isFalse()
54-
assertThat(sut.numberOfSelectRooms).isEqualTo(1)
54+
assertThat(sut.selectedRoomsCount).isEqualTo(1)
5555
}
5656

5757
@Test
@@ -67,7 +67,7 @@ class LeaveSpaceStateTest {
6767
assertThat(sut.showQuickAction).isTrue()
6868
assertThat(sut.areAllSelected).isTrue()
6969
assertThat(sut.hasOnlyLastAdminRoom).isFalse()
70-
assertThat(sut.numberOfSelectRooms).isEqualTo(2)
70+
assertThat(sut.selectedRoomsCount).isEqualTo(2)
7171
}
7272

7373
@Test
@@ -84,7 +84,7 @@ class LeaveSpaceStateTest {
8484
assertThat(sut.showQuickAction).isTrue()
8585
assertThat(sut.areAllSelected).isTrue()
8686
assertThat(sut.hasOnlyLastAdminRoom).isFalse()
87-
assertThat(sut.numberOfSelectRooms).isEqualTo(2)
87+
assertThat(sut.selectedRoomsCount).isEqualTo(2)
8888
}
8989

9090
@Test
@@ -98,8 +98,8 @@ class LeaveSpaceStateTest {
9898
)
9999
)
100100
assertThat(sut.showQuickAction).isFalse()
101-
assertThat(sut.areAllSelected).isFalse()
101+
assertThat(sut.areAllSelected).isTrue()
102102
assertThat(sut.hasOnlyLastAdminRoom).isTrue()
103-
assertThat(sut.numberOfSelectRooms).isEqualTo(0)
103+
assertThat(sut.selectedRoomsCount).isEqualTo(0)
104104
}
105105
}

0 commit comments

Comments
 (0)