From 0f250d72bb614e2b63e2fe299d308980f10f6720 Mon Sep 17 00:00:00 2001 From: Helium314 Date: Sun, 10 Mar 2024 05:29:05 +0100 Subject: [PATCH] allow storing AllColors in prefs (still not used) --- .../helium314/keyboard/latin/common/Colors.kt | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/helium314/keyboard/latin/common/Colors.kt b/app/src/main/java/helium314/keyboard/latin/common/Colors.kt index 19c644dbc..07931bc30 100644 --- a/app/src/main/java/helium314/keyboard/latin/common/Colors.kt +++ b/app/src/main/java/helium314/keyboard/latin/common/Colors.kt @@ -3,6 +3,7 @@ package helium314.keyboard.latin.common import android.content.Context +import android.content.SharedPreferences import android.content.res.ColorStateList import android.content.res.Configuration import android.content.res.TypedArray @@ -18,6 +19,7 @@ import android.widget.ImageView import androidx.annotation.ColorInt import androidx.annotation.RequiresApi import androidx.core.content.ContextCompat +import androidx.core.content.edit import androidx.core.graphics.BlendModeColorFilterCompat import androidx.core.graphics.BlendModeCompat import androidx.core.graphics.ColorUtils @@ -540,7 +542,7 @@ class DefaultColors ( // todo: allow users to use this class // the colorMap should be stored in settings -// reading should consider that colors might be added and removed, i.e. no "simple" serialization +// settings read and write untested // color settings should add another menu option for "all colors" // just show all ColorTypes with current value read from the map (default to black, same as in get) // no string name, as it is not stable @@ -581,6 +583,25 @@ class AllColors(private val colorMap: EnumMap, override val them } } +fun readAllColorsMap(prefs: SharedPreferences): EnumMap { + val s = prefs.getString("all_colors", "") ?: "" + val c = EnumMap(ColorType::class.java) + s.split(";").forEach { + val ct = try { + ColorType.valueOf(it.substringBefore(",").uppercase()) + } catch (_: Exception) { // todo: which one? + return@forEach + } + val i = it.substringAfter(",").toIntOrNull() ?: return@forEach + c[ct] = i + } + return c +} + +fun writeAllColorsMap(c: EnumMap, prefs: SharedPreferences) { + prefs.edit { putString("all_colors", c.map { "${it.key},${it.value}" }.joinToString(";")) } +} + private fun colorFilter(color: Int, mode: BlendModeCompat = BlendModeCompat.MODULATE): ColorFilter { // using !! for the color filter because null is only returned for unsupported blend modes, which are not used return BlendModeColorFilterCompat.createBlendModeColorFilterCompat(color, mode)!!