0

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);
    }
}
omega_prime
  • 65
  • 3
  • 17

1 Answers1

0

Reviewed Google samples and made an object to wrap around it inside the ViewModel.

My end result:

@Inject
ItemViewModel(@NonNull ItemRepository itemRepository){
    this.itemQuery = new MutableLiveData<>();
    items = Transformations.switchMap(itemQuery, input -> {
        if (input.isEmpty()){
            return AbsentLiveData.create();
        }
        return itemRepository.getDBItems(input.query, input.hyperlink, input.downloadId);
    });

 @VisibleForTesting
    public void setItemQuery(String query, String hyperlink, long downloadId) {
        ItemQuery update = new ItemQuery(query,hyperlink,downloadId);
        if (Objects.equals(itemQuery.getValue(), update)) {
            return;
        }
        itemQuery.setValue(update);
    }

@VisibleForTesting
    static class ItemQuery{
    public final String query;
    public final String hyperlink;
    public final long downloadId;

    ItemQuery(String query, String hyperlink, long downloadId) {
        this.query = query;
        this.hyperlink = hyperlink;
        this.downloadId = downloadId;
    }

    boolean isEmpty() {
        return query == null;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        ItemQuery  itemQuery = (ItemQuery) o;

        if (query != null ? !query.equals(itemQuery.query) : itemQuery.query != null) {
            return false;
        }
        return hyperlink != null ? hyperlink.equals(itemQuery.hyperlink) : itemQuery.hyperlink == null;
    }
}

Seems to work for my intended purposes.

omega_prime
  • 65
  • 3
  • 17