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 ViewPager viewPager;

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

	@Override
	public void onPageScrollStateChanged(int state) {}

	@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) {
				viewPager.setCurrentItem(viewPager.getAdapter().getCount() - 2, false);
				return;
			}
			if (position == viewPager.getAdapter().getCount() - 1) {
				viewPager.setCurrentItem(1, false);
				return;
			}
		}
		onLoopPageSelected(position - 1);
	}

	public abstract void onLoopPageSelected(int position);
}

@@ -954,19 +954,16 @@
 ;%0A%09%09%09%7D%0A%09
-%09%7D%0A
 %09%09onLoop
@@ -990,16 +990,64 @@
 n - 1);%0A
+%09%09%7D else %7B%0A%09%09%09onLoopPageSelected(position);%0A%09%09%7D%0A
 %09%7D%0A%0A%09pub

fix viewPager for nonlooping pageSelected callback