Improve user color computation and add unit tests

This commit is contained in:
Benoit Marty 2019-12-14 10:38:50 +01:00
parent 3e4b07cec3
commit 8fc1400bab
4 changed files with 54 additions and 22 deletions

View File

@ -20,7 +20,7 @@ import androidx.annotation.ColorRes
import im.vector.riotx.R
@ColorRes
fun getColorFromRoomId(roomId: String? = null): Int {
fun getColorFromRoomId(roomId: String?): Int {
return when ((roomId?.toList()?.sumBy { it.toInt() } ?: 0) % 3) {
1 -> R.color.riotx_avatar_fill_2
2 -> R.color.riotx_avatar_fill_3

View File

@ -22,28 +22,18 @@ import kotlin.math.abs
@ColorRes
fun getColorFromUserId(userId: String?): Int {
if (userId.isNullOrBlank()) {
return R.color.riotx_username_1
}
var hash = 0
var i = 0
var chr: Char
while (i < userId.length) {
chr = userId[i]
hash = (hash shl 5) - hash + chr.toInt()
i++
}
userId?.toList()?.map { chr -> hash = (hash shl 5) - hash + chr.toInt() }
return when (abs(hash) % 8 + 1) {
1 -> R.color.riotx_username_1
2 -> R.color.riotx_username_2
3 -> R.color.riotx_username_3
4 -> R.color.riotx_username_4
5 -> R.color.riotx_username_5
6 -> R.color.riotx_username_6
7 -> R.color.riotx_username_7
else -> R.color.riotx_username_8
return when (abs(hash) % 8) {
1 -> R.color.riotx_username_2
2 -> R.color.riotx_username_3
3 -> R.color.riotx_username_4
4 -> R.color.riotx_username_5
5 -> R.color.riotx_username_6
6 -> R.color.riotx_username_7
7 -> R.color.riotx_username_8
else -> R.color.riotx_username_1
}
}

View File

@ -60,7 +60,7 @@ class MessageInformationDataFactory @Inject constructor(private val session: Ses
val avatarUrl = event.senderAvatar
val memberName = event.getDisambiguatedDisplayName()
val formattedMemberName = span(memberName) {
textColor = colorProvider.getColor(getColorFromUserId(event.root.senderId ?: ""))
textColor = colorProvider.getColor(getColorFromUserId(event.root.senderId))
}
return MessageInformationData(

View File

@ -0,0 +1,42 @@
/*
* Copyright 2019 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.riotx.features.home
import im.vector.riotx.R
import org.junit.Assert.assertEquals
import org.junit.Test
class UserColorTest {
@Test
fun testNull() {
assertEquals(R.color.riotx_username_1, getColorFromUserId(null))
}
@Test
fun testEmpty() {
assertEquals(R.color.riotx_username_1, getColorFromUserId(""))
}
@Test
fun testName() {
assertEquals(R.color.riotx_username_1, getColorFromUserId("@ganfra:matrix.org"))
assertEquals(R.color.riotx_username_4, getColorFromUserId("@benoit0816:matrix.org"))
assertEquals(R.color.riotx_username_5, getColorFromUserId("@hubert:uhoreg.ca"))
assertEquals(R.color.riotx_username_7, getColorFromUserId("@nadonomy:matrix.org"))
}
}