feat(#232): dynamic light/dark theme switch without restart
This commit is contained in:
1 parent
7ce19cd52c
commit
78da6af6be
3 files changed
+10
-5
No files matched your search
@@ -7,6 +7,8 @@ import androidx.activity.enableEdgeToEdge
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import com.m57.hermescontrol.data.local.AuthManager
|
||||
import com.m57.hermescontrol.notification.NotificationReplyReceiver
|
||||
@@ -21,11 +23,8 @@ class MainActivity : ComponentActivity() {
|
||||
|
||||
enableEdgeToEdge()
|
||||
setContent {
|
||||
// B6 (Jun 18 2026, kanban t_86e9be9b): read the stored theme
|
||||
// preference at startup so HermesControlTheme actually applies
|
||||
// the user's SYSTEM/LIGHT/DARK choice rather than defaulting to
|
||||
// SYSTEM on every cold start.
|
||||
HermesControlTheme(themePreference = AuthManager.getThemePreference()) {
|
||||
val themePreference by AuthManager.themePreferenceFlow.collectAsState()
|
||||
HermesControlTheme(themePreference = themePreference) {
|
||||
Surface(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
color = MaterialTheme.colorScheme.background,
|
||||
|
||||
@@ -41,6 +41,9 @@ object AuthManager {
|
||||
private val _bottomNavItemsFlow = MutableStateFlow<List<String>>(emptyList())
|
||||
val bottomNavItemsFlow: StateFlow<List<String>> = _bottomNavItemsFlow.asStateFlow()
|
||||
|
||||
private val _themePreferenceFlow = MutableStateFlow<ThemePreference>(ThemePreference.SYSTEM)
|
||||
val themePreferenceFlow: StateFlow<ThemePreference> = _themePreferenceFlow.asStateFlow()
|
||||
|
||||
private val gson = com.google.gson.Gson()
|
||||
|
||||
/**
|
||||
@@ -67,6 +70,7 @@ object AuthManager {
|
||||
)
|
||||
|
||||
_bottomNavItemsFlow.value = getBottomNavItems()
|
||||
_themePreferenceFlow.value = getThemePreference()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,6 +210,7 @@ object AuthManager {
|
||||
|
||||
fun setThemePreference(theme: ThemePreference) {
|
||||
requirePrefs().edit().putString(KEY_THEME_PREFERENCE, theme.name).apply()
|
||||
_themePreferenceFlow.value = theme
|
||||
}
|
||||
|
||||
/** Convenience: build the base URL from current host + port.
|
||||
|
||||
@@ -119,6 +119,7 @@ class SettingsViewModel : ViewModel() {
|
||||
|
||||
fun onThemeChange(theme: ThemePreference) {
|
||||
_uiState.update { it.copy(themePreference = theme, isSaved = false) }
|
||||
AuthManager.setThemePreference(theme)
|
||||
}
|
||||
|
||||
fun onTypingEffectEnabledChange(enabled: Boolean) {
|
||||
|
||||
Reference in new issue
Block a user