Browse Source

Move class AvatarColorsProvider to its own file.

pull/1264/head
Benoit Marty 1 year ago committed by Benoit Marty
parent
commit
4e40d694e7
  1. 40
      libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/colors/AvatarColors.kt
  2. 58
      libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/colors/AvatarColorsProvider.kt

40
libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/colors/AvatarColors.kt

@ -16,50 +16,10 @@ @@ -16,50 +16,10 @@
package io.element.android.libraries.designsystem.colors
import androidx.collection.LruCache
import androidx.compose.ui.graphics.Color
import io.element.android.libraries.theme.colors.avatarColorsDark
import io.element.android.libraries.theme.colors.avatarColorsLight
data class AvatarColors(
val background: Color,
val foreground: Color,
)
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()
}
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 AvatarColors(
background = colors.first,
foreground = colors.second,
)
}
}
internal fun String.toHash(): Int {
return toList().sumOf { it.code } % avatarColorsLight.size
}

58
libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/colors/AvatarColorsProvider.kt

@ -0,0 +1,58 @@ @@ -0,0 +1,58 @@
/*
* Copyright (c) 2023 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 io.element.android.libraries.designsystem.colors
import androidx.collection.LruCache
import io.element.android.libraries.theme.colors.avatarColorsDark
import io.element.android.libraries.theme.colors.avatarColorsLight
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()
}
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 AvatarColors(
background = colors.first,
foreground = colors.second,
)
}
}
internal fun String.toHash(): Int {
return toList().sumOf { it.code } % avatarColorsLight.size
}
Loading…
Cancel
Save