This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ThemeViewModel: ViewModel() { | |
var isDarkThemeEnabled = mutableStateOf(false) | |
private set | |
fun setTheme(isDarkTheme: Boolean) { | |
isDarkThemeEnabled.value = isDarkTheme | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : ComponentActivity() { | |
private val themeViewModel: ThemeViewModel by viewModels() | |
private lateinit var dataStoreUtil: DataStoreUtil | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
dataStoreUtil = DataStoreUtil(applicationContext) | |
//Checks if the system theme is DarkMode or not. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : ComponentActivity() { | |
private val themeViewModel: ThemeViewModel by viewModels() | |
private lateinit var dataStoreUtil: DataStoreUtil | |
override fun onCreate(savedInstanceState: Bundle?) { | |
//... | |
dataStoreUtil = DataStoreUtil(applicationContext) | |
val systemTheme = when (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) { | |
Configuration.UI_MODE_NIGHT_YES -> { true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun DarkThemeScreen( | |
dataStoreUtil: DataStoreUtil, | |
themeViewModel: ThemeViewModel, | |
) { | |
var switchState by remember {themeViewModel.isDarkThemeEnabled } | |
val coroutineScope = rememberCoroutineScope() | |
Scaffold { | |
Switch( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : ComponentActivity() { | |
private lateinit var navController: NavHostController | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
JetpackComposeUIComponentsTheme { | |
navController = rememberNavController() | |
Surface( | |
//... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun MainScreen( | |
navController: NavHostController, | |
) { | |
Scaffold( | |
//... | |
topBar = { | |
TopAppBar( | |
title = { | |
Text(text = "Jetpack Compose") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
navigationIcon = { | |
IconButton( | |
onClick = { coroutineScope.launch { scaffoldState.drawerState.open() } } | |
) { | |
Icon(imageVector = Icons.Rounded.Menu, contentDescription = "Drawer Icon") | |
} | |
}, |