A map comes from another screen and when I make a deletion on that map, the data in the map is deleted, but since there is no UI update, there is no change on the screen, but when I make a change to update the UI, for example, when I go back and come back, then it is deleted, so I realized that this is an indication of this. The element I delete from the list is deleted, but since there is no UI update, I cannot see the change on the UI side. Is there a part I missed? I couldn't find it. Can you please help me? Here is my codes.
FoodCategoryContent
this is parcelable data that came from another screen
data class FoodItem(val id: Int?, val name: String?)
@Parcelize
data class FoodCategoryContent(
val selectedFoodList: @RawValue MutableMap<String, MutableSet<FoodItem>> = mutableMapOf()
):Parcelable
MY UI:
@Composable
fun FilteredResponseRoute(
navHostController: NavHostController,
foodFilterSearchResponse: FoodCategoryContent?,
viewModel: FilteredResponseViewModel = hiltViewModel()
) {
var doOnce by remember { mutableStateOf(true) }
if (doOnce && foodFilterSearchResponse != null) {
viewModel.saveTagList(foodFilterSearchResponse.selectedFoodList)
doOnce = false
}
val state by viewModel.state.collectAsState()
FilteredResponseScreen(
navHostController,
state,
removeTagList = viewModel::removeTagList
)
}
@Composable
fun FilteredResponseScreen(
navHostController: NavHostController,
state: FilteredResponseScreenState,
removeTagList: (FoodItem, String, Map<String, MutableSet<FoodItem>>) -> Unit
) {
...
if (state.selectedFilterItems.isNotEmpty()) {
Row(
modifier = Modifier
.fillMaxWidth()
.horizontalScroll(rememberScrollState())
) {
state.selectedFilterItems.forEach { tags ->
tags.value.forEach { tag ->
FilterCategories(
foodItem = tag,
key = tags.key,
remove = { removedFoodItem, categoryKey ->
removeTagList(
removedFoodItem,
categoryKey,
state.selectedFilterItems
)
}
)
}
}
}
}
...
MY VIEWMODEL:
@HiltViewModel
class FilteredResponseViewModel @Inject constructor() : ViewModel() {
private val _state = MutableStateFlow(FilteredResponseScreenState())
val state: StateFlow<FilteredResponseScreenState> = _state.asStateFlow()
fun saveTagList(tagList: Map<String, MutableSet<FoodItem>>) {
_state.update {
it.copy(
selectedFilterItems = tagList
)
}
}
fun removeTagList(
removedItem: FoodItem,
categoryKey: String,
selectedFilterList: Map<String, MutableSet<FoodItem>>
) {
val categorySet = selectedFilterList[categoryKey]
if (categorySet != null && categorySet.contains(removedItem)) {
categorySet.remove(removedItem)
_state.update { currentState ->
val updatedFilterList = currentState.selectedFilterItems.toMutableMap()
updatedFilterList[categoryKey] = categorySet
currentState.copy(selectedFilterItems = updatedFilterList)
}
}
}
}
data class FilteredResponseScreenState(
val isLoading: Boolean = false,
val error: Boolean = false,
val selectedFilterItems: Map<String, MutableSet<FoodItem>> = mutableMapOf(),
)
SOLVED
How come I sometimes encounter such problems, but I solved the problem like this
_state.value = state.value.copy(
tagList = newTagList,
)
Instead of state.update I updated the value like this and it worked