diff --git a/app/src/main/java/com/mindorks/test/ActivityTinder.java b/app/src/main/java/com/mindorks/test/ActivityTinder.java index c798d43..1ba261f 100644 --- a/app/src/main/java/com/mindorks/test/ActivityTinder.java +++ b/app/src/main/java/com/mindorks/test/ActivityTinder.java @@ -34,13 +34,13 @@ protected void onCreate(Bundle savedInstanceState) { @Override protected void onPostCreate(@Nullable Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); - + mSwipView.disableTouchSwipe(); mSwipView.getBuilder() // .setSwipeType(SwipePlaceHolderView.SWIPE_TYPE_VERTICAL) .setDisplayViewCount(3) .setIsUndoEnabled(true) -// .setWidthSwipeDistFactor(15) -// .setHeightSwipeDistFactor(20) + .setWidthSwipeDistFactor(15) + .setHeightSwipeDistFactor(20) .setSwipeDecor(new SwipeDecor() // .setMarginTop(300) // .setMarginLeft(100) @@ -65,7 +65,7 @@ protected void onPostCreate(@Nullable Bundle savedInstanceState) { public void run() { try { Thread.currentThread().sleep(8000); - mSwipView.disableTouchSwipe(); + mSwipView.enableTouchSwipe(); // mSwipView.lockViews(); // Thread.currentThread().sleep(4000); // mSwipView.unlockViews(); diff --git a/build.gradle b/build.gradle index daecaa8..cca0438 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.2.1' + classpath 'com.android.tools.build:gradle:2.2.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/placeholderview/build.gradle b/placeholderview/build.gradle index bee4280..6308753 100644 --- a/placeholderview/build.gradle +++ b/placeholderview/build.gradle @@ -13,7 +13,7 @@ ext { siteUrl = 'https://github.com/janishar/PlaceHolderView' gitUrl = 'https://github.com/janishar/PlaceHolderView.git' - libraryVersion = '0.5.0' + libraryVersion = '0.5.1' developerId = 'janishar.ali@gmail.com' developerName = 'Janishar Ali' @@ -32,7 +32,7 @@ android { minSdkVersion 16 targetSdkVersion 24 versionCode 7 - versionName "0.5.0" + versionName "0.5.1" } buildTypes { release { diff --git a/placeholderview/src/main/java/com/mindorks/placeholderview/SwipeDecor.java b/placeholderview/src/main/java/com/mindorks/placeholderview/SwipeDecor.java index 607efd1..61efb84 100644 --- a/placeholderview/src/main/java/com/mindorks/placeholderview/SwipeDecor.java +++ b/placeholderview/src/main/java/com/mindorks/placeholderview/SwipeDecor.java @@ -28,11 +28,6 @@ public class SwipeDecor { private int mSwipeAnimTime; private float mSwipeAnimFactor; private int mSwipeRotationAngle; - private AtomicBoolean mIsViewLocked; - private AtomicBoolean mIsPutBackActive; - private AtomicBoolean mIsViewToRestoreOnLock; - private AtomicBoolean mIsViewToRestoreOnTouchLock; - private AtomicBoolean mIsTouchSwipeLocked; public SwipeDecor() { mViewWidth = 0; @@ -52,11 +47,6 @@ public SwipeDecor() { mSwipeAnimTime = 200; mSwipeAnimFactor = 0.75f; mSwipeRotationAngle = 15; - mIsViewLocked = new AtomicBoolean(false); - mIsPutBackActive = new AtomicBoolean(false); - mIsViewToRestoreOnLock = new AtomicBoolean(true); - mIsViewToRestoreOnTouchLock = new AtomicBoolean(true); - mIsTouchSwipeLocked = new AtomicBoolean(false); } public SwipeDecor setViewWidth(int width){ @@ -330,46 +320,4 @@ public int getViewHeight() { public int getViewGravity() { return mViewGravity; } - - protected boolean getIsViewLocked() { - return mIsViewLocked.get(); - } - - protected void setIsViewLocked(boolean isViewLocked) { - this.mIsViewToRestoreOnLock.set(true); - this.mIsViewLocked.set(isViewLocked); - } - - protected boolean getIsPutBackActive() { - return mIsPutBackActive.get(); - } - - protected void setIsPutBackActive(boolean isPutBackActive) { - this.mIsPutBackActive.set(isPutBackActive); - } - - protected boolean getIsViewToRestoredOnLock() { - return mIsViewToRestoreOnLock.get(); - } - - protected void setIsViewToRestoredOnLock(boolean isViewToRestoredOnLock) { - this.mIsViewToRestoreOnLock.set(isViewToRestoredOnLock); - } - - protected boolean getIsViewToRestoreOnTouchLock() { - return mIsViewToRestoreOnTouchLock.get(); - } - - protected void setIsViewToRestoreOnTouchLock(boolean isViewToRestoreOnTouchLock) { - this.mIsViewToRestoreOnTouchLock.set(isViewToRestoreOnTouchLock); - } - - protected boolean getIsTouchSwipeLocked(){ - return mIsTouchSwipeLocked.get(); - } - - protected void setIsTouchSwipeLocked(boolean locked){ - this.mIsViewToRestoreOnTouchLock.set(true); - mIsTouchSwipeLocked.set(locked); - } } diff --git a/placeholderview/src/main/java/com/mindorks/placeholderview/SwipePlaceHolderView.java b/placeholderview/src/main/java/com/mindorks/placeholderview/SwipePlaceHolderView.java index 0c32870..45ab14b 100644 --- a/placeholderview/src/main/java/com/mindorks/placeholderview/SwipePlaceHolderView.java +++ b/placeholderview/src/main/java/com/mindorks/placeholderview/SwipePlaceHolderView.java @@ -14,6 +14,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; /** * Created by janisharali on 26/08/16. @@ -31,10 +32,9 @@ public class SwipePlaceHolderView extends FrameLayout implements private LayoutInflater mLayoutInflater; private int mDisplayViewCount = DEFAULT_DISPLAY_VIEW_COUNT; private int mSwipeType = SWIPE_TYPE_DEFAULT; - private float mWidthSwipeDistFactor = 3f; - private float mHeightSwipeDistFactor = 3f; private boolean mIsReverse = false; private SwipeDecor mSwipeDecor; + private SwipeOption mSwipeOption; private boolean mIsBtnSwipeDone = true; private boolean mIsUndoEnabled; private Object mRestoreResolverOnUndo; @@ -94,6 +94,7 @@ private void setupView(List> swipeViewBinderL mSwipeViewBuilder = swipeViewBuilder; mLayoutInflater = LayoutInflater.from(getContext()); mSwipeDecor = new SwipeDecor(); + mSwipeOption = new SwipeOption(); setChildrenDrawingOrderEnabled(true); } @@ -156,10 +157,10 @@ protected void setSwipeDecor(SwipeDecor swipeDecor) { /** * - * @param Factor + * @param factor */ - protected void setWidthSwipeDistFactor(float Factor) { - mWidthSwipeDistFactor = Factor; + protected void setWidthSwipeDistFactor(float factor) { + mSwipeOption.setWidthSwipeDistFactor(factor); } /** @@ -167,7 +168,7 @@ protected void setWidthSwipeDistFactor(float Factor) { * @param factor */ protected void setHeightSwipeDistFactor(float factor) { - mHeightSwipeDistFactor = factor; + mSwipeOption.setHeightSwipeDistFactor(factor); } /** @@ -187,7 +188,7 @@ public SwipePlaceHolderView addView(T resolver){ attachSwipeInfoViews(frameView, swipeViewBinder, mSwipeDecor); addView(frameView); setRelativeScale(frameView, position, mSwipeDecor); - swipeViewBinder.bindView(frameView, position, mSwipeType, mWidthSwipeDistFactor, mHeightSwipeDistFactor, mSwipeDecor, this); + swipeViewBinder.bindView(frameView, position, mSwipeType, mSwipeDecor, mSwipeOption, this); if(mSwipeViewBinderList.indexOf(swipeViewBinder) == 0){ swipeViewBinder.setOnTouch(); @@ -207,7 +208,7 @@ private void addView(T resolver, int position){ attachSwipeInfoViews(frameView, swipeViewBinder, mSwipeDecor); addView(frameView, position); setRelativeScale(frameView, binderPosition, mSwipeDecor); - swipeViewBinder.bindView(frameView, binderPosition, mSwipeType, mWidthSwipeDistFactor, mHeightSwipeDistFactor, mSwipeDecor, this); + swipeViewBinder.bindView(frameView, binderPosition, mSwipeType, mSwipeDecor, mSwipeOption, this); if(mSwipeViewBinderList.indexOf(swipeViewBinder) == 0){ swipeViewBinder.setOnTouch(); @@ -228,7 +229,7 @@ protected void addPendingView(SwipeViewBinder swipeViewBinder) attachSwipeInfoViews(frameView, swipeViewBinder, mSwipeDecor); addView(frameView); setRelativeScale(frameView, position, mSwipeDecor); - swipeViewBinder.bindView(frameView, position, mSwipeType, mWidthSwipeDistFactor, mHeightSwipeDistFactor, mSwipeDecor, this); + swipeViewBinder.bindView(frameView, position, mSwipeType, mSwipeDecor, mSwipeOption, this); } /** @@ -375,27 +376,27 @@ public void onFinish() { } public void lockViews(){ - mSwipeDecor.setIsViewLocked(true); + mSwipeOption.setIsViewLocked(true); } public void unlockViews(){ - mSwipeDecor.setIsViewLocked(false); + mSwipeOption.setIsViewLocked(false); } public void activatePutBack(){ - mSwipeDecor.setIsPutBackActive(true); + mSwipeOption.setIsPutBackActive(true); } - public void deActivatePutBack(){ - mSwipeDecor.setIsPutBackActive(false); + public void deactivatePutBack(){ + mSwipeOption.setIsPutBackActive(false); } public void disableTouchSwipe() { - mSwipeDecor.setIsTouchSwipeLocked(true); + mSwipeOption.setIsTouchSwipeLocked(true); } public void enableTouchSwipe() { - mSwipeDecor.setIsTouchSwipeLocked(false); + mSwipeOption.setIsTouchSwipeLocked(false); } protected void setIsUndoEnabled(boolean isUndoEnabled) { @@ -847,4 +848,80 @@ public void reset() { mIsBeingDragged = false; } } + + protected class SwipeOption{ + private float mWidthSwipeDistFactor = 3f; + private float mHeightSwipeDistFactor = 3f; + private AtomicBoolean mIsViewLocked; + private AtomicBoolean mIsPutBackActive; + private AtomicBoolean mIsViewToRestoreOnLock; + private AtomicBoolean mIsViewToRestoreOnTouchLock; + private AtomicBoolean mIsTouchSwipeLocked; + + public SwipeOption() { + mIsViewLocked = new AtomicBoolean(false); + mIsPutBackActive = new AtomicBoolean(false); + mIsViewToRestoreOnLock = new AtomicBoolean(true); + mIsViewToRestoreOnTouchLock = new AtomicBoolean(true); + mIsTouchSwipeLocked = new AtomicBoolean(false); + } + + protected boolean getIsViewLocked() { + return mIsViewLocked.get(); + } + + protected void setIsViewLocked(boolean isViewLocked) { + this.mIsViewToRestoreOnLock.set(true); + this.mIsViewLocked.set(isViewLocked); + } + + protected boolean getIsPutBackActive() { + return mIsPutBackActive.get(); + } + + protected void setIsPutBackActive(boolean isPutBackActive) { + this.mIsPutBackActive.set(isPutBackActive); + } + + protected boolean getIsViewToRestoredOnLock() { + return mIsViewToRestoreOnLock.get(); + } + + protected void setIsViewToRestoredOnLock(boolean isViewToRestoredOnLock) { + this.mIsViewToRestoreOnLock.set(isViewToRestoredOnLock); + } + + protected boolean getIsViewToRestoreOnTouchLock() { + return mIsViewToRestoreOnTouchLock.get(); + } + + protected void setIsViewToRestoreOnTouchLock(boolean isViewToRestoreOnTouchLock) { + this.mIsViewToRestoreOnTouchLock.set(isViewToRestoreOnTouchLock); + } + + protected boolean getIsTouchSwipeLocked(){ + return mIsTouchSwipeLocked.get(); + } + + protected void setIsTouchSwipeLocked(boolean locked){ + this.mIsViewToRestoreOnTouchLock.set(true); + mIsTouchSwipeLocked.set(locked); + } + + public float getWidthSwipeDistFactor() { + return mWidthSwipeDistFactor; + } + + public void setWidthSwipeDistFactor(float widthSwipeDistFactor) { + this.mWidthSwipeDistFactor = widthSwipeDistFactor; + } + + public float getHeightSwipeDistFactor() { + return mHeightSwipeDistFactor; + } + + public void setHeightSwipeDistFactor(float heightSwipeDistFactor) { + this.mHeightSwipeDistFactor = heightSwipeDistFactor; + } + } } diff --git a/placeholderview/src/main/java/com/mindorks/placeholderview/SwipeViewBinder.java b/placeholderview/src/main/java/com/mindorks/placeholderview/SwipeViewBinder.java index 75e6c8d..7a74988 100644 --- a/placeholderview/src/main/java/com/mindorks/placeholderview/SwipeViewBinder.java +++ b/placeholderview/src/main/java/com/mindorks/placeholderview/SwipeViewBinder.java @@ -37,11 +37,10 @@ public class SwipeViewBinder extends ViewBinder{ private Animator.AnimatorListener mViewRestoreAnimatorListener; private Animator.AnimatorListener mViewPutBackAnimatorListener; private int mSwipeType = SwipePlaceHolderView.SWIPE_TYPE_DEFAULT; - private float mWidthSwipeDistFactor; - private float mHeightSwipeDistFactor; private View mSwipeInMsgView; private View mSwipeOutMsgView; private SwipeDecor mSwipeDecor; + private SwipePlaceHolderView.SwipeOption mSwipeOption; private boolean mHasInterceptedEvent = false; private int mOriginalLeftMargin; private int mOriginalTopMargin; @@ -61,18 +60,15 @@ public SwipeViewBinder(T resolver) { * @param promptsView * @param position * @param swipeType - * @param widthSwipeDistFactor - * @param heightSwipeDistFactor * @param decor * @param callback */ - protected void bindView(V promptsView, int position, int swipeType, float widthSwipeDistFactor, - float heightSwipeDistFactor, SwipeDecor decor, SwipeCallback callback) { + protected void bindView(V promptsView, int position, int swipeType, SwipeDecor decor, + SwipePlaceHolderView.SwipeOption swipeOption, SwipeCallback callback) { mLayoutView = promptsView; mSwipeType = swipeType; + mSwipeOption = swipeOption; mSwipeDecor = decor; - mWidthSwipeDistFactor = widthSwipeDistFactor; - mHeightSwipeDistFactor = heightSwipeDistFactor; mCallback = callback; bindSwipeView(promptsView); @@ -230,7 +226,7 @@ public void onAnimationStart(Animator animation) {} @Override public void onAnimationEnd(Animator animation) { - if(mSwipeDecor.getIsPutBackActive()){ + if(mSwipeOption.getIsPutBackActive()){ mLayoutView.animate() .translationX(mTransXToRestore) .translationY(mTransYToRestore) @@ -310,17 +306,17 @@ private void setDefaultTouchListener(final V view){ @Override public boolean onTouch(final View v, MotionEvent event) { - if(mSwipeDecor.getIsTouchSwipeLocked()){ - if(mSwipeDecor.getIsViewToRestoreOnTouchLock()){ - mSwipeDecor.setIsViewToRestoreOnTouchLock(false); + if(mSwipeOption.getIsTouchSwipeLocked()){ + if(mSwipeOption.getIsViewToRestoreOnTouchLock()){ + mSwipeOption.setIsViewToRestoreOnTouchLock(false); animateSwipeRestore(v, mOriginalTopMargin, mOriginalLeftMargin, mSwipeType); } return true; } - if(mSwipeDecor.getIsViewLocked()){ - if(mSwipeDecor.getIsViewToRestoredOnLock()){ - mSwipeDecor.setIsViewToRestoredOnLock(false); + if(mSwipeOption.getIsViewLocked()){ + if(mSwipeOption.getIsViewToRestoredOnLock()){ + mSwipeOption.setIsViewToRestoredOnLock(false); animateSwipeRestore(v, mOriginalTopMargin, mOriginalLeftMargin, mSwipeType); } return true; @@ -350,12 +346,12 @@ public boolean onTouch(final View v, MotionEvent event) { distSlideX = distSlideX < 0 ? -distSlideX : distSlideX; distSlideY = distSlideY < 0 ? -distSlideY : distSlideY; - if (distSlideX < displayMetrics.widthPixels / mWidthSwipeDistFactor - && distSlideY < displayMetrics.heightPixels / mHeightSwipeDistFactor) { + if (distSlideX < displayMetrics.widthPixels / mSwipeOption.getWidthSwipeDistFactor() + && distSlideY < displayMetrics.heightPixels / mSwipeOption.getHeightSwipeDistFactor()) { animateSwipeRestore(v, mOriginalTopMargin, mOriginalLeftMargin, mSwipeType); } else { - if(!mSwipeDecor.getIsPutBackActive()) { + if(!mSwipeOption.getIsPutBackActive()) { blockTouch(); } @@ -405,8 +401,8 @@ public void onTick(long millisUntilFinished) {} int distanceMovedTop = layoutParamsTemp.topMargin - mOriginalTopMargin; int distanceMovedLeft = layoutParamsTemp.leftMargin - mOriginalLeftMargin; - mCallback.onAnimateView(distanceMovedLeft, distanceMovedTop, displayMetrics.widthPixels / mWidthSwipeDistFactor, - displayMetrics.heightPixels / mHeightSwipeDistFactor, SwipeViewBinder.this); + mCallback.onAnimateView(distanceMovedLeft, distanceMovedTop, displayMetrics.widthPixels / mSwipeOption.getWidthSwipeDistFactor(), + displayMetrics.heightPixels / mSwipeOption.getHeightSwipeDistFactor(), SwipeViewBinder.this); } break; } @@ -438,17 +434,17 @@ private void setHorizontalTouchListener(final V view){ @Override public boolean onTouch(View v, MotionEvent event) { - if(mSwipeDecor.getIsTouchSwipeLocked()){ - if(mSwipeDecor.getIsViewToRestoreOnTouchLock()){ - mSwipeDecor.setIsViewToRestoreOnTouchLock(false); + if(mSwipeOption.getIsTouchSwipeLocked()){ + if(mSwipeOption.getIsViewToRestoreOnTouchLock()){ + mSwipeOption.setIsViewToRestoreOnTouchLock(false); animateSwipeRestore(v, mOriginalTopMargin, mOriginalLeftMargin, mSwipeType); } return true; } - if(mSwipeDecor.getIsViewLocked()){ - if(mSwipeDecor.getIsViewToRestoredOnLock()){ - mSwipeDecor.setIsViewToRestoredOnLock(false); + if(mSwipeOption.getIsViewLocked()){ + if(mSwipeOption.getIsViewToRestoredOnLock()){ + mSwipeOption.setIsViewToRestoredOnLock(false); animateSwipeRestore(v, mOriginalTopMargin, mOriginalLeftMargin, mSwipeType); } return true; @@ -474,10 +470,10 @@ public boolean onTouch(View v, MotionEvent event) { if(!resetDone) { float distSlideX = x - dx; distSlideX = distSlideX < 0 ? -distSlideX : distSlideX; - if (distSlideX < displayMetrics.widthPixels / mWidthSwipeDistFactor) { + if (distSlideX < displayMetrics.widthPixels / mSwipeOption.getWidthSwipeDistFactor()) { animateSwipeRestore(v, mOriginalTopMargin, mOriginalLeftMargin, mSwipeType); } else { - if(!mSwipeDecor.getIsPutBackActive()) { + if(!mSwipeOption.getIsPutBackActive()) { blockTouch(); } @@ -510,8 +506,8 @@ public void onTick(long millisUntilFinished) {} layoutParamsTemp.leftMargin = (int) (x - dx); v.setLayoutParams(layoutParamsTemp); int distanceMoved = layoutParamsTemp.leftMargin - mOriginalLeftMargin; - mCallback.onAnimateView(distanceMoved, 0, displayMetrics.widthPixels / mWidthSwipeDistFactor, - displayMetrics.heightPixels / mHeightSwipeDistFactor, SwipeViewBinder.this); + mCallback.onAnimateView(distanceMoved, 0, displayMetrics.widthPixels / mSwipeOption.getWidthSwipeDistFactor(), + displayMetrics.heightPixels / mSwipeOption.getHeightSwipeDistFactor(), SwipeViewBinder.this); } break; } @@ -543,17 +539,17 @@ private void setVerticalTouchListener(final V view){ @Override public boolean onTouch(View v, MotionEvent event) { - if(mSwipeDecor.getIsTouchSwipeLocked()){ - if(mSwipeDecor.getIsViewToRestoreOnTouchLock()){ - mSwipeDecor.setIsViewToRestoreOnTouchLock(false); + if(mSwipeOption.getIsTouchSwipeLocked()){ + if(mSwipeOption.getIsViewToRestoreOnTouchLock()){ + mSwipeOption.setIsViewToRestoreOnTouchLock(false); animateSwipeRestore(v, mOriginalTopMargin, mOriginalLeftMargin, mSwipeType); } return true; } - if(mSwipeDecor.getIsViewLocked()){ - if(mSwipeDecor.getIsViewToRestoredOnLock()){ - mSwipeDecor.setIsViewToRestoredOnLock(false); + if(mSwipeOption.getIsViewLocked()){ + if(mSwipeOption.getIsViewToRestoredOnLock()){ + mSwipeOption.setIsViewToRestoredOnLock(false); animateSwipeRestore(v, mOriginalTopMargin, mOriginalLeftMargin, mSwipeType); } return true; @@ -579,10 +575,10 @@ public boolean onTouch(View v, MotionEvent event) { if(!resetDone) { float distSlideY = y - dy; distSlideY = distSlideY < 0 ? -distSlideY : distSlideY; - if (distSlideY < displayMetrics.heightPixels / mHeightSwipeDistFactor) { + if (distSlideY < displayMetrics.heightPixels / mSwipeOption.getHeightSwipeDistFactor()) { animateSwipeRestore(v, mOriginalTopMargin, mOriginalLeftMargin, mSwipeType); } else { - if(!mSwipeDecor.getIsPutBackActive()) { + if(!mSwipeOption.getIsPutBackActive()) { blockTouch(); } @@ -616,8 +612,8 @@ public void onTick(long millisUntilFinished) {} v.setLayoutParams(layoutParamsTemp); int distanceMoved = layoutParamsTemp.topMargin - mOriginalTopMargin; - mCallback.onAnimateView(0, distanceMoved, displayMetrics.widthPixels / mWidthSwipeDistFactor, - displayMetrics.heightPixels / mHeightSwipeDistFactor, SwipeViewBinder.this); + mCallback.onAnimateView(0, distanceMoved, displayMetrics.widthPixels / mSwipeOption.getWidthSwipeDistFactor(), + displayMetrics.heightPixels / mSwipeOption.getHeightSwipeDistFactor(), SwipeViewBinder.this); } break; } @@ -697,8 +693,8 @@ public void onAnimationUpdate(ValueAnimator valueAnimator) { * @param isSwipeIn */ protected void doSwipe(boolean isSwipeIn){ - if(mLayoutView != null && mViewRemoveAnimatorListener != null && !mSwipeDecor.getIsViewLocked()) { - if(!mSwipeDecor.getIsPutBackActive()) { + if(mLayoutView != null && mViewRemoveAnimatorListener != null && !mSwipeOption.getIsViewLocked()) { + if(!mSwipeOption.getIsPutBackActive()) { blockTouch(); }