简体   繁体   中英

Android LiveData: Transformation switchMap: Apply filter on the original list and show the filtered data

public class FlightViewModel extends BaseViewModel {

    private FlightRepository flightRepository;
    private MediatorLiveData<Resource<FlightSearchMainOuterModel>> mSearchFlights = new MediatorLiveData<>();
    private MediatorLiveData<Resource<FlightSearchMainOuterModel>> mOriginalList = new MediatorLiveData<>();
    private MediatorLiveData<Resource<FlightSearchMainOuterModel>> mSortedSearchFlights = new MediatorLiveData<>();



    public FlightViewModel(@NonNull Application application) {
        super(application);
        flightRepository = FlightRepository.getInstance(application);
    }

    @Override
    public void updateResults() {

    }

    public void postFlightSearch() {


        mSearchFlights.addSource(flightRepository.postFlightSearchData(requestJson), mSearchFlights::setValue);
    }




    public LiveData<Resource<FlightSearchMainOuterModel>> getFlightResult() {
        return Transformations.map(mSearchFlights, input -> {

            if (input == null || input.data == null || input.status != Resource.Status.SUCCESS)
                return null;

            if (input.status == Resource.Status.SUCCESS && input.data != null) {
                if (input.data.getError().getErrorCode().equalsIgnoreCase("1")) {
                    FlightSearchModel flightSearchModel;
                    List<FlightSearchMainOuterResultOnwordReturnModel> onword = input.data.getResults().getOnword();
                    for (FlightSearchMainOuterResultOnwordReturnModel onwordLiveData : onword) {
                        flightSearchModel = onwordLiveData.getSegments().get(0);
                        flightSearchModel.getDurationFormat(onwordLiveData.getSegments());
                    }
                    return Resource.cloneResource(input, input.data);
                }
            } else if(input.status == Resource.Status.LOADING){
                return Resource.loading(null);
            } else {
                return Resource.error("Error! Please try again.", null);
            }

            return null;
        });

    }

    public void copyToOrignal(){
        mOriginalList = new MediatorLiveData<>();
        mOriginalList.setValue(mSearchFlights.getValue());
    }

    public LiveData<Resource<FlightSearchMainOuterModel>> getSortedFlightResult() {
        return mSortedSearchFlights;
    }





    public void nonStop(boolean isOneStop) {
        copyToOrignal();
        LiveData<Resource<FlightSearchMainOuterModel>> onwordLiveData = Transformations.map(mOriginalList, input -> {
            if (input == null || input.data == null || input.status != Resource.Status.SUCCESS)
                return null;

            List<FlightSearchMainOuterResultOnwordReturnModel> onwordNewList = new ArrayList<>();

            List<FlightSearchMainOuterResultOnwordReturnModel> onword = input.data.getResults().getOnword();
            if (onword.size() > 0) {

                if(isOneStop){
                    for(int i =0; i<onword.size(); i++){
                        if(onword.get(i).getSegments().size()>1 || !onword.get(i).getSegments().get(0).getNumberofStops().equalsIgnoreCase("0")){
                            onwordNewList.add(onword.get(i));

                        }
                    }
                }else {
                    for(int i =0; i<onword.size(); i++){
                        if(onword.get(i).getSegments().size()==1){
                            if(onword.get(i).getSegments().get(0).getNumberofStops().equalsIgnoreCase("0")){
                                onwordNewList.add(onword.get(i));
                            }
                        }
                    }
                }
                input.data.getResults().setOnword(onwordNewList);
            }
            return Resource.cloneResource(input, input.data);
        });

        mSortedSearchFlights.addSource(onwordLiveData, mSortedSearchFlights::setValue);

    }

}

In mSearchFlights I'm getting the whole list on which I have to apply filter. After applying one filter the mSearchFlights original list gets filtered even after copying the data in another liveData. So while applying filter second time it works on the filtered list rather than the original one list which is mSearchFlights. So please help me in applying the filter.

In the fragment I'm observing the two livedata which is same :

flightViewModel.getFlightResult().observe(this, flightSearchModelResource -> {
            if (flightSearchModelResource == null)
                return;
            if (flightSearchModelResource.status == Resource.Status.SUCCESS && flightSearchModelResource.data != null) {

                if (flightSearchModelResource.data.getError().getErrorCode().equalsIgnoreCase("1")) {
                    firstOrderAdapter.setData(flightSearchModelResource.data.getResults().getOnword());

                } else {
                    Utils.toastLong(getContext(), flightSearchModelResource.data.getError().getErrorMessage());

                }
            } 
        });


flightViewModel.getSortedFlightResult().observe(this, flightSearchModelResource -> {
            if (flightSearchModelResource == null)
                return;
            if (flightSearchModelResource.status == Resource.Status.SUCCESS && flightSearchModelResource.data != null) {

                if (flightSearchModelResource.data.getError().getErrorCode().equalsIgnoreCase("1")) {
                    if(flightSearchModelResource.data.getResults().getOnword().size()>0){
                        firstOrderAdapter.setData(flightSearchModelResource.data.getResults().getOnword());

                    }
                } else {
                    Utils.toastLong(getContext(), flightSearchModelResource.data.getError().getErrorMessage());

                }
            } 
        });

Please read this document and apply switchMap transformation to your source livedata. It will not alter your original source LiveData values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM