We went ahead and added the configChanges activity...
# compose
j
We went ahead and added the configChanges activity settings that have been suggested here previously in our app. Everything works great except I’m seeing that Images that use the painterResource() composable are not being updated when switching from light to dark mode. Has anyone else seen this?
I tried looking for issues in issuetracker and didn’t find any. Wanted to check here to make sure I’m not missing something before I log an issue.
c
Look into the implementation of stringResources, and you will find that before resolving the string from the context the configuration is resolved first, and not used. This is done to run the string reading logic every time the configuratioj changes.
a
go ahead and file it; checking the source real quick, it looks like
painterResource
is not subscribing to configuration changes the way the other resource functions do
yeah what @Csaba Szugyiczki said 🙂
🙌 1
c
Based on this you can create your own painterResource implementation which does just that
something like this should do the trick
Copy code
@Composable
fun myPainterResource(@DrawableRes id: Int): Painter {
    LocalConfiguration.current
    return painterResource(id = id)
}
j
Thanks for the quick response! That makes sense. I did compare the code between those two, but I mistook the LocalContext.current call on the first line of painterResource to be LocalConfiguration.current. That totally makes sense
👍 1
a
if the snippet above works it's probably an incidental artifact of the implementation; use at your own risk, future changes might not cause the invalidation of everything you're looking at
j
Noted
c
What do you mean by that? Wouldn’t that go against the concept of CompositionLocals?
a
It might cause
myPainterResource
to recompose, but that doesn't mean
painterResource
can't skip at some point if it believes nothing else it would return has changed
👍 1
c
Also, be aware that you still need to handle/check for configuration changes. Things like wallpaper changes will cause a config change without a way to opt out IIRC.
👍 1
😮 1
a
the internal implementation of
painterResource
causing a re-check and reload of related things, if it does today, is also coincidental
👍 1
j
Yeah, good callout @Colton Idle, we mainly wanted to add these settings so the common config changes like rotate and dark mode switch are a little more smooth.
1
a
How does
myPainterResource
differ from how `stringResource`s invalidate themselves?
Copy code
@Composable
@ReadOnlyComposable
private fun resources(): Resources {
    LocalConfiguration.current
    return LocalContext.current.resources
}
c
I think the main difference is after this stringResource just uses the resources to read the current value of the string by the given id
painterResource does a lot more internally
👍 1
a
@Alex Vanyo that utility is the inner composable being called. If it invalidates then it has to recompose, and so does the outer scope that called it
painterResource
accepts only an
Int
as an argument, which would make it otherwise stable; there's no affirmative signal that
painterResource
must recompose if its caller recomposes
👍 3
a
Ah so you can rely on a “side-effect” read (like
LocalConfiguration.current
without actually using the value) to “bubble up” to callers to force recomposition, but not to “bubble down” to other calls
mind blown 4
a
yes
if you poke around the implementation of
painterResource
too you can see and imagine all sorts of other valid implementations that wouldn't trigger a reload of the resource from an outer recomposition either
👍 1
a
painterResource
is not subscribing to configuration changes the way the other resource functions do
Looking around that code more, I think only
stringResource
and company are subscribing to configuration changes, so probably everything else should also do so?
booleanResource
,
dimensionResource
, etc.
a
probably
though if you're using `booleanResource`/`dimensionResource` and such from compose code it's probably because you're in a hybrid views/compose app and you're not globally turning off activity recreation for config changes anyway
1
if you're in a compose-only app and you're using those, why? 🙃
a
To create an edge case that
booleanResource
should probably handle 😄
😂 7
j
Issue Link: https://issuetracker.google.com/issues/228862715 I didn’t add a code snippet because it seemed pretty straightforward, but if that would help I can do so.
👍 1