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

Scroll NumberOfVisibleDays by NumberOfVisibleDays (inspired by #68) #88

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
126 changes: 97 additions & 29 deletions library/src/main/java/com/alamkanak/weekview/WeekView.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ private enum Direction {
private int mScaledTouchSlop = 0;
private EventRect mNewEventRect;
private TextColorPicker textColorPicker;
private float mSizeOfWeekView;
private float mDistanceDone = 0;
private float mDistanceMin;
protected int mOffsetValueToSecureScreen = 9;
private float mStartOriginForScroll = 0;

// Attributes and their default values.
private int mHourHeight = 50;
Expand Down Expand Up @@ -174,6 +179,7 @@ private enum Direction {
private boolean mAutoLimitTime = false;
private boolean mEnableDropListener = false;
private int mMinOverlappingMinutes = 0;
private boolean mIsScrollNumberOfVisibleDays = false;

// Listeners.
private EventClickListener mEventClickListener;
Expand All @@ -190,6 +196,7 @@ private enum Direction {

@Override
public boolean onDown(MotionEvent e) {
mStartOriginForScroll = mCurrentOrigin.x;
goToNearestOrigin();
return true;
}
Expand Down Expand Up @@ -238,6 +245,13 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
case RIGHT:
float minX = getXMinLimit();
float maxX = getXMaxLimit();

if (e2.getX() < 0) {
mDistanceDone = e2.getX() - e1.getX();
} else {
mDistanceDone = e1.getX() - e2.getX();
}

if ((mCurrentOrigin.x - (distanceX * mXScrollingSpeed)) > maxX) {
mCurrentOrigin.x = maxX;
} else if ((mCurrentOrigin.x - (distanceX * mXScrollingSpeed)) < minX) {
Expand Down Expand Up @@ -282,7 +296,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(!mIsScrollNumberOfVisibleDays) {
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 @@ -494,6 +510,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
if (a.getBoolean(R.styleable.WeekView_dropListenerEnabled, false))
this.enableDropListener();
mMinOverlappingMinutes = a.getInt(R.styleable.WeekView_minOverlappingMinutes, 0);
mIsScrollNumberOfVisibleDays = a.getBoolean(R.styleable.WeekView_isScrollNumberOfVisibleDays, false);
} finally {
a.recycle();
}
Expand Down Expand Up @@ -2517,6 +2534,15 @@ public int getMinOverlappingMinutes() {
return this.mMinOverlappingMinutes;
}

public boolean isScrollNumberOfVisibleDays() {
return this.mIsScrollNumberOfVisibleDays;
}

public void setScrollNumberOfVisibleDays(boolean scrollNumberOfVisibleDays) {
this.mIsScrollNumberOfVisibleDays = scrollNumberOfVisibleDays;
invalidate();
}

/////////////////////////////////////////////////////////////////
//
// Functions related to scrolling.
Expand All @@ -2525,6 +2551,10 @@ public int getMinOverlappingMinutes() {

@Override
public boolean onTouchEvent(MotionEvent event) {

mSizeOfWeekView = (mWidthPerDay + mColumnGap) * getNumberOfVisibleDays();
mDistanceMin = mSizeOfWeekView / mOffsetValueToSecureScreen;

mScaleDetector.onTouchEvent(event);
boolean val = mGestureDetector.onTouchEvent(event);

Expand All @@ -2542,41 +2572,79 @@ 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);
}
float beforeScroll = mStartOriginForScroll;
boolean isPassed = false;

if (mDistanceDone > mDistanceMin || mDistanceDone < -mDistanceMin || !mIsScrollNumberOfVisibleDays) {

if (!mIsScrollNumberOfVisibleDays && mCurrentFlingDirection != Direction.NONE) {
// snap to nearest day
leftDays = Math.round(leftDays);
} else if (mCurrentScrollDirection == Direction.LEFT) {
// snap to last day
leftDays = Math.floor(leftDays);
mStartOriginForScroll -= mSizeOfWeekView;
isPassed = true;
} else if (mCurrentScrollDirection == Direction.RIGHT) {
// snap to next day
leftDays = Math.floor(leftDays);
mStartOriginForScroll += mSizeOfWeekView;
isPassed = true;
} else {
// snap to nearest day
leftDays = Math.round(leftDays);
}

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 (mIsScrollNumberOfVisibleDays) {
boolean mayScrollHorizontal = beforeScroll - mStartOriginForScroll < getXMaxLimit() && mCurrentOrigin.x - mStartOriginForScroll > getXMinLimit();
if (isPassed && mayScrollHorizontal) {
// Stop current animation.
mScroller.forceFinished(true);
// Snap to date.
if (mCurrentScrollDirection == Direction.LEFT) {
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, (int) ((beforeScroll - mCurrentOrigin.x) - mSizeOfWeekView), 0, 200);
} else if (mCurrentScrollDirection == Direction.RIGHT) {
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, (int) (mSizeOfWeekView - (mCurrentOrigin.x - beforeScroll)), 0, 200);
}
ViewCompat.postInvalidateOnAnimation(WeekView.this);
}
}
else{
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.
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 {
mScroller.forceFinished(true);
// Snap to date.
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, -nearestOrigin, 0, (int) (Math.abs(nearestOrigin) / mWidthPerDay * mScrollDuration));
if (mCurrentScrollDirection == Direction.LEFT) {
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, (int) beforeScroll - (int) mCurrentOrigin.x, 0, 200);
} else if (mCurrentScrollDirection == Direction.RIGHT) {
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, (int) beforeScroll - (int) mCurrentOrigin.x, 0, 200);
}
ViewCompat.postInvalidateOnAnimation(WeekView.this);

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


@Override
public void computeScroll() {
super.computeScroll();
Expand Down Expand Up @@ -2851,4 +2919,4 @@ public boolean onDrag(View v, DragEvent e) {
return true;
}
}
}
}
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@
<attr name="zoomFocusPoint" format="fraction" />
<attr name="zoomFocusPointEnabled" format="boolean" />
<attr name="dropListenerEnabled" format="boolean" />
<attr name="isScrollNumberOfVisibleDays" format="boolean" />
</declare-styleable>
</resources>