I have an activity that has a list of items loaded through the Paging 3 library. The list screen is designed by using jetpack compose and the list of items is shown in a Lazy column.
Clicking on any list item would take me to a detailed screen and some manipulations happen there. On clicking back, I'm unable to update my list item with an updated value, since LazyPagingItems has an immutable list. Any help is deeply appreciated.
val lazyPagingEventList : LazyPagingItems<Event>= viewModel.getEventList()
.collectAsLazyPagingItems()
LazyColumn(modifier = Modifier
.padding(horizontal = 8.dp),
content = {
itemsIndexed(items = lazyPagingEventList,
key = { index, item ->
item.id
},
itemContent = { index, item ->
//displays each item
EventItem(value = item, onItemClicked = { item ->
val intent = Intent(context, EventDetails::class.java)
intent.putExtra("eventId",item.id)
activityResult.launch(intent)
})
})
}
)
//activity result handle
val activityResult = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartActivityForResult(),
onResult = { activityResult ->
if (activityResult.resultCode == ACTIVITY_SUCCESS) {
activityResult.data?.let { intent ->
if (intent.hasExtra(EVENT_ID)) {
val eventId = intent.getIntExtra(EVENT_ID, 0)
val updatedPrice =
intent.getStringExtra(TRADE_PRICE) ?: EMPTY_STRING
val index = lazyPagingEventList.itemSnapshotList
.indexOf(lazyPagingEventList.itemSnapshotList.find { it.id ==
eventId })
/*
below line is not working since itemSnapshotList.items is not
a mutable list
*/
lazyPagingEventList.itemSnapshotList.items[index] =
lazyPagingEventList.itemSnapshotList.items[index].copy(price =
updatedPrice)
}
}
}
}
)