refactor: Use TypedArray.use extension

This commit is contained in:
Isira Seneviratne 2023-10-07 05:15:11 +05:30
parent 0b8de2764b
commit e376e34348

View File

@ -6,6 +6,7 @@ import android.graphics.drawable.Drawable
import android.util.AttributeSet import android.util.AttributeSet
import android.view.Gravity import android.view.Gravity
import androidx.appcompat.widget.AppCompatTextView import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.res.use
import com.github.libretube.R import com.github.libretube.R
/** /**
@ -16,30 +17,26 @@ class DrawableTextView(
context: Context, context: Context,
attrs: AttributeSet? = null attrs: AttributeSet? = null
) : AppCompatTextView(context, attrs) { ) : AppCompatTextView(context, attrs) {
private var drawableStartDimen = 0F private var drawableStartDimen = 0F
private var drawableTopDimen = 0F private var drawableTopDimen = 0F
private var drawableEndDimen = 0F private var drawableEndDimen = 0F
private var drawableBottomDimen = 0F private var drawableBottomDimen = 0F
init { init {
val ta = getContext().obtainStyledAttributes(attrs, R.styleable.DrawableTextView, 0, 0) context.obtainStyledAttributes(attrs, R.styleable.DrawableTextView).use {
try { drawableStartDimen = getDimen(it, R.styleable.DrawableTextView_drawableStartDimen)
drawableStartDimen = getDimen(ta, R.styleable.DrawableTextView_drawableStartDimen) drawableTopDimen = getDimen(it, R.styleable.DrawableTextView_drawableTopDimen)
drawableTopDimen = getDimen(ta, R.styleable.DrawableTextView_drawableTopDimen) drawableEndDimen = getDimen(it, R.styleable.DrawableTextView_drawableEndDimen)
drawableEndDimen = getDimen(ta, R.styleable.DrawableTextView_drawableEndDimen) drawableBottomDimen = getDimen(it, R.styleable.DrawableTextView_drawableBottomDimen)
drawableBottomDimen = getDimen(ta, R.styleable.DrawableTextView_drawableBottomDimen)
gravity = ta.getInt( gravity = it.getInt(
R.styleable.DrawableTextView_android_gravity, R.styleable.DrawableTextView_android_gravity,
Gravity.CENTER_VERTICAL Gravity.CENTER_VERTICAL
) )
compoundDrawablePadding = ta.getDimensionPixelOffset( compoundDrawablePadding = it.getDimensionPixelOffset(
R.styleable.DrawableTextView_android_drawablePadding, R.styleable.DrawableTextView_android_drawablePadding,
20 20
) )
} finally {
ta.recycle()
} }
} }