Tung97 Hl
01/17/2023, 11:10 AMTung97 Hl
01/17/2023, 11:12 AMTung97 Hl
01/17/2023, 11:14 AMTung97 Hl
01/17/2023, 11:15 AMStylianos Gakis
01/17/2023, 11:22 AMbuttons: List<String>
is marked as unstable
, maybe consider checking this out https://medium.com/androiddevelopers/jetpack-compose-stability-explained-79c10db270c8 specifically in the tl;dr version, the point that reads:
āCollection classes like List, Set and Map are always determined unstable as it is not guaranteed they are immutable. You can use Kotlinx immutable collections instead or annotate your classes as @Immutable or @Stable.ā
Then as an easy step to see if this is your issue, use https://github.com/Kotlin/kotlinx.collections.immutable and mark the first parameter of NumPad
as buttons: ImmutableList<String>
and check if that helps or not.Stylianos Gakis
01/17/2023, 11:27 AMinputText
, its body recomposes, then at the bottom when NumPad
is called, you run this map + "Delete" + ... +
on every recomposition, and then when the NumPad
composable is run, it canāt infer if that List<String>
is the same as it was before, so it recomposes everything.
Now I am not sure if what I am saying is not exactly the problem, and if it has something to do with the LazyVerticalGrid
itself though and you need to provide a key
to all the items, but Iād say probably no since it by default should use the index of the item as a key. So Iād start with the ImmutableList first.Tung97 Hl
01/17/2023, 11:43 AMJakub Syty
01/17/2023, 11:48 AMJakub Syty
01/17/2023, 11:49 AMJakub Syty
01/17/2023, 11:50 AMJakub Syty
01/17/2023, 11:51 AMTung97 Hl
01/17/2023, 11:51 AMTung97 Hl
01/17/2023, 11:52 AMJakub Syty
01/17/2023, 11:55 AMStylianos Gakis
01/17/2023, 11:59 AMImmutableList
change in code? I am fairly certain that should be enough to avoid recomposition there.Stylianos Gakis
01/17/2023, 12:01 PMpersistentListOf(*((1..9).map(Int::toString) + "Delete" + "0" + "Clear").toTypedArray()),
and changing the NumPad signature to
@Composable
fun NumPad(
buttons: ImmutableList<String>,
onInputTextChange: (String) -> Unit,
)
?Jakub Syty
01/17/2023, 12:06 PMTung97 Hl
01/17/2023, 3:35 PMTung97 Hl
01/17/2023, 3:38 PMTung97 Hl
01/17/2023, 3:47 PMpersistentListOf(*((1..9)
mean? @Stylianos GakisStylianos Gakis
01/17/2023, 3:53 PMList
into an Array
and then using the spread operator *
to pass it to the varargs of persistentListOf
. Read about it here https://kotlinlang.org/docs/functions.html#variable-number-of-arguments-varargs or google āKotlin spread operatorā.
But yeah what you did should be correct, the NumPad() composable should just skip completely (can even inline numPadButtons
, there isnāt really a reason for it to be a val
as you have it there).
Did you run this and did it still recompose unnecessarily?
Also no need to ping people who are already in the thread, I get a notification for this regardless.Tung97 Hl
01/18/2023, 3:35 AMTung97 Hl
01/18/2023, 4:30 AMinputChar: ((String) -> Unit)? = null
. Because this callback refers to the function inside ViewModel, but the ViewModel is not stable, the compiler treats this lambda as not stable. So if I remember
this lambda, the view will not be recomposed unnecessarily.Jakub Syty
01/18/2023, 8:19 AMonInputTextChange = viewModel::setNumber
?Jakub Syty
01/18/2023, 8:19 AMTung97 Hl
01/18/2023, 8:30 AMJakub Syty
01/18/2023, 8:31 AMJakub Syty
01/18/2023, 8:31 AMTung97 Hl
01/18/2023, 8:45 AMTung97 Hl
01/18/2023, 8:47 AMStylianos Gakis
01/18/2023, 8:48 AMJakub Syty
01/18/2023, 8:48 AMTung97 Hl
01/18/2023, 8:51 AMStylianos Gakis
01/18/2023, 8:53 AMJakub Syty
01/18/2023, 8:56 AMStylianos Gakis
01/18/2023, 8:56 AMJakub Syty
01/18/2023, 8:56 AMTung97 Hl
01/18/2023, 8:56 AMTung97 Hl
01/18/2023, 8:58 AMStylianos Gakis
01/18/2023, 9:09 AMnumbers
is stable if itās a List
.
Just tested it myself in a compose for desktop project and you are right that passing the VM functions in a lambda was forcing recomposition here, since it was capturing ViewModel which is in fact unstable, however when I fix that and use lambda references to the functions, and I turn it into List<String>
it also recomposes due to what I said above, so I had to pass VM functions by reference and also use ImmutableList, are you sure you tested without the ImmutableList and it still did not recompose?Tung97 Hl
01/18/2023, 10:45 AMJakub Syty
01/18/2023, 10:48 AMTung97 Hl
01/18/2023, 10:48 AMJakub Syty
01/18/2023, 10:49 AMJakub Syty
01/18/2023, 10:49 AMJakub Syty
01/18/2023, 10:49 AMTung97 Hl
01/18/2023, 10:50 AMJakub Syty
01/18/2023, 10:50 AMJakub Syty
01/18/2023, 10:50 AMTung97 Hl
01/18/2023, 10:51 AMZach Klippenstein (he/him) [MOD]
01/18/2023, 4:30 PMTung97 Hl
01/19/2023, 3:57 AM