Remove string usage from SDK - step 1 - Role

Lint will also be able to detect more possible errors with this change
This commit is contained in:
Benoit Marty 2021-03-03 12:13:08 +01:00
parent 898c2d514c
commit c42b42cb61
4 changed files with 46 additions and 20 deletions

View file

@ -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<Role> {
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<Role> {
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)

View file

@ -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)
}

View file

@ -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)
}
}
}

View file

@ -32,6 +32,7 @@ import javax.inject.Inject
class RoomPermissionsController @Inject constructor(
private val stringProvider: StringProvider,
private val roleFormatter: RoleFormatter,
colorProvider: ColorProvider
) : TypedEpoxyController<RoomPermissionsViewState>() {
@ -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