From 472162d2e1a6c2d31d7584e31626e90f2b0502c7 Mon Sep 17 00:00:00 2001 From: AndroidDeveloperLB Date: Mon, 7 May 2018 11:09:52 +0300 Subject: [PATCH] -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. --- build.gradle | 4 +- gradle/wrapper/gradle-wrapper.properties | 4 +- library/build.gradle | 13 +- .../java/com/alamkanak/weekview/WeekView.java | 125 ++++++++++++++---- library/src/main/res/values/attrs.xml | 121 ++++++++--------- sample/build.gradle | 7 +- 6 files changed, 171 insertions(+), 103 deletions(-) diff --git a/build.gradle b/build.gradle index bd1e1fd48..f400b29bf 100644 --- a/build.gradle +++ b/build.gradle @@ -3,9 +3,10 @@ buildscript { repositories { jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.3' + classpath 'com.android.tools.build:gradle:3.2.0-alpha13' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -19,5 +20,6 @@ allprojects { repositories { jcenter() + google() } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 66b2b75d1..3dc5c89ac 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Sun Aug 06 18:02:35 CEST 2017 +#Mon May 07 11:03:02 IDT 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip diff --git a/library/build.gradle b/library/build.gradle index a9291dd32..1df6cb954 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -5,12 +5,11 @@ repositories { } android { - compileSdkVersion 25 - buildToolsVersion '25.0.2' + compileSdkVersion 27 defaultConfig { - minSdkVersion 9 - targetSdkVersion 25 + minSdkVersion 14 + targetSdkVersion 27 } } @@ -18,8 +17,8 @@ configurations { javadocDeps } dependencies { - compile 'com.android.support:appcompat-v7:25.1.0' - javadocDeps 'com.android.support:appcompat-v7:25.1.0' + compile 'com.android.support:appcompat-v7:27.1.1' + javadocDeps 'com.android.support:appcompat-v7:27.1.1' } -apply from: 'gradle-mvn-push.gradle' \ No newline at end of file +apply from: 'gradle-mvn-push.gradle' diff --git a/library/src/main/java/com/alamkanak/weekview/WeekView.java b/library/src/main/java/com/alamkanak/weekview/WeekView.java index f6685e020..79f40eaee 100755 --- a/library/src/main/java/com/alamkanak/weekview/WeekView.java +++ b/library/src/main/java/com/alamkanak/weekview/WeekView.java @@ -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; @@ -118,7 +123,7 @@ private enum Direction { private int mEffectiveMinHourHeight = mMinHourHeight; //compensates for the fact that you can't keep zooming out. private int mMaxHourHeight = 250; private int mColumnGap = 10; - private int mFirstDayOfWeek = Calendar.MONDAY; + private int mFirstDayOfWeek = Calendar.getInstance().getFirstDayOfWeek(); private int mTextSize = 12; private int mHeaderColumnPadding = 10; private int mHeaderColumnTextColor = Color.BLACK; @@ -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; @@ -190,6 +196,7 @@ private enum Direction { @Override public boolean onDown(MotionEvent e) { + mStartOriginForScroll = mCurrentOrigin.x; goToNearestOrigin(); return true; } @@ -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) { @@ -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()); @@ -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(); } @@ -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. @@ -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); @@ -2542,41 +2572,78 @@ 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(); diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 516f90227..a12fdebae 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -1,69 +1,70 @@ - - - - + + + + - - + + - - - - - + + + + + - - - - - - - + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sample/build.gradle b/sample/build.gradle index 23e0f47cc..c7734ab7b 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -1,13 +1,12 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 25 - buildToolsVersion '25.0.2' + compileSdkVersion 27 defaultConfig { applicationId "com.alamkanak.weekview" minSdkVersion 14 - targetSdkVersion 25 + targetSdkVersion 27 versionCode 1 versionName "1.0" } @@ -22,6 +21,6 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile project(':library') - compile 'com.android.support:appcompat-v7:25.1.0' + compile 'com.android.support:appcompat-v7:27.1.1' compile 'com.squareup.retrofit:retrofit:1.9.0' }