package com.kkbox.toolkit.ui;

import android.support.v4.view.ViewPager;

import com.kkbox.toolkit.listview.adapter.InfiniteViewPagerAdapter;

public abstract class OnInfiniteViewPagerPageChangeListener implements ViewPager.OnPageChangeListener {

	private InfiniteViewPager viewPager;
	private float currentPosition = 0;
	private boolean scrolled = false;

	public OnInfiniteViewPagerPageChangeListener(InfiniteViewPager viewPager) {
		this.viewPager = viewPager;
	}

	@Override
	public void onPageScrollStateChanged(int state) {
		if (state == ViewPager.SCROLL_STATE_DRAGGING) {
			scrolled = true;
		}
	}

	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

	@Override
	public void onPageSelected(int position) {
		InfiniteViewPagerAdapter adapter = (InfiniteViewPagerAdapter) viewPager.getAdapter();
		if (adapter.isLoopEnabled()) {
			if (position == 0) {
				if (scrolled) {
					scrolled = false;
					onPageScrollLeft();
				}
				viewPager.setCurrentItem(viewPager.getAdapter().getCount() - 3, false);
				currentPosition = viewPager.getAdapter().getCount() - 2;
				return;
			} else if (position == viewPager.getAdapter().getCount() - 1) {
				if (scrolled) {
					scrolled = false;
					onPageScrollRight();
				}
				viewPager.setCurrentItem(0, false);
				currentPosition = 1;
				return;
			}
			onLoopPageSelected(position - 1);
		} else {
			onLoopPageSelected(position);
		}
		if (scrolled) {
			scrolled = false;
			if (currentPosition > position) {
				onPageScrollLeft();
			} else if (currentPosition < position) {
				onPageScrollRight();
			}
		}
		currentPosition = position;
	}

	public abstract void onLoopPageSelected(int position);

	public abstract void onPageScrollLeft();

	public abstract void onPageScrollRight();
}

@@ -566,13 +566,13 @@
 ATE_
-DRAGG
+SETTL
 ING)
@@ -594,16 +594,90 @@
 = true;%0A
+%09%09%7D else if (state == ViewPager.SCROLL_STATE_IDLE) %7B%0A%09%09%09scrolled = false;%0A
 %09%09%7D%0A%09%7D%0A%0A

Correct ViewPager page scroll state changed