Skip to content

SwitchLoadingPreference

SwitchLoadingPreference is a switch preference row with a loading state, mirroring ColorOS's COUISwitchLoadingPreference. It looks like a SwitchPreference, but while isLoading is true the switch thumb shows the COUI loading spinner and neither the row nor the switch can be toggled. It is meant for settings that take effect asynchronously (e.g. toggling a network feature).

Import

kotlin
import io.github.suqi8.coui.kmp.preference.SwitchLoadingPreference

Basic Usage

The typical flow: the user taps the row, onCheckedChange fires, the caller turns the loading state on, performs the asynchronous work, then updates checked and turns the loading state off.

kotlin
var checked by remember { mutableStateOf(false) }
var target by remember { mutableStateOf(false) }
var isLoading by remember { mutableStateOf(false) }

LaunchedEffect(isLoading) {
    if (isLoading) {
        delay(1500) // Simulate asynchronous work
        checked = target
        isLoading = false
    }
}

SwitchLoadingPreference(
    checked = checked,
    onCheckedChange = {
        target = it
        isLoading = true
    },
    title = "Async Switch",
    summary = "Applies the change after a short delay",
    isLoading = isLoading
)

Component States

Loading State

While loading, clicks on the row and the switch are ignored (COUISwitch swallows touch while its loading style is active):

kotlin
SwitchLoadingPreference(
    checked = true,
    onCheckedChange = {},
    title = "Loading Switch",
    isLoading = true
)

Disabled State

kotlin
SwitchLoadingPreference(
    checked = true,
    onCheckedChange = {},
    title = "Disabled Switch",
    enabled = false
)

Properties

SwitchLoadingPreference Properties

Property NameTypeDescriptionDefault ValueRequired
checkedBooleanSwitch checked state-Yes
onCheckedChange(Boolean) -> UnitSwitch state change callback (not invoked while loading)-Yes
titleStringPreference title-Yes
modifierModifierComponent modifierModifierNo
isLoadingBooleanWhether the switch shows the loading spinner; the row cannot be toggled while loadingfalseNo
titleColorBasicComponentColorsTitle text color configurationBasicComponentDefaults.titleColor()No
summaryString?Preference summarynullNo
summaryColorBasicComponentColorsSummary text color configurationBasicComponentDefaults.summaryColor()No
startAction@Composable (() -> Unit)?Custom start side contentnullNo
endActions@Composable RowScope.() -> UnitCustom end side content (before the switch){}No
bottomAction@Composable (() -> Unit)?Custom bottom side contentnullNo
switchColorsSwitchColorsSwitch control color configurationSwitchDefaults.switchColors()No
insideMarginPaddingValuesComponent internal content paddingBasicComponentDefaults.InsideMarginNo
cardListPositionCardListPositionRow position inside its card group; rounded outer edges gain extra paddingCardListPosition.NoneNo
holdDownStateBooleanWhether the component is held downfalseNo
enabledBooleanComponent interactive statetrueNo

Advanced Usage

With ViewModel-Driven Async Toggle

kotlin
SwitchLoadingPreference(
    checked = uiState.wifiEnabled,
    onCheckedChange = { viewModel.setWifiEnabled(it) }, // Flips uiState.isApplying while working
    title = "WiFi",
    summary = "Turn on to connect to wireless networks",
    isLoading = uiState.isApplying
)

With End Actions

kotlin
SwitchLoadingPreference(
    checked = checked,
    onCheckedChange = { /* start async work */ },
    title = "Sync",
    isLoading = isLoading,
    endActions = {
        Text(
            text = if (isLoading) "Applying…" else "",
            color = COUITheme.colorScheme.onSurfaceVariantActions,
            modifier = Modifier.padding(end = 6.dp)
        )
    }
)

Changelog

Released under the Apache-2.0 License