@@ -35,14 +35,6 @@ object ColorManager {
35
35
36
36
private var isNightMode = false
37
37
38
- val presetColorSchemes: List <ColorScheme >
39
- get() = theme.colorSchemes
40
-
41
- private val customFallbackRules: Map <String , String >
42
- get() = theme.fallbackColors
43
-
44
- private val fullFallbackRules get() = customFallbackRules + defaultFallbackColors
45
-
46
38
private lateinit var _activeColorScheme : ColorScheme
47
39
48
40
var activeColorScheme: ColorScheme
@@ -65,7 +57,7 @@ object ColorManager {
65
57
66
58
private var darkModeColorScheme: ColorScheme ? = null
67
59
68
- private val defaultFallbackColors =
60
+ private val BuiltinFallbackColors =
69
61
mapOf (
70
62
" candidate_text_color" to " text_color" ,
71
63
" comment_text_color" to " candidate_text_color" ,
@@ -128,7 +120,7 @@ object ColorManager {
128
120
onChangeListeners.forEach { it.onColorChange(theme) }
129
121
}
130
122
131
- private fun colorScheme (id : String ) = presetColorSchemes .find { it.id == id }
123
+ private fun colorScheme (id : String ) = theme.colorSchemes .find { it.id == id }
132
124
133
125
fun init (configuration : Configuration ) {
134
126
isNightMode = configuration.isNightMode()
@@ -169,7 +161,7 @@ object ColorManager {
169
161
}
170
162
171
163
@ColorInt
172
- fun resolveColor (
164
+ private fun resolveColor (
173
165
key : String ,
174
166
putCache : Boolean = true,
175
167
): Int {
@@ -187,7 +179,7 @@ object ColorManager {
187
179
return color
188
180
}
189
181
190
- fun resolveDrawable (
182
+ private fun resolveDrawable (
191
183
key : String ,
192
184
putCache : Boolean = true,
193
185
): Drawable ? {
@@ -210,20 +202,23 @@ object ColorManager {
210
202
parser : (String ) -> T ,
211
203
): T {
212
204
var currentKey = key
213
- val visitedKeys = mutableSetOf<String >()
214
205
215
206
while (true ) {
216
- when {
217
- activeColorScheme.colors.containsKey(currentKey) -> {
218
- return parser(activeColorScheme.colors[currentKey]!! )
219
- }
220
- fullFallbackRules.containsKey(currentKey) -> {
221
- currentKey =
222
- fullFallbackRules[currentKey]!! .also {
223
- check(visitedKeys.add(it)) { " Circular fallback: $key " }
224
- }
225
- }
226
- else -> throw IllegalArgumentException (" Color not found: $key " )
207
+ val target = activeColorScheme.colors[currentKey]
208
+ if (! target.isNullOrEmpty()) {
209
+ Timber .d(" current: $currentKey , origin: $key , target: $target " )
210
+ return parser(target)
211
+ }
212
+ val fallback = theme.fallbackColors[currentKey]
213
+ if (! fallback.isNullOrEmpty()) {
214
+ currentKey = fallback
215
+ continue
216
+ }
217
+ val altFallback = BuiltinFallbackColors [currentKey]
218
+ if (! altFallback.isNullOrEmpty()) {
219
+ currentKey = altFallback
220
+ } else {
221
+ throw IllegalArgumentException (" $key not found" )
227
222
}
228
223
}
229
224
}
@@ -276,16 +271,15 @@ object ColorManager {
276
271
@ColorInt
277
272
fun getColor (key : String ): Int = colorCache[key] ? : resolveColor(key)
278
273
279
- fun getDrawable (key : String ): Drawable ? = drawableCache[key] ? : resolveDrawable(key)
280
-
281
274
fun getDrawable (
282
275
colorKey : String ,
283
276
borderColorKey : String? = null,
284
277
borderPx : Int = 0,
285
278
cornerRadius : Float = 0f,
286
279
alpha : Int = 255,
287
- ): Drawable ? =
288
- when (val drawable = getDrawable(colorKey)) {
280
+ ): Drawable ? {
281
+ val drawable = drawableCache[colorKey] ? : resolveDrawable(colorKey)
282
+ return when (drawable) {
289
283
is BitmapDrawable -> drawable.also { it.alpha = MathUtils .clamp(alpha, 0 , 255 ) }
290
284
is GradientDrawable ->
291
285
drawable.also {
@@ -301,6 +295,7 @@ object ColorManager {
301
295
}
302
296
else -> null
303
297
}
298
+ }
304
299
305
300
private val SUPPORTED_IMG_FORMATS = arrayOf(" .png" , " .webp" , " .jpg" , " .gif" )
306
301
}
0 commit comments