I’m writing a custom layout using `Layout` but the...
# compose
j
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
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
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
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
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
How do you check for overlaps?
There are interesting data structures to speed this up
j
How do you check for overlaps?
Copy code
fun Interval.overlapsWith(interval: Interval): Boolean =
  maxOf(this.start, interval.start) <= minOf(this.end, interval.end)