From c42b42cb6107c9b78c36a17f1ef1be46acbaff1e Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 3 Mar 2021 12:13:08 +0100 Subject: [PATCH] Remove string usage from SDK - step 1 - Role Lint will also be able to detect more possible errors with this change --- .../sdk/api/session/room/powerlevels/Role.kt | 13 +++---- .../timeline/format/NoticeEventFormatter.kt | 6 ++-- .../roomprofile/permissions/RoleFormatter.kt | 35 +++++++++++++++++++ .../permissions/RoomPermissionsController.kt | 12 ++----- 4 files changed, 46 insertions(+), 20 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/features/roomprofile/permissions/RoleFormatter.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/powerlevels/Role.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/powerlevels/Role.kt index ecfddad0a4..5fe9bf6993 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/powerlevels/Role.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/powerlevels/Role.kt @@ -17,14 +17,11 @@ package org.matrix.android.sdk.api.session.room.powerlevels -import androidx.annotation.StringRes -import org.matrix.android.sdk.R - -sealed class Role(open val value: Int, @StringRes val res: Int) : Comparable { - object Admin : Role(100, R.string.power_level_admin) - object Moderator : Role(50, R.string.power_level_moderator) - object Default : Role(0, R.string.power_level_default) - data class Custom(override val value: Int) : Role(value, R.string.power_level_custom) +sealed class Role(open val value: Int) : Comparable { + object Admin : Role(100) + object Moderator : Role(50) + object Default : Role(0) + data class Custom(override val value: Int) : Role(value) override fun compareTo(other: Role): Int { return value.compareTo(other.value) diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/format/NoticeEventFormatter.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/format/NoticeEventFormatter.kt index 1c86749abc..878cec0a07 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/format/NoticeEventFormatter.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/format/NoticeEventFormatter.kt @@ -20,6 +20,7 @@ import im.vector.app.ActiveSessionDataSource import im.vector.app.R import im.vector.app.core.resources.StringProvider import im.vector.app.features.home.room.detail.timeline.helper.RoomSummariesHolder +import im.vector.app.features.roomprofile.permissions.RoleFormatter import im.vector.app.features.settings.VectorPreferences import org.matrix.android.sdk.api.extensions.appendNl import org.matrix.android.sdk.api.extensions.orFalse @@ -55,6 +56,7 @@ import javax.inject.Inject class NoticeEventFormatter @Inject constructor( private val activeSessionDataSource: ActiveSessionDataSource, private val roomHistoryVisibilityFormatter: RoomHistoryVisibilityFormatter, + private val roleFormatter: RoleFormatter, private val vectorPreferences: VectorPreferences, private val roomSummariesHolder: RoomSummariesHolder, private val sp: StringProvider @@ -124,8 +126,8 @@ class NoticeEventFormatter @Inject constructor( val from = PowerLevelsHelper(previousPowerLevelsContent).getUserRole(userId) val to = PowerLevelsHelper(powerLevelsContent).getUserRole(userId) if (from != to) { - val fromStr = sp.getString(from.res, from.value) - val toStr = sp.getString(to.res, to.value) + val fromStr = roleFormatter.format(from) + val toStr = roleFormatter.format(to) val diff = sp.getString(R.string.notice_power_level_diff, userId, fromStr, toStr) diffs.add(diff) } diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/permissions/RoleFormatter.kt b/vector/src/main/java/im/vector/app/features/roomprofile/permissions/RoleFormatter.kt new file mode 100644 index 0000000000..699cdd29e2 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/roomprofile/permissions/RoleFormatter.kt @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2021 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 im.vector.app.features.roomprofile.permissions + +import im.vector.app.R +import im.vector.app.core.resources.StringProvider +import org.matrix.android.sdk.api.session.room.powerlevels.Role +import javax.inject.Inject + +class RoleFormatter @Inject constructor( + private val stringProvider: StringProvider +) { + fun format(role: Role): String { + return when (role) { + Role.Admin -> stringProvider.getString(R.string.power_level_admin) + Role.Moderator -> stringProvider.getString(R.string.power_level_moderator) + Role.Default -> stringProvider.getString(R.string.power_level_default) + is Role.Custom -> stringProvider.getString(R.string.power_level_custom, role.value) + } + } +} diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/permissions/RoomPermissionsController.kt b/vector/src/main/java/im/vector/app/features/roomprofile/permissions/RoomPermissionsController.kt index 92bdac7ae0..8f99132bc9 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/permissions/RoomPermissionsController.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/permissions/RoomPermissionsController.kt @@ -32,6 +32,7 @@ import javax.inject.Inject class RoomPermissionsController @Inject constructor( private val stringProvider: StringProvider, + private val roleFormatter: RoleFormatter, colorProvider: ColorProvider ) : TypedEpoxyController() { @@ -124,7 +125,7 @@ class RoomPermissionsController @Inject constructor( buildProfileAction( id = editablePermission.labelResId.toString(), title = stringProvider.getString(editablePermission.labelResId), - subtitle = getSubtitle(currentRole), + subtitle = roleFormatter.format(currentRole), dividerColor = dividerColor, divider = true, editable = editable, @@ -136,15 +137,6 @@ class RoomPermissionsController @Inject constructor( ) } - private fun getSubtitle(currentRole: Role): String { - return when (currentRole) { - Role.Admin, - Role.Moderator, - Role.Default -> stringProvider.getString(currentRole.res) - is Role.Custom -> stringProvider.getString(currentRole.res, currentRole.value) - } - } - private fun getCurrentRole(editablePermission: EditablePermission, content: PowerLevelsContent): Role { val value = when (editablePermission) { is EditablePermission.EventTypeEditablePermission -> content.events[editablePermission.eventType] ?: content.stateDefault