Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions app/src/main/java/com/appeaser/sublimepicker/Sampler.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,31 @@

public class Sampler extends AppCompatActivity {

// Keys for saving state
private static final String SS_DATE_PICKER_CHECKED = "saved.state.date.picker.checked";
private static final String SS_TIME_PICKER_CHECKED = "saved.state.time.picker.checked";
private static final String SS_RECURRENCE_PICKER_CHECKED = "saved.state.recurrence.picker.checked";
private static final String SS_ALLOW_DATE_RANGE_SELECTION = "saved.state.allow.date.range.selection";
private static final String SS_TOGGLE_DATE_RANGE_SELECTION_WITHOUT_LONG_PRESS = "saved.state.allow.date.range.selection.without.longpress";
private static final String SS_START_YEAR = "saved.state.start.year";
private static final String SS_START_MONTH = "saved.state.start.month";
private static final String SS_START_DAY = "saved.state.start.day";
private static final String SS_END_YEAR = "saved.state.end.year";
private static final String SS_END_MONTH = "saved.state.end.month";
private static final String SS_END_DAY = "saved.state.end.day";
private static final String SS_HOUR = "saved.state.hour";
private static final String SS_MINUTE = "saved.state.minute";
private static final String SS_RECURRENCE_OPTION = "saved.state.recurrence.option";
private static final String SS_RECURRENCE_RULE = "saved.state.recurrence.rule";
private static final String SS_INFO_VIEW_VISIBILITY = "saved.state.info.view.visibility";
private static final String SS_SCROLL_Y = "saved.state.scroll.y";
private final int INVALID_VAL = -1;

// Launches SublimePicker
ImageView ivLaunchPicker;

// SublimePicker options
CheckBox cbDatePicker, cbTimePicker, cbRecurrencePicker, cbAllowDateRangeSelection;
CheckBox cbDatePicker, cbTimePicker, cbRecurrencePicker, cbAllowDateRangeSelection, cbToggleRangeSelectionWithoutLongPress;
RadioButton rbDatePicker, rbTimePicker, rbRecurrencePicker;

// Labels
Expand Down Expand Up @@ -137,6 +155,7 @@ public void onClick(View v) {
svMainContainer = (ScrollView) findViewById(R.id.svMainContainer);

cbAllowDateRangeSelection = (CheckBox) findViewById(R.id.cbAllowDateRangeSelection);
cbToggleRangeSelectionWithoutLongPress = (CheckBox) findViewById(R.id.cbToggleRangeSelectionWithoutLongPress);

llDateHolder = (LinearLayout) findViewById(R.id.llDateHolder);
llDateRangeHolder = (LinearLayout) findViewById(R.id.llDateRangeHolder);
Expand Down Expand Up @@ -232,6 +251,7 @@ void dealWithSavedInstanceState(Bundle savedInstanceState) {
cbTimePicker.setChecked(true);
cbRecurrencePicker.setChecked(true);
cbAllowDateRangeSelection.setChecked(false);
cbToggleRangeSelectionWithoutLongPress.setChecked(false);

rbDatePicker.setChecked(true);
} else { // Restore
Expand All @@ -241,6 +261,7 @@ void dealWithSavedInstanceState(Bundle savedInstanceState) {
.setChecked(savedInstanceState.getBoolean(SS_RECURRENCE_PICKER_CHECKED));
cbAllowDateRangeSelection
.setChecked(savedInstanceState.getBoolean(SS_ALLOW_DATE_RANGE_SELECTION));
cbToggleRangeSelectionWithoutLongPress.setChecked(savedInstanceState.getBoolean(SS_TOGGLE_DATE_RANGE_SELECTION_WITHOUT_LONG_PRESS));

rbDatePicker.setVisibility(cbDatePicker.isChecked() ?
View.VISIBLE : View.GONE);
Expand Down Expand Up @@ -324,6 +345,7 @@ Pair<Boolean, SublimeOptions> getOptions() {

// Enable/disable the date range selection feature
options.setCanPickDateRange(cbAllowDateRangeSelection.isChecked());
options.setToggleRangeWithoutLongPress(cbToggleRangeSelectionWithoutLongPress.isChecked());

// Example for setting date range:
// Note that you can pass a date range as the initial date params
Expand Down Expand Up @@ -423,24 +445,6 @@ private SpannableStringBuilder applyBoldStyle(String text) {
return ss;
}

// Keys for saving state
final String SS_DATE_PICKER_CHECKED = "saved.state.date.picker.checked";
final String SS_TIME_PICKER_CHECKED = "saved.state.time.picker.checked";
final String SS_RECURRENCE_PICKER_CHECKED = "saved.state.recurrence.picker.checked";
final String SS_ALLOW_DATE_RANGE_SELECTION = "saved.state.allow.date.range.selection";
final String SS_START_YEAR = "saved.state.start.year";
final String SS_START_MONTH = "saved.state.start.month";
final String SS_START_DAY = "saved.state.start.day";
final String SS_END_YEAR = "saved.state.end.year";
final String SS_END_MONTH = "saved.state.end.month";
final String SS_END_DAY = "saved.state.end.day";
final String SS_HOUR = "saved.state.hour";
final String SS_MINUTE = "saved.state.minute";
final String SS_RECURRENCE_OPTION = "saved.state.recurrence.option";
final String SS_RECURRENCE_RULE = "saved.state.recurrence.rule";
final String SS_INFO_VIEW_VISIBILITY = "saved.state.info.view.visibility";
final String SS_SCROLL_Y = "saved.state.scroll.y";

@Override
protected void onSaveInstanceState(Bundle outState) {
// Save state of CheckBoxes
Expand All @@ -449,6 +453,7 @@ protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(SS_TIME_PICKER_CHECKED, cbTimePicker.isChecked());
outState.putBoolean(SS_RECURRENCE_PICKER_CHECKED, cbRecurrencePicker.isChecked());
outState.putBoolean(SS_ALLOW_DATE_RANGE_SELECTION, cbAllowDateRangeSelection.isChecked());
outState.putBoolean(SS_TOGGLE_DATE_RANGE_SELECTION_WITHOUT_LONG_PRESS, cbToggleRangeSelectionWithoutLongPress.isChecked());

int startYear = mSelectedDate != null ? mSelectedDate.getStartDate().get(Calendar.YEAR) : INVALID_VAL;
int startMonth = mSelectedDate != null ? mSelectedDate.getStartDate().get(Calendar.MONTH) : INVALID_VAL;
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/layout/sampler.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@
android:text="Allow date range selection?"
android:textSize="@dimen/sampler_text_size" />

<CheckBox
android:id="@+id/cbToggleRangeSelectionWithoutLongPress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:text="Allow date range selection without long press?"
android:textSize="@dimen/sampler_text_size" />

<RelativeLayout
android:id="@+id/rlDateTimeRecurrenceInfo"
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ private void processOptions() {
// dateParams[2] /* day of month */,
// mOptions.canPickDateRange(),
// this);
mDatePicker.init(mOptions.getDateParams(), mOptions.canPickDateRange(), this);
mDatePicker.init(mOptions.getDateParams(), mOptions.canPickDateRange(), mOptions.canToggleRangeWithoutLongPress(), this);

long[] dateRange = mOptions.getDateRange();

Expand Down Expand Up @@ -636,7 +636,7 @@ public void onDateChanged(SublimeDatePicker view, SelectedDate selectedDate) {
//selectedDate.getStartDate().get(Calendar.MONTH),
//selectedDate.getStartDate().get(Calendar.DAY_OF_MONTH),
//mOptions.canPickDateRange(), this);
mDatePicker.init(selectedDate, mOptions.canPickDateRange(), this);
mDatePicker.init(selectedDate, mOptions.canPickDateRange(), mOptions.canToggleRangeWithoutLongPress(), this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ public void setCanPickRange(boolean canPickRange) {
mViewPager.setCanPickRange(canPickRange);
}

public void setToggleRangeWithoutLongPress(boolean setToggleRangeWithoutLongPress) {
mViewPager.setToggleRangeWithoutLongPress(setToggleRangeWithoutLongPress);
}

private void updateButtonVisibility(int position) {
final boolean hasPrev = position > 0;
final boolean hasNext = position < (mAdapter.getCount() - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
Expand Down Expand Up @@ -55,8 +57,10 @@ class DayPickerViewPager extends ViewPager {

private float mInitialDownX, mInitialDownY;
private boolean mIsLongPressed = false;

private boolean toggleRange = false;
private boolean mCanToggleRangeWithoutLongPress = false;
private CheckForLongPress mCheckForLongPress;
@Nullable
private SelectedDate mTempSelectedDate;

// Scrolling support
Expand Down Expand Up @@ -204,6 +208,10 @@ protected void setCanPickRange(boolean canPickRange) {
mCanPickRange = canPickRange;
}

public void setToggleRangeWithoutLongPress(boolean setToggleRangeWithoutLongPress) {
this.mCanToggleRangeWithoutLongPress = setToggleRangeWithoutLongPress;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (!mCanPickRange) {
Expand Down Expand Up @@ -252,7 +260,7 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
}
}

return mIsLongPressed || super.onInterceptTouchEvent(ev);
return isRangeSelectionActive() || super.onInterceptTouchEvent(ev);
}

private boolean isStillALongPress(int x, int y) {
Expand Down Expand Up @@ -291,7 +299,7 @@ public boolean onTouchEvent(MotionEvent ev) {
removeCallbacks(mCheckForLongPress);
}

if (mIsLongPressed && ev.getAction() == MotionEvent.ACTION_UP
if (isRangeSelectionActive() && ev.getAction() == MotionEvent.ACTION_UP
|| ev.getAction() == MotionEvent.ACTION_CANCEL) {
if (Config.DEBUG) {
Log.i(TAG, "OTE: LONGPRESS && (UP || CANCEL)");
Expand All @@ -301,6 +309,11 @@ public boolean onTouchEvent(MotionEvent ev) {
if (mDayPickerPagerAdapter != null) {
mTempSelectedDate = mDayPickerPagerAdapter.resolveEndDateForRange((int) ev.getX(),
(int) ev.getY(), getCurrentItem(), false);

if (mCanToggleRangeWithoutLongPress && mTempSelectedDate != null) {
mTempSelectedDate = toggleLongSelectAction(mTempSelectedDate);
}

mDayPickerPagerAdapter.onDateRangeSelectionEnded(mTempSelectedDate);
}
}
Expand All @@ -314,13 +327,13 @@ public boolean onTouchEvent(MotionEvent ev) {
removeCallbacks(mScrollerRunnable);
}
//return true;
} else if (mIsLongPressed && ev.getAction() == MotionEvent.ACTION_DOWN) {
} else if (isRangeSelectionActive() && ev.getAction() == MotionEvent.ACTION_DOWN) {
if (Config.DEBUG) {
Log.i(TAG, "OTE: LONGPRESS && DOWN");
}

mScrollingDirection = NOT_SCROLLING;
} else if (mIsLongPressed && ev.getAction() == MotionEvent.ACTION_MOVE) {
} else if (isRangeSelectionActive() && ev.getAction() == MotionEvent.ACTION_MOVE) {
if (Config.DEBUG) {
Log.i(TAG, "OTE: LONGPRESS && MOVE");
}
Expand Down Expand Up @@ -354,7 +367,20 @@ public boolean onTouchEvent(MotionEvent ev) {
}
}

return mIsLongPressed || super.onTouchEvent(ev);
return isRangeSelectionActive() || super.onTouchEvent(ev);
}

@NonNull
private SelectedDate toggleLongSelectAction(@NonNull final SelectedDate selectedDate) {
if (!toggleRange) {
selectedDate.setFirstDate(selectedDate.getSecondDate());
}
toggleRange = !toggleRange;
return selectedDate;
}

private boolean isRangeSelectionActive() {
return mIsLongPressed || mCanToggleRangeWithoutLongPress;
}

private int resolveDirectionForScroll(float x) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,21 +514,21 @@ private void setCurrentView(int viewIndex) {
/**
* Initialize the state. If the provided values designate an inconsistent
* date the values are normalized before updating the spinners.
*
* @param selectedDate The initial date or date range.
* @param selectedDate The initial date or date range.
* @param canPickRange Enable/disable date range selection
* @param setToggleRangeWithoutLongPress Enable/disable date range selection without long press
* @param callback How user is notified date is changed by
* user, can be null.
*/
//public void init(int year, int monthOfYear, int dayOfMonth, boolean canPickRange,
public void init(SelectedDate selectedDate, boolean canPickRange,
SublimeDatePicker.OnDateChangedListener callback) {
boolean setToggleRangeWithoutLongPress, OnDateChangedListener callback) {
//mCurrentDate.set(Calendar.YEAR, year);
//mCurrentDate.set(Calendar.MONTH, monthOfYear);
//mCurrentDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
mCurrentDate = new SelectedDate(selectedDate);

mDayPickerView.setCanPickRange(canPickRange);
mDayPickerView.setToggleRangeWithoutLongPress(setToggleRangeWithoutLongPress);
mDateChangedListener = callback;

onDateChanged(false, false, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public enum Picker {DATE_PICKER, TIME_PICKER, REPEAT_OPTION_PICKER, INVALID}

// Allow date range selection
private boolean mCanPickDateRange;
// Allow date range selection without long presses
private boolean mToggleRangeWithoutLongPress;

// Defaults
private Picker mPickerToShow = Picker.DATE_PICKER;
Expand Down Expand Up @@ -313,6 +315,19 @@ public boolean canPickDateRange() {
return mCanPickDateRange;
}

public SublimeOptions setToggleRangeWithoutLongPress(boolean toggleRangeWithoutLongPress) {
this.mToggleRangeWithoutLongPress = toggleRangeWithoutLongPress;
if (toggleRangeWithoutLongPress) {
return setCanPickDateRange(true);
} else {
return this;
}
}

public boolean canToggleRangeWithoutLongPress() {
return mToggleRangeWithoutLongPress;
}

@Override
public int describeContents() {
return 0;
Expand All @@ -333,6 +348,7 @@ private void readFromParcel(Parcel in) {
mIs24HourView = in.readByte() != 0;
mRecurrenceRule = in.readString();
mCanPickDateRange = in.readByte() != 0;
mToggleRangeWithoutLongPress = in.readByte() != 0;
}

@Override
Expand All @@ -351,6 +367,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeByte((byte) (mIs24HourView ? 1 : 0));
dest.writeString(mRecurrenceRule);
dest.writeByte((byte) (mCanPickDateRange ? 1 : 0));
dest.writeByte((byte) (mToggleRangeWithoutLongPress ? 1 : 0));
}

public static final Parcelable.Creator<SublimeOptions> CREATOR = new Parcelable.Creator<SublimeOptions>() {
Expand Down