bryankeltonadams
06/15/2023, 10:33 PMval topLevelDestinationsWithUnreadResources: StateFlow<Set<TopLevelDestination>> =
userNewsResourceRepository.observeAllForFollowedTopics()
.combine(userNewsResourceRepository.observeAllBookmarked()) { forYouNewsResources, bookmarkedNewsResources ->
setOfNotNull(
FOR_YOU.takeIf { forYouNewsResources.any { !it.hasBeenViewed } },
BOOKMARKS.takeIf { bookmarkedNewsResources.any { !it.hasBeenViewed } },
)
}.stateIn(
coroutineScope,
SharingStarted.WhileSubscribed(5_000),
initialValue = emptySet(),
)
This value is only used in one spot, inside of NiaApp.kt to pass into the BottomBar to determine whether a UI element in the bottom bar should have a badge indicating if there's unread resources or not. I feel like normally this is something I'd put in a viewModel perhaps, but in our own project we've heavily followed the pattern of having a stateHolder at least for our main App composable. Another question I have then, is grabbing a user from the UserRepository a good idea to do in the stateHolder? I need to have the user and pass it into my ModalDrawerSheet which I'm using for a side navigation hamburger menu.
I can't tell the difference between something like fetching a user to display the data in a drawer, versus grabbing topics that aren't viewed and deciding whether to show a badge or not. To me it seems like the same type of thing, but I'm still not confident on why this block of code from now in android is even in the NiaAppState.ktFrancesc
06/15/2023, 10:42 PMsnapshotFlow
and push that to the viewmodel as the field is updated, but the validation will remain in the state holder.
At the other end, when I need to access the data layer, or do more complex logic, then I would do that in the viewmodel instead of a state holder.
Then there is the in-between, where you are accessing some resource (like a network monitor), where you could do either a state holder or a viewmodel, and both would be perfectly fine.bryankeltonadams
06/15/2023, 11:46 PMFrancesc
06/16/2023, 1:53 AMbryankeltonadams
06/16/2023, 4:01 PMFrancesc
06/16/2023, 5:00 PM