package com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.undo;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;

import com.nhaarman.listviewanimations.itemmanipulation.OnDismissCallback;

/**
 * An implementation of {@link SwipeUndoAdapter} which puts the primary and undo {@link View} in a {@link android.widget.FrameLayout},
 * and handles the undo click event.
 */
public class SimpleSwipeUndoAdapter extends SwipeUndoAdapter implements UndoCallback {

    private final Context mContext;

    /**
     * The {@link com.nhaarman.listviewanimations.itemmanipulation.OnDismissCallback} that is notified of dismissed items.
     */
    private final OnDismissCallback mOnDismissCallback;

    /**
     * The {@link UndoAdapter} that provides the undo {@link View}s.
     */
    private final UndoAdapter mUndoAdapter;

    /**
     * Create a new {@code SwipeUndoAdapter}, decorating given {@link android.widget.BaseAdapter}.
     * @param undoAdapter the {@code} BaseAdapter to decorate.
     */
    public <T extends BaseAdapter & UndoAdapter> SimpleSwipeUndoAdapter(final T undoAdapter, final Context context, final OnDismissCallback dismissCallback) {
        super(undoAdapter, null);
        setUndoCallback(this);
        mUndoAdapter = undoAdapter;
        mContext = context;
        mOnDismissCallback = dismissCallback;
    }

    @Override
    public View getView(final int position, final View convertView, final ViewGroup parent) {
        SwipeUndoView view = (SwipeUndoView) convertView;
        if (view == null) {
            view = new SwipeUndoView(mContext);
        }
        View primaryView = super.getView(position, view.getPrimaryView(), view);
        view.setPrimaryView(primaryView);

        View undoView = mUndoAdapter.getUndoView(position, view.getUndoView(), view);
        view.setUndoView(undoView);

        mUndoAdapter.getUndoClickView(undoView).setOnClickListener(new UndoClickListener(view));

        return view;
    }

    @Override
    public View getPrimaryView(final View view) {
        return ((SwipeUndoView) view).getPrimaryView();
    }

    @Override
    public View getUndoView(final View view) {
        return ((SwipeUndoView) view).getUndoView();
    }

    @Override
    public void onUndoShown(final View view, final int position) {
    }

    @Override
    public void onDismiss(final View view, final int position) {
    }

    @Override
    public void onDismiss(final AbsListView absListView, final int[] reverseSortedPositions) {
        mOnDismissCallback.onDismiss(absListView, reverseSortedPositions);
    }

    private class UndoClickListener implements View.OnClickListener {
        private final SwipeUndoView mView;

        UndoClickListener(final SwipeUndoView view) {
            mView = view;
        }

        @Override
        public void onClick(final View v) {
            undo(mView);
        }
    }
}

@@ -306,16 +306,72 @@
 lback;%0A%0A
+import java.util.Collection;%0Aimport java.util.HashSet;%0A%0A
 /**%0A * A
@@ -993,16 +993,175 @@
 apter;%0A%0A
+    /**%0A     * The positions of the items currently in the undo state.%0A     */%0A    private final Collection%3CInteger%3E mUndoPositions = new HashSet%3CInteger%3E();%0A%0A
     /**%0A
@@ -2263,17 +2263,246 @@
 ner(view
-)
+, position));%0A%0A        boolean isInUndoState = mUndoPositions.contains(position);%0A        primaryView.setVisibility(isInUndoState ? View.GONE : View.VISIBLE);%0A        undoView.setVisibility(isInUndoState ? View.VISIBLE : View.GONE
 );%0A%0A    
@@ -2846,32 +2846,70 @@
 int position) %7B%0A
+        mUndoPositions.add(position);%0A
     %7D%0A%0A    @Over
@@ -2974,24 +2974,65 @@
 position) %7B%0A
+        mUndoPositions.remove(position);%0A
     %7D%0A%0A    @
@@ -3329,16 +3329,53 @@
 w mView;
+%0A        private final int mPosition;
 %0A%0A      
@@ -3410,32 +3410,52 @@
 ipeUndoView view
+, final int position
 ) %7B%0A            
@@ -3464,24 +3464,58 @@
 iew = view;%0A
+            mPosition = position;%0A
         %7D%0A%0A 
@@ -3596,24 +3596,70 @@
 ndo(mView);%0A
+            mUndoPositions.remove(mPosition);%0A
         %7D%0A  

Properly recycle undo state