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

View File

@@ -20,6 +20,7 @@ class PreferencesManager(private val context: Context) {
// Onboarding & Theme // Onboarding & Theme
val HAS_SEEN_ONBOARDING = booleanPreferencesKey("has_seen_onboarding") val HAS_SEEN_ONBOARDING = booleanPreferencesKey("has_seen_onboarding")
val IS_DARK_THEME = booleanPreferencesKey("is_dark_theme") val IS_DARK_THEME = booleanPreferencesKey("is_dark_theme")
val THEME_MODE = stringPreferencesKey("theme_mode") // "light", "dark", "auto"
// Notifications // Notifications
val NOTIFICATIONS_ENABLED = booleanPreferencesKey("notifications_enabled") 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 } 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 // 🔔 NOTIFICATIONS
// ═════════════════════════════════════════════════════════════ // ═════════════════════════════════════════════════════════════

View File

@@ -29,16 +29,17 @@ import com.rosetta.messenger.ui.components.AppleEmojiText
@Composable @Composable
fun ThemeScreen( fun ThemeScreen(
isDarkTheme: Boolean, isDarkTheme: Boolean,
currentThemeMode: String,
onBack: () -> Unit, onBack: () -> Unit,
onThemeChange: (Boolean) -> Unit onThemeModeChange: (String) -> Unit
) { ) {
val backgroundColor = if (isDarkTheme) Color(0xFF1A1A1A) else Color(0xFFFFFFFF) val backgroundColor = if (isDarkTheme) Color(0xFF1A1A1A) else Color(0xFFFFFFFF)
val surfaceColor = if (isDarkTheme) Color(0xFF2C2C2E) else Color(0xFFF2F2F7) val surfaceColor = if (isDarkTheme) Color(0xFF2C2C2E) else Color(0xFFF2F2F7)
val textColor = if (isDarkTheme) Color.White else Color.Black val textColor = if (isDarkTheme) Color.White else Color.Black
val secondaryTextColor = if (isDarkTheme) Color(0xFF8E8E93) else Color(0xFF666666) val secondaryTextColor = if (isDarkTheme) Color(0xFF8E8E93) else Color(0xFF666666)
// Theme mode: "light", "dark", "auto" // Theme mode: "light", "dark", "auto"
var themeMode by remember { mutableStateOf(if (isDarkTheme) "dark" else "light") } var themeMode by remember { mutableStateOf(currentThemeMode) }
// Handle back gesture // Handle back gesture
BackHandler { onBack() } BackHandler { onBack() }
@@ -105,7 +106,7 @@ fun ThemeScreen(
onClick = { onClick = {
if (themeMode != "light") { if (themeMode != "light") {
themeMode = "light" themeMode = "light"
onThemeChange(false) onThemeModeChange("light")
} }
}, },
textColor = textColor, textColor = textColor,
@@ -113,7 +114,7 @@ fun ThemeScreen(
showDivider = true, showDivider = true,
isDarkTheme = isDarkTheme isDarkTheme = isDarkTheme
) )
TelegramThemeOption( TelegramThemeOption(
icon = TablerIcons.Moon, icon = TablerIcons.Moon,
title = "Dark", title = "Dark",
@@ -121,7 +122,7 @@ fun ThemeScreen(
onClick = { onClick = {
if (themeMode != "dark") { if (themeMode != "dark") {
themeMode = "dark" themeMode = "dark"
onThemeChange(true) onThemeModeChange("dark")
} }
}, },
textColor = textColor, textColor = textColor,
@@ -129,14 +130,16 @@ fun ThemeScreen(
showDivider = true, showDivider = true,
isDarkTheme = isDarkTheme isDarkTheme = isDarkTheme
) )
TelegramThemeOption( TelegramThemeOption(
icon = TablerIcons.DeviceMobile, icon = TablerIcons.DeviceMobile,
title = "System", title = "System",
isSelected = themeMode == "auto", isSelected = themeMode == "auto",
onClick = { onClick = {
themeMode = "auto" if (themeMode != "auto") {
// For now, we'll just keep the current theme themeMode = "auto"
onThemeModeChange("auto")
}
}, },
textColor = textColor, textColor = textColor,
secondaryTextColor = secondaryTextColor, secondaryTextColor = secondaryTextColor,