Skip to content

Commit

Permalink
Update with dark / light mode
Browse files Browse the repository at this point in the history
  • Loading branch information
alorma committed Jan 5, 2025
1 parent 882903a commit e942871
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
3 changes: 3 additions & 0 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ kotlin {
implementation(projects.uiTiles)
implementation(projects.uiTilesExtended)

implementation(projects.storageMemory)
implementation(projects.storageDisk)

implementation(libs.kotlinx.immutable)
}

Expand Down
18 changes: 15 additions & 3 deletions composeApp/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
Expand All @@ -22,6 +23,7 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.alorma.compose.settings.storage.disk.rememberBooleanSettingState
import kotlinx.coroutines.launch
import theme.ComposeSettingsTheme

Expand All @@ -34,14 +36,24 @@ fun App(
) {
val windowSizeClass = calculateWindowSizeClass()

ComposeSettingsTheme {
val darkModeState = rememberBooleanSettingState(
key = "darkMode",
defaultValue = isSystemInDarkTheme(),
)

ComposeSettingsTheme(
darkModeState = darkModeState,
) {
Scaffold(
modifier = Modifier.fillMaxSize().then(modifier),
topBar = { SampleTopBar() },
) {
when (windowSizeClass.widthSizeClass) {
WindowWidthSizeClass.Compact, WindowWidthSizeClass.Medium -> {
SettingsScreen(modifier = Modifier.padding(it))
SettingsScreen(
modifier = Modifier.padding(it),
darkModeState = darkModeState,
)
}

else -> {
Expand All @@ -52,7 +64,7 @@ fun App(
modifier = Modifier.padding(it),
drawerState = drawerState,
drawerContent = {
DismissibleDrawerSheet { SettingsScreen() }
DismissibleDrawerSheet { SettingsScreen(darkModeState = darkModeState) }
},
content = {
Surface {
Expand Down
22 changes: 21 additions & 1 deletion composeApp/src/commonMain/kotlin/SettingsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.alorma.compose.settings.storage.disk.BooleanSettingValueState
import com.alorma.compose.settings.ui.SettingsCheckbox
import com.alorma.compose.settings.ui.SettingsMenuLink
import com.alorma.compose.settings.ui.SettingsRadioButton
Expand All @@ -34,7 +35,10 @@ import internal.iconSampleOrNull
import kotlinx.collections.immutable.toImmutableList

@Composable
fun SettingsScreen(modifier: Modifier = Modifier) {
fun SettingsScreen(
modifier: Modifier = Modifier,
darkModeState: BooleanSettingValueState,
) {
Scaffold(
modifier = modifier,
) { padding ->
Expand All @@ -51,6 +55,22 @@ fun SettingsScreen(modifier: Modifier = Modifier) {
title = { Text(text = "Show icon") },
onCheckedChange = { iconState.value = it },
)

val darkModes = listOf(true, false)

SettingsSegmented(
title = { Text("Theme mode") },
items = darkModes,
selectedItem = darkModeState.value,
onItemSelected = { darkMode -> darkModeState.value = darkMode },
itemTitleMap = { item ->
when (item) {
true -> "Dark"
false -> "Light"
}
},
)

SettingsSwitchSampleSection(iconState.value)
SettingsCheckboxSampleSection(iconState.value)
SettingsTriStateCheckboxSampleSection(iconState.value)
Expand Down
7 changes: 5 additions & 2 deletions composeApp/src/commonMain/kotlin/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import com.alorma.compose.settings.storage.base.SettingValueState
import com.alorma.compose.settings.storage.disk.rememberBooleanSettingState
import com.alorma.compose.settings.storage.memory.rememberMemoryBooleanSettingState

val DarkColorScheme = darkColorScheme(
primary = Purple40,
Expand All @@ -20,12 +23,12 @@ val LightColorScheme = lightColorScheme(

@Composable
fun ComposeSettingsTheme(
isSystemDark: Boolean = isSystemInDarkTheme(),
darkModeState: SettingValueState<Boolean> = rememberMemoryBooleanSettingState(),
content: @Composable () -> Unit
) {

MaterialTheme(
colorScheme = if (isSystemDark) {
colorScheme = if (darkModeState.value) {
DarkColorScheme
} else {
LightColorScheme
Expand Down

0 comments on commit e942871

Please sign in to comment.