Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply user dark mode preference to splashscreen #1718

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ dependencies {
implementation(projects.sync.work)

implementation(libs.androidx.activity.compose)
implementation(libs.androidx.appcompat)
implementation(libs.androidx.compose.material3.adaptive)
implementation(libs.androidx.compose.material3.adaptive.layout)
implementation(libs.androidx.compose.material3.adaptive.navigation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@

package com.google.samples.apps.nowinandroid

import android.app.UiModeManager
import android.content.Context
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand Down Expand Up @@ -86,6 +90,7 @@ class MainActivity : ComponentActivity() {
darkTheme = resources.configuration.isSystemInDarkTheme,
androidTheme = Loading.shouldUseAndroidTheme,
disableDynamicTheming = Loading.shouldDisableDynamicTheming,
shouldFollowSystemTheme = Loading.shouldFollowSystemTheme,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused why should Follow System Theme need in here? Wouldn't the System Follow Dark Theme being considered before in isSystemInDarkTheme logic?

),
)

Expand All @@ -100,6 +105,7 @@ class MainActivity : ComponentActivity() {
darkTheme = uiState.shouldUseDarkTheme(systemDark),
androidTheme = uiState.shouldUseAndroidTheme,
disableDynamicTheming = uiState.shouldDisableDynamicTheming,
shouldFollowSystemTheme = uiState.shouldFollowSystemTheme,
)
}
.onEach { themeSettings = it }
Expand All @@ -122,6 +128,10 @@ class MainActivity : ComponentActivity() {
darkScrim = darkScrim,
) { darkTheme },
)
setAppTheme(
uiModeManager = getSystemService(Context.UI_MODE_SERVICE) as UiModeManager,
themeSettings = themeSettings,
)
}
}
}
Expand Down Expand Up @@ -167,6 +177,30 @@ class MainActivity : ComponentActivity() {
}
}

/**
* Sets app theme to reflect user choice.
*/
private fun setAppTheme(
uiModeManager: UiModeManager,
themeSettings: ThemeSettings,
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val mode = when {
themeSettings.shouldFollowSystemTheme -> UiModeManager.MODE_NIGHT_AUTO
themeSettings.darkTheme -> UiModeManager.MODE_NIGHT_YES
else -> UiModeManager.MODE_NIGHT_NO
}
uiModeManager.setApplicationNightMode(mode)
} else {
val mode = when {
themeSettings.shouldFollowSystemTheme -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
themeSettings.darkTheme -> AppCompatDelegate.MODE_NIGHT_YES
else -> AppCompatDelegate.MODE_NIGHT_NO
}
AppCompatDelegate.setDefaultNightMode(mode)
}
}

/**
* The default light scrim, as defined by androidx and the platform:
* https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:activity/activity/src/main/java/androidx/activity/EdgeToEdge.kt;l=35-38;drc=27e7d52e8604a080133e8b842db10c89b4482598
Expand All @@ -187,4 +221,5 @@ data class ThemeSettings(
val darkTheme: Boolean,
val androidTheme: Boolean,
val disableDynamicTheming: Boolean,
val shouldFollowSystemTheme: Boolean,
)
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ sealed interface MainActivityUiState {
DarkThemeConfig.LIGHT -> false
DarkThemeConfig.DARK -> true
}

override val shouldFollowSystemTheme =
userData.darkThemeConfig == DarkThemeConfig.FOLLOW_SYSTEM
}

/**
Expand All @@ -82,4 +85,9 @@ sealed interface MainActivityUiState {
* Returns `true` if dark theme should be used.
*/
fun shouldUseDarkTheme(isSystemDarkTheme: Boolean) = isSystemDarkTheme

/**
* Returns `true` if app theme should follow system theme.
*/
val shouldFollowSystemTheme: Boolean get() = true
}
Loading