[Fix] system theme initial, toggle theme first time

This commit is contained in:
2026-06-17 02:29:21 +07:00
parent 8f97891843
commit 6a5de42b89
4 changed files with 8 additions and 3 deletions
@@ -24,6 +24,7 @@ class AppStoragePlatform(context: Context) : AppStorage {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
override fun <T : Any> load(key: String, type: KClass<T>): T? { override fun <T : Any> load(key: String, type: KClass<T>): T? {
if (!prefs.contains(key)) return null
return when (type) { return when (type) {
String::class -> prefs.getString(key, null) as T? String::class -> prefs.getString(key, null) as T?
Int::class -> prefs.getInt(key, -1) as T? Int::class -> prefs.getInt(key, -1) as T?
@@ -10,8 +10,9 @@ import org.koin.compose.viewmodel.koinViewModel
@Composable @Composable
@Preview @Preview
fun App() { fun App(
val viewModel = koinViewModel<AppViewModel>() viewModel: AppViewModel = koinViewModel()
) {
val savedTheme = viewModel.isDarkTheme.value val savedTheme = viewModel.isDarkTheme.value
val isDarkTheme = savedTheme ?: isSystemInDarkTheme() val isDarkTheme = savedTheme ?: isSystemInDarkTheme()
val isLoggedIn = viewModel.isLoggedIn() val isLoggedIn = viewModel.isLoggedIn()
@@ -12,6 +12,7 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.navigation.NavType import androidx.navigation.NavType
import androidx.navigation.compose.NavHost import androidx.navigation.compose.NavHost
@@ -62,6 +63,7 @@ fun MainScreen(
val navBackStackEntry by navController.currentBackStackEntryAsState() val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination val currentDestination = navBackStackEntry?.destination
val noNavbar = listOf(AuthDestination.route, CommunicationDestination.route) val noNavbar = listOf(AuthDestination.route, CommunicationDestination.route)
val startRoute = rememberSaveable { if (isLoggedIn) HomeDestination.route else AuthDestination.route }
val sessionExpired by viewModel.sessionExpired val sessionExpired by viewModel.sessionExpired
@@ -122,7 +124,7 @@ fun MainScreen(
) { innerPadding -> ) { innerPadding ->
NavHost( NavHost(
navController = navController, navController = navController,
startDestination = if (isLoggedIn) HomeDestination.route else AuthDestination.route, startDestination = startRoute,
modifier = Modifier.padding(innerPadding).fillMaxSize() modifier = Modifier.padding(innerPadding).fillMaxSize()
) { ) {
composable(AuthDestination.route) { LoginScreen(navController) } composable(AuthDestination.route) { LoginScreen(navController) }
@@ -25,6 +25,7 @@ class AppStoragePlatform : AppStorage {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
override fun <T : Any> load(key: String, type: KClass<T>): T? { override fun <T : Any> load(key: String, type: KClass<T>): T? {
if (defaults.objectForKey(key) == null) return null
return when (type) { return when (type) {
String::class -> defaults.stringForKey(key) as T? String::class -> defaults.stringForKey(key) as T?
Int::class -> defaults.integerForKey(key).toInt() as T? Int::class -> defaults.integerForKey(key).toInt() as T?