Skip to content

Instantly share code, notes, and snippets.

@pavlospt
Created April 11, 2020 12:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavlospt/a35ae8edc9c5f5d7a2cd8ae9d68ad0fa to your computer and use it in GitHub Desktop.
Save pavlospt/a35ae8edc9c5f5d7a2cd8ae9d68ad0fa to your computer and use it in GitHub Desktop.
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