package se.emilsjolander.flipview;

import android.util.SparseArray;
import android.view.View;

public class Recycler {

	/** Unsorted views that can be used by the adapter as a convert view. */
	private SparseArray<View>[] scrapViews;

	private int viewTypeCount;

	private SparseArray<View> currentScrapViews;

	void setViewTypeCount(int viewTypeCount) {
		if (viewTypeCount < 1) {
			throw new IllegalArgumentException("Can't have a viewTypeCount < 1");
		}
		// noinspection unchecked
		@SuppressWarnings("unchecked")
		SparseArray<View>[] scrapViews = new SparseArray[viewTypeCount];
		for (int i = 0; i < viewTypeCount; i++) {
			scrapViews[i] = new SparseArray<View>();
		}
		this.viewTypeCount = viewTypeCount;
		currentScrapViews = scrapViews[0];
		this.scrapViews = scrapViews;
	}

	/** @return A view from the ScrapViews collection. These are unordered. */
	View getScrapView(int position, int viewType) {
		if (viewTypeCount == 1) {
			return retrieveFromScrap(currentScrapViews, position);
		} else if (viewType >= 0 && viewType < scrapViews.length) {
			return retrieveFromScrap(scrapViews[viewType], position);
		}
		return null;
	}

	/**
	 * Put a view into the ScrapViews list. These views are unordered.
	 * 
	 * @param scrap
	 *            The view to add
	 */
	void addScrapView(View scrap, int position, int viewType) {
		if (viewTypeCount == 1) {
			currentScrapViews.put(position, scrap);
		} else {
			scrapViews[viewType].put(position, scrap);
		}

		scrap.setAccessibilityDelegate(null);
	}

	static View retrieveFromScrap(SparseArray<View> scrapViews, int position) {
		int size = scrapViews.size();
		if (size > 0) {
			// See if we still have a view for this position.
			for (int i = 0; i < size; i++) {
				View view = scrapViews.get(i);
				int fromPosition = scrapViews.keyAt(i);
				if (fromPosition == position) {
					scrapViews.remove(i);
					return view;
				}
			}
			int index = size - 1;
			View r = scrapViews.valueAt(index);
			scrapViews.removeAt(index);
			return r;
		} else {
			return null;
		}
	}

}

@@ -1273,16 +1273,68 @@
 dd%0A%09 */%0A
+%09@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)%0A
 %09void ad
@@ -1518,17 +1518,54 @@
 p);%0A%09%09%7D%0A
-%0A
+%09%09if (Build.VERSION.SDK_INT %3E= 14) %7B%0A%09
 %09%09scrap.
@@ -1596,19 +1596,66 @@
 (null);%0A
+%09
 %09%7D%0A
+%09%7D%0A%0A%09@TargetApi(Build.VERSION_CODES.HONEYCOMB)
 %0A%09static
@@ -1834,157 +1834,79 @@
 %0A%09%09%09
-for (int i = 0; i %3C size; i++) %7B%0A%09%09%09%09View view = scrapViews.get(i);%0A%09%09%09%09int fromPosition = scrapViews.keyAt(i
+View result = scrapViews.get(position, null
 );%0A%09%09%09
-%09
 if (
-fromPosition == position
+result != null
 ) %7B%0A
-%09
 %09%09%09%09
@@ -1927,13 +1927,19 @@
 ove(
-i
+position
 );%0A
-%09
 %09%09%09%09
@@ -1949,25 +1949,21 @@
 urn 
-view
+result
 ;%0A
-%09
 %09%09%09%7D%0A
-%09%09%09%7D%0A
 %09%09%09i
@@ -1990,17 +1990,86 @@
 %0A%09%09%09
-View r = 
+result = scrapViews.valueAt(index);%0A%09%09%09if (Build.VERSION.SDK_INT %3E= 11) %7B%0A%09%09%09%09
 scra
@@ -2067,36 +2067,37 @@
 %0A%09%09%09%09scrapViews.
-valu
+remov
 eAt(index);%0A%09%09%09s
@@ -2092,16 +2092,29 @@
 index);%0A
+%09%09%09%7D else %7B%0A%09
 %09%09%09scrap
@@ -2117,32 +2117,47 @@
 crapViews.remove
+(scrapViews.key
 At(index);%0A%09%09%09re
@@ -2149,18 +2149,24 @@
 t(index)
+)
 ;%0A
+%09%09%09%7D%0A
 %09%09%09retur
@@ -2168,30 +2168,27 @@
 return r
-;%0A%09%09%7D else %7B%0A%09
+esult;%0A%09%09%7D%0A
 %09%09return
@@ -2194,18 +2194,14 @@
 n null;%0A
-%09%09%7D%0A
 %09%7D%0A%0A%7D%0A

Fix Recycler (incorrect use of key and index)