From 2da7bc5bc852dce581b7a7ed17f95563b841ad17 Mon Sep 17 00:00:00 2001 From: Goooler Date: Sun, 26 Feb 2023 04:30:52 +0800 Subject: [PATCH] Use TypedArray.use to obtain attrs (#3349) https://github.com/androidx/androidx/blob/17346638fffb173a69801ee4c9ea293588800214/core/core-ktx/src/main/java/androidx/core/content/res/TypedArray.kt#L227-L236 --- .../components/compose/ComposeActivity.kt | 7 +- .../keylesspalace/tusky/util/ThemeUtils.kt | 8 +-- .../com/keylesspalace/tusky/view/GraphView.kt | 71 +++++++++---------- .../keylesspalace/tusky/view/LicenseCard.kt | 16 +++-- 4 files changed, 53 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt index 7ff0733e1..db606a318 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt @@ -51,6 +51,7 @@ import androidx.appcompat.app.AlertDialog import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import androidx.core.content.FileProvider +import androidx.core.content.res.use import androidx.core.view.ContentInfoCompat import androidx.core.view.OnReceiveContentListener import androidx.core.view.isGone @@ -571,9 +572,9 @@ class ComposeActivity : private fun setupAvatar(activeAccount: AccountEntity) { val actionBarSizeAttr = intArrayOf(androidx.appcompat.R.attr.actionBarSize) - val a = obtainStyledAttributes(null, actionBarSizeAttr) - val avatarSize = a.getDimensionPixelSize(0, 1) - a.recycle() + val avatarSize = obtainStyledAttributes(null, actionBarSizeAttr).use { a -> + a.getDimensionPixelSize(0, 1) + } val animateAvatars = preferences.getBoolean("animateGifAvatars", false) loadAvatar( diff --git a/app/src/main/java/com/keylesspalace/tusky/util/ThemeUtils.kt b/app/src/main/java/com/keylesspalace/tusky/util/ThemeUtils.kt index 8c7c0b23e..a03a50260 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/ThemeUtils.kt +++ b/app/src/main/java/com/keylesspalace/tusky/util/ThemeUtils.kt @@ -22,6 +22,7 @@ import android.graphics.PorterDuff import android.graphics.drawable.Drawable import androidx.annotation.AttrRes import androidx.appcompat.app.AppCompatDelegate +import androidx.core.content.res.use 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 fun getDimension(context: Context, @AttrRes attribute: Int): Int { - val array = context.obtainStyledAttributes(intArrayOf(attribute)) - val dimen = array.getDimensionPixelSize(0, -1) - array.recycle() - return dimen + return context.obtainStyledAttributes(intArrayOf(attribute)).use { array -> + array.getDimensionPixelSize(0, -1) + } } fun setDrawableTint(context: Context, drawable: Drawable, @AttrRes attribute: Int) { diff --git a/app/src/main/java/com/keylesspalace/tusky/view/GraphView.kt b/app/src/main/java/com/keylesspalace/tusky/view/GraphView.kt index 1d3c49d91..cc779432d 100644 --- a/app/src/main/java/com/keylesspalace/tusky/view/GraphView.kt +++ b/app/src/main/java/com/keylesspalace/tusky/view/GraphView.kt @@ -26,6 +26,7 @@ import androidx.annotation.ColorInt import androidx.annotation.Dimension import androidx.appcompat.widget.AppCompatImageView import androidx.core.content.ContextCompat +import androidx.core.content.res.use import com.keylesspalace.tusky.R import kotlin.math.max @@ -95,49 +96,49 @@ class GraphView @JvmOverloads constructor( } private fun initFromXML(attr: AttributeSet?) { - val a = context.obtainStyledAttributes(attr, R.styleable.GraphView) - - primaryLineColor = ContextCompat.getColor( - context, - a.getResourceId( - R.styleable.GraphView_primaryLineColor, - R.color.tusky_blue, + context.obtainStyledAttributes(attr, R.styleable.GraphView).use { a -> + primaryLineColor = ContextCompat.getColor( + context, + a.getResourceId( + R.styleable.GraphView_primaryLineColor, + R.color.tusky_blue, + ) ) - ) - secondaryLineColor = ContextCompat.getColor( - context, - a.getResourceId( - R.styleable.GraphView_secondaryLineColor, - R.color.tusky_red, + secondaryLineColor = ContextCompat.getColor( + context, + a.getResourceId( + R.styleable.GraphView_secondaryLineColor, + R.color.tusky_red, + ) ) - ) - lineWidth = a.getDimensionPixelSize( - R.styleable.GraphView_lineWidth, - R.dimen.graph_line_thickness - ).toFloat() + lineWidth = a.getDimensionPixelSize( + R.styleable.GraphView_lineWidth, + R.dimen.graph_line_thickness + ).toFloat() - graphColor = ContextCompat.getColor( - context, - a.getResourceId( - R.styleable.GraphView_graphColor, - R.color.colorBackground, + graphColor = ContextCompat.getColor( + context, + a.getResourceId( + R.styleable.GraphView_graphColor, + R.color.colorBackground, + ) ) - ) - metaColor = ContextCompat.getColor( - context, - a.getResourceId( - R.styleable.GraphView_metaColor, - R.color.dividerColor, + metaColor = ContextCompat.getColor( + context, + a.getResourceId( + R.styleable.GraphView_metaColor, + R.color.dividerColor, + ) ) - ) - proportionalTrending = a.getBoolean( - R.styleable.GraphView_proportionalTrending, - proportionalTrending, - ) + proportionalTrending = a.getBoolean( + R.styleable.GraphView_proportionalTrending, + proportionalTrending, + ) + } primaryLinePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = primaryLineColor @@ -170,8 +171,6 @@ class GraphView @JvmOverloads constructor( strokeWidth = 0f style = Paint.Style.STROKE } - - a.recycle() } private fun initializeVertices() { diff --git a/app/src/main/java/com/keylesspalace/tusky/view/LicenseCard.kt b/app/src/main/java/com/keylesspalace/tusky/view/LicenseCard.kt index 44febf40f..6d553a26c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/view/LicenseCard.kt +++ b/app/src/main/java/com/keylesspalace/tusky/view/LicenseCard.kt @@ -19,6 +19,7 @@ import android.content.Context import android.graphics.Color import android.util.AttributeSet import android.view.LayoutInflater +import androidx.core.content.res.use import com.google.android.material.card.MaterialCardView import com.google.android.material.color.MaterialColors import com.keylesspalace.tusky.R @@ -38,12 +39,15 @@ class LicenseCard 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: String? = a.getString(R.styleable.LicenseCard_name) - val license: String? = a.getString(R.styleable.LicenseCard_license) - val link: String? = a.getString(R.styleable.LicenseCard_link) - a.recycle() + val (name, license, link) = context.theme.obtainStyledAttributes( + attrs, R.styleable.LicenseCard, 0, 0 + ).use { a -> + Triple( + a.getString(R.styleable.LicenseCard_name), + a.getString(R.styleable.LicenseCard_license), + a.getString(R.styleable.LicenseCard_link), + ) + } binding.licenseCardName.text = name binding.licenseCardLicense.text = license