using the existing theme utils logic for reading attributes

This commit is contained in:
Adam Brown 2021-09-28 14:57:42 +01:00
parent 0d737a9a5d
commit 12ca487e93
2 changed files with 5 additions and 16 deletions

View File

@ -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
}

View File

@ -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)
}