Skip to content
This repository has been archived by the owner on Mar 11, 2022. It is now read-only.

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
fix - scroll in middle of button bug
  • Loading branch information
leandroBorgesFerreira committed Jun 14, 2017
1 parent 1de269a commit fff61b7
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,14 @@ class SwipeButtonInstrumentedTest {
}
}

@Test
fun shouldNotAcceptSwipeFromOutsideMovingPart() {
test {
enableFromMidOfButton()
} result {
checkButtonIsDisabled(activityTest.activity.findViewById(R.id.swipe_btn_disabled) as SwipeButton)
}
}

fun test(func: SwipeButtonTestRobot.() -> Unit) = SwipeButtonTestRobot().apply { func() }
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.ebanx.swipebutton

import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.ViewAction
import android.support.test.espresso.action.CoordinatesProvider
import android.support.test.espresso.action.GeneralLocation
import android.support.test.espresso.action.Press
import android.support.test.espresso.action.Swipe
import android.support.test.espresso.action.ViewActions.click
import android.support.test.espresso.action.ViewActions.swipeRight
import android.support.test.espresso.matcher.ViewMatchers.withId
import ebanx.com.ego.utils.CenterSwipeAction

/**
* Created by vinicius on 13/06/17.
Expand All @@ -20,5 +26,22 @@ class SwipeButtonTestRobot {
return this
}

fun enableFromMidOfButton() : SwipeButtonTestRobot {
onView(withId(R.id.swipe_btn_disabled)).perform(swipeFromCenterToRight())
return this
}

private fun swipeFromCenterToRight(): ViewAction {
val endCoordProvide = CoordinatesProvider { view ->
val coordinates = GeneralLocation.CENTER.calculateCoordinates(view)
coordinates[0] = 1500f
coordinates
}
return CenterSwipeAction(Swipe.FAST,
GeneralLocation.CENTER,
endCoordProvide,
Press.FINGER)
}

infix fun result(func: SwipeButtonResultRobot.() -> Unit) = SwipeButtonResultRobot().apply { func() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ebanx.com.ego.utils

import android.support.test.espresso.PerformException
import android.support.test.espresso.UiController
import android.support.test.espresso.ViewAction
import android.support.test.espresso.action.CoordinatesProvider
import android.support.test.espresso.action.PrecisionDescriber
import android.support.test.espresso.action.Swiper
import android.support.test.espresso.matcher.ViewMatchers
import android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility
import android.support.test.espresso.util.HumanReadables
import android.view.View
import android.view.ViewConfiguration
import org.hamcrest.Matcher

class CenterSwipeAction(private val swiper: Swiper,
private val startCoordProvide: CoordinatesProvider,
private val endCoordProvide: CoordinatesProvider,
private val precDesc: PrecisionDescriber) : ViewAction {

override fun getConstraints(): Matcher<View> {
return withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)
}

override fun getDescription(): String {
return "swipe from middle of screen"
}

@Suppress("CatchRuntimeException")
override fun perform(uiController: UiController, view: View) {
val startCoord = startCoordProvide.calculateCoordinates(view)
val finalCoord = endCoordProvide.calculateCoordinates(view)
val precision = precDesc.describePrecision()

// you could try this for several times until Swiper.Status is achieved or try count is reached
try {
swiper.sendSwipe(uiController, startCoord, finalCoord, precision)
} catch (re: RuntimeException) {
throw PerformException.Builder()
.withActionDescription(this.description)
.withViewDescription(HumanReadables.describe(view))
.withCause(re)
.build()
}

// ensures that the swipe has been run.
uiController.loopMainThreadForAtLeast(ViewConfiguration.getPressedStateDuration().toLong())
}
}
4 changes: 2 additions & 2 deletions swipe-button/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'com.jfrog.bintray'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'gnag'

version = '0.5.1'
version = '0.5.2'
group = 'com.ebanx'

android {
Expand Down Expand Up @@ -103,7 +103,7 @@ install {
packaging 'aar'
groupId 'com.ebanx'
artifactId 'swipe-button'
version '0.5.1'
version '0.5.2'

licenses {
license {
Expand Down
64 changes: 32 additions & 32 deletions swipe-button/src/main/java/com/ebanx/swipebtn/SwipeButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class SwipeButton extends RelativeLayout {


private ImageView swipeButton;
private ImageView swipeButtonInner;
private float initialX;
private boolean active;
private TextView centerText;
Expand Down Expand Up @@ -87,21 +87,21 @@ public void setDisabledDrawable(Drawable drawable) {
disabledDrawable = drawable;

if (!active) {
swipeButton.setImageDrawable(drawable);
swipeButtonInner.setImageDrawable(drawable);
}
}

public void setButtonBackground(Drawable buttonBackground) {
if (buttonBackground != null) {
swipeButton.setBackground(buttonBackground);
swipeButtonInner.setBackground(buttonBackground);
}
}

public void setEnabledDrawable(Drawable drawable) {
enabledDrawable = drawable;

if (active) {
swipeButton.setImageDrawable(drawable);
swipeButtonInner.setImageDrawable(drawable);
}
}

Expand All @@ -114,7 +114,7 @@ public void setInnerTextPadding(int left, int top, int right, int bottom) {
}

public void setSwipeButtonPadding(int left, int top, int right, int bottom) {
swipeButton.setPadding(left, top, right, bottom);
swipeButtonInner.setPadding(left, top, right, bottom);
}

private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
Expand All @@ -141,7 +141,7 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr, int def
background.addView(centerText, layoutParams);

final ImageView swipeButton = new ImageView(context);
this.swipeButton = swipeButton;
this.swipeButtonInner = swipeButton;

if (attrs != null && defStyleAttr == -1 && defStyleRes == -1) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SwipeButton,
Expand Down Expand Up @@ -249,34 +249,34 @@ private OnTouchListener getButtonTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
return true;
return !TouchUtils.isTouchOutsideInitialPosition(event, swipeButtonInner);
case MotionEvent.ACTION_MOVE:
if (initialX == 0) {
initialX = swipeButton.getX();
initialX = swipeButtonInner.getX();
}

if (event.getX() > initialX + swipeButton.getWidth() / 2 &&
event.getX() + swipeButton.getWidth() / 2 < getWidth()) {
swipeButton.setX(event.getX() - swipeButton.getWidth() / 2);
centerText.setAlpha(1 - 1.3f * (swipeButton.getX() + swipeButton.getWidth()) / getWidth());
if (event.getX() > initialX + swipeButtonInner.getWidth() / 2 &&
event.getX() + swipeButtonInner.getWidth() / 2 < getWidth()) {
swipeButtonInner.setX(event.getX() - swipeButtonInner.getWidth() / 2);
centerText.setAlpha(1 - 1.3f * (swipeButtonInner.getX() + swipeButtonInner.getWidth()) / getWidth());
}

if (event.getX() + swipeButton.getWidth() / 2 > getWidth() &&
swipeButton.getX() + swipeButton.getWidth() / 2 < getWidth()) {
swipeButton.setX(getWidth() - swipeButton.getWidth());
if (event.getX() + swipeButtonInner.getWidth() / 2 > getWidth() &&
swipeButtonInner.getX() + swipeButtonInner.getWidth() / 2 < getWidth()) {
swipeButtonInner.setX(getWidth() - swipeButtonInner.getWidth());
}

if (event.getX() < swipeButton.getWidth() / 2 &&
swipeButton.getX() > 0) {
swipeButton.setX(0);
if (event.getX() < swipeButtonInner.getWidth() / 2 &&
swipeButtonInner.getX() > 0) {
swipeButtonInner.setX(0);
}

return true;
case MotionEvent.ACTION_UP:
if (active) {
collapseButton();
} else {
if (swipeButton.getX() + swipeButton.getWidth() > getWidth() * 0.85) {
if (swipeButtonInner.getX() + swipeButtonInner.getWidth() > getWidth() * 0.85) {
expandButton();
} else {
moveButtonBack();
Expand All @@ -293,26 +293,26 @@ public boolean onTouch(View v, MotionEvent event) {

private void expandButton() {
final ValueAnimator positionAnimator =
ValueAnimator.ofFloat(swipeButton.getX(), 0);
ValueAnimator.ofFloat(swipeButtonInner.getX(), 0);
positionAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float x = (Float) positionAnimator.getAnimatedValue();
swipeButton.setX(x);
swipeButtonInner.setX(x);
}
});


final ValueAnimator widthAnimator = ValueAnimator.ofInt(
swipeButton.getWidth(),
swipeButtonInner.getWidth(),
getWidth());

widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewGroup.LayoutParams params = swipeButton.getLayoutParams();
ViewGroup.LayoutParams params = swipeButtonInner.getLayoutParams();
params.width = (Integer) widthAnimator.getAnimatedValue();
swipeButton.setLayoutParams(params);
swipeButtonInner.setLayoutParams(params);
}
});

Expand All @@ -324,7 +324,7 @@ public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);

active = true;
swipeButton.setImageDrawable(enabledDrawable);
swipeButtonInner.setImageDrawable(enabledDrawable);

if (onStateChangeListener != null) {
onStateChangeListener.onStateChange(active);
Expand All @@ -338,13 +338,13 @@ public void onAnimationEnd(Animator animation) {

private void moveButtonBack() {
final ValueAnimator positionAnimator =
ValueAnimator.ofFloat(swipeButton.getX(), 0);
ValueAnimator.ofFloat(swipeButtonInner.getX(), 0);
positionAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
positionAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float x = (Float) positionAnimator.getAnimatedValue();
swipeButton.setX(x);
swipeButtonInner.setX(x);
}
});

Expand All @@ -360,15 +360,15 @@ public void onAnimationUpdate(ValueAnimator animation) {

private void collapseButton() {
final ValueAnimator widthAnimator = ValueAnimator.ofInt(
swipeButton.getWidth(),
swipeButton.getHeight());
swipeButtonInner.getWidth(),
swipeButtonInner.getHeight());

widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewGroup.LayoutParams params = swipeButton.getLayoutParams();
ViewGroup.LayoutParams params = swipeButtonInner.getLayoutParams();
params.width = (Integer) widthAnimator.getAnimatedValue();
swipeButton.setLayoutParams(params);
swipeButtonInner.setLayoutParams(params);
}
});

Expand All @@ -377,7 +377,7 @@ public void onAnimationUpdate(ValueAnimator animation) {
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
active = false;
swipeButton.setImageDrawable(disabledDrawable);
swipeButtonInner.setImageDrawable(disabledDrawable);

if (onStateChangeListener != null) {
onStateChangeListener.onStateChange(active);
Expand Down
13 changes: 13 additions & 0 deletions swipe-button/src/main/java/com/ebanx/swipebtn/TouchUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.ebanx.swipebtn;

import android.view.MotionEvent;
import android.view.View;

final class TouchUtils {
private TouchUtils() {
}

static boolean isTouchOutsideInitialPosition(MotionEvent event, View view) {
return event.getX() > view.getX() + view.getWidth();
}
}

0 comments on commit fff61b7

Please sign in to comment.