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

泪湿孤枕 提交于 2021-02-11 16:51:27

问题


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());

                }
            } 
        });

回答1:


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



来源:https://stackoverflow.com/questions/60014677/android-livedata-transformation-switchmap-apply-filter-on-the-original-list-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!