Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 08bb7f08 authored by Sebastiano Barezzi's avatar Sebastiano Barezzi
Browse files

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
parent 77af37e1
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
/*
 * SPDX-FileCopyrightText: 2025 The LineageOS Project
 * SPDX-License-Identifier: Apache-2.0
 */

package org.lineageos.twelve.ext

import android.content.res.TypedArray
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

@OptIn(ExperimentalContracts::class)
inline fun <T : TypedArray?, R> T.use(block: (T) -> R): R {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    var exception: Throwable? = null
    try {
        return block(this)
    } catch (e: Exception) {
        exception = e
        throw e
    } finally {
        try {
            this?.close()
        } catch (closeException: Throwable) {
            exception?.apply {
                addSuppressed(closeException)
            } ?: throw closeException
        }
    }
}
+19 −22
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import androidx.core.view.isNotEmpty
import androidx.core.view.isVisible
import com.google.android.material.card.MaterialCardView
import org.lineageos.twelve.R
import org.lineageos.twelve.ext.use

/**
 * A poor man's Material Design 3 ListItem implementation.
@@ -140,32 +141,28 @@ class ListItem @JvmOverloads constructor(
            )
        }

        context.obtainStyledAttributes(attrs, R.styleable.ListItem, 0, 0).apply {
            try {
                leadingIconImage = getDrawable(R.styleable.ListItem_leadingIconImage)
                leadingText = getString(R.styleable.ListItem_leadingText)
                getResourceId(R.styleable.ListItem_leadingViewLayout, 0).takeUnless {
                    it == 0
                }?.let {
                    setLeadingView(it)
                }
                leadingViewIsVisible = getBoolean(R.styleable.ListItem_leadingViewIsVisible, true)
                headlineText = getString(R.styleable.ListItem_headlineText)
                supportingText = getString(R.styleable.ListItem_supportingText)
                trailingIconImage = getDrawable(R.styleable.ListItem_trailingIconImage)
                trailingSupportingText = getString(R.styleable.ListItem_trailingSupportingText)
                getResourceId(R.styleable.ListItem_trailingViewLayout, 0).takeUnless {
                    it == 0
                }?.let {
                    setTrailingView(it)
                }
                trailingViewIsVisible = getBoolean(R.styleable.ListItem_trailingViewIsVisible, true)
                isDimmed = getBoolean(R.styleable.ListItem_isDimmed, false)
                hasRoundedCorners = getBoolean(R.styleable.ListItem_hasRoundedCorners, false)
            } finally {
                recycle()
        context.obtainStyledAttributes(attrs, R.styleable.ListItem, 0, 0).use {
            leadingIconImage = it.getDrawable(R.styleable.ListItem_leadingIconImage)
            leadingText = it.getString(R.styleable.ListItem_leadingText)
            it.getResourceId(R.styleable.ListItem_leadingViewLayout, 0).let { leadingViewLayout ->
                if (leadingViewLayout != 0) {
                    setLeadingView(leadingViewLayout)
                }
            }
            leadingViewIsVisible = it.getBoolean(R.styleable.ListItem_leadingViewIsVisible, true)
            headlineText = it.getString(R.styleable.ListItem_headlineText)
            supportingText = it.getString(R.styleable.ListItem_supportingText)
            trailingIconImage = it.getDrawable(R.styleable.ListItem_trailingIconImage)
            trailingSupportingText = it.getString(R.styleable.ListItem_trailingSupportingText)
            it.getResourceId(R.styleable.ListItem_trailingViewLayout, 0).let { trailingViewLayout ->
                if (trailingViewLayout != 0) {
                    setTrailingView(trailingViewLayout)
                }
            }
            trailingViewIsVisible = it.getBoolean(R.styleable.ListItem_trailingViewIsVisible, true)
            isDimmed = it.getBoolean(R.styleable.ListItem_isDimmed, false)
            hasRoundedCorners = it.getBoolean(R.styleable.ListItem_hasRoundedCorners, false)
        }
    }

    override fun setEnabled(enabled: Boolean) {