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);
}
}
在mSearchFlights中,我得到了必须对其应用过滤器的整个列表。应用一个过滤器后,即使将数据复制到另一个liveData中,mSearchFlights原始列表也会被过滤。因此,在第二次应用过滤器时,它适用于过滤后的列表,而不是原始的一个列表mSearchFlights。因此,请帮助我应用过滤器。
在片段中,我观察到两个相同的livedata:
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());
}
}
});
请阅读此document,并将switchMap转换应用于源实时数据。它不会更改您的原始LiveData值。