alorma/Compose-Settings

Recompose problems

Closed this issue ยท 14 comments

@alorma
Hm, can not recompose switchers Material 3 component when changed language

@Composable
fun SetLanguage(locale: Locale) {
    val configuration = LocalConfiguration.current
    configuration.setLocale(locale)
    val resources = LocalContext.current.resources
    resources.updateConfiguration(configuration, resources.displayMetrics)
}
 setContent {
            SetLanguage(locale = Locale.forLanguageTag(settingsViewModel.settings.languageTag))
-----
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Settings(
    navController: NavController,
    isDarkMode: Boolean,
) {
val darkSwitcherState = rememberBooleanSettingState(isDarkMode)
SettingsSwitch(
                state = darkSwitcherState,
                modifier = Modifier
                    .background(MaterialTheme.colorScheme.background),
                icon = {
                    Icon(
                        imageVector = Icons.Default.DarkMode,
                        contentDescription = stringResource(R.string.dark_mode),
                        tint = MaterialTheme.colorScheme.onBackground
                    )
                },
                title = {
                    Text(
                        stringResource(R.string.dark_mode),
                        color = MaterialTheme.colorScheme.onBackground
                    )
                },
                onCheckedChange = onDarkModeChange
            )
}

Looks like https://prnt.sc/GG-Owt-VbALQ
I mean recompose does not work for your switchers when changed configuration (locale).
Please try check, thanks a lot.

Hi!

Can you provide a small project to reproduce the case?

I will check, thanks

@alorma Thanks a lot.

Hi!

The project is cool, but it doesn't contains compose-settings on it ๐Ÿ˜…

I mean.. on the zip you provided, at least on the "develop" branch, I've removed the git and keystores files as I don't want that info

BTW, there are few things I see...

Maybe

val darkSwitcherState = rememberBooleanSettingState(isDarkMode)

shouldn't belong to the settings composable, instead try to pass it through params, so it has to change whenever theme change

please check again, I unpacked archive
image

oh, I see it now.. on git. checking

@alorma I tried check guides and bla bla, so I saw no state values as properties, only plain objects

Well... I get the problem, but not understand why it's happening...

I have a temporal solution... while I investigate more

Instead of

      SettingsSwitch(
          title = { Text(text = stringResource(R.string.dark_mode)) } // declare `stringResource` here
      )

Use

      val darkModeText = stringResource(R.string.dark_mode) // declare `stringResource` here, OUTSIDE of settings
      SettingsSwitch(
          title = { Text(text = darkModeText) },
      )

That sloves the problem... not sure why recomposition is not happening there... but at least problem is mitigated

@alorma magic, I will test, thanks a lot. Magic, but it works