-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Align checkbox with state of the art * Add checkbox demo * TriState checkbox running * TriState checkbox with inmemory state
- Loading branch information
Showing
7 changed files
with
181 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,4 +41,4 @@ class BooleanSettingValueState( | |
override fun reset() { | ||
value = defaultValue | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...c/commonMain/kotlin/com/alorma/compose/settings/storage/disk/TriStateSettingValueState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.alorma.compose.settings.storage.disk | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import com.alorma.compose.settings.storage.base.SettingValueState | ||
import com.russhwolf.settings.Settings | ||
|
||
@Composable | ||
fun rememberTriStateSetting( | ||
key: String, | ||
defaultValue: Boolean?, | ||
settings: Settings = Settings() | ||
): TriStateSettingValueState { | ||
return remember { | ||
TriStateSettingValueState( | ||
settings = settings, | ||
key = key, | ||
defaultValue = defaultValue, | ||
) | ||
} | ||
} | ||
|
||
class TriStateSettingValueState( | ||
private val settings: Settings, | ||
val key: String, | ||
val defaultValue: Boolean? = null, | ||
) : SettingValueState<Boolean?> { | ||
|
||
private var _value by mutableStateOf(settings.getBooleanOrNull(key)) | ||
|
||
override var value: Boolean? | ||
set(value) { | ||
_value = value | ||
if (value == null) { | ||
settings.remove(key) | ||
} else { | ||
settings.putBoolean(key, value) | ||
} | ||
} | ||
get() = _value | ||
|
||
override fun reset() { | ||
value = defaultValue | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...commonMain/kotlin/com/alorma/compose/settings/storage/memory/TriStateSettingValueState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.alorma.compose.settings.storage.memory | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import com.alorma.compose.settings.storage.base.SettingValueState | ||
|
||
@Composable | ||
fun rememberMemoryTriStateSettingState(defaultValue: Boolean? = null): SettingValueState<Boolean?> { | ||
return remember { InMemoryTriStateSettingValueState(defaultValue) } | ||
} | ||
|
||
internal class InMemoryTriStateSettingValueState(private val defaultValue: Boolean?) : | ||
SettingValueState<Boolean?> { | ||
override var value: Boolean? by mutableStateOf(defaultValue) | ||
override fun reset() { | ||
value = defaultValue | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...tings-ui/src/commonMain/kotlin/com/alorma/compose/settings/ui/SettingsTriStateCheckbox.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.alorma.compose.settings.ui | ||
|
||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.selection.triStateToggleable | ||
import androidx.compose.material3.CheckboxColors | ||
import androidx.compose.material3.CheckboxDefaults | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.material3.TriStateCheckbox | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.semantics.Role | ||
import androidx.compose.ui.state.ToggleableState | ||
import com.alorma.compose.settings.ui.internal.SettingsTileScaffold | ||
|
||
@Composable | ||
fun SettingsTriStateCheckbox( | ||
state: Boolean?, | ||
title: @Composable () -> Unit, | ||
modifier: Modifier = Modifier, | ||
enabled: Boolean = true, | ||
icon: @Composable (() -> Unit)? = null, | ||
subtitle: @Composable (() -> Unit)? = null, | ||
checkboxColors: CheckboxColors = CheckboxDefaults.colors(), | ||
onCheckedChange: (Boolean?) -> Unit = {}, | ||
) { | ||
val update: () -> Unit = { onCheckedChange(state?.not() ?: true) } | ||
Surface { | ||
Row( | ||
modifier = modifier | ||
.fillMaxWidth() | ||
.triStateToggleable( | ||
state = mapNullableBooleanToToggleableState(state), | ||
onClick = update, | ||
enabled = enabled, | ||
role = Role.Checkbox, | ||
), | ||
verticalAlignment = Alignment.CenterVertically, | ||
) { | ||
SettingsTileScaffold( | ||
enabled = enabled, | ||
title = title, | ||
subtitle = subtitle, | ||
icon = icon, | ||
action = { | ||
TriStateCheckbox( | ||
enabled = enabled, | ||
state = mapNullableBooleanToToggleableState(state), | ||
onClick = update, | ||
colors = checkboxColors, | ||
) | ||
}, | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun mapNullableBooleanToToggleableState(state: Boolean?) = when (state) { | ||
true -> ToggleableState.On | ||
false -> ToggleableState.Off | ||
null -> ToggleableState.Indeterminate | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters