Use TypedArray.use to obtain attrs (#3349)
17346638ff/core/core-ktx/src/main/java/androidx/core/content/res/TypedArray.kt (L227-L236)
This commit is contained in:
parent
92bd2153e9
commit
2da7bc5bc8
|
@ -51,6 +51,7 @@ import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.core.app.ActivityCompat
|
import androidx.core.app.ActivityCompat
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.core.content.FileProvider
|
import androidx.core.content.FileProvider
|
||||||
|
import androidx.core.content.res.use
|
||||||
import androidx.core.view.ContentInfoCompat
|
import androidx.core.view.ContentInfoCompat
|
||||||
import androidx.core.view.OnReceiveContentListener
|
import androidx.core.view.OnReceiveContentListener
|
||||||
import androidx.core.view.isGone
|
import androidx.core.view.isGone
|
||||||
|
@ -571,9 +572,9 @@ class ComposeActivity :
|
||||||
|
|
||||||
private fun setupAvatar(activeAccount: AccountEntity) {
|
private fun setupAvatar(activeAccount: AccountEntity) {
|
||||||
val actionBarSizeAttr = intArrayOf(androidx.appcompat.R.attr.actionBarSize)
|
val actionBarSizeAttr = intArrayOf(androidx.appcompat.R.attr.actionBarSize)
|
||||||
val a = obtainStyledAttributes(null, actionBarSizeAttr)
|
val avatarSize = obtainStyledAttributes(null, actionBarSizeAttr).use { a ->
|
||||||
val avatarSize = a.getDimensionPixelSize(0, 1)
|
a.getDimensionPixelSize(0, 1)
|
||||||
a.recycle()
|
}
|
||||||
|
|
||||||
val animateAvatars = preferences.getBoolean("animateGifAvatars", false)
|
val animateAvatars = preferences.getBoolean("animateGifAvatars", false)
|
||||||
loadAvatar(
|
loadAvatar(
|
||||||
|
|
|
@ -22,6 +22,7 @@ import android.graphics.PorterDuff
|
||||||
import android.graphics.drawable.Drawable
|
import android.graphics.drawable.Drawable
|
||||||
import androidx.annotation.AttrRes
|
import androidx.annotation.AttrRes
|
||||||
import androidx.appcompat.app.AppCompatDelegate
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
|
import androidx.core.content.res.use
|
||||||
import com.google.android.material.color.MaterialColors
|
import com.google.android.material.color.MaterialColors
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -37,10 +38,9 @@ private const val THEME_SYSTEM = "auto_system"
|
||||||
const val APP_THEME_DEFAULT = THEME_NIGHT
|
const val APP_THEME_DEFAULT = THEME_NIGHT
|
||||||
|
|
||||||
fun getDimension(context: Context, @AttrRes attribute: Int): Int {
|
fun getDimension(context: Context, @AttrRes attribute: Int): Int {
|
||||||
val array = context.obtainStyledAttributes(intArrayOf(attribute))
|
return context.obtainStyledAttributes(intArrayOf(attribute)).use { array ->
|
||||||
val dimen = array.getDimensionPixelSize(0, -1)
|
array.getDimensionPixelSize(0, -1)
|
||||||
array.recycle()
|
}
|
||||||
return dimen
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setDrawableTint(context: Context, drawable: Drawable, @AttrRes attribute: Int) {
|
fun setDrawableTint(context: Context, drawable: Drawable, @AttrRes attribute: Int) {
|
||||||
|
|
|
@ -26,6 +26,7 @@ import androidx.annotation.ColorInt
|
||||||
import androidx.annotation.Dimension
|
import androidx.annotation.Dimension
|
||||||
import androidx.appcompat.widget.AppCompatImageView
|
import androidx.appcompat.widget.AppCompatImageView
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.core.content.res.use
|
||||||
import com.keylesspalace.tusky.R
|
import com.keylesspalace.tusky.R
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
|
|
||||||
|
@ -95,49 +96,49 @@ class GraphView @JvmOverloads constructor(
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initFromXML(attr: AttributeSet?) {
|
private fun initFromXML(attr: AttributeSet?) {
|
||||||
val a = context.obtainStyledAttributes(attr, R.styleable.GraphView)
|
context.obtainStyledAttributes(attr, R.styleable.GraphView).use { a ->
|
||||||
|
primaryLineColor = ContextCompat.getColor(
|
||||||
primaryLineColor = ContextCompat.getColor(
|
context,
|
||||||
context,
|
a.getResourceId(
|
||||||
a.getResourceId(
|
R.styleable.GraphView_primaryLineColor,
|
||||||
R.styleable.GraphView_primaryLineColor,
|
R.color.tusky_blue,
|
||||||
R.color.tusky_blue,
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
secondaryLineColor = ContextCompat.getColor(
|
secondaryLineColor = ContextCompat.getColor(
|
||||||
context,
|
context,
|
||||||
a.getResourceId(
|
a.getResourceId(
|
||||||
R.styleable.GraphView_secondaryLineColor,
|
R.styleable.GraphView_secondaryLineColor,
|
||||||
R.color.tusky_red,
|
R.color.tusky_red,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
lineWidth = a.getDimensionPixelSize(
|
lineWidth = a.getDimensionPixelSize(
|
||||||
R.styleable.GraphView_lineWidth,
|
R.styleable.GraphView_lineWidth,
|
||||||
R.dimen.graph_line_thickness
|
R.dimen.graph_line_thickness
|
||||||
).toFloat()
|
).toFloat()
|
||||||
|
|
||||||
graphColor = ContextCompat.getColor(
|
graphColor = ContextCompat.getColor(
|
||||||
context,
|
context,
|
||||||
a.getResourceId(
|
a.getResourceId(
|
||||||
R.styleable.GraphView_graphColor,
|
R.styleable.GraphView_graphColor,
|
||||||
R.color.colorBackground,
|
R.color.colorBackground,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
metaColor = ContextCompat.getColor(
|
metaColor = ContextCompat.getColor(
|
||||||
context,
|
context,
|
||||||
a.getResourceId(
|
a.getResourceId(
|
||||||
R.styleable.GraphView_metaColor,
|
R.styleable.GraphView_metaColor,
|
||||||
R.color.dividerColor,
|
R.color.dividerColor,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
proportionalTrending = a.getBoolean(
|
proportionalTrending = a.getBoolean(
|
||||||
R.styleable.GraphView_proportionalTrending,
|
R.styleable.GraphView_proportionalTrending,
|
||||||
proportionalTrending,
|
proportionalTrending,
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
primaryLinePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
primaryLinePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||||||
color = primaryLineColor
|
color = primaryLineColor
|
||||||
|
@ -170,8 +171,6 @@ class GraphView @JvmOverloads constructor(
|
||||||
strokeWidth = 0f
|
strokeWidth = 0f
|
||||||
style = Paint.Style.STROKE
|
style = Paint.Style.STROKE
|
||||||
}
|
}
|
||||||
|
|
||||||
a.recycle()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initializeVertices() {
|
private fun initializeVertices() {
|
||||||
|
|
|
@ -19,6 +19,7 @@ import android.content.Context
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
|
import androidx.core.content.res.use
|
||||||
import com.google.android.material.card.MaterialCardView
|
import com.google.android.material.card.MaterialCardView
|
||||||
import com.google.android.material.color.MaterialColors
|
import com.google.android.material.color.MaterialColors
|
||||||
import com.keylesspalace.tusky.R
|
import com.keylesspalace.tusky.R
|
||||||
|
@ -38,12 +39,15 @@ class LicenseCard
|
||||||
|
|
||||||
setCardBackgroundColor(MaterialColors.getColor(context, com.google.android.material.R.attr.colorSurface, Color.BLACK))
|
setCardBackgroundColor(MaterialColors.getColor(context, com.google.android.material.R.attr.colorSurface, Color.BLACK))
|
||||||
|
|
||||||
val a = context.theme.obtainStyledAttributes(attrs, R.styleable.LicenseCard, 0, 0)
|
val (name, license, link) = context.theme.obtainStyledAttributes(
|
||||||
|
attrs, R.styleable.LicenseCard, 0, 0
|
||||||
val name: String? = a.getString(R.styleable.LicenseCard_name)
|
).use { a ->
|
||||||
val license: String? = a.getString(R.styleable.LicenseCard_license)
|
Triple(
|
||||||
val link: String? = a.getString(R.styleable.LicenseCard_link)
|
a.getString(R.styleable.LicenseCard_name),
|
||||||
a.recycle()
|
a.getString(R.styleable.LicenseCard_license),
|
||||||
|
a.getString(R.styleable.LicenseCard_link),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
binding.licenseCardName.text = name
|
binding.licenseCardName.text = name
|
||||||
binding.licenseCardLicense.text = license
|
binding.licenseCardLicense.text = license
|
||||||
|
|
Loading…
Reference in New Issue