Aaron Waller
01/04/2023, 7:25 AMAaron Waller
01/04/2023, 7:25 AMvar openModalBottomSheet by remember{ mutableStateOf(false)}
val currentPost: Post? = remember{mutableStateOf(null)}
if(openModalBottomSheet){
ModalBottomSheetLayout(
hidePost = { viewModel.hidePost() },
currentPost = currentPost) {
}
}
Scaffold(bottomBar = BottomNavigationBar()) {
MyPostListScreen(
openModalBottomSheet = { it ->
openModalBottomSheet = true,
currentPost.value = it})
}
So whenever the user opens the options for a specific Post it assigns this Post to the currentPost mutableState so I know which one to call the actions on in my BottomSheet. But now I somehow need to access the list inside the MyPostListScreen and filter it for the post that should get hidden.
Main problem here is that the List of Posts gets created inside the MyPostListScreen and the Hide action is getting called outside the Scaffold and inside the BottomSheet layout.Filip Wiesner
01/04/2023, 7:32 AMviewModel.hidePost()
removes the post from the state in your viewmodel which would be observed in your screen where you observe this state.
But now I somehow need to access the list inside the MyPostListScreen and filter it for the post that should get hidden.Your list inside
MyPostListScreen
should observe the state from which this post will be removedAaron Waller
01/04/2023, 7:33 AMAaron Waller
01/04/2023, 7:35 AMFilip Wiesner
01/04/2023, 7:35 AMhidePost()
on your MyPostListScreenViewModel
instead of some other VM you have hereAaron Waller
01/04/2023, 7:40 AMFilip Wiesner
01/04/2023, 7:42 AMAaron Waller
01/04/2023, 7:43 AMAaron Waller
01/04/2023, 7:45 AMFilip Wiesner
01/04/2023, 7:45 AMAaron Waller
01/04/2023, 7:46 AMFilip Wiesner
01/04/2023, 7:46 AMAaron Waller
01/04/2023, 8:20 AMAaron Waller
01/04/2023, 8:23 AMAaron Waller
01/04/2023, 8:24 AMFilip Wiesner
01/04/2023, 8:31 AMSomehow I always refused to do it like this because I thought that would worsen the performance 🤷🏼♂️This is called premature optimisation 😄
Having in the absolute worst case 20 lists variables with 100 posts eachYou probably don't have to save the lists separately. If you do, you could end up with one post being in multiple lists which is not desirable. You can have one list with all posts and have each post only once there (assuming that your posts are identifiable with some ID).
Aaron Waller
01/04/2023, 8:37 AMFilip Wiesner
01/04/2023, 8:39 AMFilip Wiesner
01/04/2023, 8:40 AMAaron Waller
01/04/2023, 8:41 AMFilip Wiesner
01/04/2023, 8:43 AMtopPostsOfAllTime
could be just a List<PostId>
and whenever you retrieve them, you check the cache and return the full Post
entitiesAaron Waller
01/04/2023, 8:50 AMFilip Wiesner
01/04/2023, 8:59 AMAaron Waller
01/04/2023, 9:19 AMFilip Wiesner
01/04/2023, 9:24 AMAaron Waller
01/04/2023, 7:11 PMprivate val _mutedPost: MutableSharedFlow<Post> =
MutableSharedFlow(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
val mutedPost: MutableSharedFlow<Post> get() = _mutedPost
fun setMutedPost(post: Post) {
_mutedPost.tryEmit(post)
}
then I call the setMutedPost method from my BottomSheet.
2. I collect any change happening to the mutedPost SharedFlow inside all my viewModels and take the mutedPost Item to filter all lists:
init {
viewModelScope.launch {
communityRepository.mutedPost().collect(){ post ->
hidePostFromAllLists(post.id) //function to filter all lists in this ViewModel
}
}
}
Thats it, super simple and just requires a couple lines of code.dorche
01/04/2023, 7:15 PMAaron Waller
01/04/2023, 7:33 PM