https://kotlinlang.org logo
Title
a

André Thiele

06/04/2021, 1:12 PM
How can I force to recompose LazyColumn when the list does not update even if the list is changed?
f

Filip Wiesner

06/04/2021, 1:15 PM
If you have to force a recomposition of any component, it's most likely bad state management on your side. Can you post a snippet of your state (List) and how you update it?
☝️ 2
a

André Thiele

06/04/2021, 1:19 PM
Give me a second I try to put something together for you
@Composable
fun PlacesSearch(
    currentlySearchedPlaces: List<PlaceEntity>,
    onItemBookmarkClicked: (PlaceEntity) -> Unit,
    onPlaceSearched: (PlaceEntity) -> Unit,
    onPlaceClicked: (PlaceEntity) -> Unit,
) {
    if (currentlySearchedPlaces.isNotEmpty()) {
        Log.e("Searched places", "$currentlySearchedPlaces")

        RulonaSearchList(
            places = currentlySearchedPlaces,
            onItemClick = { uuid ->
                onPlaceClicked(uuid)
                onPlaceSearched(uuid)
            },
            onItemBookmarkClicked = {
                onItemBookmarkClicked(it)
            },
        )
    }
}
@Composable
fun RulonaSearchList(
    places: List<PlaceEntity>,
    onItemClick: (PlaceEntity) -> Unit,
    onItemBookmarkClicked: (PlaceEntity) -> Unit,
) {
    Log.e("rendering places", "$places")

    LazyColumn {
        items(places) { place ->
            Log.e("item in list", "$place")

            RulonaSearchItem(
                title = place.name,
                isBookmarked = place.isBookmarked,
                onClick = { onItemClick(place) },
                onBookmarkClicked = { onItemBookmarkClicked(place) },
            )
        }
    }
}
As you can see in the logs, the items correctly update with the new bookmarked state but the "item in list" log is not called again, which means that the SearchItem does not recompose inside the list
f

Filip Wiesner

06/04/2021, 1:27 PM
And where do you hold the
currentlySearchedPlaces
? It looks that you have that data in a VM somewhere. Do you use
collextAsState()
on your search places?
a

André Thiele

06/04/2021, 1:29 PM
I have something like this:
val placesUiState = viewModel.collectAsState(PlacesState::uiState)

PlacesSearch(
    currentlySearchedPlaces = uiState.currentlySearchedPlaces,
    onItemBookmarkClicked = { place ->
        viewModel.action(TogglePlaceBookmarkUseCase(place))
    },
    onPlaceSearched = { place ->
        viewModel.action(SetPlaceSearchTimestampUseCase(place))
    },
    onPlaceClicked = { place -> onPlaceClicked(place.uuid) },
)
a

Andrey Kulikov

06/04/2021, 1:29 PM
a

André Thiele

06/04/2021, 1:30 PM
the thing is, I already tried to create a brand new list with toMutableList() or .map {it.copy() } and it did not recompose either
a

Andrey Kulikov

06/04/2021, 1:36 PM
if you change from
items(places) {…}
to
items(places, key = { it.toString() } ) {}
will it work?
a

André Thiele

06/04/2021, 1:39 PM
yes, now it works 😅 thank you very much @Andrey Kulikov
i tried to use key before but the signature was not showing it since items() is overloaded
a

Andrey Kulikov

06/04/2021, 1:41 PM
just to clarify: it is not really a correct way of doing it and your code should work without such hack. but it helps to workaround the bug introduced in beta08 which I am trying to fix atm. once it is fixed you should consider passing your uuid as a key, not toString()
a

André Thiele

06/04/2021, 1:42 PM
yea it worked before i updated to beta 08 i am sure, will add a comment