While browsing CLs, I noticed this one about docum...
# compose
s
While browsing CLs, I noticed this one about documentation updates, which is good. However, inside the code on
line 157
, I saw the comment
"Don't do this."
But why? I know that
.onSizeChanged
will only fire after the first composition, and we have to wait to get the required attribute. This seems acceptable to me. What other ways can we get the attributes of the child layout besides writing a custom layout using
Layout
composable or
Modifier.layout
? https://android-review.googlesource.com/c/platform/frameworks/support/+/1856275/7/compose/in[…]va/androidx/compose/integration/docs/phases/Phases.kt
a
If you are just acquiring layout size for after use, it's ok. But if you are using the size for layout, it is inefficient (because it needs two layout passes) and causes bad UX (flickering).
a
Right, it's illustrating a circular data dependency where composition (the creation of a padding modifier with a given padding value) has a data dependency on layout, which happens after composition. You're trying to get your UI to converge on a solution across multiple frames as opposed to determining the correct results from a single calculation pass.
s
Ok, here's my actual use case. I have an app image function to display network images using
Coil
under the hud. It takes a
URL
of an image is required to show. I have a
Coil
image painter and a regular
Image(painter = ...)
widget inside my wrapper function. Also, I am using
Modifier.onSizeChanged
to get the widget's size and pass it to the
Coil
Image Request Builder to specify the width and height of the image to load (I also reduced its size slightly). Is this considered a sub-optimal solution? I cannot specify strict dimensions for the image widget of my application because it is used throughout the application and has dynamic dimensions, usually filling a parent size.
a
I'm not familiar with the API that coil landed on for reporting this in particular but iirc accompanist's
CoilImage
had some of this built-in. You want to avoid using the dimensions to construct the painter, you want to inform the painter of the dimensions once you know them, which should signal the load.
and somewhat tangentially, avoid using
java.net.URL
for anything ever. Here's why: https://docs.oracle.com/javase/7/docs/api/java/net/URL.html#equals(java.lang.Object) its
.equals
method is defined to perform blocking DNS lookup in order to compare if the hosts in two URLs are the same.
s
By URL, I meant just a regular string, autocorrection. I apologize.