首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在viewModel上处理内部Transformations.switchMap

在viewModel上处理内部Transformations.switchMap
EN

Stack Overflow用户
提问于 2020-02-03 02:47:51
回答 1查看 143关注 0票数 0

我正在将数据从API加载到适配器中,当用户点击它时,它使用DownloadManager下载,然后我使用广播器让我的活动知道downloadId和超链接(房间的唯一标识符)。

到目前为止,我还不知道如何最好地使用同一个观察者,因为最初它只是获取数据(没有downloadId),然后通过downloadId和超链接到存储库。到目前为止,我已经能够在存储库中以硬编码数据的形式成功地做到这一点。

我的ViewModel:

代码语言:javascript
复制
    @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);
        });

因为我不做switchMap就不能从downloadable中获取数据,而且我不能在不返回的情况下获取itemRepository.getDBItems,所以我被卡住了。

我的播放结果:

代码语言:javascript
复制
@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);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-07 21:33:05

查看了谷歌示例,并在ViewModel中创建了一个对象来包装它。

我的最终结果:

代码语言:javascript
复制
@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;
    }
}

似乎能达到我的预期目的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60029409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档