jkasper
jkasper

Reputation: 246

Efficiently track bounds of fragments of composables on the screen in android without onGloballyPositioned

I'm working on a multi-modal chat application where users send text requests, and the chatbot responds with text, images, and thumbnails. One of the requirements is to send context information about what is currently visible on the screen to the backend, including the bounds of visible elements.

Requirements for Bounds Calculation

Composable Hierarchy

ChatScreenComposable 
    ├── UIFragmentsComposable 
    │   ├── RequestFragmentsComposable  (User request) 
    │   ├── ResponseFragmentsComposable (Bot responses) 
    │       ├── ChildFragmentsComposable (Bot images, thumbnails, etc.)

Current Approach (Using onGloballyPositioned)

Currently, I use Modifier.onGloballyPositioned on:

I store these bounds in a map and use them later when generating the JSON payload to send to the backend.

Problem

A reviewer pointed out that onGloballyPositioned has previously caused lag in scrolling performance.

What I've Tried

Question

What are better-performing alternatives to track visible bounds of nested elements in Jetpack Compose, without using onGloballyPositioned?

Any insights on efficient ways to track position and size while avoiding expensive recompositions would be appreciated.

Upvotes: 0

Views: 38

Answers (0)

Related Questions