mirror of
https://github.com/nextcloud/talk-android
synced 2025-03-06 06:15:12 +00:00
Make ColorUtil an injectable and clean up a little
Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
This commit is contained in:
parent
3a9009231d
commit
a45166a9db
@ -26,45 +26,45 @@ import androidx.annotation.ColorRes
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.graphics.ColorUtils
|
||||
import com.nextcloud.talk.R
|
||||
import javax.inject.Inject
|
||||
|
||||
object ColorUtil {
|
||||
private const val HSL_SIZE: Int = 3
|
||||
private const val INDEX_LUMINATION: Int = 2
|
||||
private const val LUMINATION_DARK_THRESHOLD: Float = 0.6f
|
||||
class ColorUtil @Inject constructor(private val context: Context) {
|
||||
|
||||
fun getPrimaryColor(context: Context, primaryColor: String?, @ColorRes fallbackColor: Int): Int {
|
||||
return if (primaryColor != null) {
|
||||
Color.parseColor(primaryColor)
|
||||
} else {
|
||||
ContextCompat.getColor(context, fallbackColor)
|
||||
}
|
||||
}
|
||||
|
||||
fun getNullsafeColor(color: String?, @ColorInt fallbackColor: Int): Int {
|
||||
return if (color != null) {
|
||||
Color.parseColor(color)
|
||||
} else {
|
||||
fallbackColor
|
||||
}
|
||||
}
|
||||
|
||||
fun getTextColor(context: Context, colorText: String?, @ColorInt fallBackPrimaryColor: Int): Int {
|
||||
return if (colorText != null) {
|
||||
Color.parseColor(colorText)
|
||||
} else {
|
||||
getForegroundColorForBackgroundColor(context, fallBackPrimaryColor)
|
||||
}
|
||||
@ColorInt
|
||||
fun getNullSafeColor(color: String?, @ColorInt fallbackColor: Int): Int {
|
||||
return color.parseColorOrFallback { fallbackColor }
|
||||
}
|
||||
|
||||
@ColorInt
|
||||
public fun getForegroundColorForBackgroundColor(context: Context, @ColorInt color: Int): Int {
|
||||
fun getNullSafeColorWithFallbackRes(color: String?, @ColorRes fallbackColorRes: Int): Int {
|
||||
return color.parseColorOrFallback { ContextCompat.getColor(context, fallbackColorRes) }
|
||||
}
|
||||
|
||||
@ColorInt
|
||||
fun getTextColor(colorText: String?, @ColorInt backgroundColor: Int): Int {
|
||||
return colorText.parseColorOrFallback { getForegroundColorForBackgroundColor(backgroundColor) }
|
||||
}
|
||||
|
||||
@ColorInt
|
||||
fun getForegroundColorForBackgroundColor(@ColorInt color: Int): Int {
|
||||
val hsl = FloatArray(HSL_SIZE)
|
||||
ColorUtils.RGBToHSL(Color.red(color), Color.green(color), Color.blue(color), hsl)
|
||||
|
||||
return if (hsl[INDEX_LUMINATION] < LUMINATION_DARK_THRESHOLD) {
|
||||
return if (hsl[INDEX_LIGHTNESS] < LIGHTNESS_DARK_THRESHOLD) {
|
||||
Color.WHITE
|
||||
} else {
|
||||
ContextCompat.getColor(context, R.color.grey_900)
|
||||
}
|
||||
}
|
||||
|
||||
@ColorInt
|
||||
private fun String?.parseColorOrFallback(fallback: () -> Int): Int {
|
||||
return this?.let { Color.parseColor(this) } ?: fallback()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val HSL_SIZE: Int = 3
|
||||
private const val INDEX_LIGHTNESS: Int = 2
|
||||
private const val LIGHTNESS_DARK_THRESHOLD: Float = 0.6f
|
||||
}
|
||||
}
|
||||
|
@ -23,11 +23,10 @@
|
||||
|
||||
package com.nextcloud.talk.ui.theme
|
||||
|
||||
import android.content.Context
|
||||
import com.nextcloud.talk.R
|
||||
import com.nextcloud.talk.models.json.capabilities.ThemingCapability
|
||||
|
||||
internal class ServerThemeImpl(context: Context, themingCapability: ThemingCapability?) :
|
||||
internal class ServerThemeImpl(themingCapability: ThemingCapability?, colorUtil: ColorUtil) :
|
||||
ServerTheme {
|
||||
|
||||
override val primaryColor: Int
|
||||
@ -37,12 +36,12 @@ internal class ServerThemeImpl(context: Context, themingCapability: ThemingCapab
|
||||
override val colorText: Int
|
||||
|
||||
init {
|
||||
primaryColor = ColorUtil.getPrimaryColor(context, themingCapability?.color, R.color.colorPrimary)
|
||||
primaryColor = colorUtil.getNullSafeColorWithFallbackRes(themingCapability?.color, R.color.colorPrimary)
|
||||
|
||||
colorElement = ColorUtil.getNullsafeColor(themingCapability?.colorElement, primaryColor)
|
||||
colorElementBright = ColorUtil.getNullsafeColor(themingCapability?.colorElementBright, primaryColor)
|
||||
colorElementDark = ColorUtil.getNullsafeColor(themingCapability?.colorElementDark, primaryColor)
|
||||
colorElement = colorUtil.getNullSafeColor(themingCapability?.colorElement, primaryColor)
|
||||
colorElementBright = colorUtil.getNullSafeColor(themingCapability?.colorElementBright, primaryColor)
|
||||
colorElementDark = colorUtil.getNullSafeColor(themingCapability?.colorElementDark, primaryColor)
|
||||
|
||||
colorText = ColorUtil.getTextColor(context, themingCapability?.colorText, primaryColor)
|
||||
colorText = colorUtil.getTextColor(themingCapability?.colorText, primaryColor)
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,6 @@
|
||||
package com.nextcloud.talk.ui.theme
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import com.nextcloud.talk.data.user.model.User
|
||||
import com.nextcloud.talk.models.json.capabilities.Capabilities
|
||||
import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew
|
||||
@ -32,11 +31,11 @@ import java.util.concurrent.ConcurrentHashMap
|
||||
import javax.inject.Inject
|
||||
|
||||
internal class ServerThemeProviderImpl @Inject constructor(
|
||||
private val context: Context,
|
||||
private val userProvider: CurrentUserProviderNew
|
||||
private val userProvider: CurrentUserProviderNew,
|
||||
private val colorUtil: ColorUtil
|
||||
) : ServerThemeProvider {
|
||||
|
||||
val themeCache: ConcurrentHashMap<String, ServerTheme> = ConcurrentHashMap()
|
||||
private val themeCache: ConcurrentHashMap<String, ServerTheme> = ConcurrentHashMap()
|
||||
|
||||
// TODO move this logic to currentUserProvider or something
|
||||
private var _currentUser: User? = null
|
||||
@ -76,7 +75,7 @@ internal class ServerThemeProviderImpl @Inject constructor(
|
||||
}
|
||||
|
||||
override fun getServerThemeForCapabilities(capabilities: Capabilities?): ServerTheme {
|
||||
return ServerThemeImpl(context, capabilities?.themingCapability)
|
||||
return ServerThemeImpl(capabilities?.themingCapability, colorUtil)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
@ -56,7 +56,6 @@ import com.yarolegovich.mp.MaterialPreferenceCategory
|
||||
import com.yarolegovich.mp.MaterialSwitchPreference
|
||||
import javax.inject.Inject
|
||||
|
||||
@Suppress("Detekt.TooManyFunctions")
|
||||
class ViewThemeUtils @Inject constructor(private val theme: ServerTheme) {
|
||||
|
||||
private fun isDarkMode(context: Context): Boolean = when (
|
||||
@ -235,7 +234,7 @@ class ViewThemeUtils @Inject constructor(private val theme: ServerTheme) {
|
||||
checkbox.buttonTintList = ColorStateList(
|
||||
arrayOf(
|
||||
intArrayOf(-android.R.attr.state_checked),
|
||||
intArrayOf(android.R.attr.state_checked),
|
||||
intArrayOf(android.R.attr.state_checked)
|
||||
),
|
||||
intArrayOf(Color.GRAY, color)
|
||||
)
|
||||
@ -247,7 +246,7 @@ class ViewThemeUtils @Inject constructor(private val theme: ServerTheme) {
|
||||
radioButton.buttonTintList = ColorStateList(
|
||||
arrayOf(
|
||||
intArrayOf(-android.R.attr.state_checked),
|
||||
intArrayOf(android.R.attr.state_checked),
|
||||
intArrayOf(android.R.attr.state_checked)
|
||||
),
|
||||
intArrayOf(Color.GRAY, color)
|
||||
)
|
||||
@ -357,9 +356,9 @@ class ViewThemeUtils @Inject constructor(private val theme: ServerTheme) {
|
||||
ColorUtils.RGBToHSL(Color.red(color), Color.green(color), Color.blue(color), hsl)
|
||||
|
||||
if (isDarkMode(context)) {
|
||||
hsl[INDEX_LUMINATION] = LUMINATION_DARK_THEME
|
||||
hsl[INDEX_LIGHTNESS] = LIGHTNESS_DARK_THEME
|
||||
} else {
|
||||
hsl[INDEX_LUMINATION] = LUMINATION_LIGHT_THEME
|
||||
hsl[INDEX_LIGHTNESS] = LIGHTNESS_LIGHT_THEME
|
||||
}
|
||||
|
||||
return ColorUtils.HSLToColor(hsl)
|
||||
@ -372,8 +371,8 @@ class ViewThemeUtils @Inject constructor(private val theme: ServerTheme) {
|
||||
)
|
||||
private const val TRACK_ALPHA: Int = 77
|
||||
private const val HSL_SIZE: Int = 3
|
||||
private const val INDEX_LUMINATION: Int = 2
|
||||
private const val LUMINATION_LIGHT_THEME: Float = 0.76f
|
||||
private const val LUMINATION_DARK_THEME: Float = 0.28f
|
||||
private const val INDEX_LIGHTNESS: Int = 2
|
||||
private const val LIGHTNESS_LIGHT_THEME: Float = 0.76f
|
||||
private const val LIGHTNESS_DARK_THEME: Float = 0.28f
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user