Hafiz Al Asad
Hafiz Al Asad

Reputation: 49

Multiple MutableLiveData change results in a single MutableLiveData

In my android project, I have three MutableLiveData variables. Upon the change of their values, I want to change the value of another MutableLiveData and observe its value to make an API call. Is the process I followed a standard approach? Or are there any other good approaches?

ViewModel Code

    private MutableLiveData<ChemistEntity> chemist = new MediatorLiveData<>();
    private MutableLiveData<List<OrderPreviewRequest.LineItem>> lineItemList = new MutableLiveData<>();
    private MutableLiveData<String> paymentType = new MutableLiveData<>();
    private MutableLiveData<Boolean> isAllDataReadyForCall = new MutableLiveData<>();

public void checkAllDataReady() {
        ChemistEntity chemist = this.chemist.getValue();
        List<OrderPreviewRequest.LineItem> productListValue = this.lineItemList.getValue();
        String paymentTypeValue = this.getPaymentType().getValue();
        boolean allReady = chemist != null && productListValue != null && paymentTypeValue != null;
        isAllDataReadyForCall.postValue(allReady);
    }

Fragment Code

private void subscribeUiToChemist(LiveData<ChemistEntity> liveData) {
        liveData.observe(getViewLifecycleOwner(), chemist -> {
            mViewModel.checkAllDataReady();
        });
    }

    private void subscribeUiToPaymentType(LiveData<String> liveData) {
        liveData.observe(getViewLifecycleOwner(), paymentType -> {
            mViewModel.checkAllDataReady();
        });
    }

    private void subscribeUiToLineItemList(LiveData<List<OrderPreviewRequest.LineItem>> liveData) {
        liveData.observe(getViewLifecycleOwner(), lineItems -> {
            mViewModel.checkAllDataReady();
        });
    }

    private void subscribeUiToIsAllDataReady(LiveData<Boolean> liveData) {
        liveData.observe(getViewLifecycleOwner(), isReady -> {
            if(isReady) {
                mViewModel.callApi();
            }
        });
    }

Upvotes: 0

Views: 48

Answers (2)

joaquinVidal
joaquinVidal

Reputation: 11

So, the issue seems to be that LiveData only emmits when being observed. To fix this you can add a fake observer in the Fragment. This means observing it but not doing anything with it. Also, you shoud be using setValue instead of postValue, i recommend you to check the docs on this.

Upvotes: 0

joaquinVidal
joaquinVidal

Reputation: 11

I see that you've used a MediatorLiveData, but you're using it as a MutableLiveData, not making use of any of its properties. Now, you can change chemist to be a MediatorLiveData and add the other liveDatas as sources of the chemist MediatorLiveData.

private MediatorLiveData<Boolean> isAllDataReadyForCall = new MediatorLiveData<>();

in ViewModelInit:

isAllDataReadyForCall.addSource(chemist, {chemist -> checkAllDataReady()})

and the same for the other two liveDatas. Then you can remove all the observers in Fragment, but the one of isAllDataReady.

Upvotes: 0

Related Questions