Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
103 changes: 74 additions & 29 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,11 @@ private enum Direction {
private ScrollListener mScrollListener;
private AddEventClickListener mAddEventClickListener;

//Scroll by amount of days desired functionnality
private boolean mIsCustomScrollableAmountsOfDaysEnabled = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have a better name, mIsScrollByAmountOfVisibleDaysEnabled for example

Copy link
Author

@SkyleKayma SkyleKayma Jan 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok ( Mixing bad english and bad imagination created that x) )

private float startOriginForScroll = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference and relation with mCurrentOrigin.x ?

Copy link
Author

@SkyleKayma SkyleKayma Jan 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current origin.x is updated when I started to scroll.
For exemple I start at 0 on x axis.
I start a scroll and now I'am at -200 (Scroll Direction Left)
The mCurrentOrigin.x value is -200.0, and startOriginForScroll will stay at 0. I've done that because I have added in my own repo the functionnality "safe scrolling" that let you go back to the latest valid position you had.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, but wouldn't it be possible without this variable ? If it is possible, please remove this extra variable

private float sizeOfScrollableView;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't create a field for this, but use a method.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok


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

@Override
Expand Down Expand Up @@ -279,7 +284,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(!mIsCustomScrollableAmountsOfDaysEnabled) {
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 @@ -1643,6 +1650,7 @@ public void setNumberOfVisibleDays(int numberOfVisibleDays) {
this.mNumberOfVisibleDays = numberOfVisibleDays;
resetHomeDate();
mCurrentOrigin.x = 0;
startOriginForScroll = mCurrentOrigin.x;
mCurrentOrigin.y = 0;
invalidate();
}
Expand Down Expand Up @@ -2418,40 +2426,72 @@ 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(!mIsCustomScrollableAmountsOfDaysEnabled) {
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);
}
// Reset scrolling and fling direction.
mCurrentScrollDirection = mCurrentFlingDirection = Direction.NONE;
}else {
sizeOfScrollableView = (mWidthPerDay + mColumnGap) * getNumberOfVisibleDays();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some code duplication in the if part and in the else part. It would be best, if that part can be executed outside of the if else. Especially the mScroller.startScroll etc

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could move outside :

// Reset scrolling and fling direction.
mCurrentScrollDirection = mCurrentFlingDirection = Direction.NONE;

But the rest of the code is not really a duplicate part :/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are a lot of lines that are equal but have different values for the parameters, you can probably create variables for those parameters, set these variables in the if and else and place a lot of code after the if else

float beforeScroll = startOriginForScroll;
boolean isPassed = false;

if (mCurrentScrollDirection == Direction.LEFT) {
startOriginForScroll -= sizeOfScrollableView;
isPassed = true;
} else if (mCurrentScrollDirection == Direction.RIGHT) {
startOriginForScroll += sizeOfScrollableView;
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) - sizeOfScrollableView), 0, 200);
} else if (mCurrentScrollDirection == Direction.RIGHT) {
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, (int) (sizeOfScrollableView - (mCurrentOrigin.x - beforeScroll)), 0, 200);
}
ViewCompat.postInvalidateOnAnimation(WeekView.this);
}
// Reset scrolling and fling direction.
mCurrentScrollDirection = mCurrentFlingDirection = Direction.NONE;
}
// Reset scrolling and fling direction.
mCurrentScrollDirection = mCurrentFlingDirection = Direction.NONE;
}


Expand Down Expand Up @@ -2488,6 +2528,10 @@ private boolean forceFinishScroll() {
}
}

public void setCustomScrolableDaysEnabled(boolean isEnabled){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name if this setter has to change, according to the new name for the field.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

mIsCustomScrollableAmountsOfDaysEnabled = isEnabled;
}


/////////////////////////////////////////////////////////////////
//
Expand Down Expand Up @@ -2524,6 +2568,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