Android架构组件和LiveData,MutableLiveData对象太多了?

时间:2017-08-17 08:01:18

标签: android android-architecture-components android-livedata android-viewmodel

在android体系结构组件发布之前,我开始研究一个项目,我有自己的ViewModel,它与Fragment具有相同的生命周期,ViewModels状态保存在Loader中的StateObject中,以便状态可以存活更改。 ViewModel通过接口与Fragment交谈。这很好,因为ViewModel和Fragment具有相同的生命周期。 我的ViewModel包含各种状态。它有一个isLoading boolean,isEmptyStateVisible boolean等。每次状态改变时,我调用了类似view.notifyIsLoadingChanged(true / false)的东西,在这种情况下,Fragment会显示或隐藏一个微调器。

现在我要改变我的实现,将新的ViewModel与LiveData一起使用。实现LiveData的最快方法是更改​​ViewModel正在使用的UI界面的实现。所以我可以保留我当前的实现,只需添加这个UI界面实现:

public class LiveDataProductReviewSheetUI extends LiveDataUI implements ProductReviewSheetUI {
    public final MutableLiveData<ReviewViewModelState> ratingDescChanged = new MutableLiveData<>();
    public final MutableLiveData<ReviewViewModelState> ratingChanged = new MutableLiveData<>();
    public final MutableLiveData<ReviewViewModelState> reviewChanged = new MutableLiveData<>();
    public final MutableLiveData<ReviewViewModelState> reviewValid = new MutableLiveData<>();
    public final MutableLiveData<ReviewViewModelState> expandReview = new MutableLiveData<>();
    public final MutableLiveData<ReviewViewModelState> reviewQuestion = new MutableLiveData<>();
    public final MutableLiveData<ReviewViewModelState> reviewCreated = new MutableLiveData<>();
    public final MutableLiveData<ReviewViewModelState> showMsg = new MutableLiveData<>();
    public final MutableLiveData<ReviewViewModelState> dismiss = new MutableLiveData<>();

    public void observe(LifecycleOwner owner, final ProductReviewSheetUI observer) {
        ratingDescChanged.observe(owner, state -> observer.onRatingDescriptionChanged(state));
        ratingChanged.observe(owner, state -> observer.onRatingChanged(state));
        reviewChanged.observe(owner, state -> observer.onReviewChanged(state));
        reviewValid.observe(owner, state -> observer.onHasValidReviewDataChanged(state));
        expandReview.observe(owner, state -> observer.onExpandReviewFieldHasChanged(state));
        reviewQuestion.observe(owner, state -> observer.onProductReviewQuestionChanged(state));
        reviewCreated.observe(owner, state -> observer.onReviewCreated(state));
        showMsg.observe(owner, state -> observer.onShowMessage(state));
        dismiss.observe(owner, state -> observer.onCloseView());
    }

    @Override
    public void onRatingDescriptionChanged(ReviewViewModelState state) {
        ratingDescChanged.setValue(state);
    }

    @Override
    public void onRatingChanged(ReviewViewModelState state) {
        ratingChanged.setValue(state);
    }

    @Override
    public void onReviewChanged(ReviewViewModelState state) {
        reviewChanged.setValue(state);
    }

    @Override
    public void onHasValidReviewDataChanged(ReviewViewModelState state) {
        reviewValid.setValue(state);
    }

    @Override
    public void onExpandReviewFieldHasChanged(ReviewViewModelState state) {
        expandReview.setValue(state);
    }

    @Override
    public void onProductReviewQuestionChanged(ReviewViewModelState state) {
        reviewQuestion.setValue(state);
    }

    @Override
    public void onReviewCreated(ReviewViewModelState state) {
        reviewCreated.setValue(state);
    }

    @Override
    public void onShowMessage(ReviewViewModelState state) {
        showMsg.setValue(state);
    }

    @Override
    public void onCloseView() {
        dismiss.setValue(dismiss.getValue());
    }
}

此类扩展的LiveDataUI类具有更多方法,如:

public final MutableLiveData<Boolean> showLoading = new MutableLiveData<>();
public final MutableLiveData<Boolean> showEmptyState = new MutableLiveData<>();

通过这个实现,我最终会得到很多MutableLiveData对象,感觉不对。我是否将更多状态置于我的ViewModel中?我的想法是在ViewModel中包含所有逻辑,以便我可以编写测试,以便在加载数据时验证isLoading是否为真,如果没有从Api返回的数据,则isEmptyState为true等。

我也注意到如果我调用setValue(state);在同一个MutableLiveData对象的很短时间内多次,onChanged方法只被调用一次。这是对的吗?

0 个答案:

没有答案