From f4b2c40f0de9da149c9cc8260fba9ff45d33b80c Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 8 Sep 2023 14:53:46 +0200 Subject: [PATCH] Move class AvatarColorsProvider to its own file. --- .../designsystem/colors/AvatarColors.kt | 40 ------------- .../colors/AvatarColorsProvider.kt | 58 +++++++++++++++++++ 2 files changed, 58 insertions(+), 40 deletions(-) create mode 100644 libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/colors/AvatarColorsProvider.kt diff --git a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/colors/AvatarColors.kt b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/colors/AvatarColors.kt index c12be3b37d..abac299ca6 100644 --- a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/colors/AvatarColors.kt +++ b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/colors/AvatarColors.kt @@ -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(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 -} - diff --git a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/colors/AvatarColorsProvider.kt b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/colors/AvatarColorsProvider.kt new file mode 100644 index 0000000000..bf195e8160 --- /dev/null +++ b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/colors/AvatarColorsProvider.kt @@ -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(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 +}