Update dependency io.element.android:compound-android to v0.1.0 (#3524)
* Update dependency io.element.android:compound-android to v0.1.0 * Fix usages of `AvatarColorsProvider` * Update screenshots * Fix tests and lint issues --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Jorge Martín <jorgem@element.io> Co-authored-by: ElementBot <android@element.io>
This commit is contained in:
parent
c5f5ff356b
commit
231217c438
814 changed files with 1631 additions and 1659 deletions
|
|
@ -7,41 +7,19 @@
|
|||
|
||||
package io.element.android.libraries.designsystem.colors
|
||||
|
||||
import androidx.collection.LruCache
|
||||
import androidx.compose.runtime.Composable
|
||||
import io.element.android.compound.theme.AvatarColors
|
||||
import io.element.android.compound.theme.avatarColorsDark
|
||||
import io.element.android.compound.theme.avatarColorsLight
|
||||
import io.element.android.compound.theme.avatarColors
|
||||
|
||||
object AvatarColorsProvider {
|
||||
private val cache = LruCache<String, AvatarColors>(200)
|
||||
private var currentThemeIsLight = true
|
||||
|
||||
fun provide(id: String, isLightTheme: Boolean): AvatarColors {
|
||||
if (currentThemeIsLight != isLightTheme) {
|
||||
currentThemeIsLight = isLightTheme
|
||||
cache.evictAll()
|
||||
@Composable
|
||||
fun provide(id: String): AvatarColors {
|
||||
return avatarColors().let { colors ->
|
||||
colors[id.toHash(colors.size)]
|
||||
}
|
||||
val valueFromCache = cache.get(id)
|
||||
return if (valueFromCache != null) {
|
||||
valueFromCache
|
||||
} else {
|
||||
val colors = avatarColors(id, isLightTheme)
|
||||
cache.put(id, colors)
|
||||
colors
|
||||
}
|
||||
}
|
||||
|
||||
private fun avatarColors(id: String, isLightTheme: Boolean): AvatarColors {
|
||||
val hash = id.toHash()
|
||||
val colors = if (isLightTheme) {
|
||||
avatarColorsLight[hash]
|
||||
} else {
|
||||
avatarColorsDark[hash]
|
||||
}
|
||||
return colors
|
||||
}
|
||||
}
|
||||
|
||||
internal fun String.toHash(): Int {
|
||||
return toList().sumOf { it.code } % avatarColorsLight.size
|
||||
internal fun String.toHash(maxSize: Int): Int {
|
||||
return toList().sumOf { it.code } % maxSize
|
||||
}
|
||||
|
|
|
|||
|
|
@ -364,7 +364,7 @@ fun Modifier.avatarBloom(
|
|||
)
|
||||
} else {
|
||||
// There is no URL so we'll generate an avatar with the initials and use that as the bloom source
|
||||
val avatarColors = AvatarColorsProvider.provide(avatarData.id, ElementTheme.isLightTheme)
|
||||
val avatarColors = AvatarColorsProvider.provide(avatarData.id)
|
||||
val initialsBitmap = initialsBitmap(
|
||||
width = BloomDefaults.ENCODE_SIZE_PX.toDp(),
|
||||
height = BloomDefaults.ENCODE_SIZE_PX.toDp(),
|
||||
|
|
@ -538,7 +538,7 @@ class InitialsColorIntProvider : PreviewParameterProvider<Int> {
|
|||
@ShowkaseComposable(group = PreviewGroup.Bloom)
|
||||
internal fun BloomInitialsPreview(@PreviewParameter(InitialsColorIntProvider::class) color: Int) {
|
||||
ElementPreview {
|
||||
val avatarColors = AvatarColorsProvider.provide("$color", ElementTheme.isLightTheme)
|
||||
val avatarColors = AvatarColorsProvider.provide("$color")
|
||||
val bitmap = initialsBitmap(text = "F", backgroundColor = avatarColors.background, textColor = avatarColors.foreground)
|
||||
val hash = BlurHash.encode(
|
||||
bitmap = bitmap.asAndroidBitmap(),
|
||||
|
|
|
|||
|
|
@ -115,7 +115,7 @@ private fun InitialsAvatar(
|
|||
forcedAvatarSize: Dp?,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
val avatarColors = AvatarColorsProvider.provide(avatarData.id, ElementTheme.isLightTheme)
|
||||
val avatarColors = AvatarColorsProvider.provide(avatarData.id)
|
||||
Box(
|
||||
modifier.background(color = avatarColors.background)
|
||||
) {
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ import androidx.compose.runtime.Composable
|
|||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import io.element.android.compound.theme.avatarColorsLight
|
||||
import io.element.android.compound.theme.avatarColors
|
||||
import io.element.android.libraries.designsystem.preview.ElementPreview
|
||||
import io.element.android.libraries.designsystem.preview.PreviewsDayNight
|
||||
import io.element.android.libraries.designsystem.theme.components.Text
|
||||
|
|
@ -27,7 +27,7 @@ internal fun UserAvatarColorsPreview() = ElementPreview {
|
|||
modifier = Modifier.padding(8.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp),
|
||||
) {
|
||||
repeat(avatarColorsLight.size) {
|
||||
repeat(avatarColors().size) {
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
|
|
|
|||
|
|
@ -8,32 +8,26 @@
|
|||
package io.element.android.libraries.designsystem.colors
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import io.element.android.compound.theme.avatarColorsDark
|
||||
import io.element.android.compound.theme.avatarColorsLight
|
||||
import org.junit.Test
|
||||
|
||||
class AvatarColorsTest {
|
||||
@Test
|
||||
fun `ensure the size of the avatar color are equal for light and dark theme`() {
|
||||
assertThat(avatarColorsDark.size).isEqualTo(avatarColorsLight.size)
|
||||
}
|
||||
|
||||
private val maxSize = 6
|
||||
@Test
|
||||
fun `compute string hash`() {
|
||||
assertThat("@alice:domain.org".toHash()).isEqualTo(6)
|
||||
assertThat("@bob:domain.org".toHash()).isEqualTo(3)
|
||||
assertThat("@charlie:domain.org".toHash()).isEqualTo(0)
|
||||
assertThat("@alice:domain.org".toHash(maxSize)).isEqualTo(0)
|
||||
assertThat("@bob:domain.org".toHash(maxSize)).isEqualTo(1)
|
||||
assertThat("@charlie:domain.org".toHash(maxSize)).isEqualTo(2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `compute string hash reverse`() {
|
||||
assertThat("0".toHash()).isEqualTo(0)
|
||||
assertThat("1".toHash()).isEqualTo(1)
|
||||
assertThat("2".toHash()).isEqualTo(2)
|
||||
assertThat("3".toHash()).isEqualTo(3)
|
||||
assertThat("4".toHash()).isEqualTo(4)
|
||||
assertThat("5".toHash()).isEqualTo(5)
|
||||
assertThat("6".toHash()).isEqualTo(6)
|
||||
assertThat("7".toHash()).isEqualTo(7)
|
||||
assertThat("0".toHash(maxSize)).isEqualTo(0)
|
||||
assertThat("1".toHash(maxSize)).isEqualTo(1)
|
||||
assertThat("2".toHash(maxSize)).isEqualTo(2)
|
||||
assertThat("3".toHash(maxSize)).isEqualTo(3)
|
||||
assertThat("4".toHash(maxSize)).isEqualTo(4)
|
||||
assertThat("5".toHash(maxSize)).isEqualTo(5)
|
||||
assertThat("6".toHash(maxSize)).isEqualTo(0)
|
||||
assertThat("7".toHash(maxSize)).isEqualTo(1)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue