package com.wasu.frescoimagefetchermodule;

import android.widget.AbsListView;

import com.facebook.drawee.backends.pipeline.Fresco;


/**
 * Created by jeepc on 2016/10/31.
 * 用于AbsListView滚动时暂停加载图片
 */

 public class PauseOnScrollListener implements AbsListView.OnScrollListener {


        private final boolean pauseOnScroll;
        private final boolean pauseOnFling;
        private final AbsListView.OnScrollListener externalListener;

        public PauseOnScrollListener( boolean pauseOnScroll, boolean pauseOnFling) {
            this(pauseOnScroll, pauseOnFling, null);
        }

        public PauseOnScrollListener( boolean pauseOnScroll, boolean pauseOnFling,
                                     AbsListView.OnScrollListener customListener) {
            this.pauseOnScroll = pauseOnScroll;
            this.pauseOnFling = pauseOnFling;
            externalListener = customListener;
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            switch (scrollState) {
                case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                    Fresco.getImagePipeline().resume();
                    break;
                case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                    if (pauseOnScroll) {
                        Fresco.getImagePipeline().pause();
                    }
                    break;
                case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
                    if (pauseOnFling) {
                        Fresco.getImagePipeline().pause();
                    }
                    break;
            }
            if (externalListener != null) {
                externalListener.onScrollStateChanged(view, scrollState);
            }
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (externalListener != null) {
                externalListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
            }
        }

}
