Skip to content

Commit d271ea9

Browse files
committed
Fix test and improve code.
1 parent fb9aaad commit d271ea9

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

features/home/impl/src/main/kotlin/io/element/android/features/home/impl/HomePresenter.kt

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import io.element.android.libraries.sessionstorage.api.SessionData
3636
import io.element.android.libraries.sessionstorage.api.SessionStore
3737
import kotlinx.collections.immutable.persistentListOf
3838
import kotlinx.collections.immutable.toPersistentList
39-
import kotlinx.coroutines.flow.map
39+
import kotlinx.coroutines.flow.combine
40+
import kotlinx.coroutines.flow.onEach
4041
import kotlinx.coroutines.launch
4142

4243
@Inject
@@ -56,8 +57,19 @@ class HomePresenter(
5657
val coroutineState = rememberCoroutineScope()
5758
val matrixUser by client.userProfile.collectAsState()
5859
val matrixUserAndNeighbors by remember {
59-
sessionStore.sessionsFlow().map { list ->
60-
list.takeCurrentUserWithNeighbors(matrixUser).toPersistentList()
60+
combine(
61+
client.userProfile.onEach { user ->
62+
// Ensure that the profile is always up to date in our
63+
// session storage when it changes
64+
sessionStore.updateUserProfile(
65+
sessionId = user.userId.value,
66+
displayName = user.displayName,
67+
avatarUrl = user.avatarUrl,
68+
)
69+
},
70+
sessionStore.sessionsFlow()
71+
) { user, sessions ->
72+
sessions.takeCurrentUserWithNeighbors(user).toPersistentList()
6173
}
6274
}.collectAsState(initial = persistentListOf(matrixUser))
6375
val isOnline by syncService.isOnline.collectAsState()
@@ -76,15 +88,6 @@ class HomePresenter(
7688
// Force a refresh of the profile
7789
client.getUserProfile()
7890
}
79-
LaunchedEffect(matrixUser) {
80-
// Ensure that the profile is always up to date in our
81-
// session storage when it changes
82-
sessionStore.updateUserProfile(
83-
sessionId = matrixUser.userId.value,
84-
displayName = matrixUser.displayName,
85-
avatarUrl = matrixUser.avatarUrl,
86-
)
87-
}
8891
// Avatar indicator
8992
val showAvatarIndicator by indicatorService.showRoomListTopBarIndicator()
9093
val directLogoutState = logoutPresenter.present()

features/home/impl/src/test/kotlin/io/element/android/features/home/impl/HomePresenterTest.kt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class HomePresenterTest {
5959
sessionStore = InMemorySessionStore(
6060
initialSessionData = aSessionData(
6161
sessionId = matrixClient.sessionId.value,
62+
userDisplayName = null,
63+
userAvatarUrl = null,
6264
),
6365
updateUserProfileResult = updateUserProfileResult,
6466
),
@@ -71,17 +73,26 @@ class HomePresenterTest {
7173
MatrixUser(A_USER_ID, null, null)
7274
)
7375
assertThat(initialState.canReportBug).isFalse()
76+
skipItems(1)
7477
val withUserState = awaitItem()
7578
assertThat(withUserState.matrixUserAndNeighbors.first()).isEqualTo(
7679
MatrixUser(A_USER_ID, A_USER_NAME, AN_AVATAR_URL)
7780
)
7881
assertThat(withUserState.showAvatarIndicator).isFalse()
7982
assertThat(withUserState.isSpaceFeatureEnabled).isFalse()
80-
updateUserProfileResult.assertions().isCalledOnce().with(
81-
value(matrixClient.sessionId),
82-
value(A_USER_NAME),
83-
value(AN_AVATAR_URL),
84-
)
83+
updateUserProfileResult.assertions().isCalledExactly(2)
84+
.withSequence(
85+
listOf(
86+
value(matrixClient.sessionId.value),
87+
value(null),
88+
value(null),
89+
),
90+
listOf(
91+
value(matrixClient.sessionId.value),
92+
value(A_USER_NAME),
93+
value(AN_AVATAR_URL),
94+
),
95+
)
8596
}
8697
}
8798

0 commit comments

Comments
 (0)