Skip to content

Instantly share code, notes, and snippets.

@dturner
Created December 13, 2022 11:07
Show Gist options
  • Save dturner/113993ed4f8b4f5d34240ae7e35ad941 to your computer and use it in GitHub Desktop.
Save dturner/113993ed4f8b4f5d34240ae7e35ad941 to your computer and use it in GitHub Desktop.
BookmarksViewModel - Before adding domain layer
/* 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