Skip to content

Commit 08bb7f0

Browse files
committed
Twelve: ListItem: Fix an API issue on older SDK versions
TL;DR TypedArray didn't implement AutoCloseable until an SDK version higher than the minimum one Change-Id: Ib68ef0b02cbfc94a34175513d9049fdb4424c7c2
1 parent 77af37e commit 08bb7f0

File tree

2 files changed

+52
-22
lines changed

2 files changed

+52
-22
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 The LineageOS Project
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.lineageos.twelve.ext
7+
8+
import android.content.res.TypedArray
9+
import kotlin.contracts.ExperimentalContracts
10+
import kotlin.contracts.InvocationKind
11+
import kotlin.contracts.contract
12+
13+
@OptIn(ExperimentalContracts::class)
14+
inline fun <T : TypedArray?, R> T.use(block: (T) -> R): R {
15+
contract {
16+
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
17+
}
18+
var exception: Throwable? = null
19+
try {
20+
return block(this)
21+
} catch (e: Exception) {
22+
exception = e
23+
throw e
24+
} finally {
25+
try {
26+
this?.close()
27+
} catch (closeException: Throwable) {
28+
exception?.apply {
29+
addSuppressed(closeException)
30+
} ?: throw closeException
31+
}
32+
}
33+
}

app/src/main/java/org/lineageos/twelve/ui/views/ListItem.kt

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import androidx.core.view.isNotEmpty
2424
import androidx.core.view.isVisible
2525
import com.google.android.material.card.MaterialCardView
2626
import org.lineageos.twelve.R
27+
import org.lineageos.twelve.ext.use
2728

2829
/**
2930
* A poor man's Material Design 3 ListItem implementation.
@@ -140,31 +141,27 @@ class ListItem @JvmOverloads constructor(
140141
)
141142
}
142143

143-
context.obtainStyledAttributes(attrs, R.styleable.ListItem, 0, 0).apply {
144-
try {
145-
leadingIconImage = getDrawable(R.styleable.ListItem_leadingIconImage)
146-
leadingText = getString(R.styleable.ListItem_leadingText)
147-
getResourceId(R.styleable.ListItem_leadingViewLayout, 0).takeUnless {
148-
it == 0
149-
}?.let {
150-
setLeadingView(it)
144+
context.obtainStyledAttributes(attrs, R.styleable.ListItem, 0, 0).use {
145+
leadingIconImage = it.getDrawable(R.styleable.ListItem_leadingIconImage)
146+
leadingText = it.getString(R.styleable.ListItem_leadingText)
147+
it.getResourceId(R.styleable.ListItem_leadingViewLayout, 0).let { leadingViewLayout ->
148+
if (leadingViewLayout != 0) {
149+
setLeadingView(leadingViewLayout)
151150
}
152-
leadingViewIsVisible = getBoolean(R.styleable.ListItem_leadingViewIsVisible, true)
153-
headlineText = getString(R.styleable.ListItem_headlineText)
154-
supportingText = getString(R.styleable.ListItem_supportingText)
155-
trailingIconImage = getDrawable(R.styleable.ListItem_trailingIconImage)
156-
trailingSupportingText = getString(R.styleable.ListItem_trailingSupportingText)
157-
getResourceId(R.styleable.ListItem_trailingViewLayout, 0).takeUnless {
158-
it == 0
159-
}?.let {
160-
setTrailingView(it)
151+
}
152+
leadingViewIsVisible = it.getBoolean(R.styleable.ListItem_leadingViewIsVisible, true)
153+
headlineText = it.getString(R.styleable.ListItem_headlineText)
154+
supportingText = it.getString(R.styleable.ListItem_supportingText)
155+
trailingIconImage = it.getDrawable(R.styleable.ListItem_trailingIconImage)
156+
trailingSupportingText = it.getString(R.styleable.ListItem_trailingSupportingText)
157+
it.getResourceId(R.styleable.ListItem_trailingViewLayout, 0).let { trailingViewLayout ->
158+
if (trailingViewLayout != 0) {
159+
setTrailingView(trailingViewLayout)
161160
}
162-
trailingViewIsVisible = getBoolean(R.styleable.ListItem_trailingViewIsVisible, true)
163-
isDimmed = getBoolean(R.styleable.ListItem_isDimmed, false)
164-
hasRoundedCorners = getBoolean(R.styleable.ListItem_hasRoundedCorners, false)
165-
} finally {
166-
recycle()
167161
}
162+
trailingViewIsVisible = it.getBoolean(R.styleable.ListItem_trailingViewIsVisible, true)
163+
isDimmed = it.getBoolean(R.styleable.ListItem_isDimmed, false)
164+
hasRoundedCorners = it.getBoolean(R.styleable.ListItem_hasRoundedCorners, false)
168165
}
169166
}
170167

0 commit comments

Comments
 (0)