Created
December 13, 2022 11:07
-
-
Save dturner/113993ed4f8b4f5d34240ae7e35ad941 to your computer and use it in GitHub Desktop.
BookmarksViewModel - Before adding domain layer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Copyright 2022 Google LLC. | |
SPDX-License-Identifier: Apache-2.0 */ | |
class BookmarksViewModel @Inject constructor( | |
newsRepository: NewsRepository, | |
private val userDataRepository: UserDataRepository | |
) : ViewModel() { | |
// #1. Obtain a list of bookmarks. | |
private val bookmarks: StateFlow<Set<String>> = | |
userDataRepository.userDataStream | |
.map { userData -> | |
userData.bookmarkedNewsResources | |
} | |
.stateIn(...) | |
val feedState: StateFlow<NewsFeedUiState> = | |
newsRepository | |
.getNewsResourcesStream() // #2 Obtain news resources | |
.combine(bookmarks) { newsResources, bookmarks -> // #3 Combine data | |
newsResources | |
// #4 Only show bookmarked news resources | |
.filter { newsResource -> bookmarks.contains(newsResource.id) } | |
.map { SaveableNewsResource(it, true) } | |
} | |
.map<List<SaveableNewsResource>, <NewsFeedUiState>(NewsFeedUiState::Success) | |
.onStart { emit(Loading) } | |
.stateIn(...) | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment