ComposePreference icon indicating copy to clipboard operation
ComposePreference copied to clipboard

remove internal on ListPreferenceDefaults

Open RichardLuo0 opened this issue 1 year ago • 1 comments

I am new to jetpack compose so I am unsure if I am doing this properly. I want to create a custom wrapper for listPreference. So I do the following:

inline fun <T> LazyListScope.MylistPreference(
    key: String,
    defaultValue: T,
    values: List<T>,
    crossinline title: @Composable (T) -> Unit,
    modifier: Modifier = Modifier.fillMaxWidth(),
    crossinline rememberState: @Composable () -> MutableState<T> = {
        rememberPreferenceState(key, defaultValue)
    },
    crossinline enabled: (T) -> Boolean = { true },
    noinline icon: @Composable ((T) -> Unit)? = null,
    noinline summary: @Composable ((T) -> Unit)? = null,
    type: ListPreferenceType = ListPreferenceType.ALERT_DIALOG,
    noinline valueToText: (T) -> AnnotatedString = { AnnotatedString(it.toString()) },
    noinline item: @Composable (value: T, currentValue: T, onClick: () -> Unit) -> Unit =
        ListPreferenceDefaults.item(type, valueToText),
    // Some other options to customize
) {
    item(key = key, contentType = "ListPreference") {
        // Some other state

        val state = rememberState()
        val value by state
        ListPreference(
            state = state,
            values = values,
            title = { title(value) },
            modifier = modifier,
            enabled = enabled(value),
            icon = icon?.let { { it(value) } },
            summary = summary?.let { { it(value) } },
            type = type,
            valueToText = valueToText,
            item = item,
        )
    }
}

Then android studio complains ListPreferenceDefaults is internal and can not be used. So how do I keep the item while I can still use the default item if not provided? Or am I doing wrong? Thx in advance.

RichardLuo0 avatar Dec 05 '24 06:12 RichardLuo0

I want to create a custom wrapper for listPreference

That's indeed not directly supported right now if you want to keep the defaults. That being said, you can make a copy of the defaults, which isn't too long anyway.

zhanghai avatar Dec 05 '24 10:12 zhanghai