In a list of posts each post has a like button, which calls a db transaction. But db transactions and gui works accurately if either the list consists of only one post or for the last post in the list
How it was supposed to work: If a post is liked button is filled, otherwise it’s outlined. If the post is liked, like is being deleted from the db after the button is pressed. If the post is not liked, the like is being inserted in the db after the button is pressed
How it acctually works: If the last post is liked all the posts are displayed as liked despite being not liked. If the last post is not liked all the posts are displayed as not liked, despite being liked. If I press the button of not the last post, it is added to database again and again, but it is not being deleted
Code from viewModel:
private val _checkedPostFlow = MutableStateFlow<Int?>(null)
val checkedPostFlow: StateFlow<Int?> get() = _checkedPostFlow.asStateFlow()
fun checkLikedPostExistence(userId: Int, postId: Int) {
viewModelScope.launch {
val checkedPost = likedPostsRepository
.getLikedPostByIds(userId = userId, postId = postId)
.stateIn(
scope = viewModelScope
).value
_checkedPostFlow.update { checkedPost }
}
}
My full ViewModel:
My composable in which it is used