Update Android light/dark mode system bar theme (#520)

This commit is contained in:
Leendert de Borst
2025-10-21 22:33:15 +02:00
parent 646416c069
commit af06bbfd12
3 changed files with 96 additions and 1 deletions

View File

@@ -1,5 +1,10 @@
package net.aliasvault.app
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.WindowInsetsController
import androidx.core.content.ContextCompat
import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
@@ -11,6 +16,10 @@ import expo.modules.splashscreen.SplashScreenManager
* The main activity of the app.
*/
class MainActivity : ReactActivity() {
companion object {
private const val TAG = "MainActivity"
}
/**
* Called when the activity is created.
*/
@@ -24,6 +33,77 @@ class MainActivity : ReactActivity() {
// @generated end expo-splashscreen
super.onCreate(null)
// Configure system bars based on dark mode
configureSystemBars()
}
override fun onResume() {
super.onResume()
// Reapply system bar configuration when app resumes
// This ensures our settings persist even if other code tries to override them
configureSystemBars()
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
// Reapply system bar configuration when theme changes
configureSystemBars()
}
/**
* Configure system bars (status bar and navigation bar) colors and appearance
* based on current theme (light/dark mode)
*/
private fun configureSystemBars() {
val isDarkMode = (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
Log.d(TAG, "Configuring system bars - isDarkMode: $isDarkMode")
window.apply {
if (isDarkMode) {
// Dark mode: black background with light icons
val bgColor = ContextCompat.getColor(context, R.color.av_background)
Log.d(TAG, "Setting dark mode colors - bgColor: ${String.format("#%06X", 0xFFFFFF and bgColor)}")
statusBarColor = bgColor
navigationBarColor = bgColor
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// Android 11+ (API 30+): Use WindowInsetsController
insetsController?.apply {
setSystemBarsAppearance(
0, // Light icons/text
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS or WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS,
)
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Android 8-10 (API 26-29): Use deprecated flags
@Suppress("DEPRECATION")
decorView.systemUiVisibility = decorView.systemUiVisibility and
android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv() and
android.view.View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR.inv()
}
} else {
// Light mode: light gray background with dark icons
statusBarColor = ContextCompat.getColor(context, R.color.av_background)
navigationBarColor = ContextCompat.getColor(context, R.color.av_background)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// Android 11+ (API 30+): Use WindowInsetsController
insetsController?.apply {
setSystemBarsAppearance(
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS or WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS,
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS or WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS,
)
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Android 8-10 (API 26-29): Use deprecated flags
@Suppress("DEPRECATION")
decorView.systemUiVisibility = decorView.systemUiVisibility or
android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or
android.view.View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
}
}
}
}
/**

View File

@@ -1,4 +1,16 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Dark mode overrides for main app theme -->
<style name="AppTheme" parent="Theme.EdgeToEdge">
<item name="android:statusBarColor">@color/av_background</item>
<item name="android:navigationBarColor">@color/av_background</item>
<!-- Force opaque navigation bar (override EdgeToEdge transparency) -->
<item name="android:enforceNavigationBarContrast">false</item>
<item name="android:windowTranslucentNavigation">false</item>
<!-- Dark mode: use light icons/text on dark background -->
<item name="android:windowLightStatusBar">false</item>
<item name="android:windowLightNavigationBar">false</item>
</style>
<style name="PasskeyRegistrationTheme" parent="Theme.Material3.DayNight.NoActionBar">
<item name="colorPrimary">@color/av_primary</item>
<item name="colorOnPrimary">#FFFFFF</item>

View File

@@ -3,9 +3,12 @@
<item name="android:textColor">@android:color/black</item>
<item name="android:editTextStyle">@style/ResetEditText</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="android:statusBarColor">#fff</item>
<item name="android:statusBarColor">@color/av_background</item>
<item name="android:navigationBarColor">#fff</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<!-- Light mode: use dark icons/text on light background -->
<item name="android:windowLightStatusBar">true</item>
<item name="android:windowLightNavigationBar">true</item>
</style>
<style name="ResetEditText" parent="@android:style/Widget.EditText">
<item name="android:padding">0dp</item>