Skip to content

Commit 479e65d

Browse files
committed
Improved variable and method names to make touch-handling flow clearer. Minor tweaks.
1 parent d3c9e64 commit 479e65d

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

library/src/main/java/com/jmedeisis/draglinearlayout/DragLinearLayout.java

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@
3535
* <p/>
3636
* Currently, no error-checking is done on standard {@link #addView(android.view.View)} and
3737
* {@link #removeView(android.view.View)} calls, so avoid using these with children previously
38-
* declared as draggable to prevent memory leaks and/or subtle bugs.
39-
* <p/>
40-
* Apologies - this class is neither clear nor readable. Maybe someday. But it works!
38+
* declared as draggable to prevent memory leaks and/or subtle bugs. Pull requests welcome!
4139
*/
4240
public class DragLinearLayout extends LinearLayout {
4341
private static final String LOG_TAG = "DragLinearLayout";
@@ -89,10 +87,10 @@ public void cancelExistingAnimation() {
8987
* Holds state information about the currently dragged item.
9088
* <p/>
9189
* Rough lifecycle:
92-
* <li>#setValidOnPossibleDrag - #valid == true</li>
90+
* <li>#startDetectingOnPossibleDrag - #detecting == true</li>
9391
* <li> if drag is recognised, #onDragStart - #dragging == true</li>
9492
* <li> if drag ends, #onDragStop - #dragging == false, #settling == true</li>
95-
* <li>if gesture ends without drag, or settling finishes, #setInvalid - #valid == false</li>
93+
* <li>if gesture ends without drag, or settling finishes, #stopDetecting - #detecting == false</li>
9694
*/
9795
private class DragItem {
9896
private View view;
@@ -105,14 +103,14 @@ private class DragItem {
105103
private int targetTopOffset;
106104
private ValueAnimator settleAnimation;
107105

108-
private boolean valid;
106+
private boolean detecting;
109107
private boolean dragging;
110108

111109
public DragItem() {
112-
setInvalid();
110+
stopDetecting();
113111
}
114112

115-
public void setValidOnPossibleDrag(final View view, final int position) {
113+
public void startDetectingOnPossibleDrag(final View view, final int position) {
116114
this.view = view;
117115
this.startVisibility = view.getVisibility();
118116
this.viewDrawable = getDragDrawable(view);
@@ -123,7 +121,7 @@ public void setValidOnPossibleDrag(final View view, final int position) {
123121
this.targetTopOffset = 0;
124122
this.settleAnimation = null;
125123

126-
this.valid = true;
124+
this.detecting = true;
127125
}
128126

129127
public void onDragStart() {
@@ -148,8 +146,8 @@ public boolean settling() {
148146
return null != settleAnimation;
149147
}
150148

151-
public void setInvalid() {
152-
this.valid = false;
149+
public void stopDetecting() {
150+
this.detecting = false;
153151
if (null != view) view.setVisibility(startVisibility);
154152
view = null;
155153
startVisibility = -1;
@@ -165,7 +163,7 @@ public void setInvalid() {
165163
}
166164

167165
/**
168-
* The currently dragged item, if {@link com.jmedeisis.draglinearlayout.DragLinearLayout.DragItem#valid}.
166+
* The currently dragged item, if {@link com.jmedeisis.draglinearlayout.DragLinearLayout.DragItem#detecting}.
169167
*/
170168
private final DragItem draggedItem;
171169
private final int slop;
@@ -338,17 +336,18 @@ private long getTranslateAnimationDuration(float distance) {
338336

339337
/**
340338
* Initiates a new {@link #draggedItem} unless the current one is still
341-
* {@link com.jmedeisis.draglinearlayout.DragLinearLayout.DragItem#valid}.
339+
* {@link com.jmedeisis.draglinearlayout.DragLinearLayout.DragItem#detecting}.
342340
*/
343341
private void startDetectingDrag(View child) {
344-
if (draggedItem.valid) return; // existing drag in process, only one at a time is allowed
342+
if (draggedItem.detecting)
343+
return; // existing drag in process, only one at a time is allowed
345344

346345
final int position = indexOfChild(child);
347346

348347
// complete any existing animations, both for the newly selected child and the previous dragged one
349348
draggableChildren.get(position).endExistingAnimation();
350349

351-
draggedItem.setValidOnPossibleDrag(child, position);
350+
draggedItem.startDetectingOnPossibleDrag(child, position);
352351
}
353352

354353
private void startDrag() {
@@ -359,14 +358,14 @@ private void startDrag() {
359358
/**
360359
* Animates the dragged item to its final resting position.
361360
*/
362-
private void stopDrag() {
361+
private void onDragStop() {
363362
draggedItem.settleAnimation = ValueAnimator.ofFloat(draggedItem.totalDragOffset,
364363
draggedItem.totalDragOffset - draggedItem.targetTopOffset)
365364
.setDuration(getTranslateAnimationDuration(draggedItem.targetTopOffset));
366365
draggedItem.settleAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
367366
@Override
368367
public void onAnimationUpdate(ValueAnimator animation) {
369-
if (!draggedItem.valid) return; // already stopped
368+
if (!draggedItem.detecting) return; // already stopped
370369

371370
draggedItem.setTotalOffset(((Float) animation.getAnimatedValue()).intValue());
372371

@@ -384,12 +383,12 @@ public void onAnimationStart(Animator animation) {
384383

385384
@Override
386385
public void onAnimationEnd(Animator animation) {
387-
if (!draggedItem.valid) {
386+
if (!draggedItem.detecting) {
388387
return; // already stopped
389388
}
390389

391390
draggedItem.settleAnimation = null;
392-
draggedItem.setInvalid();
391+
draggedItem.stopDetecting();
393392

394393
if (null != dragTopShadowDrawable) dragTopShadowDrawable.setAlpha(255);
395394
dragBottomShadowDrawable.setAlpha(255);
@@ -491,7 +490,7 @@ public boolean onPreDraw() {
491490
Log.d(LOG_TAG, "Updating settle animation");
492491
draggedItem.settleAnimation.removeAllListeners();
493492
draggedItem.settleAnimation.cancel();
494-
stopDrag();
493+
onDragStop();
495494
}
496495
return true;
497496
}
@@ -555,7 +554,7 @@ private static float smootherStep(float edge1, float edge2, float val) {
555554
protected void dispatchDraw(@NonNull Canvas canvas) {
556555
super.dispatchDraw(canvas);
557556

558-
if (draggedItem.valid && (draggedItem.dragging || draggedItem.settling())) {
557+
if (draggedItem.detecting && (draggedItem.dragging || draggedItem.settling())) {
559558
canvas.save();
560559
canvas.translate(0, draggedItem.totalDragOffset);
561560
draggedItem.viewDrawable.draw(canvas);
@@ -583,37 +582,37 @@ protected void dispatchDraw(@NonNull Canvas canvas) {
583582
* 1) User taps outside any children.
584583
* #onInterceptTouchEvent receives DOWN
585584
* #onTouchEvent receives DOWN
586-
* draggedItem.valid == false, we return false and no further events are received
585+
* draggedItem.detecting == false, we return false and no further events are received
587586
* 2) User taps on non-interactive drag handle / child, e.g. TextView or ImageView.
588587
* #onInterceptTouchEvent receives DOWN
589588
* DragHandleOnTouchListener (attached to each draggable child) #onTouch receives DOWN
590-
* #startDetectingDrag is called, draggedItem is now valid
589+
* #startDetectingDrag is called, draggedItem is now detecting
591590
* view does not handle touch, so our #onTouchEvent receives DOWN
592-
* draggedItem.valid == true, we #startDrag() and proceed to handle the drag
591+
* draggedItem.detecting == true, we #startDrag() and proceed to handle the drag
593592
* 3) User taps on interactive drag handle / child, e.g. Button.
594593
* #onInterceptTouchEvent receives DOWN
595594
* DragHandleOnTouchListener (attached to each draggable child) #onTouch receives DOWN
596-
* #startDetectingDrag is called, draggedItem is now valid
595+
* #startDetectingDrag is called, draggedItem is now detecting
597596
* view handles touch, so our #onTouchEvent is not called yet
598597
* #onInterceptTouchEvent receives ACTION_MOVE
599598
* if dy > touch slop, we assume user wants to drag and intercept the event
600599
* #onTouchEvent receives further ACTION_MOVE events, proceed to handle the drag
601600
*
602601
* For cases 2) and 3), lifting the active pointer at any point in the sequence of events
603-
* triggers #onTouchEnded and the draggedItem, if valid, is #setInvalid.
602+
* triggers #onTouchEnd and the draggedItem, if detecting, is #stopDetecting.
604603
*/
605604

606605
@Override
607606
public boolean onInterceptTouchEvent(MotionEvent event) {
608607
switch (MotionEventCompat.getActionMasked(event)) {
609608
case MotionEvent.ACTION_DOWN: {
610-
if (draggedItem.valid) return false; // an existing item is (likely) settling
609+
if (draggedItem.detecting) return false; // an existing item is (likely) settling
611610
downY = (int) MotionEventCompat.getY(event, 0);
612611
activePointerId = MotionEventCompat.getPointerId(event, 0);
613612
break;
614613
}
615614
case MotionEvent.ACTION_MOVE: {
616-
if (!draggedItem.valid) return false;
615+
if (!draggedItem.detecting) return false;
617616
if (INVALID_POINTER_ID == activePointerId) break;
618617
final int pointerIndex = event.findPointerIndex(activePointerId);
619618
final float y = MotionEventCompat.getY(event, pointerIndex);
@@ -633,9 +632,9 @@ public boolean onInterceptTouchEvent(MotionEvent event) {
633632
}
634633
case MotionEvent.ACTION_CANCEL:
635634
case MotionEvent.ACTION_UP: {
636-
onTouchEnded();
635+
onTouchEnd();
637636

638-
if (draggedItem.valid) draggedItem.setInvalid();
637+
if (draggedItem.detecting) draggedItem.stopDetecting();
639638
break;
640639
}
641640
}
@@ -647,7 +646,7 @@ public boolean onInterceptTouchEvent(MotionEvent event) {
647646
public boolean onTouchEvent(@NonNull MotionEvent event) {
648647
switch (MotionEventCompat.getActionMasked(event)) {
649648
case MotionEvent.ACTION_DOWN: {
650-
if (!draggedItem.valid || draggedItem.settling()) return false;
649+
if (!draggedItem.detecting || draggedItem.settling()) return false;
651650
startDrag();
652651
return true;
653652
}
@@ -671,16 +670,20 @@ public boolean onTouchEvent(@NonNull MotionEvent event) {
671670
}
672671
case MotionEvent.ACTION_CANCEL:
673672
case MotionEvent.ACTION_UP: {
674-
onTouchEnded();
673+
onTouchEnd();
675674

676-
if (draggedItem.dragging) stopDrag(); // TODO test whether check necessary
675+
if (draggedItem.dragging) {
676+
onDragStop();
677+
} else if (draggedItem.detecting) {
678+
draggedItem.stopDetecting();
679+
}
677680
return true;
678681
}
679682
}
680683
return false;
681684
}
682685

683-
private void onTouchEnded() {
686+
private void onTouchEnd() {
684687
downY = -1;
685688
activePointerId = INVALID_POINTER_ID;
686689
}

0 commit comments

Comments
 (0)