Skip to content

Instantly share code, notes, and snippets.

@florent37
Last active December 11, 2018 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save florent37/130c148f33ed23628407a95a5a46bf5f to your computer and use it in GitHub Desktop.
Save florent37/130c148f33ed23628407a95a5a46bf5f to your computer and use it in GitHub Desktop.
class SearchViewModel(application: Application) : AndroidViewModel(application), CoroutineScope {
private val job = Job()
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main
private val mutableViewState = MutableLiveData<SearchViewState>()
val viewState : LiveData<SearchViewState> = mutableViewState
private val repository by lazy {
mainApplication.omdbRepository
}
init {
start()
}
private fun start(){
search("avengers")
}
fun search(name : String) {
launch {
val movies = repository.searchMovies(name)
mutableViewState.postValue(SearchViewState(movies))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment