I am loading data from an API into an adapter, when the user clicks on it, it downloads using DownloadManager, i then use a broadcaster to let my activity know the downloadId and the hyperlink (the unique identifer for room).
I have so far been unable to figure out how best to use the same observer as initially this will just be getting the data (which has no downloadId) and then later passing through the downloadId and hyperlink to the repository. I have been able to do this successfully from the repository as hardcoded data so far.
My ViewModel:
@Inject
ItemViewModel(@NonNull ItemRepository itemRepository){
items = Transformations.switchMap(query, search -> {
if (search == null){
return AbsentLiveData.create();
}
// Transformations.switchMap(downloadable, inner -> {
// itemRepository.getDBItems(search, inner.getHyperlink(), inner.getDownloadId());
// });
return itemRepository.getDBItems(search, null, 0);
});
As I can't get the data from downloadable without doing switchMap, and I am unable to get itemRepository.getDBItems without it returning, I am stuck.
My broadcast result:
@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
if (resultCode == DOWNLOAD_ID){
Item i = new Item();
i.setHyperlink(resultData.getString("hyperlink"));
i.setDownloadId(resultData.getLong("downloadId"));
itemViewModel.setDownloadable(i);
}
}