Changed card tinting method to use strings rather than raw integers to prevent mix-up of colours on app update; added migration strategy to convert old raw integer colours into string values

This commit is contained in:
Deniz Düzgören
2019-08-09 21:18:27 +02:00
parent c2561b8354
commit 60831aa2b2
6 changed files with 107 additions and 41 deletions
@@ -1,9 +1,40 @@
package com.denizd.substitutionplan
import android.content.SharedPreferences
object MiscData {
fun emoji(unicode: Int): String { return String(Character.toChars(unicode)) }
fun getColourForString(name: String): Int {
return when(name) {
"red" -> R.color.bgRed
"orange" -> R.color.bgOrange
"yellow" -> R.color.bgYellow
"green" -> R.color.bgGreen
"teal" -> R.color.bgTeal
"cyan" -> R.color.bgCyan
"blue" -> R.color.bgBlue
"purple" -> R.color.bgPurple
"pink" -> R.color.bgPink
"brown" -> R.color.bgBrown
"grey" -> R.color.bgGrey
"pureWhite" -> R.color.bgPureWhite
"salmon" -> R.color.bgSalmon
"tangerine" -> R.color.bgTangerine
"banana" -> R.color.bgBanana
"flora" -> R.color.bgFlora
"spindrift" -> R.color.bgSpindrift
"sky" -> R.color.bgSky
"orchid" -> R.color.bgOrchid
"lavender" -> R.color.bgLavender
"carnation" -> R.color.bgCarnation
"brown2" -> R.color.bgBrown2
"pureBlack" -> R.color.bgPureBlack
else -> R.color.colorBackgroundLight
}
}
fun getIcon(course: String): Int {
return with (course.toLowerCase()) {
when {
@@ -36,4 +67,30 @@ object MiscData {
}
}
}
fun transferOldColourIntsToString(prefs: SharedPreferences) {
val colourIntegers = intArrayOf(0, R.color.bgRed, R.color.bgOrange, R.color.bgYellow, R.color.bgGreen,
R.color.bgTeal, R.color.bgCyan, R.color.bgBlue, R.color.bgPurple, R.color.bgPink, R.color.bgBrown, R.color.bgGrey,
R.color.bgPureWhite, R.color.bgSalmon, R.color.bgTangerine, R.color.bgBanana, R.color.bgFlora, R.color.bgSpindrift,
R.color.bgSky, R.color.bgOrchid, R.color.bgLavender, R.color.bgCarnation, R.color.bgBrown2, R.color.bgPureBlack)
val colourStrings = arrayOf("default", "red", "orange", "yellow", "green", "teal", "cyan", "blue", "purple", "pink",
"brown", "grey", "pureWhite", "salmon", "tangerine", "banana", "flora", "spindrift", "sky", "orchid",
"lavender", "carnation", "brown2", "pureBlack")
val courses = arrayOf("German", "English", "French", "Spanish", "Latin", "Turkish", "Chinese", "Arts", "Music",
"Theatre", "Geography", "History", "Politics", "Philosophy", "Religion", "Maths", "Biology", "Chemistry",
"Physics", "CompSci", "PhysEd", "GLL", "WAT", "Forder", "WP")
var colour = ""
val edit = prefs.edit()
for (course in courses) {
for (i in 0 until colourIntegers.size) {
if (prefs.getInt("bg$course", 0) == colourIntegers[i]) {
colour = colourStrings[i]
break
}
colour = ""
}
edit.putString("card$course", colour)
}
edit.apply()
}
}