Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow scroll by the amount of days setted in NumberOfVisibleDays #68

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
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
102 changes: 74 additions & 28 deletions library/src/main/java/com/alamkanak/weekview/WeekView.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ private enum Direction {
private ScrollListener mScrollListener;
private AddEventClickListener mAddEventClickListener;

//Scroll by amount of days desired functionnality
private boolean mScrollByAmountOfVisibleDaysEnabled = false;
private float startOriginForScroll = 0;

private final GestureDetector.SimpleOnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener() {

@Override
Expand Down Expand Up @@ -279,7 +283,9 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
switch (mCurrentFlingDirection) {
case LEFT:
case RIGHT:
mScroller.fling((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, (int) (velocityX * mXScrollingSpeed), 0, (int) getXMinLimit(), (int) getXMaxLimit(), (int) getYMinLimit(), (int) getYMaxLimit());
if(!mScrollByAmountOfVisibleDaysEnabled) {
mScroller.fling((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, (int) (velocityX * mXScrollingSpeed), 0, (int) getXMinLimit(), (int) getXMaxLimit(), (int) getYMinLimit(), (int) getYMaxLimit());
}
break;
case VERTICAL:
mScroller.fling((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, 0, (int) velocityY, (int) getXMinLimit(), (int) getXMaxLimit(), (int) getYMinLimit(), (int) getYMaxLimit());
Expand Down Expand Up @@ -1446,7 +1452,6 @@ private boolean isEventsCollide(WeekViewEvent event1, WeekViewEvent event2) {
return !((start1 >= end2) || (end1 <= start2));
}


/**
* Checks if time1 occurs after (or at the same time) time2.
* @param time1 The time to check.
Expand Down Expand Up @@ -1643,6 +1648,7 @@ public void setNumberOfVisibleDays(int numberOfVisibleDays) {
this.mNumberOfVisibleDays = numberOfVisibleDays;
resetHomeDate();
mCurrentOrigin.x = 0;
startOriginForScroll = mCurrentOrigin.x;
mCurrentOrigin.y = 0;
invalidate();
}
Expand Down Expand Up @@ -1715,6 +1721,10 @@ public void setHeaderColumnPadding(int headerColumnPadding) {
invalidate();
}

public float getSizeOfScrollableView(){
return (mWidthPerDay + mColumnGap) * getNumberOfVisibleDays();
}

public int getHeaderColumnTextColor() {
return mHeaderColumnTextColor;
}
Expand Down Expand Up @@ -2418,38 +2428,69 @@ public boolean onTouchEvent(MotionEvent event) {
}

private void goToNearestOrigin(){
double leftDays = mCurrentOrigin.x / (mWidthPerDay + mColumnGap);

if (mCurrentFlingDirection != Direction.NONE) {
// snap to nearest day
leftDays = Math.round(leftDays);
} else if (mCurrentScrollDirection == Direction.LEFT) {
// snap to last day
leftDays = Math.floor(leftDays);
} else if (mCurrentScrollDirection == Direction.RIGHT) {
// snap to next day
leftDays = Math.ceil(leftDays);
} else {
// snap to nearest day
leftDays = Math.round(leftDays);
}
if(!mScrollByAmountOfVisibleDaysEnabled ) {
double leftDays = mCurrentOrigin.x / (mWidthPerDay + mColumnGap);

int nearestOrigin = (int) (mCurrentOrigin.x - leftDays * (mWidthPerDay + mColumnGap));
boolean mayScrollHorizontal = mCurrentOrigin.x - nearestOrigin < getXMaxLimit()
&& mCurrentOrigin.x - nearestOrigin > getXMinLimit();
if (mCurrentFlingDirection != Direction.NONE) {
// snap to nearest day
leftDays = Math.round(leftDays);
} else if (mCurrentScrollDirection == Direction.LEFT) {
// snap to last day
leftDays = Math.floor(leftDays);
} else if (mCurrentScrollDirection == Direction.RIGHT) {
// snap to next day
leftDays = Math.ceil(leftDays);
} else {
// snap to nearest day
leftDays = Math.round(leftDays);
}

if (mayScrollHorizontal) {
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, - nearestOrigin, 0);
ViewCompat.postInvalidateOnAnimation(WeekView.this);
}
int nearestOrigin = (int) (mCurrentOrigin.x - leftDays * (mWidthPerDay + mColumnGap));
boolean mayScrollHorizontal = mCurrentOrigin.x - nearestOrigin < getXMaxLimit()
&& mCurrentOrigin.x - nearestOrigin > getXMinLimit();

if (mayScrollHorizontal) {
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, -nearestOrigin, 0);
ViewCompat.postInvalidateOnAnimation(WeekView.this);
}

if (nearestOrigin != 0 && mayScrollHorizontal) {
// Stop current animation.
mScroller.forceFinished(true);
// Snap to date.
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, -nearestOrigin, 0, (int) (Math.abs(nearestOrigin) / mWidthPerDay * mScrollDuration));
ViewCompat.postInvalidateOnAnimation(WeekView.this);
}

}else {
float beforeScroll = startOriginForScroll;
boolean isPassed = false;

if (mCurrentScrollDirection == Direction.LEFT) {
startOriginForScroll -= getSizeOfScrollableView();
isPassed = true;
} else if (mCurrentScrollDirection == Direction.RIGHT) {
startOriginForScroll += getSizeOfScrollableView();
isPassed = true;
}

boolean mayScrollHorizontal = beforeScroll - startOriginForScroll < getXMaxLimit()
&& mCurrentOrigin.x - startOriginForScroll > getXMinLimit();

if (nearestOrigin != 0 && mayScrollHorizontal) {
// Stop current animation.
mScroller.forceFinished(true);
// Snap to date.
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, -nearestOrigin, 0, (int) (Math.abs(nearestOrigin) / mWidthPerDay * mScrollDuration));
ViewCompat.postInvalidateOnAnimation(WeekView.this);

if (isPassed && mayScrollHorizontal) {
// Snap to date.
if (mCurrentScrollDirection == Direction.LEFT) {
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, (int) ((beforeScroll - mCurrentOrigin.x) - getSizeOfScrollableView()), 0, 200);
} else if (mCurrentScrollDirection == Direction.RIGHT) {
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, (int) (getSizeOfScrollableView() - (mCurrentOrigin.x - beforeScroll)), 0, 200);
}
ViewCompat.postInvalidateOnAnimation(WeekView.this);
}
}

// Reset scrolling and fling direction.
mCurrentScrollDirection = mCurrentFlingDirection = Direction.NONE;
}
Expand Down Expand Up @@ -2488,6 +2529,10 @@ private boolean forceFinishScroll() {
}
}

public void setScrollByAmountOfVisibleDaysEnabled (boolean isEnabled){
mScrollByAmountOfVisibleDaysEnabled = isEnabled;
}


/////////////////////////////////////////////////////////////////
//
Expand Down Expand Up @@ -2524,6 +2569,7 @@ public void goToDate(Calendar date) {
mRefreshEvents = true;

mCurrentOrigin.x = - daysBetween(mHomeDate, date) * (mWidthPerDay + mColumnGap);
startOriginForScroll = mCurrentOrigin.x;
invalidate();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ protected void onCreate(Bundle savedInstanceState) {
// Set AddEvent Click Listener
mWeekView.setAddEventClickListener(this);

//Activate the scrolling by the NumberOfVisibleDays desired
mWeekView.setCustomScrolableDaysEnabled(true);

// Set minDate
/*Calendar minDate = Calendar.getInstance();
minDate.set(Calendar.DAY_OF_MONTH, 1);
Expand Down