Skip to content

Bool Preference

Screenshot Screenshot

This shows a simple boolean preference. It allows to toggle a boolean value.

Check out the composable and it's documentation in the code snipplet below.

Example#

val bool1 = dataStore.getBool("bool1", true).collectAsState(initial = true)
PreferenceBool(
    style = PreferenceBool.Style.Switch,
    value = bool1.value,
    onValueChange = {
        scope.launch(Dispatchers.IO) {
            dataStore.update("bool1", it)
        }
    },
    title = "Bool 1",
    icon = { Icon(Icons.Default.Check, null) }
)

Composable#

/**
 * A bool preference item - this item shows a checkbox/switch which reflects the preference state
 *
 *  
 *
 * **Basic Parameters:** all params not described here are derived from [com.michaelflisar.composepreferences.core.composables.BasePreference], check it out for more details
 *
 * @param style the [PreferenceBool.Style] of this item ([PreferenceBool.Style.Switch] or [PreferenceBool.Style.Checkbox])
 * @param value the [MutableState] of this item
 */
@Composable
fun PreferenceScope.PreferenceBool(
    style: PreferenceBool.Style = PreferenceBool.Style.Switch,
    // Special
    value: MutableState<Boolean>,
    // Base Preference
    title: String,
    enabled: Dependency = Dependency.Enabled,
    visible: Dependency = Dependency.Enabled,
    subtitle: String? = null,
    icon: (@Composable () -> Unit)? = null,
    itemStyle: PreferenceItemStyle = LocalPreferenceSettings.current.style.defaultItemStyle,
    itemSetup: PreferenceItemSetup = PreferenceBoolDefaults.itemSetup(),
    titleRenderer: @Composable (text: AnnotatedString) -> Unit = { Text(it) },
    subtitleRenderer: @Composable (text: AnnotatedString) -> Unit = { Text(it) },
    filterTags: List<String> = emptyList()
)
/**
 * A bool preference item - this item shows a checkbox/switch which reflects the preference state
 *
 * &nbsp;
 *
 * **Basic Parameters:** all params not described here are derived from [com.michaelflisar.composepreferences.core.composables.BasePreference], check it out for more details
 *
 * @param style the [PreferenceBool.Style] of this item ([PreferenceBool.Style.Switch] or [PreferenceBool.Style.Checkbox])
 * @param value the value of this item
 * @param onValueChange the value changed callback of this item
 */
@Composable
fun PreferenceScope.PreferenceBool(
    style: PreferenceBool.Style = PreferenceBool.Style.Switch,
    // Special
    value: Boolean,
    onValueChange: (selected: Boolean) -> Unit,
    // Base Preference
    title: String,
    enabled: Dependency = Dependency.Enabled,
    visible: Dependency = Dependency.Enabled,
    subtitle: String? = null,
    icon: (@Composable () -> Unit)? = null,
    itemStyle: PreferenceItemStyle = LocalPreferenceSettings.current.style.defaultItemStyle,
    itemSetup: PreferenceItemSetup = PreferenceBoolDefaults.itemSetup(),
    titleRenderer: @Composable (text: AnnotatedString) -> Unit = { Text(it) },
    subtitleRenderer: @Composable (text: AnnotatedString) -> Unit = { Text(it) },
    filterTags: List<String> = emptyList()
)
{
    val stateEnabled = enabled.state()
    val onClick = if (stateEnabled.value && LocalPreferenceSettings.current.toggleBooleanOnItemClick) {
        {
            val updated = !value
            onValueChange(updated)
        }
    } else null

    // Switch is larger than Checkbox and has 52x32 DP
    BasePreference(
        itemSetup = itemSetup,
        enabled = enabled,
        visible = visible,
        title = title,
        subtitle = subtitle,
        icon = icon,
        itemStyle = itemStyle,
        titleRenderer = titleRenderer,
        subtitleRenderer = subtitleRenderer,
        filterTags = filterTags,
        onClick = onClick
    ) {
        when (style) {
            PreferenceBool.Style.Checkbox -> {
                Checkbox(checked = value, onCheckedChange = {
                    onValueChange(it)
                }, enabled = stateEnabled.value)
            }

            PreferenceBool.Style.Switch -> {
                Switch(checked = value, onCheckedChange = {
                    onValueChange(it)
                }, enabled = stateEnabled.value)
            }
        }
    }
}

@Stable
object PreferenceBool {
    enum class Style {
        Checkbox,
        Switch
    }
}

@Stable
object PreferenceBoolDefaults {
    @Composable
    fun itemSetup() = PreferenceItemSetup(
        trailingContentSize = PreferenceItemSetupDefaults.trailingContentSize(52.dp)
    )
}

Screenshots#

Screenshot Screenshot