mirror of
https://github.com/element-hq/element-android
synced 2024-11-28 21:48:50 +03:00
Improve user color computation and add unit tests
This commit is contained in:
parent
3e4b07cec3
commit
8fc1400bab
4 changed files with 54 additions and 22 deletions
|
@ -20,7 +20,7 @@ import androidx.annotation.ColorRes
|
||||||
import im.vector.riotx.R
|
import im.vector.riotx.R
|
||||||
|
|
||||||
@ColorRes
|
@ColorRes
|
||||||
fun getColorFromRoomId(roomId: String? = null): Int {
|
fun getColorFromRoomId(roomId: String?): Int {
|
||||||
return when ((roomId?.toList()?.sumBy { it.toInt() } ?: 0) % 3) {
|
return when ((roomId?.toList()?.sumBy { it.toInt() } ?: 0) % 3) {
|
||||||
1 -> R.color.riotx_avatar_fill_2
|
1 -> R.color.riotx_avatar_fill_2
|
||||||
2 -> R.color.riotx_avatar_fill_3
|
2 -> R.color.riotx_avatar_fill_3
|
||||||
|
|
|
@ -22,28 +22,18 @@ import kotlin.math.abs
|
||||||
|
|
||||||
@ColorRes
|
@ColorRes
|
||||||
fun getColorFromUserId(userId: String?): Int {
|
fun getColorFromUserId(userId: String?): Int {
|
||||||
if (userId.isNullOrBlank()) {
|
|
||||||
return R.color.riotx_username_1
|
|
||||||
}
|
|
||||||
|
|
||||||
var hash = 0
|
var hash = 0
|
||||||
var i = 0
|
|
||||||
var chr: Char
|
|
||||||
|
|
||||||
while (i < userId.length) {
|
userId?.toList()?.map { chr -> hash = (hash shl 5) - hash + chr.toInt() }
|
||||||
chr = userId[i]
|
|
||||||
hash = (hash shl 5) - hash + chr.toInt()
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
|
|
||||||
return when (abs(hash) % 8 + 1) {
|
return when (abs(hash) % 8) {
|
||||||
1 -> R.color.riotx_username_1
|
1 -> R.color.riotx_username_2
|
||||||
2 -> R.color.riotx_username_2
|
2 -> R.color.riotx_username_3
|
||||||
3 -> R.color.riotx_username_3
|
3 -> R.color.riotx_username_4
|
||||||
4 -> R.color.riotx_username_4
|
4 -> R.color.riotx_username_5
|
||||||
5 -> R.color.riotx_username_5
|
5 -> R.color.riotx_username_6
|
||||||
6 -> R.color.riotx_username_6
|
6 -> R.color.riotx_username_7
|
||||||
7 -> R.color.riotx_username_7
|
7 -> R.color.riotx_username_8
|
||||||
else -> R.color.riotx_username_8
|
else -> R.color.riotx_username_1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,7 @@ class MessageInformationDataFactory @Inject constructor(private val session: Ses
|
||||||
val avatarUrl = event.senderAvatar
|
val avatarUrl = event.senderAvatar
|
||||||
val memberName = event.getDisambiguatedDisplayName()
|
val memberName = event.getDisambiguatedDisplayName()
|
||||||
val formattedMemberName = span(memberName) {
|
val formattedMemberName = span(memberName) {
|
||||||
textColor = colorProvider.getColor(getColorFromUserId(event.root.senderId ?: ""))
|
textColor = colorProvider.getColor(getColorFromUserId(event.root.senderId))
|
||||||
}
|
}
|
||||||
|
|
||||||
return MessageInformationData(
|
return MessageInformationData(
|
||||||
|
|
|
@ -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"))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue