Skip to content

Commit 9309184

Browse files
committed
feat: Move alert showing logic into state presenter.
1 parent 253ec70 commit 9309184

File tree

6 files changed

+103
-46
lines changed

6 files changed

+103
-46
lines changed

features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/crypto/historyvisible/HistoryVisibleState.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77

88
package io.element.android.features.messages.impl.crypto.historyvisible
99

10-
import io.element.android.libraries.matrix.api.room.history.RoomHistoryVisibility
11-
1210
data class HistoryVisibleState(
13-
val roomHistoryVisibility: RoomHistoryVisibility,
14-
val roomIsEncrypted: Boolean,
15-
val acknowledged: Boolean,
11+
val showAlert: Boolean,
1612
val eventSink: (HistoryVisibleEvent) -> Unit,
1713
)

features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/crypto/historyvisible/HistoryVisibleStatePresenter.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ class HistoryVisibleStatePresenter(
3838
}
3939

4040
return HistoryVisibleState(
41-
roomHistoryVisibility = roomInfo.historyVisibility,
42-
roomIsEncrypted = roomInfo.isEncrypted == true,
43-
acknowledged = acknowledged,
41+
showAlert = roomInfo.historyVisibility != RoomHistoryVisibility.Joined && roomInfo.isEncrypted == true && !acknowledged,
4442
eventSink = { event ->
4543
when (event) {
4644
is HistoryVisibleEvent.Acknowledge ->

features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/crypto/historyvisible/HistoryVisibleStateProvider.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,15 @@ import io.element.android.libraries.matrix.api.room.history.RoomHistoryVisibilit
1313
class HistoryVisibleStateProvider : PreviewParameterProvider<HistoryVisibleState> {
1414
override val values: Sequence<HistoryVisibleState>
1515
get() = sequenceOf(
16-
aHistoryVisibleState(RoomHistoryVisibility.Joined, roomIsEncrypted = false, acknowledged = false),
17-
aHistoryVisibleState(RoomHistoryVisibility.Shared, roomIsEncrypted = true, acknowledged = false),
18-
aHistoryVisibleState(RoomHistoryVisibility.Shared, roomIsEncrypted = true, acknowledged = true)
16+
aHistoryVisibleState(showAlert = false),
17+
aHistoryVisibleState(showAlert = true),
1918
)
2019
}
2120

2221
internal fun aHistoryVisibleState(
23-
roomHistoryVisibility: RoomHistoryVisibility = RoomHistoryVisibility.Joined,
24-
roomIsEncrypted: Boolean = false,
25-
acknowledged: Boolean = false,
22+
showAlert: Boolean = false,
2623
eventSink: (HistoryVisibleEvent) -> Unit = {},
2724
) = HistoryVisibleState(
28-
roomHistoryVisibility = roomHistoryVisibility,
29-
roomIsEncrypted = roomIsEncrypted,
30-
acknowledged = acknowledged,
25+
showAlert,
3126
eventSink = eventSink,
3227
)

features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/crypto/historyvisible/HistoryVisibleStateView.kt

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import io.element.android.libraries.designsystem.atomic.molecules.ComposerAlertL
2323
import io.element.android.libraries.designsystem.atomic.molecules.ComposerAlertMolecule
2424
import io.element.android.libraries.designsystem.preview.ElementPreview
2525
import io.element.android.libraries.designsystem.preview.PreviewsDayNight
26-
import io.element.android.libraries.matrix.api.room.history.RoomHistoryVisibility
2726
import io.element.android.libraries.ui.strings.CommonStrings
2827

2928
@Composable
@@ -32,18 +31,7 @@ fun HistoryVisibleStateView(
3231
onLinkClick: (String, Boolean) -> Unit,
3332
modifier: Modifier = Modifier,
3433
) {
35-
// Ignore non-encrypted rooms.
36-
if (!state.roomIsEncrypted) {
37-
return
38-
}
39-
40-
// Ignore rooms with `history_visibility = joined`.
41-
if (state.roomHistoryVisibility == RoomHistoryVisibility.Joined) {
42-
return
43-
}
44-
45-
// Ignore non-joined rooms we have acknowledged.
46-
if (state.acknowledged) {
34+
if (!state.showAlert) {
4735
return
4836
}
4937

features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/crypto/historyvisible/FakeHistoryVisibleAcknowledgementRepository.kt

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,34 @@ package io.element.android.features.messages.impl.crypto.historyvisible
99

1010
import io.element.android.libraries.matrix.api.core.RoomId
1111
import kotlinx.coroutines.flow.Flow
12-
import kotlinx.coroutines.flow.MutableSharedFlow
13-
14-
class FakeHistoryVisibleAcknowledgementRepository : HistoryVisibleAcknowledgementRepository {
15-
private val acknowledgements = mutableMapOf<RoomId, MutableSharedFlow<Boolean>>()
12+
import kotlinx.coroutines.flow.MutableStateFlow
1613

14+
class FakeHistoryVisibleAcknowledgementRepository(
15+
private val acknowledgements: MutableMap<RoomId, MutableStateFlow<Boolean>> = mutableMapOf()
16+
) : HistoryVisibleAcknowledgementRepository {
1717
override fun hasAcknowledged(roomId: RoomId): Flow<Boolean> {
18-
return acknowledgements.getOrPut(roomId) { MutableSharedFlow() }
18+
return acknowledgements.getOrPut(roomId) {
19+
MutableStateFlow(false)
20+
}
1921
}
2022

2123
override suspend fun setAcknowledged(roomId: RoomId, value: Boolean) {
22-
val flow = acknowledgements.getOrPut(roomId) { MutableSharedFlow() }
24+
val flow = acknowledgements.getOrPut(roomId) {
25+
MutableStateFlow(value)
26+
}
2327
flow.emit(value)
2428
}
29+
30+
companion object {
31+
/**
32+
* Create the repository with a pre-existing entry.
33+
*/
34+
fun withRoom(roomId: RoomId, acknowledged: Boolean = false): FakeHistoryVisibleAcknowledgementRepository {
35+
return FakeHistoryVisibleAcknowledgementRepository(
36+
mutableMapOf(
37+
roomId to MutableStateFlow(acknowledged)
38+
)
39+
)
40+
}
41+
}
2542
}

features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/crypto/historyvisible/HistoryVisibleStatePresenterTest.kt

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,98 @@ class HistoryVisibleStatePresenterTest {
2222
@get:Rule
2323
val warmUpRule = WarmUpRule()
2424

25+
26+
@Test
27+
fun `present - initial with room shared, unencrypted`() = runTest {
28+
val room = FakeJoinedRoom()
29+
room.givenRoomInfo(aRoomInfo(historyVisibility = RoomHistoryVisibility.Shared, isEncrypted = false))
30+
val presenter = createHistoryVisibleStatePresenter(room)
31+
presenter.test {
32+
val initialState = awaitItem()
33+
assertThat(initialState.showAlert).isFalse()
34+
val nextState = awaitItem()
35+
assertThat(nextState.showAlert).isFalse()
36+
}
37+
}
38+
2539
@Test
26-
fun `present - initial state`() = runTest {
27-
val presenter = createHistoryVisibleStatePresenter()
40+
fun `present - initial with room joined, encrypted`() = runTest {
41+
val room = FakeJoinedRoom()
42+
room.givenRoomInfo(aRoomInfo(historyVisibility = RoomHistoryVisibility.Joined, isEncrypted = false))
43+
val presenter = createHistoryVisibleStatePresenter(room)
2844
presenter.test {
2945
val initialState = awaitItem()
30-
assertThat(initialState.acknowledged).isFalse()
31-
assertThat(initialState.roomHistoryVisibility).isEqualTo(RoomHistoryVisibility.Joined)
46+
assertThat(initialState.showAlert).isFalse()
47+
val nextState = awaitItem()
48+
assertThat(nextState.showAlert).isFalse()
3249
}
3350
}
3451

3552
@Test
36-
fun `present - when the room history visibility changes, the presenter emits a new state`() = runTest {
53+
fun `present - initial with room shared, encrypted, unacknowledged`() = runTest {
3754
val room = FakeJoinedRoom()
38-
val presenter = createHistoryVisibleStatePresenter(room)
55+
room.givenRoomInfo(aRoomInfo(historyVisibility = RoomHistoryVisibility.Shared, isEncrypted = true))
56+
val presenter = createHistoryVisibleStatePresenter(room, acknowledged = false)
3957
presenter.test {
4058
val initialState = awaitItem()
41-
assertThat(initialState.roomHistoryVisibility).isEqualTo(RoomHistoryVisibility.Joined)
42-
room.givenRoomInfo(aRoomInfo(historyVisibility = RoomHistoryVisibility.Shared))
59+
assertThat(initialState.showAlert).isFalse()
4360
val nextState = awaitItem()
44-
assertThat(nextState.roomHistoryVisibility).isEqualTo(RoomHistoryVisibility.Shared)
61+
assertThat(nextState.showAlert).isTrue()
62+
}
63+
}
64+
65+
@Test
66+
fun `present - initial with room shared, encrypted, acknowledged`() = runTest {
67+
val room = FakeJoinedRoom()
68+
room.givenRoomInfo(aRoomInfo(historyVisibility = RoomHistoryVisibility.Shared, isEncrypted = true))
69+
val presenter = createHistoryVisibleStatePresenter(room, acknowledged = true)
70+
presenter.test {
71+
val initialState = awaitItem()
72+
assertThat(initialState.showAlert).isFalse()
73+
// we do not expect a next state here, since the room info matches the initial state and does not get re-emitted.
74+
}
75+
}
76+
77+
@Test
78+
fun `present - transition from joined + unencrypted, to shared + encrypted`() = runTest {
79+
val room = FakeJoinedRoom()
80+
val repository = FakeHistoryVisibleAcknowledgementRepository()
81+
82+
room.givenRoomInfo(aRoomInfo(historyVisibility = RoomHistoryVisibility.Joined, isEncrypted = false))
83+
84+
val presenter = HistoryVisibleStatePresenter(
85+
repository,
86+
room,
87+
)
88+
89+
presenter.test {
90+
// initial state
91+
assertThat(awaitItem().showAlert).isFalse()
92+
93+
// emitted state from room info assignment
94+
assertThat(awaitItem().showAlert).isFalse()
95+
96+
// room is marked as encrypted
97+
room.givenRoomInfo(aRoomInfo(historyVisibility = RoomHistoryVisibility.Joined, isEncrypted = true))
98+
assertThat(awaitItem().showAlert).isFalse()
99+
100+
// room history visibility is changed to shared
101+
room.givenRoomInfo(aRoomInfo(historyVisibility = RoomHistoryVisibility.Shared, isEncrypted = true))
102+
assertThat(awaitItem().showAlert).isTrue()
103+
104+
// alert is acknowledged
105+
repository.setAcknowledged(room.roomId, true)
106+
assertThat(awaitItem().showAlert).isFalse()
45107
}
46108
}
47109

48110
private fun createHistoryVisibleStatePresenter(
49111
room: JoinedRoom = FakeJoinedRoom(),
112+
acknowledged: Boolean = false
50113
): HistoryVisibleStatePresenter {
51114
return HistoryVisibleStatePresenter(
52115
room = room,
53-
repository = FakeHistoryVisibleAcknowledgementRepository()
116+
repository = FakeHistoryVisibleAcknowledgementRepository.withRoom(room.roomId, acknowledged)
54117
)
55118
}
56119
}

0 commit comments

Comments
 (0)