Created
April 11, 2020 12:33
-
-
Save pavlospt/a35ae8edc9c5f5d7a2cd8ae9d68ad0fa to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class OurViewModel(observeGithubReposUseCase: ObserveGithubReposUseCase): ViewModel { | |
val githubRepos: LiveData<List<GithubRepoItem>> | |
get() = observeGithubReposUseCase | |
.observe() | |
.map { reposList -> | |
reposList | |
.sortedByDescending { it.stars } | |
.map { | |
GithubRepoItem( | |
repoId = it.remoteId, | |
name = it.name, | |
stars = it.stars, | |
url = it.url, | |
ownerAvatarUrl = it.ownerAvatarUrl | |
) | |
} | |
} | |
.asLiveData(viewModelScope.coroutineContext) | |
private val _intentChannel = ConflatedBroadcastChannel<OurViewIntent>() | |
init { | |
_intentChannel | |
.asFlow() | |
.onEach { viewIntent -> | |
when (viewIntent) { | |
OurViewIntent.Refresh -> refreshData() | |
} | |
} | |
.launchIn(viewModelScope) | |
observeGithubReposUseCase(Unit) | |
} | |
suspend fun processIntent(intent: OurViewIntent) = | |
_intentChannel.send(intent) | |
private suspend fun refreshData() { | |
// Do some work to refresh our data here | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment