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, 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.

### Theming

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

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions preference/src/commonMain/kotlin/CheckboxPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ public inline fun LazyListScope.checkboxPreference(
}
}

public fun LazyListScope.checkboxPreference(
key: String,
value: Boolean,
onValueChange: (Boolean) -> Unit,
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
icon: @Composable (() -> Unit)? = null,
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
32 changes: 32 additions & 0 deletions preference/src/commonMain/kotlin/ListPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,38 @@ public inline fun <T> LazyListScope.listPreference(
}
}

public fun <T> LazyListScope.listPreference(
key: String,
value: T,
onValueChange: (T) -> Unit,
values: List<T>,
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
icon: @Composable (() -> Unit)? = null,
summary: @Composable (() -> Unit)? = null,
type: ListPreferenceType = ListPreferenceType.ALERT_DIALOG,
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") {
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
30 changes: 30 additions & 0 deletions preference/src/commonMain/kotlin/MultiSelectListPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,36 @@ public inline fun <T> LazyListScope.multiSelectListPreference(
}
}

public fun <T> LazyListScope.multiSelectListPreference(
key: String,
value: Set<T>,
onValueChange: (Set<T>) -> Unit,
values: List<T>,
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
icon: @Composable (() -> Unit)? = null,
summary: @Composable (() -> Unit)? = null,
valueToText: @Composable (T) -> AnnotatedString = { AnnotatedString(it.toString()) },
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
33 changes: 33 additions & 0 deletions preference/src/commonMain/kotlin/SliderPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,39 @@ public inline fun LazyListScope.sliderPreference(
}
}

public fun LazyListScope.sliderPreference(
key: String,
value: Float,
onValueChange: (Float) -> Unit,
sliderValue: Float,
onSliderValueChange: (Float) -> Unit,
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
valueRange: ClosedFloatingPointRange<Float> = 0f..1f,
valueSteps: Int = 0,
enabled: Boolean = true,
icon: @Composable (() -> Unit)? = null,
summary: @Composable (() -> Unit)? = null,
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
23 changes: 23 additions & 0 deletions preference/src/commonMain/kotlin/SwitchPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ public inline fun LazyListScope.switchPreference(
}
}

public fun LazyListScope.switchPreference(
key: String,
value: Boolean,
onValueChange: (Boolean) -> Unit,
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
icon: @Composable (() -> Unit)? = null,
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
32 changes: 32 additions & 0 deletions preference/src/commonMain/kotlin/TextFieldPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,38 @@ public inline fun <T> LazyListScope.textFieldPreference(
}
}

public fun <T> LazyListScope.textFieldPreference(
key: String,
value: T,
onValueChange: (T) -> Unit,
title: @Composable () -> Unit,
textToValue: (String) -> T?,
modifier: Modifier = Modifier,
enabled: Boolean = true,
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,
) {
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
27 changes: 27 additions & 0 deletions preference/src/commonMain/kotlin/TwoTargetSwitchPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,33 @@ public inline fun LazyListScope.twoTargetSwitchPreference(
}
}

public fun LazyListScope.twoTargetSwitchPreference(
key: String,
value: Boolean,
onValueChange: (Boolean) -> Unit,
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
icon: @Composable (() -> Unit)? = null,
summary: @Composable (() -> Unit)? = null,
switchEnabled: Boolean = enabled,
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