Prefer immutable type

This commit is contained in:
Benoit Marty 2021-01-06 12:24:47 +01:00
parent 7249c7d25a
commit 22c10f5ada
3 changed files with 24 additions and 16 deletions

View File

@ -30,23 +30,27 @@ data class PowerLevelsContent(
@Json(name = "invite") val invite: Int = Role.Moderator.value, @Json(name = "invite") val invite: Int = Role.Moderator.value,
@Json(name = "redact") val redact: Int = Role.Moderator.value, @Json(name = "redact") val redact: Int = Role.Moderator.value,
@Json(name = "events_default") val eventsDefault: Int = Role.Default.value, @Json(name = "events_default") val eventsDefault: Int = Role.Default.value,
@Json(name = "events") val events: MutableMap<String, Int> = HashMap(), @Json(name = "events") val events: Map<String, Int> = emptyMap(),
@Json(name = "users_default") val usersDefault: Int = Role.Default.value, @Json(name = "users_default") val usersDefault: Int = Role.Default.value,
@Json(name = "users") val users: MutableMap<String, Int> = HashMap(), @Json(name = "users") val users: Map<String, Int> = emptyMap(),
@Json(name = "state_default") val stateDefault: Int = Role.Moderator.value, @Json(name = "state_default") val stateDefault: Int = Role.Moderator.value,
@Json(name = "notifications") val notifications: Map<String, Any> = HashMap() @Json(name = "notifications") val notifications: Map<String, Any> = emptyMap()
) { ) {
/** /**
* Alter this content with a new power level for the specified user * Return a copy of this content with a new power level for the specified user
* *
* @param userId the userId to alter the power level of * @param userId the userId to alter the power level of
* @param powerLevel the new power level, or null to set the default value. * @param powerLevel the new power level, or null to set the default value.
*/ */
fun setUserPowerLevel(userId: String, powerLevel: Int?) { fun setUserPowerLevel(userId: String, powerLevel: Int?): PowerLevelsContent {
if (powerLevel == null || powerLevel == usersDefault) { return copy(
users.remove(userId) users = users.toMutableMap().apply {
} else { if (powerLevel == null || powerLevel == usersDefault) {
users[userId] = powerLevel remove(userId)
} } else {
put(userId, powerLevel)
}
}
)
} }
} }

View File

@ -887,13 +887,15 @@ class RoomDetailViewModel @AssistedInject constructor(
} }
private fun handleSetUserPowerLevel(setUserPowerLevel: ParsedCommand.SetUserPowerLevel) { private fun handleSetUserPowerLevel(setUserPowerLevel: ParsedCommand.SetUserPowerLevel) {
val currentPowerLevelsContent = room.getStateEvent(EventType.STATE_ROOM_POWER_LEVELS) val newPowerLevelsContent = room.getStateEvent(EventType.STATE_ROOM_POWER_LEVELS)
?.content ?.content
?.toModel<PowerLevelsContent>() ?: return ?.toModel<PowerLevelsContent>()
?.setUserPowerLevel(setUserPowerLevel.userId, setUserPowerLevel.powerLevel)
?.toContent()
?: return
launchSlashCommandFlowSuspendable { launchSlashCommandFlowSuspendable {
currentPowerLevelsContent.setUserPowerLevel(setUserPowerLevel.userId, setUserPowerLevel.powerLevel) room.sendStateEvent(EventType.STATE_ROOM_POWER_LEVELS, null, newPowerLevelsContent)
room.sendStateEvent(EventType.STATE_ROOM_POWER_LEVELS, null, currentPowerLevelsContent.toContent())
} }
} }

View File

@ -162,11 +162,13 @@ class RoomMemberProfileViewModel @AssistedInject constructor(@Assisted private v
} else if (action.askForValidation && state.isMine) { } else if (action.askForValidation && state.isMine) {
_viewEvents.post(RoomMemberProfileViewEvents.ShowPowerLevelDemoteWarning(action.previousValue, action.newValue)) _viewEvents.post(RoomMemberProfileViewEvents.ShowPowerLevelDemoteWarning(action.previousValue, action.newValue))
} else { } else {
currentPowerLevelsContent.setUserPowerLevel(state.userId, action.newValue) val newPowerLevelsContent = currentPowerLevelsContent
.setUserPowerLevel(state.userId, action.newValue)
.toContent()
viewModelScope.launch { viewModelScope.launch {
_viewEvents.post(RoomMemberProfileViewEvents.Loading()) _viewEvents.post(RoomMemberProfileViewEvents.Loading())
try { try {
room.sendStateEvent(EventType.STATE_ROOM_POWER_LEVELS, null, currentPowerLevelsContent.toContent()) room.sendStateEvent(EventType.STATE_ROOM_POWER_LEVELS, null, newPowerLevelsContent)
_viewEvents.post(RoomMemberProfileViewEvents.OnSetPowerLevelSuccess) _viewEvents.post(RoomMemberProfileViewEvents.OnSetPowerLevelSuccess)
} catch (failure: Throwable) { } catch (failure: Throwable) {
_viewEvents.post(RoomMemberProfileViewEvents.Failure(failure)) _viewEvents.post(RoomMemberProfileViewEvents.Failure(failure))