package com.simplicityapks.reminderdatepicker.lib;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * The left PickerSpinner in the Google Keep app, to select a date.
 */
public class DateSpinner extends PickerSpinner {

    // We only need this constructor since PickerSpinner handles the others.
    public DateSpinner(Context context, AttributeSet attrs, int defStyle){
        super(context);
    }

    @Override
    public List<Object> getSpinnerItems() {
        final Resources res = getResources();
        final Calendar date = Calendar.getInstance();
        ArrayList<Object> items = new ArrayList<Object>(3);
        // today item:
        items.add(new DateItem(res.getString(R.string.date_today), date));
        // tomorrow item:
        date.add(Calendar.DAY_OF_YEAR, 1);
        items.add(new DateItem(res.getString(R.string.date_tomorrow), date));
        // next weekday item:
        date.add(Calendar.DAY_OF_YEAR, 6);
        items.add(new DateItem(getNextWeekDay(date.get(Calendar.DAY_OF_WEEK)), date));
        return items;
    }

    private String getNextWeekDay(int weekDay) {
        // zero-indexed, because the weekday array is zero-indexed
        weekDay -= 1;
        return getResources().getStringArray(R.array.next_weekdays) [weekDay];
    }

    /**
     * Gets the currently selected Date (that the Spinner is showing)
     * @return The selected Date as Calendar, or null if there is none.
     */
    public Calendar getSelectedDate() {
        final DateItem selectedItem = (DateItem) getSelectedItem();
        if(selectedItem == null)
            return null;
        else
            return selectedItem.getDate();
    }

    public void setSelectedDate(Calendar date) {
        final int count = getAdapter().getCount() - 1;
        int itemPosition = -1;
        for(int i=0; i<count; i++) {
            if(((DateItem) getAdapter().getItem(i)).getDate().equals(date)) {
                itemPosition = i;
                break;
            }
        }
        if(itemPosition >= 0)
            setSelection(itemPosition);
        else {
            // we need to construct a temporary DateItem to select:
        }
    }

    @Override
    public String getFooter() {
        return getResources().getString(R.string.spinner_date_footer);
    }

    @Override
    public void onFooterClick() {
        // show the date picker
    }
}

@@ -1797,24 +1797,245 @@
 e();%0A    %7D%0A%0A
+    /**%0A     * Sets the Spinner's selection as date. If the date was not in the possible selections, a temporary%0A     * item is created and passed to selectTemporary().%0A     * @param date The date to be selected.%0A     */%0A
     public v
@@ -2067,24 +2067,24 @@
 dar date) %7B%0A
-
         fina
@@ -2454,73 +2454,957 @@
     
-// we need to construct a temporary DateItem to select:%0A        %7D
+final int dateDifference = (int) ((date.getTimeInMillis() - Calendar.getInstance().getTimeInMillis())%0A                    / (1000*60*60*24));%0A            if(dateDifference%3E0 && dateDifference%3C=7) %7B // if the date is within the next week:%0A                // we need to construct a temporary DateItem to select:%0A                final int day = date.get(Calendar.DAY_OF_WEEK);%0A                selectTemporary(new DateItem(getNextWeekDay(day), date));%0A            %7D else %7B%0A                // we need to show the date as a full text, using the current DateFormat:%0A                selectTemporary(new DateItem(getDateFormat().format(date.getTime()), date));%0A            %7D%0A        %7D%0A    %7D%0A%0A    private java.text.DateFormat savedFormat;%0A    private java.text.DateFormat getDateFormat() %7B%0A        if(savedFormat == null)%0A            savedFormat = android.text.format.DateFormat.getDateFormat(getContext().getApplicationContext());%0A        return savedFormat;
 %0A   

Work on setSelectedDate enabling temporary dates