@@ -60,6 +60,7 @@ import androidx.compose.material3.SnackbarHostState
6060import androidx.compose.material3.Text
6161import androidx.compose.material3.TextButton
6262import androidx.compose.runtime.Composable
63+ import androidx.compose.runtime.livedata.observeAsState
6364import androidx.compose.runtime.mutableStateOf
6465import androidx.compose.runtime.remember
6566import androidx.compose.runtime.rememberCoroutineScope
@@ -207,8 +208,11 @@ private fun GeneralOptionsUI(
207208 val context = LocalContext .current
208209 val coroutineScope = rememberCoroutineScope()
209210
210- val googleBooksApiSwitchState = remember { mutableStateOf(viewModel.getUseGoogleApiValue()) }
211- val internalReaderValue = when (viewModel.getInternalReaderValue()) {
211+ val googleBooksApiSwitchState = viewModel.useGoogleApi.observeAsState(initial = true )
212+ val internalReaderState = viewModel.internalReader.observeAsState(initial = true )
213+ val openLibraryAtStartState = viewModel.openLibraryAtStart.observeAsState(initial = false )
214+
215+ val internalReaderValue = when (internalReaderState.value) {
212216 true -> stringResource(id = R .string.reader_option_inbuilt)
213217 false -> stringResource(id = R .string.reader_option_external)
214218 }
@@ -268,7 +272,16 @@ private fun GeneralOptionsUI(
268272 switchState = googleBooksApiSwitchState,
269273 onCheckChange = {
270274 viewModel.setUseGoogleApiValue(it)
271- googleBooksApiSwitchState.value = it
275+ }
276+ )
277+
278+ SettingItemWIthSwitch (
279+ icon = ImageVector .vectorResource(id = R .drawable.ic_settings_library_start),
280+ mainText = stringResource(id = R .string.open_library_at_start_setting),
281+ subText = stringResource(id = R .string.open_library_at_start_setting_desc),
282+ switchState = openLibraryAtStartState,
283+ onCheckChange = {
284+ viewModel.setOpenLibraryAtStartValue(it)
272285 }
273286 )
274287 }
@@ -358,31 +371,33 @@ private fun DisplayOptionsUI(
358371 val coroutineScope = rememberCoroutineScope()
359372
360373 // Display settings for the theme
361- val displayValue =
362- when (viewModel.getThemeValue() ) {
363- ThemeMode .Light .ordinal -> stringResource(id = R .string.theme_option_light)
364- ThemeMode .Dark .ordinal -> stringResource(id = R .string.theme_option_dark)
365- else -> stringResource(id = R .string.theme_option_system)
366- }
367- val displayDialog = remember { mutableStateOf(false ) }
368- val radioOptions = listOf (
374+ val appThemeState = viewModel.theme.observeAsState(initial = ThemeMode . Auto )
375+ val selectedAppTheme = when (appThemeState.value ) {
376+ ThemeMode .Light -> stringResource(id = R .string.theme_option_light)
377+ ThemeMode .Dark -> stringResource(id = R .string.theme_option_dark)
378+ else -> stringResource(id = R .string.theme_option_system)
379+ }
380+ val appThemeDialog = remember { mutableStateOf(false ) }
381+ val appThemeRadioOpts = listOf (
369382 stringResource(id = R .string.theme_option_light),
370383 stringResource(id = R .string.theme_option_dark),
371384 stringResource(id = R .string.theme_option_system)
372385 )
373- val (selectedOption, onOptionSelected) = remember { mutableStateOf(displayValue ) }
386+ val (selectedOption, onOptionSelected) = remember { mutableStateOf(selectedAppTheme ) }
374387
375388 // Display settings for the amoled theme
376- val amoledSwitch = remember { mutableStateOf( viewModel.getAmoledThemeValue()) }
377- val amoledDesc = if (amoledSwitch .value) {
389+ val amoledState = viewModel.amoledTheme.observeAsState(initial = false )
390+ val amoledDesc = if (amoledState .value) {
378391 stringResource(id = R .string.amoled_theme_setting_enabled_desc)
379392 } else {
380393 stringResource(id = R .string.amoled_theme_setting_disabled_desc)
381394 }
382395
383396 // Display settings for the Material You theme
384- val materialYouSwitch = remember { mutableStateOf(viewModel.getMaterialYouValue()) }
385- val materialYouDesc = if (materialYouSwitch.value) {
397+ val materialYouState = viewModel.materialYou.observeAsState(
398+ initial = Build .VERSION .SDK_INT >= Build .VERSION_CODES .S
399+ )
400+ val materialYouDesc = if (materialYouState.value) {
386401 stringResource(id = R .string.material_you_setting_enabled_desc)
387402 } else {
388403 stringResource(id = R .string.material_you_setting_disabled_desc)
@@ -404,39 +419,36 @@ private fun DisplayOptionsUI(
404419 SettingItem (
405420 icon = Icons .Filled .BrightnessMedium ,
406421 mainText = stringResource(id = R .string.theme_setting),
407- subText = displayValue ,
408- onClick = { displayDialog .value = true }
422+ subText = selectedAppTheme ,
423+ onClick = { appThemeDialog .value = true }
409424 )
410425 SettingItemWIthSwitch (
411426 icon = Icons .Filled .Contrast ,
412427 mainText = stringResource(id = R .string.amoled_theme_setting),
413428 subText = amoledDesc,
414- switchState = amoledSwitch ,
429+ switchState = amoledState ,
415430 onCheckChange = {
416431 viewModel.setAmoledTheme(it)
417- amoledSwitch.value = it
418432 })
419433 SettingItemWIthSwitch (
420434 icon = Icons .Filled .Palette ,
421435 mainText = stringResource(id = R .string.material_you_setting),
422436 subText = materialYouDesc,
423- switchState = materialYouSwitch ,
437+ switchState = materialYouState ,
424438 onCheckChange = { materialYouValue ->
425439 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
426440 viewModel.setMaterialYou(materialYouValue)
427- materialYouSwitch.value = materialYouValue
428441 } else {
429442 viewModel.setMaterialYou(false )
430- materialYouSwitch.value = false
431443 coroutineScope.launch { snackBarHostState.showSnackbar(context.getString(R .string.material_you_error)) }
432444 }
433445 }
434446 )
435447 }
436448
437- if (displayDialog .value) {
449+ if (appThemeDialog .value) {
438450 AlertDialog (onDismissRequest = {
439- displayDialog .value = false
451+ appThemeDialog .value = false
440452 }, title = {
441453 Text (
442454 text = stringResource(id = R .string.theme_dialog_title),
@@ -447,7 +459,7 @@ private fun DisplayOptionsUI(
447459 modifier = Modifier .selectableGroup(),
448460 verticalArrangement = Arrangement .Center ,
449461 ) {
450- radioOptions .forEach { text ->
462+ appThemeRadioOpts .forEach { text ->
451463 Row (
452464 modifier = Modifier
453465 .fillMaxWidth()
@@ -481,7 +493,7 @@ private fun DisplayOptionsUI(
481493 }, confirmButton = {
482494 FilledTonalButton (
483495 onClick = {
484- displayDialog .value = false
496+ appThemeDialog .value = false
485497
486498 when (selectedOption) {
487499 context.getString(R .string.theme_option_light) -> {
@@ -505,7 +517,7 @@ private fun DisplayOptionsUI(
505517 }
506518 }, dismissButton = {
507519 TextButton (onClick = {
508- displayDialog .value = false
520+ appThemeDialog .value = false
509521 }) {
510522 Text (stringResource(id = R .string.cancel))
511523 }
0 commit comments