@@ -47,16 +47,17 @@ import io.element.android.libraries.architecture.BaseFlowNode
47
47
import io.element.android.libraries.architecture.NodeInputs
48
48
import io.element.android.libraries.architecture.createNode
49
49
import io.element.android.libraries.architecture.inputs
50
+ import io.element.android.libraries.core.bool.orFalse
50
51
import io.element.android.libraries.di.SessionScope
51
52
import io.element.android.libraries.matrix.api.MatrixClient
52
53
import io.element.android.libraries.matrix.api.core.RoomAlias
53
54
import io.element.android.libraries.matrix.api.core.RoomId
54
55
import io.element.android.libraries.matrix.api.core.RoomIdOrAlias
55
56
import io.element.android.libraries.matrix.api.room.CurrentUserMembership
56
- import io.element.android.libraries.matrix.api.room.RoomMembershipObserver
57
- import kotlinx.coroutines.flow.filter
57
+ import kotlinx.coroutines.flow.combine
58
+ import kotlinx.coroutines.flow.distinctUntilChanged
58
59
import kotlinx.coroutines.flow.launchIn
59
- import kotlinx.coroutines.flow.onEach
60
+ import kotlinx.coroutines.flow.map
60
61
import kotlinx.coroutines.launch
61
62
import kotlinx.parcelize.Parcelize
62
63
import timber.log.Timber
@@ -68,7 +69,6 @@ class RoomFlowNode @AssistedInject constructor(
68
69
@Assisted val buildContext : BuildContext ,
69
70
@Assisted plugins : List <Plugin >,
70
71
private val client : MatrixClient ,
71
- private val roomMembershipObserver : RoomMembershipObserver ,
72
72
private val joinRoomEntryPoint : JoinRoomEntryPoint ,
73
73
private val roomAliasResolverEntryPoint : RoomAliasResolverEntryPoint ,
74
74
private val networkMonitor : NetworkMonitor ,
@@ -121,14 +121,17 @@ class RoomFlowNode @AssistedInject constructor(
121
121
}
122
122
123
123
private fun subscribeToRoomInfoFlow (roomId : RoomId ) {
124
- client.getRoomInfoFlow(
124
+ val roomInfoFlow = client.getRoomInfoFlow(
125
125
roomId = roomId
126
- )
127
- .onEach { roomInfo ->
128
- Timber .d(" Room membership: ${roomInfo.map { it.currentUserMembership }} " )
129
- val info = roomInfo.getOrNull()
130
- if (info?.currentUserMembership == CurrentUserMembership .JOINED ) {
131
- if (info.isSpace) {
126
+ ).map { it.getOrNull() }
127
+
128
+ val isSpaceFlow = roomInfoFlow.map { it?.isSpace.orFalse() }.distinctUntilChanged()
129
+ val currentMembershipFlow = roomInfoFlow.map { it?.currentUserMembership }.distinctUntilChanged()
130
+ combine(currentMembershipFlow, isSpaceFlow) { membership, isSpace ->
131
+ Timber .d(" Room membership: $membership " )
132
+ when (membership) {
133
+ CurrentUserMembership .JOINED -> {
134
+ if (isSpace) {
132
135
// It should not happen, but probably due to an issue in the sliding sync,
133
136
// we can have a space here in case the space has just been joined.
134
137
// So navigate to the JoinRoom target for now, which will
@@ -137,19 +140,17 @@ class RoomFlowNode @AssistedInject constructor(
137
140
} else {
138
141
backstack.newRoot(NavTarget .JoinedRoom (roomId))
139
142
}
140
- } else {
143
+ }
144
+ CurrentUserMembership .LEFT -> {
145
+ // Left the room, navigate out of this flow
146
+ navigateUp()
147
+ }
148
+ else -> {
149
+ // Was invited or the room is not known, display the join room screen
141
150
backstack.newRoot(NavTarget .JoinRoom (roomId))
142
151
}
143
152
}
144
- .launchIn(lifecycleScope)
145
-
146
- // When leaving the room from this session only, navigate up.
147
- roomMembershipObserver.updates
148
- .filter { update -> update.roomId == roomId && ! update.isUserInRoom }
149
- .onEach {
150
- navigateUp()
151
- }
152
- .launchIn(lifecycleScope)
153
+ }.launchIn(lifecycleScope)
153
154
}
154
155
155
156
override fun resolve (navTarget : NavTarget , buildContext : BuildContext ): Node {
0 commit comments