Google's GithubBrowserSample is based on if there is a search made, for it to call for the results. In my scenario, there isn't any search, I just want to call it on fragment load. However, i'm not sure how to do this. Utilising breakpoints and trial and error, I can't seem to flag it to go into the repository method I select. Any tips would be appreciated.
Some snippets of my code:
public class CategoryViewModel extends ViewModel {
private final LiveData<Resource<List<Category>>> categories;
@Inject
CategoryViewModel(@NonNull CategoryRepository categoryRepository){
categories = categoryRepository.getDBCategories();
}
Equivalent in the sample:
public class SearchViewModel extends ViewModel {
private final MutableLiveData<String> query = new MutableLiveData<>();
private final LiveData<Resource<List<Repo>>> results;
private final NextPageHandler nextPageHandler;
@Inject
SearchViewModel(RepoRepository repoRepository) {
nextPageHandler = new NextPageHandler(repoRepository);
results = Transformations.switchMap(query, search -> {
if (search == null || search.trim().length() == 0) {
return AbsentLiveData.create();
} else {
return repoRepository.search(search);
}
});
}
For anyone else in the same boat, by simply amending the SearchViewModel constructor to hardcode the query, I was soon able to figure out that this could load even on the sample app right away as the data is being observed on the search fragment.
So I turned in SearchViewModel:
@Inject
SearchViewModel(RepoRepository repoRepository) {
nextPageHandler = new NextPageHandler(repoRepository);
results = Transformations.switchMap(query, search -> {
if (search == null || search.trim().length() == 0) {
return AbsentLiveData.create();
} else {
return repoRepository.search(search);
}
});
}
To
@Inject
SearchViewModel(RepoRepository repoRepository) {
results = repoRepository.search("shadow");
nextPageHandler = new NextPageHandler(repoRepository);
}
Hopefully this helps someone else.