Can we have some tooling to count recompositions b...
# compose
d
Can we have some tooling to count recompositions by screen areas?
Thinking of something similar to overdraw that show in colors where you overdraw too much, in this case it would should where recomposing too much
❤️ 1
c
This would be quite a rich improvement in understanding what’s going on in your code could maybe a Modifier be created for this?
Copy code
@Composable
fun Modifier.countRecomposition() = this.composed{
    var counter by rememberSaveable() {
        mutableStateOf(0)
    }
    counter++
    Text(text = counter.toString())
    this
}
Works but obviously creates an infinite loop 😂
Maybe a combintion of dataStore and this might do the trick as I need something for persistance
a
There are some ideas around tooling for tracing and visualizing this in some early design phases but nothing to play with just yet
👍 4
c
I got to present something in a very clunky way with data store but definitely it would be good to be able to write a Modifier with some implementation of listenToRecomposition and some sort of rememberSaveable that does not activates recomposition, even then just writing this sounds like a very big feature braking thing
Copy code
val android.content.Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")

@Composable
fun Modifier.countRecomposition() = this.composed{
    val context = LocalContext.current
    val EXAMPLE_COUNTER = intPreferencesKey("example_counter")
    var value = context.dataStore.data
        .map { preferences ->
            // No type safety.
            preferences[EXAMPLE_COUNTER] ?: 0
        }.collectAsState(initial = null)

    GlobalScope.async {
        context.dataStore.edit { settings ->
            val currentCounterValue = settings[EXAMPLE_COUNTER] ?: 0
            settings[EXAMPLE_COUNTER] = currentCounterValue + 1
        }
    }

    Text(text = value.value.toString())
    this
}
If you just run it it will just look empty and if you try to collect as state you will fall victim of recomposition tornado
I could probably manage something acceptable with some more hours and using some compose side-effects
🙂
s
chrisbanes/tivi/Debug snippet is an option with just a counter
2