From 114fc708202d1785eb9fb906bdced2dcea02540e Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Sat, 20 Nov 2021 10:18:00 +0100 Subject: [PATCH] Sort room members strictly by power level, instead of "custom" category Change-Id: I047aace63a0cd475ace85748b3b37326a599f8e6 --- .../members/RoomMemberListController.kt | 18 ++++++++----- .../members/RoomMemberListViewModel.kt | 26 +++++++++++++++++-- .../members/RoomMemberListViewState.kt | 13 ++++++++-- .../RoomMemberSummaryWithPowerComparator.kt | 14 ++++++++++ .../people/SpacePeopleListController.kt | 7 ++--- 5 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberSummaryWithPowerComparator.kt diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberListController.kt b/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberListController.kt index 1b58130d85..b826f4994f 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberListController.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberListController.kt @@ -69,13 +69,15 @@ class RoomMemberListController @Inject constructor( } .orEmpty() var threePidInvitesDone = filteredThreePidInvites.isEmpty() + var isFirstSection = true for ((powerLevelCategory, roomMemberList) in roomMembersByPowerLevel) { - val filteredRoomMemberList = roomMemberList.filter { roomMemberSummaryFilter.test(it) } + val filteredRoomMemberList = roomMemberList.filter { roomMemberSummaryFilter.test(it.roomMemberSummary) } if (filteredRoomMemberList.isEmpty()) { continue } + /* if (powerLevelCategory == RoomMemberListCategories.USER && !threePidInvitesDone) { // If there is no regular invite, display threepid invite before the regular user buildProfileSection( @@ -85,18 +87,22 @@ class RoomMemberListController @Inject constructor( buildThreePidInvites(filteredThreePidInvites, data.actionsPermissions.canRevokeThreePidInvite) threePidInvitesDone = true } + */ - buildProfileSection( - stringProvider.getString(powerLevelCategory.titleRes) - ) + if (powerLevelCategory != RoomMemberListCategories.MEMBER || !isFirstSection) { + buildProfileSection( + stringProvider.getString(powerLevelCategory.titleRes) + ) + } + isFirstSection = false filteredRoomMemberList.join( each = { _, roomMember -> - buildRoomMember(roomMember, powerLevelCategory, host, data) + buildRoomMember(roomMember.roomMemberSummary, roomMember.powerLevelCategory, host, data) }, between = { _, roomMemberBefore -> dividerItem { - id("divider_${roomMemberBefore.userId}") + id("divider_${roomMemberBefore.roomMemberSummary.userId}") } } ) diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberListViewModel.kt b/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberListViewModel.kt index 0bbdd87f3e..00904632e5 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberListViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberListViewModel.kt @@ -52,7 +52,8 @@ import org.matrix.android.sdk.flow.unwrap import timber.log.Timber class RoomMemberListViewModel @AssistedInject constructor(@Assisted initialState: RoomMemberListViewState, - private val roomMemberSummaryComparator: RoomMemberSummaryComparator, + //private val roomMemberSummaryComparator: RoomMemberSummaryComparator, + private val roomMemberSummaryWithPowerComparator: RoomMemberSummaryWithPowerComparator, private val session: Session) : VectorViewModel(initialState) { @@ -149,7 +150,11 @@ class RoomMemberListViewModel @AssistedInject constructor(@Assisted initialState } } - private fun buildRoomMemberSummaries(powerLevelsContent: PowerLevelsContent, roomMembers: List): RoomMemberSummaries { + private fun List.toPoweredMemberList(listCategory: RoomMemberListCategories, powerLevelsHelper: PowerLevelsHelper): List { + return map { RoomMemberSummaryWithPower(it, listCategory, powerLevelsHelper.getUserPowerLevelValue(it.userId)) } + } + + private fun buildRoomMemberSummaries(powerLevelsContent: PowerLevelsContent, roomMembers: List): RoomMemberSummariesWithPower { val admins = ArrayList() val moderators = ArrayList() val users = ArrayList(roomMembers.size) @@ -168,6 +173,7 @@ class RoomMemberListViewModel @AssistedInject constructor(@Assisted initialState } } + /* return listOf( RoomMemberListCategories.ADMIN to admins.sortedWith(roomMemberSummaryComparator), RoomMemberListCategories.MODERATOR to moderators.sortedWith(roomMemberSummaryComparator), @@ -175,6 +181,16 @@ class RoomMemberListViewModel @AssistedInject constructor(@Assisted initialState RoomMemberListCategories.INVITE to invites.sortedWith(roomMemberSummaryComparator), RoomMemberListCategories.USER to users.sortedWith(roomMemberSummaryComparator) ) + */ + val invitesWithPower = invites.toPoweredMemberList(RoomMemberListCategories.INVITE, powerLevelsHelper) + val membersWithPower = admins.toPoweredMemberList(RoomMemberListCategories.SG_ADMIN, powerLevelsHelper) + + moderators.toPoweredMemberList(RoomMemberListCategories.SG_MODERATOR, powerLevelsHelper) + + users.toPoweredMemberList(RoomMemberListCategories.SG_USER, powerLevelsHelper) + + customs.toPoweredMemberList(RoomMemberListCategories.SG_CUSTOM, powerLevelsHelper) + return listOf( + RoomMemberListCategories.INVITE to invitesWithPower.sortedWith(roomMemberSummaryWithPowerComparator), + RoomMemberListCategories.MEMBER to membersWithPower.sortedWith(roomMemberSummaryWithPowerComparator) + ) } override fun handle(action: RoomMemberListAction) { @@ -201,4 +217,10 @@ class RoomMemberListViewModel @AssistedInject constructor(@Assisted initialState ) } } + + data class RoomMemberSummaryWithPower constructor( + val roomMemberSummary: RoomMemberSummary, + val powerLevelCategory: RoomMemberListCategories, + val powerLevel: Int + ) } diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberListViewState.kt b/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberListViewState.kt index d736260f10..9286697496 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberListViewState.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberListViewState.kt @@ -31,7 +31,7 @@ import org.matrix.android.sdk.api.session.room.model.RoomSummary data class RoomMemberListViewState( val roomId: String, val roomSummary: Async = Uninitialized, - val roomMemberSummaries: Async = Uninitialized, + val roomMemberSummaries: Async = Uninitialized, val filter: String = "", val threePidInvites: Async> = Uninitialized, val trustLevelMap: Async> = Uninitialized, @@ -49,11 +49,20 @@ data class ActionPermissions( ) typealias RoomMemberSummaries = List>> +typealias RoomMemberSummariesWithPower = List>> enum class RoomMemberListCategories(@StringRes val titleRes: Int) { ADMIN(R.string.room_member_power_level_admins), MODERATOR(R.string.room_member_power_level_moderators), CUSTOM(R.string.room_member_power_level_custom), INVITE(R.string.room_member_power_level_invites), - USER(R.string.room_member_power_level_users) + USER(R.string.room_member_power_level_users), + + // Singular variants + SG_ADMIN(R.string.power_level_admin), + SG_MODERATOR(R.string.power_level_moderator), + SG_CUSTOM(R.string.power_level_custom_no_value), + SG_USER(R.string.power_level_default), + // Header for unified members + MEMBER(R.string.room_member_power_level_users) } diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberSummaryWithPowerComparator.kt b/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberSummaryWithPowerComparator.kt new file mode 100644 index 0000000000..64932ca1f4 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberSummaryWithPowerComparator.kt @@ -0,0 +1,14 @@ +package im.vector.app.features.roomprofile.members + +import javax.inject.Inject + +class RoomMemberSummaryWithPowerComparator @Inject constructor(private val roomMemberSummaryComparator: RoomMemberSummaryComparator) : Comparator { + + override fun compare(leftRoomMemberSummary: RoomMemberListViewModel.RoomMemberSummaryWithPower?, rightRoomMemberSummary: RoomMemberListViewModel.RoomMemberSummaryWithPower?): Int { + return if (leftRoomMemberSummary == null || rightRoomMemberSummary == null || leftRoomMemberSummary.powerLevel == rightRoomMemberSummary.powerLevel) { + roomMemberSummaryComparator.compare(leftRoomMemberSummary?.roomMemberSummary, rightRoomMemberSummary?.roomMemberSummary) + } else { + rightRoomMemberSummary.powerLevel - leftRoomMemberSummary.powerLevel + } + } +} diff --git a/vector/src/main/java/im/vector/app/features/spaces/people/SpacePeopleListController.kt b/vector/src/main/java/im/vector/app/features/spaces/people/SpacePeopleListController.kt index ca1c4564f5..6b919720dc 100644 --- a/vector/src/main/java/im/vector/app/features/spaces/people/SpacePeopleListController.kt +++ b/vector/src/main/java/im/vector/app/features/spaces/people/SpacePeopleListController.kt @@ -66,7 +66,7 @@ class SpacePeopleListController @Inject constructor( memberSummaries.forEach { memberEntry -> val filtered = memberEntry.second - .filter { roomMemberSummaryFilter.test(it) } + .filter { roomMemberSummaryFilter.test(it.roomMemberSummary) } if (filtered.isNotEmpty()) { dividerItem { id("divider_type_${memberEntry.first.titleRes}") @@ -75,7 +75,8 @@ class SpacePeopleListController @Inject constructor( foundCount += filtered.size filtered .join( - each = { _, roomMember -> + each = { _, roomMemberWrapper -> + val roomMember = roomMemberWrapper.roomMemberSummary profileMatrixItemWithPowerLevel { id(roomMember.userId) matrixItem(roomMember.toMatrixItem()) @@ -118,7 +119,7 @@ class SpacePeopleListController @Inject constructor( }, between = { _, roomMemberBefore -> dividerItem { - id("divider_${roomMemberBefore.userId}") + id("divider_${roomMemberBefore.roomMemberSummary.userId}") } } )