From 6202a9d6a144215817fc093b888f27f9a2f465a6 Mon Sep 17 00:00:00 2001 From: Tamas Kozmer Date: Tue, 21 Jun 2022 15:02:13 +0200 Subject: [PATCH] Fixed color parsing bug. --- .../com/instructure/pandautils/utils/ColorKeeper.kt | 13 +++++++++++-- .../com/instructure/pandautils/utils/ColorUtils.kt | 9 +++++++-- .../com/instructure/pandautils/utils/ThemePrefs.kt | 12 +++++++++++- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/libs/pandautils/src/main/java/com/instructure/pandautils/utils/ColorKeeper.kt b/libs/pandautils/src/main/java/com/instructure/pandautils/utils/ColorKeeper.kt index 7c9bc7e1a5..6e5da51f04 100644 --- a/libs/pandautils/src/main/java/com/instructure/pandautils/utils/ColorKeeper.kt +++ b/libs/pandautils/src/main/java/com/instructure/pandautils/utils/ColorKeeper.kt @@ -16,7 +16,6 @@ package com.instructure.pandautils.utils import android.content.Context -import android.graphics.Color import android.graphics.PorterDuff import android.graphics.drawable.Drawable import androidx.annotation.ColorInt @@ -135,11 +134,21 @@ object ColorKeeper : PrefManager(PREFERENCE_FILE_NAME) { * @return The parsed color, or [defaultColor] if the string could not be parsed */ private fun parseColor(hexColor: String): Int = try { - ColorUtils.parseColor("#${hexColor.trimMargin("#")}", "") + val trimmedColorCode = getTrimmedColorCode(hexColor) + ColorUtils.parseColor(trimmedColorCode, defaultColor = defaultColor) } catch (e: IllegalArgumentException) { defaultColor } + // There might be cases where the color code from the response contains whitespaces. + private fun getTrimmedColorCode(colorCode: String): String { + return if (colorCode.contains("#")) { + "#${colorCode.trimMargin("#")}" + } else { + colorCode + } + } + /** * Generates a generic color based on the canvas context id, this will produce consistent colors for a given course * @param canvasContext a valid canvas context diff --git a/libs/pandautils/src/main/java/com/instructure/pandautils/utils/ColorUtils.kt b/libs/pandautils/src/main/java/com/instructure/pandautils/utils/ColorUtils.kt index bfcabf2976..e8fd0cf2f4 100644 --- a/libs/pandautils/src/main/java/com/instructure/pandautils/utils/ColorUtils.kt +++ b/libs/pandautils/src/main/java/com/instructure/pandautils/utils/ColorUtils.kt @@ -19,6 +19,7 @@ package com.instructure.pandautils.utils import android.graphics.* import android.graphics.drawable.Drawable import android.widget.ImageView +import androidx.annotation.ColorInt import androidx.core.graphics.drawable.DrawableCompat object ColorUtils { @@ -46,7 +47,7 @@ object ColorUtils { @JvmStatic @JvmOverloads - fun parseColor(colorCode: String?, defaultColorCode: String = ColorApiHelper.K5_DEFAULT_COLOR): Int { + fun parseColor(colorCode: String?, @ColorInt defaultColor: Int? = null): Int { return try { val fullColorCode = if (colorCode?.length == 4 && colorCode[0].toString() == "#") { "#${colorCode[1]}${colorCode[1]}${colorCode[2]}${colorCode[2]}${colorCode[3]}${colorCode[3]}" @@ -55,7 +56,11 @@ object ColorUtils { } Color.parseColor(fullColorCode) } catch (e: Exception) { - Color.parseColor(defaultColorCode) + if (defaultColor != null) { + defaultColor + } else { + Color.parseColor(ColorApiHelper.K5_DEFAULT_COLOR) + } } } } diff --git a/libs/pandautils/src/main/java/com/instructure/pandautils/utils/ThemePrefs.kt b/libs/pandautils/src/main/java/com/instructure/pandautils/utils/ThemePrefs.kt index abf7656c1f..c28b2e9392 100644 --- a/libs/pandautils/src/main/java/com/instructure/pandautils/utils/ThemePrefs.kt +++ b/libs/pandautils/src/main/java/com/instructure/pandautils/utils/ThemePrefs.kt @@ -122,10 +122,20 @@ object ThemePrefs : PrefManager("CanvasTheme") { private fun parseColor(hexColor: String, defaultColor: Int): Int { try { - return ColorUtils.parseColor("#${hexColor.trimMargin("#")}", "") + val trimmedColorCode = getTrimmedColorCode(hexColor) + return ColorUtils.parseColor(trimmedColorCode, defaultColor = defaultColor) } catch (e: IllegalArgumentException) { return defaultColor } } + // There might be cases where the color codes from the response contain whitespaces. + private fun getTrimmedColorCode(colorCode: String): String { + return if (colorCode.contains("#")) { + "#${colorCode.trimMargin("#")}" + } else { + colorCode + } + } + }