Skip to content

Commit 472162d

Browse files
-updated sdks
-added ability to scroll whole view (entire week, for example) like a ViewPager. -made default first day of week to be the one of the calendar.
1 parent 2a5f09a commit 472162d

File tree

6 files changed

+171
-103
lines changed

6 files changed

+171
-103
lines changed

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
buildscript {
44
repositories {
55
jcenter()
6+
google()
67
}
78
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.3.3'
9+
classpath 'com.android.tools.build:gradle:3.2.0-alpha13'
910

1011
// NOTE: Do not place your application dependencies here; they belong
1112
// in the individual module build.gradle files
@@ -19,5 +20,6 @@ allprojects {
1920

2021
repositories {
2122
jcenter()
23+
google()
2224
}
2325
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Sun Aug 06 18:02:35 CEST 2017
1+
#Mon May 07 11:03:02 IDT 2018
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip

library/build.gradle

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@ repositories {
55
}
66

77
android {
8-
compileSdkVersion 25
9-
buildToolsVersion '25.0.2'
8+
compileSdkVersion 27
109

1110
defaultConfig {
12-
minSdkVersion 9
13-
targetSdkVersion 25
11+
minSdkVersion 14
12+
targetSdkVersion 27
1413
}
1514
}
1615

1716
configurations {
1817
javadocDeps
1918
}
2019
dependencies {
21-
compile 'com.android.support:appcompat-v7:25.1.0'
22-
javadocDeps 'com.android.support:appcompat-v7:25.1.0'
20+
compile 'com.android.support:appcompat-v7:27.1.1'
21+
javadocDeps 'com.android.support:appcompat-v7:27.1.1'
2322
}
2423

25-
apply from: 'gradle-mvn-push.gradle'
24+
apply from: 'gradle-mvn-push.gradle'

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

Lines changed: 96 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ private enum Direction {
110110
private int mScaledTouchSlop = 0;
111111
private EventRect mNewEventRect;
112112
private TextColorPicker textColorPicker;
113+
private float mSizeOfWeekView;
114+
private float mDistanceDone = 0;
115+
private float mDistanceMin;
116+
protected int mOffsetValueToSecureScreen = 9;
117+
private float mStartOriginForScroll = 0;
113118

114119
// Attributes and their default values.
115120
private int mHourHeight = 50;
@@ -118,7 +123,7 @@ private enum Direction {
118123
private int mEffectiveMinHourHeight = mMinHourHeight; //compensates for the fact that you can't keep zooming out.
119124
private int mMaxHourHeight = 250;
120125
private int mColumnGap = 10;
121-
private int mFirstDayOfWeek = Calendar.MONDAY;
126+
private int mFirstDayOfWeek = Calendar.getInstance().getFirstDayOfWeek();
122127
private int mTextSize = 12;
123128
private int mHeaderColumnPadding = 10;
124129
private int mHeaderColumnTextColor = Color.BLACK;
@@ -174,6 +179,7 @@ private enum Direction {
174179
private boolean mAutoLimitTime = false;
175180
private boolean mEnableDropListener = false;
176181
private int mMinOverlappingMinutes = 0;
182+
private boolean mIsScrollNumberOfVisibleDays = false;
177183

178184
// Listeners.
179185
private EventClickListener mEventClickListener;
@@ -190,6 +196,7 @@ private enum Direction {
190196

191197
@Override
192198
public boolean onDown(MotionEvent e) {
199+
mStartOriginForScroll = mCurrentOrigin.x;
193200
goToNearestOrigin();
194201
return true;
195202
}
@@ -238,6 +245,13 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
238245
case RIGHT:
239246
float minX = getXMinLimit();
240247
float maxX = getXMaxLimit();
248+
249+
if (e2.getX() < 0) {
250+
mDistanceDone = e2.getX() - e1.getX();
251+
} else {
252+
mDistanceDone = e1.getX() - e2.getX();
253+
}
254+
241255
if ((mCurrentOrigin.x - (distanceX * mXScrollingSpeed)) > maxX) {
242256
mCurrentOrigin.x = maxX;
243257
} else if ((mCurrentOrigin.x - (distanceX * mXScrollingSpeed)) < minX) {
@@ -282,7 +296,9 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
282296
switch (mCurrentFlingDirection) {
283297
case LEFT:
284298
case RIGHT:
285-
mScroller.fling((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, (int) (velocityX * mXScrollingSpeed), 0, (int) getXMinLimit(), (int) getXMaxLimit(), (int) getYMinLimit(), (int) getYMaxLimit());
299+
if (!mIsScrollNumberOfVisibleDays) {
300+
mScroller.fling((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, (int) (velocityX * mXScrollingSpeed), 0, (int) getXMinLimit(), (int) getXMaxLimit(), (int) getYMinLimit(), (int) getYMaxLimit());
301+
}
286302
break;
287303
case VERTICAL:
288304
mScroller.fling((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, 0, (int) velocityY, (int) getXMinLimit(), (int) getXMaxLimit(), (int) getYMinLimit(), (int) getYMaxLimit());
@@ -494,6 +510,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
494510
if (a.getBoolean(R.styleable.WeekView_dropListenerEnabled, false))
495511
this.enableDropListener();
496512
mMinOverlappingMinutes = a.getInt(R.styleable.WeekView_minOverlappingMinutes, 0);
513+
mIsScrollNumberOfVisibleDays = a.getBoolean(R.styleable.WeekView_isScrollNumberOfVisibleDays, false);
497514
} finally {
498515
a.recycle();
499516
}
@@ -2517,6 +2534,15 @@ public int getMinOverlappingMinutes() {
25172534
return this.mMinOverlappingMinutes;
25182535
}
25192536

2537+
public boolean isScrollNumberOfVisibleDays() {
2538+
return this.mIsScrollNumberOfVisibleDays;
2539+
}
2540+
2541+
public void setScrollNumberOfVisibleDays(boolean scrollNumberOfVisibleDays) {
2542+
this.mIsScrollNumberOfVisibleDays = scrollNumberOfVisibleDays;
2543+
invalidate();
2544+
}
2545+
25202546
/////////////////////////////////////////////////////////////////
25212547
//
25222548
// Functions related to scrolling.
@@ -2525,6 +2551,10 @@ public int getMinOverlappingMinutes() {
25252551

25262552
@Override
25272553
public boolean onTouchEvent(MotionEvent event) {
2554+
2555+
mSizeOfWeekView = (mWidthPerDay + mColumnGap) * getNumberOfVisibleDays();
2556+
mDistanceMin = mSizeOfWeekView / mOffsetValueToSecureScreen;
2557+
25282558
mScaleDetector.onTouchEvent(event);
25292559
boolean val = mGestureDetector.onTouchEvent(event);
25302560

@@ -2542,41 +2572,78 @@ public boolean onTouchEvent(MotionEvent event) {
25422572
private void goToNearestOrigin() {
25432573
double leftDays = mCurrentOrigin.x / (mWidthPerDay + mColumnGap);
25442574

2545-
if (mCurrentFlingDirection != Direction.NONE) {
2546-
// snap to nearest day
2547-
leftDays = Math.round(leftDays);
2548-
} else if (mCurrentScrollDirection == Direction.LEFT) {
2549-
// snap to last day
2550-
leftDays = Math.floor(leftDays);
2551-
} else if (mCurrentScrollDirection == Direction.RIGHT) {
2552-
// snap to next day
2553-
leftDays = Math.ceil(leftDays);
2554-
} else {
2555-
// snap to nearest day
2556-
leftDays = Math.round(leftDays);
2557-
}
2575+
float beforeScroll = mStartOriginForScroll;
2576+
boolean isPassed = false;
2577+
2578+
if (mDistanceDone > mDistanceMin || mDistanceDone < -mDistanceMin || !mIsScrollNumberOfVisibleDays) {
2579+
2580+
if (!mIsScrollNumberOfVisibleDays && mCurrentFlingDirection != Direction.NONE) {
2581+
// snap to nearest day
2582+
leftDays = Math.round(leftDays);
2583+
} else if (mCurrentScrollDirection == Direction.LEFT) {
2584+
// snap to last day
2585+
leftDays = Math.floor(leftDays);
2586+
mStartOriginForScroll -= mSizeOfWeekView;
2587+
isPassed = true;
2588+
} else if (mCurrentScrollDirection == Direction.RIGHT) {
2589+
// snap to next day
2590+
leftDays = Math.floor(leftDays);
2591+
mStartOriginForScroll += mSizeOfWeekView;
2592+
isPassed = true;
2593+
} else {
2594+
// snap to nearest day
2595+
leftDays = Math.round(leftDays);
2596+
}
25582597

2559-
int nearestOrigin = (int) (mCurrentOrigin.x - leftDays * (mWidthPerDay + mColumnGap));
2560-
boolean mayScrollHorizontal = mCurrentOrigin.x - nearestOrigin < getXMaxLimit()
2561-
&& mCurrentOrigin.x - nearestOrigin > getXMinLimit();
25622598

2563-
if (mayScrollHorizontal) {
2564-
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, -nearestOrigin, 0);
2565-
ViewCompat.postInvalidateOnAnimation(WeekView.this);
2566-
}
2599+
if (mIsScrollNumberOfVisibleDays) {
2600+
boolean mayScrollHorizontal = beforeScroll - mStartOriginForScroll < getXMaxLimit() && mCurrentOrigin.x - mStartOriginForScroll > getXMinLimit();
2601+
if (isPassed && mayScrollHorizontal) {
2602+
// Stop current animation.
2603+
mScroller.forceFinished(true);
2604+
// Snap to date.
2605+
if (mCurrentScrollDirection == Direction.LEFT) {
2606+
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, (int) ((beforeScroll - mCurrentOrigin.x) - mSizeOfWeekView), 0, 200);
2607+
} else if (mCurrentScrollDirection == Direction.RIGHT) {
2608+
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, (int) (mSizeOfWeekView - (mCurrentOrigin.x - beforeScroll)), 0, 200);
2609+
}
2610+
ViewCompat.postInvalidateOnAnimation(WeekView.this);
2611+
}
2612+
} else {
2613+
int nearestOrigin = (int) (mCurrentOrigin.x - leftDays * (mWidthPerDay + mColumnGap));
2614+
boolean mayScrollHorizontal = mCurrentOrigin.x - nearestOrigin < getXMaxLimit() && mCurrentOrigin.x - nearestOrigin > getXMinLimit();
2615+
if (mayScrollHorizontal) {
2616+
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, -nearestOrigin, 0);
2617+
ViewCompat.postInvalidateOnAnimation(WeekView.this);
2618+
}
25672619

2568-
if (nearestOrigin != 0 && mayScrollHorizontal) {
2569-
// Stop current animation.
2620+
if (nearestOrigin != 0 && mayScrollHorizontal) {
2621+
// Stop current animation.
2622+
mScroller.forceFinished(true);
2623+
// Snap to date.
2624+
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, -nearestOrigin, 0, (int) (Math.abs(nearestOrigin) / mWidthPerDay * mScrollDuration));
2625+
ViewCompat.postInvalidateOnAnimation(WeekView.this);
2626+
}
2627+
}
2628+
2629+
// Reset scrolling and fling direction.
2630+
mCurrentScrollDirection = mCurrentFlingDirection = Direction.NONE;
2631+
2632+
2633+
} else {
25702634
mScroller.forceFinished(true);
2571-
// Snap to date.
2572-
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, -nearestOrigin, 0, (int) (Math.abs(nearestOrigin) / mWidthPerDay * mScrollDuration));
2635+
if (mCurrentScrollDirection == Direction.LEFT) {
2636+
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, (int) beforeScroll - (int) mCurrentOrigin.x, 0, 200);
2637+
} else if (mCurrentScrollDirection == Direction.RIGHT) {
2638+
mScroller.startScroll((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, (int) beforeScroll - (int) mCurrentOrigin.x, 0, 200);
2639+
}
25732640
ViewCompat.postInvalidateOnAnimation(WeekView.this);
2641+
2642+
// Reset scrolling and fling direction.
2643+
mCurrentScrollDirection = mCurrentFlingDirection = Direction.NONE;
25742644
}
2575-
// Reset scrolling and fling direction.
2576-
mCurrentScrollDirection = mCurrentFlingDirection = Direction.NONE;
25772645
}
25782646

2579-
25802647
@Override
25812648
public void computeScroll() {
25822649
super.computeScroll();
Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,70 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33
<declare-styleable name="WeekView">
4-
<attr name="allDayEventHeight" format="dimension" />
5-
<attr name="autoLimitTime" format="boolean" />
6-
<attr name="columnGap" format="dimension" />
7-
<attr name="dayBackgroundColor" format="color" />
4+
<attr name="allDayEventHeight" format="dimension"/>
5+
<attr name="autoLimitTime" format="boolean"/>
6+
<attr name="columnGap" format="dimension"/>
7+
<attr name="dayBackgroundColor" format="color"/>
88
<attr name="dayNameLength" format="enum">
9-
<enum name="length_short" value="1" />
10-
<enum name="length_long" value="2" />
9+
<enum name="length_short" value="1"/>
10+
<enum name="length_long" value="2"/>
1111
</attr>
12-
<attr name="eventCornerRadius" format="dimension" />
13-
<attr name="eventMarginVertical" format="dimension" />
14-
<attr name="eventPadding" format="dimension" />
15-
<attr name="eventTextColor" format="color" />
16-
<attr name="eventTextSize" format="dimension" />
12+
<attr name="eventCornerRadius" format="dimension"/>
13+
<attr name="eventMarginVertical" format="dimension"/>
14+
<attr name="eventPadding" format="dimension"/>
15+
<attr name="eventTextColor" format="color"/>
16+
<attr name="eventTextSize" format="dimension"/>
1717
<attr name="firstDayOfWeek" format="enum">
18-
<enum name="sunday" value="1" />
19-
<enum name="monday" value="2" />
20-
<enum name="tuesday" value="3" />
21-
<enum name="wednesday" value="4" />
22-
<enum name="thursday" value="5" />
23-
<enum name="friday" value="6" />
24-
<enum name="saturday" value="7" />
18+
<enum name="sunday" value="1"/>
19+
<enum name="monday" value="2"/>
20+
<enum name="tuesday" value="3"/>
21+
<enum name="wednesday" value="4"/>
22+
<enum name="thursday" value="5"/>
23+
<enum name="friday" value="6"/>
24+
<enum name="saturday" value="7"/>
2525
</attr>
26-
<attr name="futureBackgroundColor" format="color" />
27-
<attr name="futureWeekendBackgroundColor" format="color" />
28-
<attr name="headerColumnBackground" format="color" />
29-
<attr name="headerColumnPadding" format="dimension" />
30-
<attr name="headerColumnTextColor" format="color" />
31-
<attr name="headerRowBackgroundColor" format="color" />
32-
<attr name="headerRowPadding" format="dimension" />
33-
<attr name="horizontalFlingEnabled" format="boolean" />
34-
<attr name="hourHeight" format="dimension" />
35-
<attr name="hourSeparatorColor" format="color" />
36-
<attr name="hourSeparatorHeight" format="dimension" />
37-
<attr name="maxHourHeight" format="dimension" />
38-
<attr name="maxTime" format="integer" />
39-
<attr name="minHourHeight" format="dimension" />
40-
<attr name="minTime" format="integer" />
41-
<attr name="minOverlappingMinutes" format="integer" />
42-
<attr name="newEventColor" format="color" />
43-
<attr name="newEventId" format="integer" />
44-
<attr name="newEventIdentifier" format="string" />
45-
<attr name="newEventIconResource" format="integer" />
46-
<attr name="newEventLengthInMinutes" format="integer" />
47-
<attr name="newEventTimeResolutionInMinutes" format="integer" />
48-
<attr name="noOfVisibleDays" format="integer" />
49-
<attr name="nowLineColor" format="color" />
50-
<attr name="nowLineThickness" format="dimension" />
51-
<attr name="overlappingEventGap" format="dimension" />
52-
<attr name="pastBackgroundColor" format="color" />
53-
<attr name="pastWeekendBackgroundColor" format="color" />
54-
<attr name="scrollDuration" format="integer" />
55-
<attr name="showDistinctPastFutureColor" format="boolean" />
56-
<attr name="showDistinctWeekendColor" format="boolean" />
57-
<attr name="showFirstDayOfWeekFirst" format="boolean" />
58-
<attr name="showNowLine" format="boolean" />
59-
<attr name="textSize" format="dimension" />
60-
<attr name="timeColumnResolution" format="integer" />
61-
<attr name="todayBackgroundColor" format="color" />
62-
<attr name="todayHeaderTextColor" format="color" />
63-
<attr name="verticalFlingEnabled" format="boolean" />
64-
<attr name="xScrollingSpeed" format="float" />
65-
<attr name="zoomFocusPoint" format="fraction" />
66-
<attr name="zoomFocusPointEnabled" format="boolean" />
67-
<attr name="dropListenerEnabled" format="boolean" />
26+
<attr name="futureBackgroundColor" format="color"/>
27+
<attr name="futureWeekendBackgroundColor" format="color"/>
28+
<attr name="headerColumnBackground" format="color"/>
29+
<attr name="headerColumnPadding" format="dimension"/>
30+
<attr name="headerColumnTextColor" format="color"/>
31+
<attr name="headerRowBackgroundColor" format="color"/>
32+
<attr name="headerRowPadding" format="dimension"/>
33+
<attr name="horizontalFlingEnabled" format="boolean"/>
34+
<attr name="hourHeight" format="dimension"/>
35+
<attr name="hourSeparatorColor" format="color"/>
36+
<attr name="hourSeparatorHeight" format="dimension"/>
37+
<attr name="maxHourHeight" format="dimension"/>
38+
<attr name="maxTime" format="integer"/>
39+
<attr name="minHourHeight" format="dimension"/>
40+
<attr name="minTime" format="integer"/>
41+
<attr name="minOverlappingMinutes" format="integer"/>
42+
<attr name="newEventColor" format="color"/>
43+
<attr name="newEventId" format="integer"/>
44+
<attr name="newEventIdentifier" format="string"/>
45+
<attr name="newEventIconResource" format="integer"/>
46+
<attr name="newEventLengthInMinutes" format="integer"/>
47+
<attr name="newEventTimeResolutionInMinutes" format="integer"/>
48+
<attr name="noOfVisibleDays" format="integer"/>
49+
<attr name="nowLineColor" format="color"/>
50+
<attr name="nowLineThickness" format="dimension"/>
51+
<attr name="overlappingEventGap" format="dimension"/>
52+
<attr name="pastBackgroundColor" format="color"/>
53+
<attr name="pastWeekendBackgroundColor" format="color"/>
54+
<attr name="scrollDuration" format="integer"/>
55+
<attr name="showDistinctPastFutureColor" format="boolean"/>
56+
<attr name="showDistinctWeekendColor" format="boolean"/>
57+
<attr name="showFirstDayOfWeekFirst" format="boolean"/>
58+
<attr name="showNowLine" format="boolean"/>
59+
<attr name="textSize" format="dimension"/>
60+
<attr name="timeColumnResolution" format="integer"/>
61+
<attr name="todayBackgroundColor" format="color"/>
62+
<attr name="todayHeaderTextColor" format="color"/>
63+
<attr name="verticalFlingEnabled" format="boolean"/>
64+
<attr name="xScrollingSpeed" format="float"/>
65+
<attr name="zoomFocusPoint" format="fraction"/>
66+
<attr name="zoomFocusPointEnabled" format="boolean"/>
67+
<attr name="dropListenerEnabled" format="boolean"/>
68+
<attr name="isScrollNumberOfVisibleDays" format="boolean"/>
6869
</declare-styleable>
6970
</resources>

0 commit comments

Comments
 (0)