浏览代码

safeguard theme values in case of null values

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
Andy Scherzinger 2 年之前
父节点
当前提交
d4c07f1278

+ 70 - 0
app/src/main/java/com/nextcloud/talk/ui/theme/ColorUtil.kt

@@ -0,0 +1,70 @@
+/*
+ * Nextcloud Talk application
+ *
+ * @author Andy Scherzinger
+ * Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.nextcloud.talk.ui.theme
+
+import android.content.Context
+import android.graphics.Color
+import androidx.annotation.ColorInt
+import androidx.annotation.ColorRes
+import androidx.core.content.ContextCompat
+import androidx.core.graphics.ColorUtils
+import com.nextcloud.talk.R
+
+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
+
+    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
+    public fun getForegroundColorForBackgroundColor(context: Context, @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) {
+            Color.WHITE
+        } else {
+            ContextCompat.getColor(context, R.color.grey_900)
+        }
+    }
+}

+ 10 - 7
app/src/main/java/com/nextcloud/talk/ui/theme/ServerThemeImpl.kt

@@ -2,7 +2,9 @@
  * Nextcloud Talk application
  *
  * @author Álvaro Brey
+ * @author Andy Scherzinger
  * Copyright (C) 2022 Álvaro Brey
+ * Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
  * Copyright (C) 2022 Nextcloud GmbH
  *
  * This program is free software: you can redistribute it and/or modify
@@ -22,7 +24,7 @@
 package com.nextcloud.talk.ui.theme
 
 import android.content.Context
-import android.graphics.Color
+import com.nextcloud.talk.R
 import com.nextcloud.talk.models.json.capabilities.ThemingCapability
 
 internal class ServerThemeImpl(context: Context, themingCapability: ThemingCapability) :
@@ -34,12 +36,13 @@ internal class ServerThemeImpl(context: Context, themingCapability: ThemingCapab
     override val colorElementDark: Int
     override val colorText: Int
 
-    // TODO fallback when some of these are null
     init {
-        primaryColor = Color.parseColor(themingCapability.color!!)
-        colorElement = Color.parseColor(themingCapability.colorElement!!)
-        colorElementBright = Color.parseColor(themingCapability.colorElementBright!!)
-        colorElementDark = Color.parseColor(themingCapability.colorElementDark!!)
-        colorText = Color.parseColor(themingCapability.colorText!!)
+        primaryColor = ColorUtil.getPrimaryColor(context, themingCapability.color, R.color.colorPrimary)
+
+        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)
     }
 }

+ 1 - 0
app/src/main/res/values/colors.xml

@@ -63,6 +63,7 @@
     <color name="nc_darkGreen">#006400</color>
     <color name="controller_chat_separator">#E8E8E8</color>
     <color name="grey_600">#757575</color>
+    <color name="grey_900">#212121</color>
     <color name="nc_grey">#D5D5D5</color>
     <color name="controller_call_incomingCallTextView">#E9FFFFFF</color>
     <color name="grey950">#111111</color>