Skip to content

Commit 69ac904

Browse files
author
Jens Claes
committed
Merge branch 'BetterGoToDateAndHour' into quivr-master
Conflicts: library/src/main/java/com/alamkanak/weekview/WeekView.java
2 parents 50979d8 + c36b8a8 commit 69ac904

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

library/src/main/java/com/alamkanak/weekview/WeekView.java

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,15 @@ public class WeekView extends View {
106106
private int mHeaderColumnBackgroundColor = Color.WHITE;
107107
private int mDefaultEventColor;
108108
private boolean mIsFirstDraw = true;
109+
private boolean mAreDimensionsInvalid = true;
109110
@Deprecated private int mDayNameLength = LENGTH_LONG;
110111
private int mOverlappingEventGap = 0;
111112
private int mEventMarginVertical = 0;
112113
private float mXScrollingSpeed = 1f;
113114
private Calendar mFirstVisibleDay;
114115
private Calendar mLastVisibleDay;
116+
private Calendar mScrollToDay = null;
117+
private double mScrollToHour = -1;
115118

116119
// Listeners.
117120
private EventClickListener mEventClickListener;
@@ -413,13 +416,22 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
413416

414417
Calendar today = today();
415418

416-
// If the week view is being drawn for the first time, then consider the first day of week.
417-
if (mIsFirstDraw && mNumberOfVisibleDays >= 7) {
418-
if (today.get(Calendar.DAY_OF_WEEK) != mFirstDayOfWeek) {
419+
if (mAreDimensionsInvalid) {
420+
mAreDimensionsInvalid = false;
421+
422+
if(mScrollToDay != null)
423+
goToDate(mScrollToDay);
424+
if(mScrollToHour >= 0)
425+
goToHour(mScrollToHour);
426+
}
427+
if (mIsFirstDraw){
428+
mIsFirstDraw = false;
429+
430+
// If the week view is being drawn for the first time, then consider the first day of the week.
431+
if(mNumberOfVisibleDays >= 7 && today.get(Calendar.DAY_OF_WEEK) != mFirstDayOfWeek) {
419432
int difference = 7 + (today.get(Calendar.DAY_OF_WEEK) - mFirstDayOfWeek);
420433
mCurrentOrigin.x += (mWidthPerDay + mColumnGap) * difference;
421434
}
422-
mIsFirstDraw = false;
423435
}
424436

425437
// Consider scroll offset.
@@ -980,6 +992,13 @@ private void deleteFarMonths(Calendar currentDay) {
980992
mEventRects.addAll(newEvents);
981993
}
982994

995+
@Override
996+
public void invalidate() {
997+
super.invalidate();
998+
mAreDimensionsInvalid = true;
999+
mScrollToDay = null;
1000+
mScrollToHour = -1;
1001+
}
9831002

9841003
/////////////////////////////////////////////////////////////////
9851004
//
@@ -1441,6 +1460,11 @@ public void goToDate(Calendar date) {
14411460
date.set(Calendar.SECOND, 0);
14421461
date.set(Calendar.MILLISECOND, 0);
14431462

1463+
if(mAreDimensionsInvalid) {
1464+
mScrollToDay = date;
1465+
return;
1466+
}
1467+
14441468
mRefreshEvents = true;
14451469

14461470
Calendar today = Calendar.getInstance();
@@ -1450,8 +1474,7 @@ public void goToDate(Calendar date) {
14501474
today.set(Calendar.MILLISECOND, 0);
14511475

14521476
int dateDifference = (int) ((date.getTimeInMillis() - today.getTimeInMillis()) / (1000 * 60 * 60 * 24));
1453-
mCurrentOrigin.x = - dateDifference * (mWidthPerDay + mColumnGap);
1454-
1477+
mStickyScroller.startScroll((int) mCurrentOrigin.x, 0, (int) (-dateDifference*(mWidthPerDay + mColumnGap)-mCurrentOrigin.x), 0);
14551478
invalidate();
14561479
}
14571480

@@ -1468,14 +1491,18 @@ public void notifyDatasetChanged(){
14681491
* @param hour The hour to scroll to in 24-hour format. Supported values are 0-24.
14691492
*/
14701493
public void goToHour(double hour){
1494+
int verticalOffset = (int) (mHourHeight * hour);
14711495
if (hour < 0)
1472-
throw new IllegalArgumentException("Cannot scroll to an hour of negative value.");
1496+
verticalOffset = 0;
14731497
else if (hour > 24)
1474-
throw new IllegalArgumentException("Cannot scroll to an hour of value greater than 24.");
1475-
else if (hour * mHourHeight > mHourHeight * 24 - getHeight() + mHeaderTextHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom)
1476-
throw new IllegalArgumentException("Cannot scroll to an hour which will result the calendar to go off the screen.");
1498+
verticalOffset = mHourHeight * 24;
1499+
1500+
if (mAreDimensionsInvalid) {
1501+
mScrollToHour = hour;
1502+
return;
1503+
} else if (verticalOffset > mHourHeight * 24 - getHeight() + mHeaderTextHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom)
1504+
verticalOffset = (int)(mHourHeight * 24 - getHeight() + mHeaderTextHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom);
14771505

1478-
int verticalOffset = (int) (mHourHeight * hour);
14791506
mCurrentOrigin.y = -verticalOffset;
14801507
invalidate();
14811508
}

0 commit comments

Comments
 (0)