Skip to content

Commit

Permalink
Improve sample (#232)
Browse files Browse the repository at this point in the history
* Reset values

* Add resets

* SOme UI

* MOre sample

* MOre sample

* Sample tri state checvkbox with child

* Update with menu link

* Update padding

* Update sample

* Update with split

* Update with show icon

* Android sample to minSdk 26

* Update settings tile

* No custom height on settings group title
  • Loading branch information
alorma committed Mar 2, 2024
1 parent 4ae0322 commit dc1ccf4
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.alorma.compose.settings.storage.base

import kotlin.reflect.KProperty

@Deprecated(message = "To be deleted")
inline operator fun <T> SettingValueState<T>.getValue(thisObj: Any?, property: KProperty<*>): T =
value

@Deprecated(message = "To be deleted")
inline operator fun <T> SettingValueState<T>.setValue(
thisObj: Any?,
property: KProperty<*>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.russhwolf.settings.Settings
@Composable
fun rememberBooleanSettingState(
key: String,
defaultValue: Boolean,
defaultValue: Boolean = false,
settings: Settings = Settings()
): BooleanSettingValueState {
return remember {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.russhwolf.settings.Settings
@Composable
fun rememberTriStateSetting(
key: String,
defaultValue: Boolean?,
defaultValue: Boolean? = null,
settings: Settings = Settings()
): TriStateSettingValueState {
return remember {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ fun SettingsCheckbox(
icon: @Composable (() -> Unit)? = null,
subtitle: @Composable (() -> Unit)? = null,
checkboxColors: CheckboxColors = CheckboxDefaults.colors(),
onCheckedChange: (Boolean) -> Unit = {},
onCheckedChange: (Boolean) -> Unit,
) {
val update: (Boolean) -> Unit = { boolean -> onCheckedChange(boolean) }
Surface {
Row(
modifier = modifier
modifier = Modifier
.fillMaxWidth()
.toggleable(
enabled = enabled,
value = state,
role = Role.Switch,
onValueChange = { update(!state) },
),
).then(modifier),
verticalAlignment = Alignment.CenterVertically,
) {
SettingsTileScaffold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.alorma.compose.settings.ui
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ProvideTextStyle
Expand All @@ -17,12 +17,16 @@ import androidx.compose.ui.unit.dp
@Composable
fun SettingsGroup(
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(0.dp),
title: @Composable (() -> Unit)? = null,
content: @Composable ColumnScope.() -> Unit,
) {
Surface {
Column(
modifier = modifier.fillMaxWidth(),
modifier = Modifier
.fillMaxWidth()
.then(modifier)
.padding(contentPadding),
) {
if (title != null) {
SettingsGroupTitle(title)
Expand All @@ -37,8 +41,7 @@ internal fun SettingsGroupTitle(title: @Composable () -> Unit) {
Box(
modifier = Modifier
.fillMaxWidth()
.height(64.dp)
.padding(horizontal = 16.dp),
.padding(vertical = 8.dp, horizontal = 16.dp),
contentAlignment = Alignment.CenterStart,
) {
val primary = MaterialTheme.colorScheme.primary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ fun SettingsMenuLink(
) {
Surface {
Row(
modifier = modifier.fillMaxWidth()
modifier = Modifier.fillMaxWidth()
.clickable(
enabled = enabled,
onClick = onClick,
),
).then(modifier),
verticalAlignment = Alignment.CenterVertically,
) {
SettingsTileScaffold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ fun SettingsSwitch(
icon: @Composable (() -> Unit)? = null,
subtitle: @Composable (() -> Unit)? = null,
switchColors: SwitchColors = SwitchDefaults.colors(),
onCheckedChange: (Boolean) -> Unit = {},
onCheckedChange: (Boolean) -> Unit,
) {
val update: (Boolean) -> Unit = { boolean -> onCheckedChange(boolean) }
Surface {
Row(
modifier = modifier
modifier = Modifier
.fillMaxWidth()
.toggleable(
enabled = enabled,
value = state,
role = Role.Switch,
onValueChange = { update(!state) },
),
).then(modifier),
verticalAlignment = Alignment.CenterVertically,
) {
SettingsTileScaffold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ fun SettingsTriStateCheckbox(
icon: @Composable (() -> Unit)? = null,
subtitle: @Composable (() -> Unit)? = null,
checkboxColors: CheckboxColors = CheckboxDefaults.colors(),
onCheckedChange: (Boolean?) -> Unit = {},
onCheckedChange: (Boolean) -> Unit = {},
) {
val update: () -> Unit = { onCheckedChange(state?.not() ?: true) }
Surface {
Row(
modifier = modifier
modifier = Modifier
.fillMaxWidth()
.triStateToggleable(
state = mapNullableBooleanToToggleableState(state),
onClick = update,
enabled = enabled,
role = Role.Checkbox,
),
).then(modifier),
verticalAlignment = Alignment.CenterVertically,
) {
SettingsTileScaffold(
Expand Down
2 changes: 1 addition & 1 deletion composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ android {
sourceSets["main"].resources.srcDirs("src/commonMain/resources")

defaultConfig {
minSdk = libs.versions.android.minSdk.get().toInt()
minSdk = libs.versions.android.minSdkSample.get().toInt()
versionCode = 1
versionName = "1.0"
}
Expand Down
18 changes: 2 additions & 16 deletions composeApp/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
import androidx.compose.runtime.Composable
import com.alorma.compose.settings.sample.shared.SettingsScreen
import com.alorma.compose.settings.sample.shared.theme.ComposeSettingsTheme
import com.alorma.compose.settings.storage.base.getValue
import com.alorma.compose.settings.storage.base.setValue
import com.alorma.compose.settings.storage.disk.rememberBooleanSettingState
import com.russhwolf.settings.Settings

@Composable
fun App() {
val settings = Settings()
var darkModeState by rememberBooleanSettingState(
key = "darkMode",
defaultValue = false,
settings = settings,
)
ComposeSettingsTheme(
darkTheme = darkModeState,
) {
SettingsScreen(
settings = settings,
darkTheme = darkModeState,
onDarkThemeChange = { darkModeState = it }
)
ComposeSettingsTheme {
SettingsScreen(settings = settings)
}
}
Loading

0 comments on commit dc1ccf4

Please sign in to comment.