Skip to content

Instantly share code, notes, and snippets.

@amit-bhandari
Created April 14, 2019 13:48
Show Gist options
  • Save amit-bhandari/2f36d9fa173d7fcaf8c6390e8ab686c1 to your computer and use it in GitHub Desktop.
Save amit-bhandari/2f36d9fa173d7fcaf8c6390e8ab686c1 to your computer and use it in GitHub Desktop.
public class QuoteViewModel extends ViewModel {
private LiveData<Quote> quote; //pojo object
private String category; //quote category
private int count; //number of quotes
private QuoteRepository quoteRepository; //data fetching object
public void init(String category, int count){
//make sure quote object should be created only once
if(this.quote!=null){
return ;
}
this.category = category;
this.count=count;
quoteRepository=new QuoteRepository();
quote = quoteRepository.getQuote(category, count);
}
public LiveData<Quote> getQuote(){
return quote;
}
//will be used when user manually wants to load new quote
public void refreshData(){
quote = quoteRepository.getQuote(category, count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment