From de046cde99396818276c900c2016dff556775220 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 29 Apr 2026 16:24:40 +0800 Subject: [PATCH 1/3] Add extension overloads for LazyListScope It's handy for reading and updating values from ViewModels. --- README.md | 7 ++-- preference/api/jvm/preference.api | 14 ++++++++ .../commonMain/kotlin/CheckboxPreference.kt | 24 +++++++++++++ .../src/commonMain/kotlin/ListPreference.kt | 33 ++++++++++++++++++ .../kotlin/MultiSelectListPreference.kt | 31 +++++++++++++++++ .../src/commonMain/kotlin/SliderPreference.kt | 34 +++++++++++++++++++ .../src/commonMain/kotlin/SwitchPreference.kt | 24 +++++++++++++ .../commonMain/kotlin/TextFieldPreference.kt | 33 ++++++++++++++++++ .../kotlin/TwoTargetSwitchPreference.kt | 28 +++++++++++++++ 9 files changed, 225 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8552d01..5dac7d1 100644 --- a/README.md +++ b/README.md @@ -70,11 +70,12 @@ Built-in types of preferences include: - [`TwoTargetIconButtonPreference`](preference/src/commonMain/kotlin/TwoTargetIconButtonPreference.kt) - [`TwoTargetSwitchPreference`](preference/src/commonMain/kotlin/TwoTargetSwitchPreference.kt) -Each type of built-in preference includes 3 kinds of APIs: +Each type of built-in preference includes 4 kinds of APIs: 1. A `LazyListScope.*Preference` extension function, which is the easiest way to use preferences in this library, and helps developers to avoid boilerplates like having to specify the key twice for the `LazyColumn` and the `Preference`. -2. A `*Preference` composable that takes a `MutableState`, which allows developers to bring in any kind of state they currently have. -3. A `*Preference` composable that takes `value` and `onValueChange`, which allows developers to use the preference without a state and even in non-preference scenarios. +2. A `LazyListScope.*Preference` extension function, overloads the first one for `value` and `onValueChange` params. +3. A `*Preference` composable that takes a `MutableState`, which allows developers to bring in any kind of state they currently have. +4. A `*Preference` composable that takes `value` and `onValueChange`, which allows developers to use the preference without a state and even in non-preference scenarios. ### Theming diff --git a/preference/api/jvm/preference.api b/preference/api/jvm/preference.api index 3360349..0378496 100644 --- a/preference/api/jvm/preference.api +++ b/preference/api/jvm/preference.api @@ -7,7 +7,9 @@ public final class me/zhanghai/compose/preference/BasicPreferenceKt { public final class me/zhanghai/compose/preference/CheckboxPreferenceKt { public static final fun CheckboxPreference (Landroidx/compose/runtime/MutableState;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V public static final fun CheckboxPreference (ZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V + public static final fun checkboxPreference (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;ZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)V public static final fun checkboxPreference (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;ZLkotlin/jvm/functions/Function3;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;)V + public static synthetic fun checkboxPreference$default (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;ZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)V public static synthetic fun checkboxPreference$default (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;ZLkotlin/jvm/functions/Function3;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;ILjava/lang/Object;)V } @@ -63,7 +65,9 @@ public final class me/zhanghai/compose/preference/ListPreferenceKt { public static final fun ListPreference (Landroidx/compose/runtime/MutableState;Ljava/util/List;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lme/zhanghai/compose/preference/ListPreferenceType;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function5;Landroidx/compose/runtime/Composer;II)V public static final fun ListPreference (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Ljava/util/List;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lme/zhanghai/compose/preference/ListPreferenceType;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function5;Landroidx/compose/runtime/Composer;III)V public static final fun listPreference (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;Ljava/lang/Object;Ljava/util/List;Lkotlin/jvm/functions/Function3;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lme/zhanghai/compose/preference/ListPreferenceType;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function5;)V + public static final fun listPreference (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Ljava/util/List;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lme/zhanghai/compose/preference/ListPreferenceType;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function5;)V public static synthetic fun listPreference$default (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;Ljava/lang/Object;Ljava/util/List;Lkotlin/jvm/functions/Function3;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lme/zhanghai/compose/preference/ListPreferenceType;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function5;ILjava/lang/Object;)V + public static synthetic fun listPreference$default (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Ljava/util/List;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lme/zhanghai/compose/preference/ListPreferenceType;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function5;ILjava/lang/Object;)V } public final class me/zhanghai/compose/preference/ListPreferenceType : java/lang/Enum { @@ -94,7 +98,9 @@ public final class me/zhanghai/compose/preference/MultiSelectListPreferenceKt { public static final fun MultiSelectListPreference (Landroidx/compose/runtime/MutableState;Ljava/util/List;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function5;Landroidx/compose/runtime/Composer;II)V public static final fun MultiSelectListPreference (Ljava/util/Set;Lkotlin/jvm/functions/Function1;Ljava/util/List;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function5;Landroidx/compose/runtime/Composer;II)V public static final fun multiSelectListPreference (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;Ljava/util/Set;Ljava/util/List;Lkotlin/jvm/functions/Function3;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function5;)V + public static final fun multiSelectListPreference (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;Ljava/util/Set;Lkotlin/jvm/functions/Function1;Ljava/util/List;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function5;)V public static synthetic fun multiSelectListPreference$default (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;Ljava/util/Set;Ljava/util/List;Lkotlin/jvm/functions/Function3;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function5;ILjava/lang/Object;)V + public static synthetic fun multiSelectListPreference$default (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;Ljava/util/Set;Lkotlin/jvm/functions/Function1;Ljava/util/List;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function5;ILjava/lang/Object;)V } public final class me/zhanghai/compose/preference/MutableMapPreferences : me/zhanghai/compose/preference/MutablePreferences { @@ -187,14 +193,18 @@ public final class me/zhanghai/compose/preference/RadioButtonPreferenceKt { public final class me/zhanghai/compose/preference/SliderPreferenceKt { public static final fun SliderPreference (FLkotlin/jvm/functions/Function1;FLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;Lkotlin/ranges/ClosedFloatingPointRange;IZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;III)V public static final fun SliderPreference (Landroidx/compose/runtime/MutableState;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;Lkotlin/ranges/ClosedFloatingPointRange;ILandroidx/compose/runtime/MutableFloatState;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V + public static final fun sliderPreference (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;FLkotlin/jvm/functions/Function1;FLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;Lkotlin/ranges/ClosedFloatingPointRange;IZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)V public static final fun sliderPreference (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;FLkotlin/jvm/functions/Function3;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/ranges/ClosedFloatingPointRange;ILkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;)V + public static synthetic fun sliderPreference$default (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;FLkotlin/jvm/functions/Function1;FLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;Lkotlin/ranges/ClosedFloatingPointRange;IZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)V public static synthetic fun sliderPreference$default (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;FLkotlin/jvm/functions/Function3;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/ranges/ClosedFloatingPointRange;ILkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;ILjava/lang/Object;)V } public final class me/zhanghai/compose/preference/SwitchPreferenceKt { public static final fun SwitchPreference (Landroidx/compose/runtime/MutableState;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V public static final fun SwitchPreference (ZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V + public static final fun switchPreference (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;ZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)V public static final fun switchPreference (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;ZLkotlin/jvm/functions/Function3;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;)V + public static synthetic fun switchPreference$default (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;ZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)V public static synthetic fun switchPreference$default (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;ZLkotlin/jvm/functions/Function3;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;ILjava/lang/Object;)V } @@ -207,7 +217,9 @@ public final class me/zhanghai/compose/preference/TextFieldPreferenceDefaults { public final class me/zhanghai/compose/preference/TextFieldPreferenceKt { public static final fun TextFieldPreference (Landroidx/compose/runtime/MutableState;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function5;Landroidx/compose/runtime/Composer;II)V public static final fun TextFieldPreference (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function5;Landroidx/compose/runtime/Composer;II)V + public static final fun textFieldPreference (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function5;)V public static final fun textFieldPreference (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;Ljava/lang/Object;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function5;)V + public static synthetic fun textFieldPreference$default (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function5;ILjava/lang/Object;)V public static synthetic fun textFieldPreference$default (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;Ljava/lang/Object;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function5;ILjava/lang/Object;)V } @@ -226,7 +238,9 @@ public final class me/zhanghai/compose/preference/TwoTargetPreferenceKt { public final class me/zhanghai/compose/preference/TwoTargetSwitchPreferenceKt { public static final fun TwoTargetSwitchPreference (Landroidx/compose/runtime/MutableState;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;ZLkotlin/jvm/functions/Function0;Landroidx/compose/runtime/Composer;II)V public static final fun TwoTargetSwitchPreference (ZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;ZLkotlin/jvm/functions/Function0;Landroidx/compose/runtime/Composer;II)V + public static final fun twoTargetSwitchPreference (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;ZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;ZLkotlin/jvm/functions/Function0;)V public static final fun twoTargetSwitchPreference (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;ZLkotlin/jvm/functions/Function3;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)V + public static synthetic fun twoTargetSwitchPreference$default (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;ZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;ZLkotlin/jvm/functions/Function0;ILjava/lang/Object;)V public static synthetic fun twoTargetSwitchPreference$default (Landroidx/compose/foundation/lazy/LazyListScope;Ljava/lang/String;ZLkotlin/jvm/functions/Function3;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V } diff --git a/preference/src/commonMain/kotlin/CheckboxPreference.kt b/preference/src/commonMain/kotlin/CheckboxPreference.kt index 38a0977..2e96384 100644 --- a/preference/src/commonMain/kotlin/CheckboxPreference.kt +++ b/preference/src/commonMain/kotlin/CheckboxPreference.kt @@ -54,6 +54,30 @@ public inline fun LazyListScope.checkboxPreference( } } +@Suppress("NOTHING_TO_INLINE") +public inline fun LazyListScope.checkboxPreference( + key: String, + value: Boolean, + noinline onValueChange: (Boolean) -> Unit, + noinline title: @Composable () -> Unit, + modifier: Modifier = Modifier, + enabled: Boolean = true, + noinline icon: @Composable (() -> Unit)? = null, + noinline summary: @Composable (() -> Unit)? = null, +) { + item(key = key, contentType = "CheckboxPreference") { + CheckboxPreference( + value = value, + onValueChange = onValueChange, + title = title, + modifier = modifier, + enabled = enabled, + icon = icon, + summary = summary, + ) + } +} + @Composable public fun CheckboxPreference( state: MutableState, diff --git a/preference/src/commonMain/kotlin/ListPreference.kt b/preference/src/commonMain/kotlin/ListPreference.kt index 3fc3d50..638c894 100644 --- a/preference/src/commonMain/kotlin/ListPreference.kt +++ b/preference/src/commonMain/kotlin/ListPreference.kt @@ -91,6 +91,39 @@ public inline fun LazyListScope.listPreference( } } +@Suppress("NOTHING_TO_INLINE") +public inline fun LazyListScope.listPreference( + key: String, + value: T, + noinline onValueChange: (T) -> Unit, + values: List, + noinline title: @Composable () -> Unit, + modifier: Modifier = Modifier, + enabled: Boolean = true, + noinline icon: @Composable (() -> Unit)? = null, + noinline summary: @Composable (() -> Unit)? = null, + type: ListPreferenceType = ListPreferenceType.ALERT_DIALOG, + noinline valueToText: @Composable (T) -> AnnotatedString = { AnnotatedString(it.toString()) }, + noinline item: @Composable (value: T, currentValue: T, onClick: () -> Unit) -> Unit = + ListPreferenceDefaults.item(type, valueToText), +) { + item(key = key, contentType = "ListPreference") { + ListPreference( + value = value, + onValueChange = onValueChange, + values = values, + title = title, + modifier = modifier, + enabled = enabled, + icon = icon, + summary = summary, + type = type, + valueToText = valueToText, + item = item, + ) + } +} + @Composable public fun ListPreference( state: MutableState, diff --git a/preference/src/commonMain/kotlin/MultiSelectListPreference.kt b/preference/src/commonMain/kotlin/MultiSelectListPreference.kt index 1a655d0..3588c4a 100644 --- a/preference/src/commonMain/kotlin/MultiSelectListPreference.kt +++ b/preference/src/commonMain/kotlin/MultiSelectListPreference.kt @@ -82,6 +82,37 @@ public inline fun LazyListScope.multiSelectListPreference( } } +@Suppress("NOTHING_TO_INLINE") +public inline fun LazyListScope.multiSelectListPreference( + key: String, + value: Set, + noinline onValueChange: (Set) -> Unit, + values: List, + noinline title: @Composable () -> Unit, + modifier: Modifier = Modifier, + enabled: Boolean = true, + noinline icon: @Composable (() -> Unit)? = null, + noinline summary: @Composable (() -> Unit)? = null, + noinline valueToText: @Composable (T) -> AnnotatedString = { AnnotatedString(it.toString()) }, + noinline item: @Composable (value: T, currentValues: Set, onToggle: (Boolean) -> Unit) -> Unit = + MultiSelectListPreferenceDefaults.item(valueToText), +) { + item(key = key, contentType = "MultiSelectListPreference") { + MultiSelectListPreference( + value = value, + onValueChange = onValueChange, + values = values, + title = title, + modifier = modifier, + enabled = enabled, + icon = icon, + summary = summary, + valueToText = valueToText, + item = item, + ) + } +} + @Composable public fun MultiSelectListPreference( state: MutableState>, diff --git a/preference/src/commonMain/kotlin/SliderPreference.kt b/preference/src/commonMain/kotlin/SliderPreference.kt index 3c001b8..212b3e8 100644 --- a/preference/src/commonMain/kotlin/SliderPreference.kt +++ b/preference/src/commonMain/kotlin/SliderPreference.kt @@ -72,6 +72,40 @@ public inline fun LazyListScope.sliderPreference( } } +@Suppress("NOTHING_TO_INLINE") +public inline fun LazyListScope.sliderPreference( + key: String, + value: Float, + noinline onValueChange: (Float) -> Unit, + sliderValue: Float, + noinline onSliderValueChange: (Float) -> Unit, + noinline title: @Composable () -> Unit, + modifier: Modifier = Modifier, + valueRange: ClosedFloatingPointRange = 0f..1f, + valueSteps: Int = 0, + enabled: Boolean = true, + noinline icon: @Composable (() -> Unit)? = null, + noinline summary: @Composable (() -> Unit)? = null, + noinline valueText: @Composable (() -> Unit)? = null, +) { + item(key = key, contentType = "SliderPreference") { + SliderPreference( + value = value, + onValueChange = onValueChange, + sliderValue = sliderValue, + onSliderValueChange = onSliderValueChange, + title = title, + modifier = modifier, + valueRange = valueRange, + valueSteps = valueSteps, + enabled = enabled, + icon = icon, + summary = summary, + valueText = valueText, + ) + } +} + @Composable public fun SliderPreference( state: MutableState, diff --git a/preference/src/commonMain/kotlin/SwitchPreference.kt b/preference/src/commonMain/kotlin/SwitchPreference.kt index 1a654dd..bc4d5ff 100644 --- a/preference/src/commonMain/kotlin/SwitchPreference.kt +++ b/preference/src/commonMain/kotlin/SwitchPreference.kt @@ -54,6 +54,30 @@ public inline fun LazyListScope.switchPreference( } } +@Suppress("NOTHING_TO_INLINE") +public inline fun LazyListScope.switchPreference( + key: String, + value: Boolean, + noinline onValueChange: (Boolean) -> Unit, + noinline title: @Composable () -> Unit, + modifier: Modifier = Modifier, + enabled: Boolean = true, + noinline icon: @Composable (() -> Unit)? = null, + noinline summary: @Composable (() -> Unit)? = null, +) { + item(key = key, contentType = "SwitchPreference") { + SwitchPreference( + value = value, + onValueChange = onValueChange, + title = title, + modifier = modifier, + enabled = enabled, + icon = icon, + summary = summary, + ) + } +} + @Composable public fun SwitchPreference( state: MutableState, diff --git a/preference/src/commonMain/kotlin/TextFieldPreference.kt b/preference/src/commonMain/kotlin/TextFieldPreference.kt index 11011ea..a19c7fe 100644 --- a/preference/src/commonMain/kotlin/TextFieldPreference.kt +++ b/preference/src/commonMain/kotlin/TextFieldPreference.kt @@ -78,6 +78,39 @@ public inline fun LazyListScope.textFieldPreference( } } +@Suppress("NOTHING_TO_INLINE") +public inline fun LazyListScope.textFieldPreference( + key: String, + value: T, + noinline onValueChange: (T) -> Unit, + noinline title: @Composable () -> Unit, + noinline textToValue: (String) -> T?, + modifier: Modifier = Modifier, + enabled: Boolean = true, + noinline icon: @Composable (() -> Unit)? = null, + noinline summary: @Composable (() -> Unit)? = null, + noinline valueToText: (T) -> String = { it.toString() }, + noinline textField: + @Composable + (value: TextFieldValue, onValueChange: (TextFieldValue) -> Unit, onOk: () -> Unit) -> Unit = + TextFieldPreferenceDefaults.TextField, +) { + item(key = key, contentType = "TextFieldPreference") { + TextFieldPreference( + value = value, + onValueChange = onValueChange, + title = title, + textToValue = textToValue, + modifier = modifier, + enabled = enabled, + icon = icon, + summary = summary, + valueToText = valueToText, + textField = textField, + ) + } +} + @Composable public fun TextFieldPreference( state: MutableState, diff --git a/preference/src/commonMain/kotlin/TwoTargetSwitchPreference.kt b/preference/src/commonMain/kotlin/TwoTargetSwitchPreference.kt index db48f62..f6005f3 100644 --- a/preference/src/commonMain/kotlin/TwoTargetSwitchPreference.kt +++ b/preference/src/commonMain/kotlin/TwoTargetSwitchPreference.kt @@ -57,6 +57,34 @@ public inline fun LazyListScope.twoTargetSwitchPreference( } } +@Suppress("NOTHING_TO_INLINE") +public inline fun LazyListScope.twoTargetSwitchPreference( + key: String, + value: Boolean, + noinline onValueChange: (Boolean) -> Unit, + noinline title: @Composable () -> Unit, + modifier: Modifier = Modifier, + enabled: Boolean = true, + noinline icon: @Composable (() -> Unit)? = null, + noinline summary: @Composable (() -> Unit)? = null, + switchEnabled: Boolean = enabled, + noinline onClick: (() -> Unit)? = null, +) { + item(key = key, contentType = "TwoTargetSwitchPreference") { + TwoTargetSwitchPreference( + value = value, + onValueChange = onValueChange, + title = title, + modifier = modifier, + enabled = enabled, + icon = icon, + summary = summary, + switchEnabled = switchEnabled, + onClick = onClick, + ) + } +} + @Composable public fun TwoTargetSwitchPreference( state: MutableState, From 4e905991e197606835ce839541d54cfbf829caca Mon Sep 17 00:00:00 2001 From: Zongle Wang Date: Fri, 15 May 2026 15:18:29 +0800 Subject: [PATCH 2/3] Update README.md Co-authored-by: Hai Zhang <4469895+zhanghai@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5dac7d1..06ef037 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ Built-in types of preferences include: Each type of built-in preference includes 4 kinds of APIs: 1. A `LazyListScope.*Preference` extension function, which is the easiest way to use preferences in this library, and helps developers to avoid boilerplates like having to specify the key twice for the `LazyColumn` and the `Preference`. -2. A `LazyListScope.*Preference` extension function, overloads the first one for `value` and `onValueChange` params. +2. A `LazyListScope.*Preference` extension function, which is an overload of the first extension function but accepts `value` and `onValueChange` instead. 3. A `*Preference` composable that takes a `MutableState`, which allows developers to bring in any kind of state they currently have. 4. A `*Preference` composable that takes `value` and `onValueChange`, which allows developers to use the preference without a state and even in non-preference scenarios. From 9200947bad15c65abf1fc2e06b4aaca7c493e432 Mon Sep 17 00:00:00 2001 From: Goooler Date: Fri, 15 May 2026 15:25:46 +0800 Subject: [PATCH 3/3] Eliminate inline and noinline --- .../src/commonMain/kotlin/CheckboxPreference.kt | 11 +++++------ .../src/commonMain/kotlin/ListPreference.kt | 15 +++++++-------- .../kotlin/MultiSelectListPreference.kt | 15 +++++++-------- .../src/commonMain/kotlin/SliderPreference.kt | 15 +++++++-------- .../src/commonMain/kotlin/SwitchPreference.kt | 11 +++++------ .../commonMain/kotlin/TextFieldPreference.kt | 17 ++++++++--------- .../kotlin/TwoTargetSwitchPreference.kt | 13 ++++++------- 7 files changed, 45 insertions(+), 52 deletions(-) diff --git a/preference/src/commonMain/kotlin/CheckboxPreference.kt b/preference/src/commonMain/kotlin/CheckboxPreference.kt index 2e96384..04c18b4 100644 --- a/preference/src/commonMain/kotlin/CheckboxPreference.kt +++ b/preference/src/commonMain/kotlin/CheckboxPreference.kt @@ -54,16 +54,15 @@ public inline fun LazyListScope.checkboxPreference( } } -@Suppress("NOTHING_TO_INLINE") -public inline fun LazyListScope.checkboxPreference( +public fun LazyListScope.checkboxPreference( key: String, value: Boolean, - noinline onValueChange: (Boolean) -> Unit, - noinline title: @Composable () -> Unit, + onValueChange: (Boolean) -> Unit, + title: @Composable () -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true, - noinline icon: @Composable (() -> Unit)? = null, - noinline summary: @Composable (() -> Unit)? = null, + icon: @Composable (() -> Unit)? = null, + summary: @Composable (() -> Unit)? = null, ) { item(key = key, contentType = "CheckboxPreference") { CheckboxPreference( diff --git a/preference/src/commonMain/kotlin/ListPreference.kt b/preference/src/commonMain/kotlin/ListPreference.kt index 638c894..09770da 100644 --- a/preference/src/commonMain/kotlin/ListPreference.kt +++ b/preference/src/commonMain/kotlin/ListPreference.kt @@ -91,20 +91,19 @@ public inline fun LazyListScope.listPreference( } } -@Suppress("NOTHING_TO_INLINE") -public inline fun LazyListScope.listPreference( +public fun LazyListScope.listPreference( key: String, value: T, - noinline onValueChange: (T) -> Unit, + onValueChange: (T) -> Unit, values: List, - noinline title: @Composable () -> Unit, + title: @Composable () -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true, - noinline icon: @Composable (() -> Unit)? = null, - noinline summary: @Composable (() -> Unit)? = null, + icon: @Composable (() -> Unit)? = null, + summary: @Composable (() -> Unit)? = null, type: ListPreferenceType = ListPreferenceType.ALERT_DIALOG, - noinline valueToText: @Composable (T) -> AnnotatedString = { AnnotatedString(it.toString()) }, - noinline item: @Composable (value: T, currentValue: T, onClick: () -> Unit) -> Unit = + valueToText: @Composable (T) -> AnnotatedString = { AnnotatedString(it.toString()) }, + item: @Composable (value: T, currentValue: T, onClick: () -> Unit) -> Unit = ListPreferenceDefaults.item(type, valueToText), ) { item(key = key, contentType = "ListPreference") { diff --git a/preference/src/commonMain/kotlin/MultiSelectListPreference.kt b/preference/src/commonMain/kotlin/MultiSelectListPreference.kt index 3588c4a..6ba765c 100644 --- a/preference/src/commonMain/kotlin/MultiSelectListPreference.kt +++ b/preference/src/commonMain/kotlin/MultiSelectListPreference.kt @@ -82,19 +82,18 @@ public inline fun LazyListScope.multiSelectListPreference( } } -@Suppress("NOTHING_TO_INLINE") -public inline fun LazyListScope.multiSelectListPreference( +public fun LazyListScope.multiSelectListPreference( key: String, value: Set, - noinline onValueChange: (Set) -> Unit, + onValueChange: (Set) -> Unit, values: List, - noinline title: @Composable () -> Unit, + title: @Composable () -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true, - noinline icon: @Composable (() -> Unit)? = null, - noinline summary: @Composable (() -> Unit)? = null, - noinline valueToText: @Composable (T) -> AnnotatedString = { AnnotatedString(it.toString()) }, - noinline item: @Composable (value: T, currentValues: Set, onToggle: (Boolean) -> Unit) -> Unit = + icon: @Composable (() -> Unit)? = null, + summary: @Composable (() -> Unit)? = null, + valueToText: @Composable (T) -> AnnotatedString = { AnnotatedString(it.toString()) }, + item: @Composable (value: T, currentValues: Set, onToggle: (Boolean) -> Unit) -> Unit = MultiSelectListPreferenceDefaults.item(valueToText), ) { item(key = key, contentType = "MultiSelectListPreference") { diff --git a/preference/src/commonMain/kotlin/SliderPreference.kt b/preference/src/commonMain/kotlin/SliderPreference.kt index 212b3e8..f331ddc 100644 --- a/preference/src/commonMain/kotlin/SliderPreference.kt +++ b/preference/src/commonMain/kotlin/SliderPreference.kt @@ -72,21 +72,20 @@ public inline fun LazyListScope.sliderPreference( } } -@Suppress("NOTHING_TO_INLINE") -public inline fun LazyListScope.sliderPreference( +public fun LazyListScope.sliderPreference( key: String, value: Float, - noinline onValueChange: (Float) -> Unit, + onValueChange: (Float) -> Unit, sliderValue: Float, - noinline onSliderValueChange: (Float) -> Unit, - noinline title: @Composable () -> Unit, + onSliderValueChange: (Float) -> Unit, + title: @Composable () -> Unit, modifier: Modifier = Modifier, valueRange: ClosedFloatingPointRange = 0f..1f, valueSteps: Int = 0, enabled: Boolean = true, - noinline icon: @Composable (() -> Unit)? = null, - noinline summary: @Composable (() -> Unit)? = null, - noinline valueText: @Composable (() -> Unit)? = null, + icon: @Composable (() -> Unit)? = null, + summary: @Composable (() -> Unit)? = null, + valueText: @Composable (() -> Unit)? = null, ) { item(key = key, contentType = "SliderPreference") { SliderPreference( diff --git a/preference/src/commonMain/kotlin/SwitchPreference.kt b/preference/src/commonMain/kotlin/SwitchPreference.kt index bc4d5ff..7086daa 100644 --- a/preference/src/commonMain/kotlin/SwitchPreference.kt +++ b/preference/src/commonMain/kotlin/SwitchPreference.kt @@ -54,16 +54,15 @@ public inline fun LazyListScope.switchPreference( } } -@Suppress("NOTHING_TO_INLINE") -public inline fun LazyListScope.switchPreference( +public fun LazyListScope.switchPreference( key: String, value: Boolean, - noinline onValueChange: (Boolean) -> Unit, - noinline title: @Composable () -> Unit, + onValueChange: (Boolean) -> Unit, + title: @Composable () -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true, - noinline icon: @Composable (() -> Unit)? = null, - noinline summary: @Composable (() -> Unit)? = null, + icon: @Composable (() -> Unit)? = null, + summary: @Composable (() -> Unit)? = null, ) { item(key = key, contentType = "SwitchPreference") { SwitchPreference( diff --git a/preference/src/commonMain/kotlin/TextFieldPreference.kt b/preference/src/commonMain/kotlin/TextFieldPreference.kt index a19c7fe..5541833 100644 --- a/preference/src/commonMain/kotlin/TextFieldPreference.kt +++ b/preference/src/commonMain/kotlin/TextFieldPreference.kt @@ -78,19 +78,18 @@ public inline fun LazyListScope.textFieldPreference( } } -@Suppress("NOTHING_TO_INLINE") -public inline fun LazyListScope.textFieldPreference( +public fun LazyListScope.textFieldPreference( key: String, value: T, - noinline onValueChange: (T) -> Unit, - noinline title: @Composable () -> Unit, - noinline textToValue: (String) -> T?, + onValueChange: (T) -> Unit, + title: @Composable () -> Unit, + textToValue: (String) -> T?, modifier: Modifier = Modifier, enabled: Boolean = true, - noinline icon: @Composable (() -> Unit)? = null, - noinline summary: @Composable (() -> Unit)? = null, - noinline valueToText: (T) -> String = { it.toString() }, - noinline textField: + icon: @Composable (() -> Unit)? = null, + summary: @Composable (() -> Unit)? = null, + valueToText: (T) -> String = { it.toString() }, + textField: @Composable (value: TextFieldValue, onValueChange: (TextFieldValue) -> Unit, onOk: () -> Unit) -> Unit = TextFieldPreferenceDefaults.TextField, diff --git a/preference/src/commonMain/kotlin/TwoTargetSwitchPreference.kt b/preference/src/commonMain/kotlin/TwoTargetSwitchPreference.kt index f6005f3..4b462ca 100644 --- a/preference/src/commonMain/kotlin/TwoTargetSwitchPreference.kt +++ b/preference/src/commonMain/kotlin/TwoTargetSwitchPreference.kt @@ -57,18 +57,17 @@ public inline fun LazyListScope.twoTargetSwitchPreference( } } -@Suppress("NOTHING_TO_INLINE") -public inline fun LazyListScope.twoTargetSwitchPreference( +public fun LazyListScope.twoTargetSwitchPreference( key: String, value: Boolean, - noinline onValueChange: (Boolean) -> Unit, - noinline title: @Composable () -> Unit, + onValueChange: (Boolean) -> Unit, + title: @Composable () -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true, - noinline icon: @Composable (() -> Unit)? = null, - noinline summary: @Composable (() -> Unit)? = null, + icon: @Composable (() -> Unit)? = null, + summary: @Composable (() -> Unit)? = null, switchEnabled: Boolean = enabled, - noinline onClick: (() -> Unit)? = null, + onClick: (() -> Unit)? = null, ) { item(key = key, contentType = "TwoTargetSwitchPreference") { TwoTargetSwitchPreference(