Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, reloads 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

Expand Down
14 changes: 14 additions & 0 deletions preference/api/jvm/preference.api

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions preference/src/commonMain/kotlin/CheckboxPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean>,
Expand Down
33 changes: 33 additions & 0 deletions preference/src/commonMain/kotlin/ListPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,39 @@ public inline fun <T> LazyListScope.listPreference(
}
}

@Suppress("NOTHING_TO_INLINE")
public inline fun <T> LazyListScope.listPreference(
key: String,
value: T,
noinline onValueChange: (T) -> Unit,
values: List<T>,
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 <T> ListPreference(
state: MutableState<T>,
Expand Down
31 changes: 31 additions & 0 deletions preference/src/commonMain/kotlin/MultiSelectListPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,37 @@ public inline fun <T> LazyListScope.multiSelectListPreference(
}
}

@Suppress("NOTHING_TO_INLINE")
public inline fun <T> LazyListScope.multiSelectListPreference(
key: String,
value: Set<T>,
noinline onValueChange: (Set<T>) -> Unit,
values: List<T>,
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<T>, 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 <T> MultiSelectListPreference(
state: MutableState<Set<T>>,
Expand Down
34 changes: 34 additions & 0 deletions preference/src/commonMain/kotlin/SliderPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Float> = 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<Float>,
Expand Down
24 changes: 24 additions & 0 deletions preference/src/commonMain/kotlin/SwitchPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean>,
Expand Down
33 changes: 33 additions & 0 deletions preference/src/commonMain/kotlin/TextFieldPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,39 @@ public inline fun <T> LazyListScope.textFieldPreference(
}
}

@Suppress("NOTHING_TO_INLINE")
public inline fun <T> 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 <T> TextFieldPreference(
state: MutableState<T>,
Expand Down
28 changes: 28 additions & 0 deletions preference/src/commonMain/kotlin/TwoTargetSwitchPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean>,
Expand Down