Room : continue improving members loading
This commit is contained in:
parent
f02ee307cc
commit
b0152059ff
26 changed files with 329 additions and 166 deletions
|
|
@ -23,6 +23,7 @@ import io.element.android.libraries.matrix.api.core.UserId
|
|||
import io.element.android.libraries.matrix.api.timeline.MatrixTimeline
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import java.io.Closeable
|
||||
|
||||
interface MatrixRoom : Closeable {
|
||||
|
|
@ -41,10 +42,10 @@ interface MatrixRoom : Closeable {
|
|||
|
||||
/**
|
||||
* The current loaded members as a StateFlow.
|
||||
* Initial value is an emptyList.
|
||||
* Initial value is [MatrixRoomMembersState.Unknown].
|
||||
* To update them you should call [updateMembers].
|
||||
*/
|
||||
val membersFlow: StateFlow<List<RoomMember>>
|
||||
val membersStateFlow: StateFlow<MatrixRoomMembersState>
|
||||
|
||||
/**
|
||||
* Try to load the room members and update the membersFlow.
|
||||
|
|
@ -70,18 +71,21 @@ interface MatrixRoom : Closeable {
|
|||
suspend fun leave(): Result<Unit>
|
||||
}
|
||||
|
||||
fun MatrixRoom.getMember(userId: UserId): RoomMember? {
|
||||
return membersFlow.value.find { it.userId == userId }
|
||||
}
|
||||
|
||||
fun MatrixRoom.getDmMember(): RoomMember? {
|
||||
return if (membersFlow.value.size == 2 && isDirect && isEncrypted) {
|
||||
membersFlow.value.find { it.userId != this.sessionId }
|
||||
} else {
|
||||
null
|
||||
fun MatrixRoom.getMemberFlow(userId: UserId): Flow<RoomMember?> {
|
||||
return membersStateFlow.map { state ->
|
||||
state.roomMembers().find {
|
||||
it.userId == userId
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun MatrixRoom.memberCount(): Int {
|
||||
return membersFlow.value.size
|
||||
fun MatrixRoom.getDmMemberFlow(): Flow<RoomMember?> {
|
||||
return membersStateFlow.map { state ->
|
||||
val members = state.roomMembers()
|
||||
if (members.size == 2 && isDirect && isEncrypted) {
|
||||
members.find { it.userId != this.sessionId }
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 2023 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.matrix.api.room
|
||||
|
||||
sealed interface MatrixRoomMembersState {
|
||||
object Unknown : MatrixRoomMembersState
|
||||
object Pending : MatrixRoomMembersState
|
||||
data class Error(val failure: Throwable) : MatrixRoomMembersState
|
||||
data class Ready(val roomMembers: List<RoomMember>) : MatrixRoomMembersState
|
||||
}
|
||||
|
||||
fun MatrixRoomMembersState.roomMembers(): List<RoomMember> {
|
||||
return when (this) {
|
||||
is MatrixRoomMembersState.Ready -> roomMembers
|
||||
else -> emptyList()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue