Skip to content

Instantly share code, notes, and snippets.

@paulocns
Last active August 4, 2018 22:18
Show Gist options
  • Save paulocns/b04fd6bd88389aa3f7fa330a1707f124 to your computer and use it in GitHub Desktop.
Save paulocns/b04fd6bd88389aa3f7fa330a1707f124 to your computer and use it in GitHub Desktop.
public class QueryViewModelArc extends ViewModel {
private MutableLiveData<String> query = new MutableLiveData<>();
private MutableLiveData<Boolean> searchEnabled = new MutableLiveData<>();
private Observer<String> queryObserver;
private SearchShows searchShows;
@Inject
public QueryViewModelArc(SearchShows searchShows) {
this.searchShows = searchShows;
searchEnabled.setValue(false);
query.setValue("");
queryObserver = query -> searchEnabled.setValue(!TextUtils.isEmpty(query));
query.observeForever(queryObserver);
}
@Override
protected void onCleared() {
super.onCleared();
query.removeObserver(queryObserver);
}
public MutableLiveData<Boolean> getSearchEnabled() {
return searchEnabled;
}
public MutableLiveData<String> getQuery() {
return query;
}
//other ViewModel Stuff
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment