package com.sheep.gamegroup.absBase;

import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.view.fragment.BaseListFragment6;

import java.util.List;

/**
 * Created by realicing on 2018/11/30.
 * realicing@sina.com
 * 可以排除无效数据
 */
public abstract class CheckErrorApiRefresh<T> extends AbsApiRefresh<T> {

    public CheckErrorApiRefresh(BaseListFragment6 baseListFragment6) {
        super(baseListFragment6);
    }

    private int errorCount = 0;//错误数据个数

    @Override
    public void clear() {
        errorCount = 0;
        super.clear();
    }

    @Override
    protected void loadList(List<T> newList) {
        ListUtil.removeItem(newList, new ListUtil.CallBack<T, Boolean>() {
            @Override
            public Boolean call(T mediaBean) {
                boolean result = isRightData(mediaBean);
                if (!result) {//错误数据，删除，并记录个数
                    errorCount++;
                }
                return !result;
            }
        });
        super.loadList(newList);
    }

    protected abstract boolean isRightData(T item);

    @Override
    protected boolean hasMore() {//有错误数据，删除后翻页就不正常了，要加上
        return ListUtil.size(list) + errorCount >= per_page * page;
    }
}
