fix: fix theme switch

This commit is contained in:
k1ngsterr1
2026-02-03 16:06:53 +05:00
parent fa22c2dccd
commit af4df0a353
3 changed files with 42 additions and 14 deletions

View File

@@ -12,6 +12,7 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.animation.*
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Surface
@@ -129,7 +130,14 @@ class MainActivity : FragmentActivity() {
}
val scope = rememberCoroutineScope()
val isDarkTheme by preferencesManager.isDarkTheme.collectAsState(initial = true)
val themeMode by preferencesManager.themeMode.collectAsState(initial = "dark")
val systemInDarkTheme = isSystemInDarkTheme()
val isDarkTheme = when (themeMode) {
"light" -> false
"dark" -> true
"auto" -> systemInDarkTheme
else -> true
}
val isLoggedIn by accountManager.isLoggedIn.collectAsState(initial = null)
var showSplash by remember { mutableStateOf(true) }
var showOnboarding by remember { mutableStateOf(true) }
@@ -296,11 +304,17 @@ class MainActivity : FragmentActivity() {
MainScreen(
account = currentAccount,
isDarkTheme = isDarkTheme,
themeMode = themeMode,
onToggleTheme = {
scope.launch {
preferencesManager.setDarkTheme(!isDarkTheme)
}
},
onThemeModeChange = { mode ->
scope.launch {
preferencesManager.setThemeMode(mode)
}
},
onLogout = {
// Set currentAccount to null immediately to prevent UI
// lag
@@ -461,7 +475,9 @@ class MainActivity : FragmentActivity() {
fun MainScreen(
account: DecryptedAccount? = null,
isDarkTheme: Boolean = true,
themeMode: String = "dark",
onToggleTheme: () -> Unit = {},
onThemeModeChange: (String) -> Unit = {},
onLogout: () -> Unit = {},
onAccountInfoUpdated: suspend () -> Unit = {}
) {
@@ -667,13 +683,12 @@ fun MainScreen(
if (showThemeScreen) {
ThemeScreen(
isDarkTheme = isDarkTheme,
currentThemeMode = themeMode,
onBack = {
showThemeScreen = false
showProfileScreen = true
},
onThemeChange = { isDark ->
onToggleTheme()
}
onThemeModeChange = onThemeModeChange
)
}
}

View File

@@ -20,6 +20,7 @@ class PreferencesManager(private val context: Context) {
// Onboarding & Theme
val HAS_SEEN_ONBOARDING = booleanPreferencesKey("has_seen_onboarding")
val IS_DARK_THEME = booleanPreferencesKey("is_dark_theme")
val THEME_MODE = stringPreferencesKey("theme_mode") // "light", "dark", "auto"
// Notifications
val NOTIFICATIONS_ENABLED = booleanPreferencesKey("notifications_enabled")
@@ -63,6 +64,15 @@ class PreferencesManager(private val context: Context) {
context.dataStore.edit { preferences -> preferences[IS_DARK_THEME] = value }
}
val themeMode: Flow<String> =
context.dataStore.data.map { preferences ->
preferences[THEME_MODE] ?: "dark" // Default to dark theme
}
suspend fun setThemeMode(value: String) {
context.dataStore.edit { preferences -> preferences[THEME_MODE] = value }
}
// ═════════════════════════════════════════════════════════════
// 🔔 NOTIFICATIONS
// ═════════════════════════════════════════════════════════════

View File

@@ -29,8 +29,9 @@ import com.rosetta.messenger.ui.components.AppleEmojiText
@Composable
fun ThemeScreen(
isDarkTheme: Boolean,
currentThemeMode: String,
onBack: () -> Unit,
onThemeChange: (Boolean) -> Unit
onThemeModeChange: (String) -> Unit
) {
val backgroundColor = if (isDarkTheme) Color(0xFF1A1A1A) else Color(0xFFFFFFFF)
val surfaceColor = if (isDarkTheme) Color(0xFF2C2C2E) else Color(0xFFF2F2F7)
@@ -38,7 +39,7 @@ fun ThemeScreen(
val secondaryTextColor = if (isDarkTheme) Color(0xFF8E8E93) else Color(0xFF666666)
// Theme mode: "light", "dark", "auto"
var themeMode by remember { mutableStateOf(if (isDarkTheme) "dark" else "light") }
var themeMode by remember { mutableStateOf(currentThemeMode) }
// Handle back gesture
BackHandler { onBack() }
@@ -105,7 +106,7 @@ fun ThemeScreen(
onClick = {
if (themeMode != "light") {
themeMode = "light"
onThemeChange(false)
onThemeModeChange("light")
}
},
textColor = textColor,
@@ -121,7 +122,7 @@ fun ThemeScreen(
onClick = {
if (themeMode != "dark") {
themeMode = "dark"
onThemeChange(true)
onThemeModeChange("dark")
}
},
textColor = textColor,
@@ -135,8 +136,10 @@ fun ThemeScreen(
title = "System",
isSelected = themeMode == "auto",
onClick = {
themeMode = "auto"
// For now, we'll just keep the current theme
if (themeMode != "auto") {
themeMode = "auto"
onThemeModeChange("auto")
}
},
textColor = textColor,
secondaryTextColor = secondaryTextColor,