From 12ca487e936889a4ec04430173bc435a85e6a621 Mon Sep 17 00:00:00 2001
From: Adam Brown <adampsbrown@gmail.com>
Date: Tue, 28 Sep 2021 14:57:42 +0100
Subject: [PATCH] using the existing theme utils logic for reading attributes

---
 .../java/im/vector/app/core/extensions/Context.kt     | 10 ----------
 .../im/vector/app/core/extensions/ViewExtensions.kt   | 11 +++++------
 2 files changed, 5 insertions(+), 16 deletions(-)

diff --git a/vector/src/main/java/im/vector/app/core/extensions/Context.kt b/vector/src/main/java/im/vector/app/core/extensions/Context.kt
index 411bea55f0..c1c435edf2 100644
--- a/vector/src/main/java/im/vector/app/core/extensions/Context.kt
+++ b/vector/src/main/java/im/vector/app/core/extensions/Context.kt
@@ -17,8 +17,6 @@
 package im.vector.app.core.extensions
 
 import android.content.Context
-import android.util.TypedValue
-import androidx.annotation.AttrRes
 import im.vector.app.core.di.HasVectorInjector
 import im.vector.app.core.di.VectorComponent
 
@@ -30,11 +28,3 @@ fun Context.vectorComponent(): VectorComponent {
         throw IllegalStateException("Your application context doesn't implement HasVectorInjector")
     }
 }
-
-fun Context.fetchThemeColor(@AttrRes themeColorResId: Int): Int {
-    val typedValue = TypedValue()
-    val a = obtainStyledAttributes(typedValue.data, intArrayOf(themeColorResId))
-    val color = a.getColor(0, 0)
-    a.recycle()
-    return color
-}
diff --git a/vector/src/main/java/im/vector/app/core/extensions/ViewExtensions.kt b/vector/src/main/java/im/vector/app/core/extensions/ViewExtensions.kt
index 8d952e7cc8..54fcac42d1 100644
--- a/vector/src/main/java/im/vector/app/core/extensions/ViewExtensions.kt
+++ b/vector/src/main/java/im/vector/app/core/extensions/ViewExtensions.kt
@@ -18,7 +18,6 @@ package im.vector.app.core.extensions
 
 import android.graphics.drawable.Drawable
 import android.text.InputType
-import android.util.TypedValue
 import android.view.View
 import android.view.ViewGroup
 import android.widget.EditText
@@ -30,6 +29,7 @@ import androidx.core.content.ContextCompat
 import androidx.core.graphics.drawable.DrawableCompat
 import androidx.core.view.isVisible
 import im.vector.app.R
+import im.vector.app.features.themes.ThemeUtils
 
 /**
  * Remove left margin of a SearchView
@@ -66,18 +66,17 @@ fun ImageView.setDrawableOrHide(drawableRes: Drawable?) {
 
 fun View.setAttributeTintedBackground(@DrawableRes drawableRes: Int, @AttrRes tint: Int) {
     val drawable = ContextCompat.getDrawable(context, drawableRes)!!
-    DrawableCompat.setTint(drawable, context.fetchThemeColor(tint))
+    DrawableCompat.setTint(drawable, ThemeUtils.getColor(context, tint))
     background = drawable
 }
 
 fun ImageView.setAttributeTintedImageResource(@DrawableRes drawableRes: Int, @AttrRes tint: Int) {
     val drawable = ContextCompat.getDrawable(context, drawableRes)!!
-    DrawableCompat.setTint(drawable, context.fetchThemeColor(tint))
+    DrawableCompat.setTint(drawable, ThemeUtils.getColor(context, tint))
     setImageDrawable(drawable)
 }
 
 fun View.setAttributeBackground(@AttrRes attributeId: Int) {
-    val typedValue = TypedValue()
-    context.theme.resolveAttribute(attributeId, typedValue, true)
-    setBackgroundResource(typedValue.resourceId)
+    val attribute = ThemeUtils.getAttribute(context, attributeId)!!
+    setBackgroundResource(attribute.resourceId)
 }