https://kotlinlang.org logo
Title
j

julioromano

03/25/2022, 3:09 PM
I’m writing a custom layout using
Layout
but the algorithm to compute the placement of the children composables (i.e. to compute both the constraints of the measurables and the x,y of the placeables) turns out to be O(n^2). Is it possible to handle this computation in a background coroutine or similar, but still while inside the composable
Layout()
function? Are there any patterns in compose to help with such cases?
r

romainguy

03/25/2022, 4:17 PM
Before you try to bring down such a hammer, is it necessary? i.e. how many children do you think you will end up measuring and placing?
j

julioromano

03/25/2022, 4:18 PM
On average about 100.
It is some kind of calendar timeline. Every children is an event in the calendar but placement of the children must follow a particular rule in which I progressively fill columns with non overlapping events and, whenever there is overlap, either place the event in a neighboring column where it wouldn't overlap or, if there isn't any, add an additional column and place it there.
Should I not bother?
m

myanmarking

03/25/2022, 5:57 PM
Ya, don’t bother for now. If in release builds you notice jumk, just rethink. Can’t you use some Lazy layout or sth ?
j

julioromano

03/25/2022, 6:19 PM
Most of the complexity comes from sorting and checking for overlaps. I could do this as “preprocessing” in the business logic so that the Composable would become way simpler computationally if it had the items delivered to it already sorted and free of overlaps. This could be a way to go.
r

romainguy

03/25/2022, 6:20 PM
How do you check for overlaps?
There are interesting data structures to speed this up
j

julioromano

03/25/2022, 6:25 PM
How do you check for overlaps?
fun Interval.overlapsWith(interval: Interval): Boolean =
  maxOf(this.start, interval.start) <= minOf(this.end, interval.end)