Skip to content

Commit 8b92d78

Browse files
author
Jens Claes
committed
Fixes to goToHour and goToDate().
Allow goToDate to be called before the weekview is drawn for the first time. goToDate relies on mWidthPerDay, which itself relies on View#getWidth() which can't be called during initialisation. Therefore the goToDate variable is cached until the first time the weekview is drawn. Fixes #45: 1. Date is cloned before use (in goToDate) 2. goToHour doesn't throw errors anymore Fixes #55: goToDate now uses an animation Fixes #65: If a dimension is altered and is followed by a goToDate or goToHour, then the hour or date to go to, is cached.
1 parent 9f9b25b commit 8b92d78

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

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

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,15 @@ public class WeekView extends View {
9494
private int mHeaderColumnBackgroundColor = Color.WHITE;
9595
private int mDefaultEventColor;
9696
private boolean mIsFirstDraw = true;
97+
private boolean mAreDimensionsInvalid = true;
9798
@Deprecated private int mDayNameLength = LENGTH_LONG;
9899
private int mOverlappingEventGap = 0;
99100
private int mEventMarginVertical = 0;
100101
private float mXScrollingSpeed = 1f;
101102
private Calendar mFirstVisibleDay;
102103
private Calendar mLastVisibleDay;
104+
private Calendar mScrollToDay = null;
105+
private double mScrollToHour = -1;
103106

104107
// Listeners.
105108
private EventClickListener mEventClickListener;
@@ -378,13 +381,23 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
378381
mWidthPerDay = getWidth() - mHeaderColumnWidth - mColumnGap * (mNumberOfVisibleDays - 1);
379382
mWidthPerDay = mWidthPerDay/mNumberOfVisibleDays;
380383

381-
// If the week view is being drawn for the first time, then consider the first day of week.
382-
if (mIsFirstDraw && mNumberOfVisibleDays >= 7) {
383-
if (mToday.get(Calendar.DAY_OF_WEEK) != mFirstDayOfWeek) {
384+
if (mAreDimensionsInvalid) {
385+
mAreDimensionsInvalid = false;
386+
double scrollToHour = mScrollToHour;
387+
388+
if(mScrollToDay != null)
389+
goToDate(mScrollToDay);
390+
if(scrollToHour >= 0)
391+
goToHour(scrollToHour);
392+
}
393+
if (mIsFirstDraw){
394+
mIsFirstDraw = false;
395+
396+
// If the week view is being drawn for the first time, then consider the first day of the week.
397+
if(mNumberOfVisibleDays >= 7 && mToday.get(Calendar.DAY_OF_WEEK) != mFirstDayOfWeek) {
384398
int difference = 7 + (mToday.get(Calendar.DAY_OF_WEEK) - mFirstDayOfWeek);
385399
mCurrentOrigin.x += (mWidthPerDay + mColumnGap) * difference;
386400
}
387-
mIsFirstDraw = false;
388401
}
389402

390403
// Consider scroll offset.
@@ -904,6 +917,13 @@ private void deleteFarMonths(Calendar currentDay) {
904917
mEventRects.addAll(newEvents);
905918
}
906919

920+
@Override
921+
public void invalidate() {
922+
super.invalidate();
923+
mAreDimensionsInvalid = true;
924+
mScrollToDay = null;
925+
mScrollToHour = -1;
926+
}
907927

908928
/////////////////////////////////////////////////////////////////
909929
//
@@ -1351,6 +1371,11 @@ public void goToDate(Calendar date) {
13511371
date.set(Calendar.SECOND, 0);
13521372
date.set(Calendar.MILLISECOND, 0);
13531373

1374+
if(mAreDimensionsInvalid) {
1375+
mScrollToDay = date;
1376+
return;
1377+
}
1378+
13541379
mRefreshEvents = true;
13551380

13561381
Calendar today = Calendar.getInstance();
@@ -1360,8 +1385,7 @@ public void goToDate(Calendar date) {
13601385
today.set(Calendar.MILLISECOND, 0);
13611386

13621387
int dateDifference = (int) ((date.getTimeInMillis() - today.getTimeInMillis()) / (1000 * 60 * 60 * 24));
1363-
mCurrentOrigin.x = - dateDifference * (mWidthPerDay + mColumnGap);
1364-
1388+
mStickyScroller.startScroll((int) mCurrentOrigin.x, 0, (int) (-dateDifference*(mWidthPerDay + mColumnGap)-mCurrentOrigin.x), 0);
13651389
invalidate();
13661390
}
13671391

@@ -1378,14 +1402,18 @@ public void notifyDatasetChanged(){
13781402
* @param hour The hour to scroll to in 24-hour format. Supported values are 0-24.
13791403
*/
13801404
public void goToHour(double hour){
1405+
int verticalOffset = (int) (mHourHeight * hour);
13811406
if (hour < 0)
1382-
throw new IllegalArgumentException("Cannot scroll to an hour of negative value.");
1407+
verticalOffset = 0;
13831408
else if (hour > 24)
1384-
throw new IllegalArgumentException("Cannot scroll to an hour of value greater than 24.");
1385-
else if (hour * mHourHeight > mHourHeight * 24 - getHeight() + mHeaderTextHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom)
1386-
throw new IllegalArgumentException("Cannot scroll to an hour which will result the calendar to go off the screen.");
1409+
verticalOffset = mHourHeight * 24;
1410+
1411+
if (mAreDimensionsInvalid) {
1412+
mScrollToHour = hour;
1413+
return;
1414+
} else if (verticalOffset > mHourHeight * 24 - getHeight() + mHeaderTextHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom)
1415+
verticalOffset = (int)(mHourHeight * 24 - getHeight() + mHeaderTextHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom);
13871416

1388-
int verticalOffset = (int) (mHourHeight * hour);
13891417
mCurrentOrigin.y = -verticalOffset;
13901418
invalidate();
13911419
}

0 commit comments

Comments
 (0)