Skip to content

Commit 08b5abe

Browse files
committed
Reduce a lot of boilerplate by creating a ColorStateList build function
Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
1 parent f77f7aa commit 08b5abe

3 files changed

Lines changed: 180 additions & 252 deletions

File tree

ui/src/main/java/com/nextcloud/android/common/ui/theme/utils/AndroidViewThemeUtils.kt

Lines changed: 36 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import com.nextcloud.android.common.ui.R
6262
import com.nextcloud.android.common.ui.color.ColorUtil
6363
import com.nextcloud.android.common.ui.theme.MaterialSchemes
6464
import com.nextcloud.android.common.ui.theme.ViewThemeUtilsBase
65+
import com.nextcloud.android.common.ui.util.buildColorStateList
6566
import scheme.Scheme
6667
import javax.inject.Inject
6768

@@ -83,29 +84,17 @@ class AndroidViewThemeUtils @Inject constructor(schemes: MaterialSchemes, privat
8384
withScheme(navigationView) { scheme ->
8485
if (navigationView.itemBackground != null) {
8586
navigationView.itemBackground!!.setTintList(
86-
ColorStateList(
87-
arrayOf(
88-
intArrayOf(android.R.attr.state_checked),
89-
intArrayOf(-android.R.attr.state_checked)
90-
),
91-
intArrayOf(
92-
scheme.secondaryContainer,
93-
Color.TRANSPARENT
94-
)
87+
buildColorStateList(
88+
android.R.attr.state_checked to scheme.secondaryContainer,
89+
-android.R.attr.state_checked to Color.TRANSPARENT
9590
)
9691
)
9792
}
9893
navigationView.setBackgroundColor(scheme.surface)
9994

100-
val colorStateList = ColorStateList(
101-
arrayOf(
102-
intArrayOf(android.R.attr.state_checked),
103-
intArrayOf(-android.R.attr.state_checked)
104-
),
105-
intArrayOf(
106-
scheme.onSecondaryContainer,
107-
scheme.onSurfaceVariant
108-
)
95+
val colorStateList = buildColorStateList(
96+
android.R.attr.state_checked to scheme.onSecondaryContainer,
97+
-android.R.attr.state_checked to scheme.onSurfaceVariant
10998
)
11099

111100
navigationView.itemTextColor = colorStateList
@@ -360,18 +349,13 @@ class AndroidViewThemeUtils @Inject constructor(schemes: MaterialSchemes, privat
360349

361350
fun themeImageButton(imageButton: ImageButton) {
362351
withScheme(imageButton) { scheme ->
363-
imageButton.imageTintList = ColorStateList(
364-
arrayOf(
365-
intArrayOf(android.R.attr.state_selected),
366-
intArrayOf(-android.R.attr.state_selected),
367-
intArrayOf(android.R.attr.state_enabled),
368-
intArrayOf(-android.R.attr.state_enabled)
369-
),
370-
intArrayOf(
371-
scheme.primary,
372-
scheme.onSurfaceVariant,
373-
scheme.onSurfaceVariant,
374-
colorUtil.adjustOpacity(scheme.onSurface, ON_SURFACE_OPACITY_BUTTON_DISABLED)
352+
imageButton.imageTintList = buildColorStateList(
353+
android.R.attr.state_selected to scheme.primary,
354+
-android.R.attr.state_selected to scheme.onSurfaceVariant,
355+
android.R.attr.state_enabled to scheme.onSurfaceVariant,
356+
-android.R.attr.state_enabled to colorUtil.adjustOpacity(
357+
scheme.onSurface,
358+
ON_SURFACE_OPACITY_BUTTON_DISABLED
375359
)
376360
)
377361
}
@@ -406,14 +390,11 @@ class AndroidViewThemeUtils @Inject constructor(schemes: MaterialSchemes, privat
406390
withScheme(buttons[0]) { scheme ->
407391
for (button in buttons) {
408392
button.setTextColor(
409-
ColorStateList(
410-
arrayOf(
411-
intArrayOf(android.R.attr.state_enabled),
412-
intArrayOf(-android.R.attr.state_enabled)
413-
),
414-
intArrayOf(
415-
color,
416-
colorUtil.adjustOpacity(scheme.onSurface, ON_SURFACE_OPACITY_BUTTON_DISABLED)
393+
buildColorStateList(
394+
android.R.attr.state_enabled to color,
395+
-android.R.attr.state_enabled to colorUtil.adjustOpacity(
396+
scheme.onSurface,
397+
ON_SURFACE_OPACITY_BUTTON_DISABLED
417398
)
418399
)
419400
)
@@ -441,14 +422,12 @@ class AndroidViewThemeUtils @Inject constructor(schemes: MaterialSchemes, privat
441422

442423
fun themeCheckedTextView(vararg checkedTextViews: CheckedTextView) {
443424
withScheme(checkedTextViews[0]) { scheme ->
444-
val colorStateList = ColorStateList(
445-
arrayOf(
446-
intArrayOf(-android.R.attr.state_checked),
447-
intArrayOf(-android.R.attr.state_enabled),
448-
intArrayOf(android.R.attr.state_checked)
449-
),
450-
intArrayOf(Color.GRAY, Color.GRAY, scheme.primary)
425+
val colorStateList = buildColorStateList(
426+
-android.R.attr.state_checked to Color.GRAY,
427+
-android.R.attr.state_enabled to Color.GRAY,
428+
android.R.attr.state_checked to scheme.primary
451429
)
430+
452431
checkedTextViews.forEach {
453432
it.checkMarkTintList = colorStateList
454433
}
@@ -457,13 +436,10 @@ class AndroidViewThemeUtils @Inject constructor(schemes: MaterialSchemes, privat
457436

458437
fun themeCheckbox(vararg checkboxes: CheckBox) {
459438
withScheme(checkboxes[0]) { scheme ->
460-
val colorStateList = ColorStateList(
461-
arrayOf(
462-
intArrayOf(-android.R.attr.state_checked),
463-
intArrayOf(-android.R.attr.state_enabled),
464-
intArrayOf(android.R.attr.state_checked)
465-
),
466-
intArrayOf(Color.GRAY, Color.GRAY, scheme.primary)
439+
val colorStateList = buildColorStateList(
440+
-android.R.attr.state_checked to Color.GRAY,
441+
-android.R.attr.state_enabled to Color.GRAY,
442+
android.R.attr.state_checked to scheme.primary
467443
)
468444
checkboxes.forEach {
469445
it.buttonTintList = colorStateList
@@ -473,12 +449,9 @@ class AndroidViewThemeUtils @Inject constructor(schemes: MaterialSchemes, privat
473449

474450
fun themeRadioButton(radioButton: RadioButton) {
475451
withScheme(radioButton) { scheme ->
476-
radioButton.buttonTintList = ColorStateList(
477-
arrayOf(
478-
intArrayOf(-android.R.attr.state_checked),
479-
intArrayOf(android.R.attr.state_checked)
480-
),
481-
intArrayOf(Color.GRAY, scheme.primary)
452+
radioButton.buttonTintList = buildColorStateList(
453+
-android.R.attr.state_checked to Color.GRAY,
454+
android.R.attr.state_checked to scheme.primary
482455
)
483456
}
484457
}
@@ -487,16 +460,11 @@ class AndroidViewThemeUtils @Inject constructor(schemes: MaterialSchemes, privat
487460
withScheme(editText) { scheme ->
488461
// TODO check API-level compatibility
489462
// editText.background.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
490-
editText.backgroundTintList = ColorStateList(
491-
arrayOf(
492-
intArrayOf(-android.R.attr.state_focused),
493-
intArrayOf(android.R.attr.state_focused)
494-
),
495-
intArrayOf(
496-
scheme.outline,
497-
scheme.primary
498-
)
463+
editText.backgroundTintList = buildColorStateList(
464+
-android.R.attr.state_focused to scheme.outline,
465+
android.R.attr.state_focused to scheme.primary
499466
)
467+
500468
editText.setHintTextColor(scheme.onSurfaceVariant)
501469
editText.setTextColor(scheme.onSurface)
502470
}
@@ -552,7 +520,7 @@ class AndroidViewThemeUtils @Inject constructor(schemes: MaterialSchemes, privat
552520
}
553521
}
554522

555-
@Deprecated("Don't do this, implement custom viewthemeutils instead")
523+
@Deprecated("Don't do this, implement custom viewThemeUtils instead")
556524
fun primaryColor(activity: Activity): Int {
557525
return withScheme(activity) { scheme ->
558526
scheme.primary

0 commit comments

Comments
 (0)