diff --git a/CHANGELOG.md b/CHANGELOG.md index 016f885..3e76d5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,8 @@ All notable changes to this project are documented here. The format follows up there (#122). - Completing a task from the list, a widget or another device now cancels its upcoming reminder and clears one already showing (#127, #129). +- The splash screen follows the app's theme, including Black and dynamic + colour, instead of flashing white in dark mode (#119). ## [1.2.0] - 2026-10-26 diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/MainActivity.kt b/app/src/main/java/de/jeanlucmakiola/agendula/MainActivity.kt index 54d439b..0a0abd0 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/MainActivity.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/MainActivity.kt @@ -16,6 +16,7 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.core.net.toUri +import de.jeanlucmakiola.agendula.data.prefs.SettingsPrefs import de.jeanlucmakiola.agendula.data.prefs.is24Hour import de.jeanlucmakiola.agendula.ui.common.LocalFirstDayOfWeek import de.jeanlucmakiola.agendula.ui.common.LocalUse24HourFormat @@ -33,9 +34,12 @@ import de.jeanlucmakiola.agendula.ui.crash.CrashReportActivity import de.jeanlucmakiola.agendula.ui.navigation.NavRequest import de.jeanlucmakiola.agendula.ui.settings.SettingsViewModel import de.jeanlucmakiola.agendula.ui.theme.AgendulaTheme +import de.jeanlucmakiola.agendula.ui.theme.SystemTheme import de.jeanlucmakiola.floret.crash.CrashReportDialog import de.jeanlucmakiola.floret.crash.CrashReporter import de.jeanlucmakiola.floret.crash.submitCrashReport +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import javax.inject.Inject @@ -49,6 +53,8 @@ class MainActivity : ComponentActivity() { @Inject lateinit var launchHooks: Set<@JvmSuppressWildcards LaunchHook> + @Inject lateinit var settingsPrefs: SettingsPrefs + // A captured crash report awaiting the user's decision, surfaced as a dialog // over the app on the next launch (the single-crash path). A startup // crash-loop is handled out of band, before setContent — see below. @@ -72,6 +78,13 @@ class MainActivity : ComponentActivity() { enableEdgeToEdge() AppShortcuts.publish(this) + // The stored setting, never the view model's placeholder: a night mode + // that flips back and forth would recreate the activity each time. + lifecycleScope.launch { + settingsPrefs.settings.map { it.themeMode to it.dynamicColor }.distinctUntilChanged() + .collect { (mode, dynamicColor) -> SystemTheme.apply(this@MainActivity, mode, dynamicColor) } + } + // Only on a fresh launch: after a rotation or process restore the intent // is the same old one and the back stack already holds its destination. val fromHistory = intent.flags and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY != 0 diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/theme/SystemTheme.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/theme/SystemTheme.kt new file mode 100644 index 0000000..963a52a --- /dev/null +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/theme/SystemTheme.kt @@ -0,0 +1,44 @@ +package de.jeanlucmakiola.agendula.ui.theme + +import android.app.Activity +import android.app.UiModeManager +import android.os.Build +import androidx.annotation.StyleRes +import de.jeanlucmakiola.agendula.R +import de.jeanlucmakiola.agendula.data.prefs.ThemeMode + +/** + * Hands the app's theme setting to the system, which draws the launch window + * and splash before any of the app runs (#119). The system keeps both across + * launches, so the next start already matches. + */ +object SystemTheme { + + /** The light or dark the app's resources follow, as [UiModeManager.setApplicationNightMode] takes it. */ + fun nightModeOf(mode: ThemeMode): Int = when (mode) { + ThemeMode.SYSTEM -> UiModeManager.MODE_NIGHT_AUTO + ThemeMode.LIGHT -> UiModeManager.MODE_NIGHT_NO + ThemeMode.DARK, ThemeMode.BLACK -> UiModeManager.MODE_NIGHT_YES + } + + /** The splash theme whose background is the one the app opens on. */ + @StyleRes + fun splashThemeOf(mode: ThemeMode, dynamicColor: Boolean): Int = when { + mode.isPureBlack -> R.style.Theme_Agendula_Black + dynamicColor -> R.style.Theme_Agendula + else -> R.style.Theme_Agendula_Fallback + } + + /** + * Applies [mode] and [dynamicColor]. A night mode the activity does not + * have yet recreates it, once; the same one again changes nothing. + */ + fun apply(activity: Activity, mode: ThemeMode, dynamicColor: Boolean) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + activity.getSystemService(UiModeManager::class.java)?.setApplicationNightMode(nightModeOf(mode)) + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + activity.splashScreen.setSplashScreenTheme(splashThemeOf(mode, dynamicColor)) + } + } +} diff --git a/app/src/main/res/values-night-v31/colors.xml b/app/src/main/res/values-night-v31/colors.xml new file mode 100644 index 0000000..4e73890 --- /dev/null +++ b/app/src/main/res/values-night-v31/colors.xml @@ -0,0 +1,4 @@ + + + @android:color/system_neutral2_900 + diff --git a/app/src/main/res/values-night-v34/colors.xml b/app/src/main/res/values-night-v34/colors.xml new file mode 100644 index 0000000..c157c54 --- /dev/null +++ b/app/src/main/res/values-night-v34/colors.xml @@ -0,0 +1,3 @@ + + @android:color/system_background_dark + diff --git a/app/src/main/res/values-night/bools.xml b/app/src/main/res/values-night/bools.xml new file mode 100644 index 0000000..8497ec7 --- /dev/null +++ b/app/src/main/res/values-night/bools.xml @@ -0,0 +1,3 @@ + + false + diff --git a/app/src/main/res/values-night/colors.xml b/app/src/main/res/values-night/colors.xml new file mode 100644 index 0000000..2c9d306 --- /dev/null +++ b/app/src/main/res/values-night/colors.xml @@ -0,0 +1,3 @@ + + #FF161113 + diff --git a/app/src/main/res/values-v31/colors.xml b/app/src/main/res/values-v31/colors.xml new file mode 100644 index 0000000..29bb018 --- /dev/null +++ b/app/src/main/res/values-v31/colors.xml @@ -0,0 +1,4 @@ + + + @android:color/system_neutral2_10 + diff --git a/app/src/main/res/values-v34/colors.xml b/app/src/main/res/values-v34/colors.xml new file mode 100644 index 0000000..53d58ea --- /dev/null +++ b/app/src/main/res/values-v34/colors.xml @@ -0,0 +1,3 @@ + + @android:color/system_background_light + diff --git a/app/src/main/res/values/bools.xml b/app/src/main/res/values/bools.xml new file mode 100644 index 0000000..0656e05 --- /dev/null +++ b/app/src/main/res/values/bools.xml @@ -0,0 +1,3 @@ + + true + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 19d27a8..0f937f6 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -2,4 +2,8 @@ #FF7A5C6B + + #FFFEFBFC + + @color/window_background_fallback diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index 49cb7b6..adf75b6 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -1,7 +1,19 @@ - + + + + + diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/ui/theme/SystemThemeTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/ui/theme/SystemThemeTest.kt new file mode 100644 index 0000000..584b0b9 --- /dev/null +++ b/app/src/test/java/de/jeanlucmakiola/agendula/ui/theme/SystemThemeTest.kt @@ -0,0 +1,26 @@ +package de.jeanlucmakiola.agendula.ui.theme + +import android.app.UiModeManager +import com.google.common.truth.Truth.assertThat +import de.jeanlucmakiola.agendula.R +import de.jeanlucmakiola.agendula.data.prefs.ThemeMode +import org.junit.jupiter.api.Test + +class SystemThemeTest { + + @Test + fun `the system follows the app's light or dark`() { + assertThat(SystemTheme.nightModeOf(ThemeMode.SYSTEM)).isEqualTo(UiModeManager.MODE_NIGHT_AUTO) + assertThat(SystemTheme.nightModeOf(ThemeMode.LIGHT)).isEqualTo(UiModeManager.MODE_NIGHT_NO) + assertThat(SystemTheme.nightModeOf(ThemeMode.DARK)).isEqualTo(UiModeManager.MODE_NIGHT_YES) + assertThat(SystemTheme.nightModeOf(ThemeMode.BLACK)).isEqualTo(UiModeManager.MODE_NIGHT_YES) + } + + @Test + fun `the splash opens on the background the app draws`() { + assertThat(SystemTheme.splashThemeOf(ThemeMode.SYSTEM, dynamicColor = true)).isEqualTo(R.style.Theme_Agendula) + assertThat(SystemTheme.splashThemeOf(ThemeMode.DARK, dynamicColor = false)).isEqualTo(R.style.Theme_Agendula_Fallback) + assertThat(SystemTheme.splashThemeOf(ThemeMode.BLACK, dynamicColor = true)).isEqualTo(R.style.Theme_Agendula_Black) + assertThat(SystemTheme.splashThemeOf(ThemeMode.BLACK, dynamicColor = false)).isEqualTo(R.style.Theme_Agendula_Black) + } +} diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/ui/theme/WindowColorsTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/ui/theme/WindowColorsTest.kt new file mode 100644 index 0000000..8c40484 --- /dev/null +++ b/app/src/test/java/de/jeanlucmakiola/agendula/ui/theme/WindowColorsTest.kt @@ -0,0 +1,27 @@ +package de.jeanlucmakiola.agendula.ui.theme + +import androidx.compose.ui.graphics.toArgb +import com.google.common.truth.Truth.assertThat +import org.junit.jupiter.api.Test +import java.io.File +import javax.xml.parsers.DocumentBuilderFactory + +/** The launch window's colour has to be the background Compose then draws, or the splash flashes. */ +class WindowColorsTest { + + private fun fallbackIn(qualifier: String): Int { + val doc = DocumentBuilderFactory.newInstance().newDocumentBuilder() + .parse(File("src/main/res/$qualifier/colors.xml")) + val colors = doc.getElementsByTagName("color") + val hex = (0 until colors.length).map { colors.item(it) } + .single { it.attributes.getNamedItem("name").nodeValue == "window_background_fallback" } + .textContent.trim().removePrefix("#") + return hex.toLong(16).toInt() + } + + @Test + fun `the window matches the fallback schemes`() { + assertThat(fallbackIn("values")).isEqualTo(AgendulaLightFallback.background.toArgb()) + assertThat(fallbackIn("values-night")).isEqualTo(AgendulaDarkFallback.background.toArgb()) + } +}