package com.simplicityapks.reminderdatepicker.lib;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

import java.util.List;

/**
 * Base class for both DateSpinner and TimeSpinner.
 *
 * Allows to use a custom last list item, which won't get selected on click. Instead,
 * onLastItemClick() will be called.
 */
public abstract class PickerSpinner extends Spinner {

    public PickerSpinner(Context context) {
        this(context, null);
    }

    public PickerSpinner(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PickerSpinner(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);

        // create our simple adapter with default layouts and set it here:
        PickerSpinnerAdapter adapter = new PickerSpinnerAdapter(context, getSpinnerItems(),
                new TwinTextItem.Simple(getFooter(), null));
        setAdapter(adapter);
    }

    @Override
    public void setAdapter(SpinnerAdapter adapter) {
        if(adapter instanceof PickerSpinnerAdapter)
            super.setAdapter(adapter);
        else throw new IllegalArgumentException(
                "adapter must extend PickerSpinnerAdapter to be used with this class");
    }

    @Override
    public void setSelection(int position) {
        if(position == getCount()-1)
            onFooterClick(); // the footer has been clicked, so don't update the selection
        else {
            // remove any previous temporary selection:
            ((PickerSpinnerAdapter)getAdapter()).selectTemporary(null);
            super.setSelection(position);
        }
    }

    /**
     * Push an item to be selected, but not shown in the dropdown menu. This is similar to calling
     * setText(item.toString()) if a Spinner had such a method.
     * @param item The item to select, or null to remove any temporary selection.
     */
    public void selectTemporary(TwinTextItem item) {
        // pass on the call to the adapter:
        ((PickerSpinnerAdapter)getAdapter()).selectTemporary(item);
        super.setSelection(getCount());
        // TODO: When the selected position was getCount() before it needs to be updated
    }

    /**
     * Gets the default list of items to be inflated into the Spinner, will be called once on
     * initializing the Spinner. Should use lazy initialization in inherited classes.
     * @return The List of Objects whose toString() method will be called for the items, or null.
     */
    public abstract List<TwinTextItem> getSpinnerItems();

    /**
     * Gets the CharSequence to be shown as footer in the drop down menu.
     * @return The footer, or null to disable showing it.
     */
    public abstract CharSequence getFooter();

    /**
     * Built-in listener for clicks on the footer. Note that the footer will not replace the
     * selection and you still need a separate OnItemSelectedListener.
     */
    public abstract void onFooterClick();
}

@@ -130,62 +130,213 @@
 oid.
-widget.Spinner;%0Aimport android.widget.SpinnerAdapter;%0A
+util.Log;%0Aimport android.view.View;%0Aimport android.widget.AdapterView;%0Aimport android.widget.Spinner;%0Aimport android.widget.SpinnerAdapter;%0A%0Aimport java.lang.reflect.Field;%0Aimport java.lang.reflect.Method;
 %0Aimp
@@ -597,16 +597,173 @@
 nner %7B%0A%0A
+    // indicates that the onItemSelectedListener callback should not be passed to the listener.%0A    private boolean interceptNextSelectionCallback = false;%0A%0A
     publ
@@ -2441,128 +2441,1164 @@
     
-super.setSelection(getCount());%0A        // TODO: When the selected position was getCount() before it needs to be updated
+final int tempItemPosition = getCount();%0A        if(getSelectedItemPosition() == tempItemPosition) %7B%0A            // this is quite a hack, first reset the position to 0 but intercept the callback,%0A            // then redo the selection:%0A            interceptNextSelectionCallback = true;%0A            super.setSelection(0, false);%0A        %7D%0A        super.setSelection(tempItemPosition);%0A    %7D%0A%0A    @Override%0A    public void setOnItemSelectedListener(final OnItemSelectedListener listener) %7B%0A        super.setOnItemSelectedListener(%0A                new OnItemSelectedListener() %7B%0A                    @Override%0A                    public void onItemSelected(AdapterView%3C?%3E parent, View view, int position, long id) %7B%0A                        if(!interceptNextSelectionCallback)%0A                            listener.onItemSelected(parent, view, position, id);%0A                        interceptNextSelectionCallback = false;%0A                    %7D%0A%0A                    @Override%0A                    public void onNothingSelected(AdapterView%3C?%3E parent) %7B%0A                        listener.onNothingSelected(parent);%0A                    %7D%0A                %7D%0A        );
 %0A   

Fix selecting two different temporary items