-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Room member details presenter improvement #2721
Merged
bmarty
merged 8 commits into
develop
from
feature/bma/roomMemberDetailsPresenterImprovement
Apr 17, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f2ff326
Observe ignoredUsersFlow to have live data about blocked user.
bmarty c9d5961
RoomMemberDetailsPresenter: fallback to user profile data if the user…
bmarty 0dba614
createRoomMemberDetailsPresenter just need a UserId.
bmarty 4d50e43
Add test covering fallback to user profile.
bmarty 47e1d6f
Changelog
bmarty 3f0159d
Format
bmarty f9d3f94
Add missing test for unblock user with error.
bmarty 5b3a2d8
Use produceState and fetch profile only if necessary.
bmarty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
RoomMember screen: fallback to userProfile data, if the member is not a user of the room. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,8 +37,13 @@ import io.element.android.libraries.matrix.api.MatrixClient | |
import io.element.android.libraries.matrix.api.core.RoomId | ||
import io.element.android.libraries.matrix.api.core.UserId | ||
import io.element.android.libraries.matrix.api.room.MatrixRoom | ||
import io.element.android.libraries.matrix.api.user.MatrixUser | ||
import io.element.android.libraries.matrix.ui.room.getRoomMemberAsState | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.launch | ||
|
||
class RoomMemberDetailsPresenter @AssistedInject constructor( | ||
|
@@ -56,20 +61,24 @@ class RoomMemberDetailsPresenter @AssistedInject constructor( | |
val coroutineScope = rememberCoroutineScope() | ||
var confirmationDialog by remember { mutableStateOf<ConfirmationDialog?>(null) } | ||
val roomMember by room.getRoomMemberAsState(roomMemberId) | ||
var userProfile by remember { mutableStateOf<MatrixUser?>(null) } | ||
val startDmActionState: MutableState<AsyncAction<RoomId>> = remember { mutableStateOf(AsyncAction.Uninitialized) } | ||
// the room member is not really live... | ||
val isBlocked: MutableState<AsyncData<Boolean>> = remember(roomMember) { | ||
val isIgnored = roomMember?.isIgnored | ||
if (isIgnored == null) { | ||
mutableStateOf(AsyncData.Uninitialized) | ||
} else { | ||
mutableStateOf(AsyncData.Success(isIgnored)) | ||
} | ||
val isBlocked: MutableState<AsyncData<Boolean>> = remember { mutableStateOf(AsyncData.Uninitialized) } | ||
LaunchedEffect(Unit) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd use |
||
client.ignoredUsersFlow | ||
.map { ignoredUsers -> roomMemberId in ignoredUsers } | ||
.distinctUntilChanged() | ||
.onEach { isBlocked.value = AsyncData.Success(it) } | ||
.launchIn(this) | ||
} | ||
LaunchedEffect(Unit) { | ||
// Update room member info when opening this screen | ||
// We don't need to assign the result as it will be automatically propagated by `room.getRoomMemberAsState` | ||
room.getUpdatedMember(roomMemberId) | ||
.onFailure { | ||
// Not a member of the room, try to get the user profile | ||
userProfile = client.getProfile(roomMemberId).getOrNull() | ||
} | ||
} | ||
|
||
fun handleEvents(event: RoomMemberDetailsEvents) { | ||
|
@@ -105,16 +114,34 @@ class RoomMemberDetailsPresenter @AssistedInject constructor( | |
} | ||
} | ||
|
||
val userName by produceState(initialValue = roomMember?.displayName) { | ||
room.userDisplayName(roomMemberId).onSuccess { displayName -> | ||
if (displayName != null) value = displayName | ||
} | ||
val userName: String? by produceState( | ||
initialValue = roomMember?.displayName ?: userProfile?.displayName, | ||
key1 = roomMember, | ||
key2 = userProfile, | ||
) { | ||
value = room.userDisplayName(roomMemberId) | ||
.fold( | ||
onSuccess = { it }, | ||
onFailure = { | ||
// Fallback to user profile | ||
userProfile?.displayName | ||
} | ||
) | ||
} | ||
|
||
val userAvatar by produceState(initialValue = roomMember?.avatarUrl) { | ||
room.userAvatarUrl(roomMemberId).onSuccess { avatarUrl -> | ||
if (avatarUrl != null) value = avatarUrl | ||
} | ||
val userAvatar: String? by produceState( | ||
initialValue = roomMember?.avatarUrl ?: userProfile?.avatarUrl, | ||
key1 = roomMember, | ||
key2 = userProfile, | ||
) { | ||
value = room.userAvatarUrl(roomMemberId) | ||
.fold( | ||
onSuccess = { it }, | ||
onFailure = { | ||
// Fallback to user profile | ||
userProfile?.avatarUrl | ||
} | ||
) | ||
} | ||
|
||
return RoomMemberDetailsState( | ||
|
@@ -124,36 +151,26 @@ class RoomMemberDetailsPresenter @AssistedInject constructor( | |
isBlocked = isBlocked.value, | ||
startDmActionState = startDmActionState.value, | ||
displayConfirmationDialog = confirmationDialog, | ||
isCurrentUser = client.isMe(roomMember?.userId), | ||
isCurrentUser = client.isMe(roomMemberId), | ||
eventSink = ::handleEvents | ||
) | ||
} | ||
|
||
private fun CoroutineScope.blockUser(userId: UserId, isBlockedState: MutableState<AsyncData<Boolean>>) = launch { | ||
isBlockedState.value = AsyncData.Loading(false) | ||
client.ignoreUser(userId) | ||
.fold( | ||
onSuccess = { | ||
isBlockedState.value = AsyncData.Success(true) | ||
room.getUpdatedMember(userId) | ||
}, | ||
onFailure = { | ||
isBlockedState.value = AsyncData.Failure(it, false) | ||
} | ||
) | ||
.onFailure { | ||
isBlockedState.value = AsyncData.Failure(it, false) | ||
} | ||
// Note: on success, ignoredUserList will be updated. | ||
} | ||
|
||
private fun CoroutineScope.unblockUser(userId: UserId, isBlockedState: MutableState<AsyncData<Boolean>>) = launch { | ||
isBlockedState.value = AsyncData.Loading(true) | ||
client.unignoreUser(userId) | ||
.fold( | ||
onSuccess = { | ||
isBlockedState.value = AsyncData.Success(false) | ||
room.getUpdatedMember(userId) | ||
}, | ||
onFailure = { | ||
isBlockedState.value = AsyncData.Failure(it, true) | ||
} | ||
) | ||
.onFailure { | ||
isBlockedState.value = AsyncData.Failure(it, true) | ||
} | ||
// Note: on success, ignoredUserList will be updated. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
produceByState
: