Skip to content

Commit

Permalink
remove methods consolidation
Browse files Browse the repository at this point in the history
  • Loading branch information
imbryk committed Jan 22, 2013
1 parent 1ccc218 commit d736daa
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 142 deletions.
2 changes: 1 addition & 1 deletion demo/res/layout/hetero_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
dslv:sort_enabled="true"
dslv:remove_enabled="true"
dslv:drag_start_mode="onDown"
dslv:remove_mode="flingLeft" />
dslv:remove_mode="flingRemove" />
6 changes: 1 addition & 5 deletions demo/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@
<string name="add_footer">Add footer</string>
<string-array name="remove_mode_labels">
<item>Click remove</item>
<item>Fling right</item>
<item>Fling left</item>
<item>Slide right</item>
<item>Slide left</item>
<item>Fling or Slide</item>
<item>Fling remove</item>
</string-array>
<string-array name="drag_init_mode_labels">
<item>On down</item>
Expand Down
7 changes: 4 additions & 3 deletions demo/src/com/mobeta/android/demodslv/DSLVFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ protected int getLayout() {
* Return list item layout resource passed to the ArrayAdapter.
*/
protected int getItemLayout() {
if (removeMode == DragSortController.FLING_LEFT_REMOVE || removeMode == DragSortController.SLIDE_LEFT_REMOVE) {
/*if (removeMode == DragSortController.FLING_LEFT_REMOVE || removeMode == DragSortController.SLIDE_LEFT_REMOVE) {
return R.layout.list_item_handle_right;
} else if (removeMode == DragSortController.CLICK_REMOVE) {
} else */
if (removeMode == DragSortController.CLICK_REMOVE) {
return R.layout.list_item_click_remove;
} else {
return R.layout.list_item_handle_left;
Expand All @@ -67,7 +68,7 @@ protected int getItemLayout() {

public int dragStartMode = DragSortController.ON_DOWN;
public boolean removeEnabled = false;
public int removeMode = DragSortController.FLING_RIGHT_REMOVE;
public int removeMode = DragSortController.FLING_REMOVE;
public boolean sortEnabled = true;
public boolean dragEnabled = true;

Expand Down
2 changes: 1 addition & 1 deletion demo/src/com/mobeta/android/demodslv/RemoveModeDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class RemoveModeDialog extends DialogFragment {

public RemoveModeDialog() {
super();
mRemoveMode = DragSortController.FLING_RIGHT_REMOVE;
mRemoveMode = DragSortController.FLING_REMOVE;
}

public RemoveModeDialog(int inRemoveMode) {
Expand Down
2 changes: 1 addition & 1 deletion demo/src/com/mobeta/android/demodslv/TestBedDSLV.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class TestBedDSLV extends FragmentActivity implements

private int mDragStartMode = DragSortController.ON_DRAG;
private boolean mRemoveEnabled = true;
private int mRemoveMode = DragSortController.FLING_OR_SLIDE_REMOVE;
private int mRemoveMode = DragSortController.FLING_REMOVE;
private boolean mSortEnabled = true;
private boolean mDragEnabled = true;

Expand Down
6 changes: 1 addition & 5 deletions library/res/values/dslv_attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
<attr name="float_background_color" format="color" />
<attr name="remove_mode">
<enum name="clickRemove" value="0" />
<enum name="flingRight" value="1" />
<enum name="flingLeft" value="2" />
<enum name="slideRight" value="3" />
<enum name="slideLeft" value="4" />
<enum name="flingOrSlide" value="5" />
<enum name="flingRemove" value="1" />
</attr>
<attr name="track_drag_sort" format="boolean"/>
<attr name="float_alpha" format="float"/>
Expand Down
158 changes: 33 additions & 125 deletions library/src/com/mobeta/android/dslv/DragSortController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.mobeta.android.dslv;

import android.util.Log;
import android.graphics.Point;
import android.view.GestureDetector;
import android.view.HapticFeedbackConstants;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.graphics.Point;
import android.widget.AdapterView;

/**
Expand Down Expand Up @@ -37,11 +36,7 @@ public class DragSortController extends SimpleFloatViewManager implements View.O
* Remove mode enum.
*/
public static final int CLICK_REMOVE = 0;
public static final int FLING_RIGHT_REMOVE = 1;
public static final int FLING_LEFT_REMOVE = 2;
public static final int SLIDE_RIGHT_REMOVE = 3;
public static final int SLIDE_LEFT_REMOVE = 4;
public static final int FLING_OR_SLIDE_REMOVE = 5;
public static final int FLING_REMOVE = 1;

/**
* The current remove mode.
Expand Down Expand Up @@ -76,8 +71,6 @@ public class DragSortController extends SimpleFloatViewManager implements View.O

private float mFlingSpeed = 500f;

private float mOrigFloatAlpha = 1.0f;

private int mDragHandleId;

private int mClickRemoveId;
Expand All @@ -98,7 +91,7 @@ public class DragSortController extends SimpleFloatViewManager implements View.O
* @param dslv The DSLV instance
*/
public DragSortController(DragSortListView dslv) {
this(dslv, 0, ON_DOWN, FLING_RIGHT_REMOVE);
this(dslv, 0, ON_DOWN, FLING_REMOVE);
}

public DragSortController(DragSortListView dslv, int dragHandleId, int dragInitMode, int removeMode) {
Expand Down Expand Up @@ -129,7 +122,6 @@ public DragSortController(DragSortListView dslv, int dragHandleId, int dragInitM
mFlingHandleId = flingHandleId;
setRemoveMode(removeMode);
setDragInitMode(dragInitMode);
mOrigFloatAlpha = dslv.getFloatAlpha();
}


Expand Down Expand Up @@ -234,16 +226,9 @@ public boolean startDrag(int position, int deltaX, int deltaY) {
if (mSortEnabled && !mIsRemoving) {
dragFlags |= DragSortListView.DRAG_POS_Y | DragSortListView.DRAG_NEG_Y;
}
if (mRemoveEnabled) {
if (mRemoveMode == FLING_RIGHT_REMOVE) {
dragFlags |= DragSortListView.DRAG_POS_X;
} else if (mRemoveMode == FLING_LEFT_REMOVE) {
dragFlags |= DragSortListView.DRAG_NEG_X;
}
else if( mRemoveMode == FLING_OR_SLIDE_REMOVE && mIsRemoving){
dragFlags |= DragSortListView.DRAG_POS_X;
dragFlags |= DragSortListView.DRAG_NEG_X;
}
if (mRemoveEnabled && mIsRemoving){
dragFlags |= DragSortListView.DRAG_POS_X;
dragFlags |= DragSortListView.DRAG_NEG_X;
}

mDragging = mDslv.startDrag(position - mDslv.getHeaderViewsCount(), dragFlags, deltaX, deltaY);
Expand All @@ -257,7 +242,7 @@ public boolean onTouch(View v, MotionEvent ev) {
}

mDetector.onTouchEvent(ev);
if (mRemoveEnabled && mDragging && (mRemoveMode == FLING_RIGHT_REMOVE || mRemoveMode == FLING_LEFT_REMOVE || mRemoveMode == FLING_OR_SLIDE_REMOVE )) {
if (mRemoveEnabled && mDragging && mRemoveMode == FLING_REMOVE ) {
mFlingRemoveDetector.onTouchEvent(ev);
}

Expand All @@ -269,23 +254,11 @@ public boolean onTouch(View v, MotionEvent ev) {
mCurrY = (int) ev.getY();
break;
case MotionEvent.ACTION_UP:
if (mRemoveEnabled) {
if ( mRemoveMode == FLING_OR_SLIDE_REMOVE && mIsRemoving ) {
int x = mPositionX >= 0 ? mPositionX : -mPositionX;
int removePoint = mDslv.getWidth() / 2;
if( x > removePoint ){
mDslv.stopDragWithVelocity(true,0);
}
}
else
{
final int x = (int) ev.getX();
int thirdW = mDslv.getWidth() / 3;
int twoThirdW = mDslv.getWidth() - thirdW;
if ((mRemoveMode == SLIDE_RIGHT_REMOVE && x > twoThirdW) ||
(mRemoveMode == SLIDE_LEFT_REMOVE && x < thirdW)) {
mDslv.stopDrag(true);
}
if (mRemoveEnabled && mIsRemoving ) {
int x = mPositionX >= 0 ? mPositionX : -mPositionX;
int removePoint = mDslv.getWidth() / 2;
if( x > removePoint ){
mDslv.stopDragWithVelocity(true,0);
}
}
case MotionEvent.ACTION_CANCEL:
Expand All @@ -303,40 +276,8 @@ public boolean onTouch(View v, MotionEvent ev) {
@Override
public void onDragFloatView(View floatView, Point position, Point touch) {

if (mRemoveEnabled) {
if( mIsRemoving ){
mPositionX = position.x;
}
int x = touch.x;
int y = touch.y;

if (mRemoveMode == SLIDE_RIGHT_REMOVE) {
int width = mDslv.getWidth();
int thirdWidth = width / 3;

float alpha;
if (x < thirdWidth) {
alpha = 1.0f;
} else if (x < width - thirdWidth) {
alpha = ((float) (width - thirdWidth - x)) / ((float) thirdWidth);
} else {
alpha = 0.0f;
}
mDslv.setFloatAlpha(mOrigFloatAlpha * alpha);
} else if (mRemoveMode == SLIDE_LEFT_REMOVE) {
int width = mDslv.getWidth();
int thirdWidth = width / 3;

float alpha;
if (x < thirdWidth) {
alpha = 0.0f;
} else if (x < width - thirdWidth) {
alpha = ((float) (x - thirdWidth)) / ((float) thirdWidth);
} else {
alpha = 1.0f;
}
mDslv.setFloatAlpha(mOrigFloatAlpha * alpha);
}
if (mRemoveEnabled && mIsRemoving ){
mPositionX = position.x;
}
}

Expand All @@ -357,7 +298,7 @@ public int startDragPosition(MotionEvent ev) {
return dragHandleHitPosition(ev);
}
public int startFlingPosition(MotionEvent ev) {
return mRemoveMode == FLING_OR_SLIDE_REMOVE ? flingHandleHitPosition(ev) : MISS;
return mRemoveMode == FLING_REMOVE ? flingHandleHitPosition(ev) : MISS;
}

/**
Expand Down Expand Up @@ -395,9 +336,7 @@ public int viewIdHitPosition(MotionEvent ev, int id) {
final int rawX = (int) ev.getRawX();
final int rawY = (int) ev.getRawY();

//View dragBox = (View) item.getTag();
View dragBox = id == 0 ? item : (View) item.findViewById(id);
boolean dragHit = false;
if (dragBox != null) {
dragBox.getLocationOnScreen(mTempLoc);

Expand Down Expand Up @@ -445,7 +384,7 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
final int deltaX = x2-mItemX;
final int deltaY = y2-mItemY;

if( mRemoveMode==FLING_OR_SLIDE_REMOVE && mCanDrag && !mDragging && (mHitPos != MISS || mFlingHitPos !=MISS )) {
if( mCanDrag && !mDragging && (mHitPos != MISS || mFlingHitPos !=MISS )) {

if( mHitPos != MISS )
{
Expand Down Expand Up @@ -476,21 +415,6 @@ else if( Math.abs(y2-y1) > mTouchSlop)
}
}
}
else if (mHitPos != MISS && mDragInitMode == ON_DRAG && !mDragging) {

boolean start = false;
if (mRemoveEnabled && mSortEnabled) {
start = true;
} else if (mRemoveEnabled) {
start = Math.abs(x2 - x1) > mTouchSlop;
} else if (mSortEnabled) {
start = Math.abs(y2 - y1) > mTouchSlop;
}

if (start) {
startDrag(mHitPos, deltaX, deltaY);
}
}
// return whatever
return false;
}
Expand Down Expand Up @@ -532,41 +456,25 @@ public void onShowPress(MotionEvent ev) {
@Override
public final boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//Log.d("mobeta", "on fling remove called");
if (mRemoveEnabled) {
switch (mRemoveMode) {
case FLING_OR_SLIDE_REMOVE:
if( mIsRemoving ) {
int w = mDslv.getWidth();
int minPos = w/5;
if (velocityX > mFlingSpeed )
{
if( mPositionX > -minPos )
{
mDslv.stopDragWithVelocity(true,velocityX);
}
}
else if (velocityX < -mFlingSpeed )
{
if( mPositionX < minPos )
{
mDslv.stopDragWithVelocity(true,velocityX);
}
}
}
break;
case FLING_RIGHT_REMOVE:
if (velocityX > mFlingSpeed) {
mDslv.stopDrag(true);
}
break;
case FLING_LEFT_REMOVE:
if (velocityX < -mFlingSpeed) {
mDslv.stopDrag(true);
}
break;
if (mRemoveEnabled && mIsRemoving ) {
int w = mDslv.getWidth();
int minPos = w/5;
if (velocityX > mFlingSpeed )
{
if( mPositionX > -minPos )
{
mDslv.stopDragWithVelocity(true,velocityX);
}
}
else if (velocityX < -mFlingSpeed )
{
if( mPositionX < minPos )
{
mDslv.stopDragWithVelocity(true,velocityX);
}
}
mIsRemoving = false;
}
mIsRemoving = false;
return false;
}
};
Expand Down
2 changes: 1 addition & 1 deletion library/src/com/mobeta/android/dslv/DragSortListView.java
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ public DragSortListView(Context context, AttributeSet attrs) {
false);
int removeMode = a.getInt(
R.styleable.DragSortListView_remove_mode,
DragSortController.FLING_RIGHT_REMOVE);
DragSortController.FLING_REMOVE);
boolean sortEnabled = a.getBoolean(
R.styleable.DragSortListView_sort_enabled,
true);
Expand Down

0 comments on commit d736daa

Please sign in to comment.