fix: fix theme switch
This commit is contained in:
@@ -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
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
// ═════════════════════════════════════════════════════════════
|
||||
|
||||
@@ -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 = {
|
||||
if (themeMode != "auto") {
|
||||
themeMode = "auto"
|
||||
// For now, we'll just keep the current theme
|
||||
onThemeModeChange("auto")
|
||||
}
|
||||
},
|
||||
textColor = textColor,
|
||||
secondaryTextColor = secondaryTextColor,
|
||||
|
||||
Reference in New Issue
Block a user