Skip to content

Commit

Permalink
refactor SwipeBack
Browse files Browse the repository at this point in the history
  • Loading branch information
cgspine committed Apr 26, 2020
1 parent 4c64aa6 commit 9cb0960
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 143 deletions.
68 changes: 44 additions & 24 deletions arch/src/main/java/com/qmuiteam/qmui/arch/QMUIActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import static com.qmuiteam.qmui.arch.SwipeBackLayout.DRAG_DIRECTION_BOTTOM_TO_TOP;
import static com.qmuiteam.qmui.arch.SwipeBackLayout.DRAG_DIRECTION_LEFT_TO_RIGHT;
import static com.qmuiteam.qmui.arch.SwipeBackLayout.DRAG_DIRECTION_NONE;
import static com.qmuiteam.qmui.arch.SwipeBackLayout.DRAG_DIRECTION_RIGHT_TO_LEFT;
import static com.qmuiteam.qmui.arch.SwipeBackLayout.DRAG_DIRECTION_TOP_TO_BOTTOM;
import static com.qmuiteam.qmui.arch.SwipeBackLayout.EDGE_BOTTOM;
Expand Down Expand Up @@ -105,16 +107,15 @@ public void onScrollOverThreshold() {
}
};
private SwipeBackLayout.Callback mSwipeCallback = new SwipeBackLayout.Callback() {
@Override
public boolean canSwipeBack(SwipeBackLayout layout, int dragDirection, int moveEdge) {
return QMUISwipeBackActivityManager.getInstance().canSwipeBack() &&
canDragBack(layout.getContext(), dragDirection, moveEdge);
}

@Override
public boolean shouldBeginDrag(SwipeBackLayout swipeBackLayout,
float downX, float downY, int direction) {
return QMUIActivity.this.shouldBeginDrag(swipeBackLayout, downX, downY, direction);
public int getDragDirection(SwipeBackLayout swipeBackLayout,
SwipeBackLayout.ViewMoveAction moveAction,
float downX, float downY, float dx, float dy, float touchSlop) {
if(!QMUISwipeBackActivityManager.getInstance().canSwipeBack()){
return SwipeBackLayout.DRAG_DIRECTION_NONE;
}
return QMUIActivity.this.getDragDirection(swipeBackLayout,moveAction,downX, downY, dx, dy, touchSlop);
}
};

Expand All @@ -135,8 +136,7 @@ public void setContentView(View view) {

@Override
public void setContentView(int layoutResID) {
SwipeBackLayout swipeBackLayout = SwipeBackLayout.wrap(this,
layoutResID, dragBackDirection(), dragViewMoveAction(), mSwipeCallback);
SwipeBackLayout swipeBackLayout = SwipeBackLayout.wrap(this, layoutResID, dragViewMoveAction(), mSwipeCallback);
if (translucentFull()) {
swipeBackLayout.getContentView().setFitsSystemWindows(false);
} else {
Expand All @@ -157,8 +157,7 @@ private View newSwipeBackLayout(View view) {
} else {
view.setFitsSystemWindows(true);
}
final SwipeBackLayout swipeBackLayout = SwipeBackLayout.wrap(
view, dragBackDirection(), dragViewMoveAction(), mSwipeCallback);
final SwipeBackLayout swipeBackLayout = SwipeBackLayout.wrap(view, dragViewMoveAction(), mSwipeCallback);
mListenerRemover = swipeBackLayout.addSwipeListener(mSwipeListener);
return swipeBackLayout;
}
Expand Down Expand Up @@ -197,14 +196,21 @@ public boolean isInSwipeBack() {
* disable or enable drag back
*
* @return if true open dragBack, otherwise close dragBack
* @deprecated Use {@link #canDragBack(Context, int, int)}
* @deprecated Use {@link #getDragDirection(SwipeBackLayout, SwipeBackLayout.ViewMoveAction, float, float, float, float, float)}
*/
@Deprecated
protected boolean canDragBack() {
return true;
}


/**
* disable or enable drag back
*
* @return if true open dragBack, otherwise close dragBack
* @deprecated Use {@link #getDragDirection(SwipeBackLayout, SwipeBackLayout.ViewMoveAction, float, float, float, float, float)}
*/
@Deprecated
protected boolean canDragBack(Context context, int dragDirection, int moveEdge) {
return canDragBack();
}
Expand All @@ -222,19 +228,33 @@ protected int backViewInitOffset(Context context, int dragDirection, int moveEdg
return backViewInitOffset();
}

protected boolean shouldBeginDrag(SwipeBackLayout swipeBackLayout,
float downX, float downY, int dragDirection){
protected int getDragDirection(@NonNull SwipeBackLayout swipeBackLayout,
@NonNull SwipeBackLayout.ViewMoveAction viewMoveAction,
float downX, float downY, float dx, float dy, float slopTouch){
int targetDirection = dragBackDirection();
if(!canDragBack(swipeBackLayout.getContext(), targetDirection, viewMoveAction.getEdge(targetDirection))){
return DRAG_DIRECTION_NONE;
}
int edgeSize = QMUIDisplayHelper.dp2px(swipeBackLayout.getContext(), 20);
if(dragDirection == DRAG_DIRECTION_LEFT_TO_RIGHT){
return downX < edgeSize;
}else if(dragDirection == DRAG_DIRECTION_RIGHT_TO_LEFT){
return downX > swipeBackLayout.getWidth() - edgeSize;
}else if(dragDirection == DRAG_DIRECTION_TOP_TO_BOTTOM){
return downY < edgeSize;
}else if(dragDirection == DRAG_DIRECTION_BOTTOM_TO_TOP){
return downY > swipeBackLayout.getHeight() - edgeSize;
if (targetDirection == DRAG_DIRECTION_LEFT_TO_RIGHT) {
if(downX < edgeSize && dx >= slopTouch){
return targetDirection;
}
} else if (targetDirection == DRAG_DIRECTION_RIGHT_TO_LEFT) {
if(downX > swipeBackLayout.getWidth() - edgeSize && -dx >= slopTouch){
return targetDirection;
}
} else if (targetDirection == DRAG_DIRECTION_TOP_TO_BOTTOM) {
if(downY < edgeSize && dy >= slopTouch){
return targetDirection;
}
} else if (targetDirection == DRAG_DIRECTION_BOTTOM_TO_TOP) {
if(downY > swipeBackLayout.getHeight() - edgeSize && -dy >= slopTouch){
return targetDirection;
}
}
return true;

return DRAG_DIRECTION_NONE;
}

/**
Expand Down
98 changes: 65 additions & 33 deletions arch/src/main/java/com/qmuiteam/qmui/arch/QMUIFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@

import static com.qmuiteam.qmui.arch.SwipeBackLayout.DRAG_DIRECTION_BOTTOM_TO_TOP;
import static com.qmuiteam.qmui.arch.SwipeBackLayout.DRAG_DIRECTION_LEFT_TO_RIGHT;
import static com.qmuiteam.qmui.arch.SwipeBackLayout.DRAG_DIRECTION_NONE;
import static com.qmuiteam.qmui.arch.SwipeBackLayout.DRAG_DIRECTION_RIGHT_TO_LEFT;
import static com.qmuiteam.qmui.arch.SwipeBackLayout.DRAG_DIRECTION_TOP_TO_BOTTOM;
import static com.qmuiteam.qmui.arch.SwipeBackLayout.EDGE_BOTTOM;
Expand Down Expand Up @@ -469,9 +470,11 @@ private int startFragment(QMUIFragment fragment, QMUIFragmentContainerProvider p
}

/**
* @deprecated use {@link #notifyEffect} for a replacement
*
* @param resultCode
* @param data
*
* @deprecated use {@link #notifyEffect} for a replacement
*/
@Deprecated
public void setFragmentResult(int resultCode, Intent data) {
Expand Down Expand Up @@ -508,59 +511,55 @@ private SwipeBackLayout newSwipeBackLayout() {
} else {
rootView.setFitsSystemWindows(true);
}
final SwipeBackLayout swipeBackLayout = SwipeBackLayout.wrap(rootView, dragBackDirection(),
final SwipeBackLayout swipeBackLayout = SwipeBackLayout.wrap(rootView,
dragViewMoveAction(),
new SwipeBackLayout.Callback() {
@Override
public boolean canSwipeBack(SwipeBackLayout layout, int dragDirection, int moveEdge) {
public int getDragDirection(SwipeBackLayout swipeBackLayout, SwipeBackLayout.ViewMoveAction viewMoveAction, float downX, float downY, float dx, float dy, float touchSlop) {
// 1. can not swipe back if in animation
if (mEnterAnimationStatus != ANIMATION_ENTER_STATUS_END) {
return false;
return SwipeBackLayout.DRAG_DIRECTION_NONE;
}

// 2. can not swipe back if it is not managed by FragmentContainer
QMUIFragmentContainerProvider provider = findFragmentContainerProvider();
if(provider == null){
return false;
return SwipeBackLayout.DRAG_DIRECTION_NONE;
}

FragmentManager fragmentManager = provider.getContainerFragmentManager();
if(fragmentManager == null || fragmentManager != getParentFragmentManager()){
return false;
return SwipeBackLayout.DRAG_DIRECTION_NONE;
}

// 3. need be handled by inner FragmentContainer
if(fragmentManager.getPrimaryNavigationFragment() != null){
return false;
}

if (!canDragBack(layout.getContext(), dragDirection, moveEdge)) {
return false;
return SwipeBackLayout.DRAG_DIRECTION_NONE;
}

// 4. can not swipe back if the view is null
View view = getView();
if (view == null) {
return false;
return SwipeBackLayout.DRAG_DIRECTION_NONE;
}

// if the Fragment is in ViewPager, then stop drag back
// 5. can not swipe back if if the Fragment is in ViewPager
ViewParent parent = view.getParent();
while (parent != null) {
if (parent instanceof ViewPager || parent instanceof ViewPager2) {
return false;
return SwipeBackLayout.DRAG_DIRECTION_NONE;
}
parent = parent.getParent();
}

if (fragmentManager.getBackStackEntryCount() <= 1) {
return QMUISwipeBackActivityManager.getInstance().canSwipeBack();
// 6. can not swipe back if the backStack entry count is less than 2
if (fragmentManager.getBackStackEntryCount() <= 1 &&
!QMUISwipeBackActivityManager.getInstance().canSwipeBack()) {
return SwipeBackLayout.DRAG_DIRECTION_NONE;
}
return true;
}

@Override
public boolean shouldBeginDrag(SwipeBackLayout swipeBackLayout, float downX, float downY, int dragDirection) {
return QMUIFragment.this.shouldBeginDrag(swipeBackLayout, downX, downY, dragDirection);
return QMUIFragment.this.getDragDirection(
swipeBackLayout, viewMoveAction, downX, downY, dx, dy, touchSlop);
}
});
mListenerRemover = swipeBackLayout.addSwipeListener(mSwipeListener);
Expand Down Expand Up @@ -1112,7 +1111,10 @@ protected void onViewCreated(@NonNull View rootView) {
* @param requestCode request code
* @param resultCode result code
* @param data extra data
*
* @deprecated use {@link #registerEffect} for a replacement
*/
@Deprecated
protected void onFragmentResult(int requestCode, int resultCode, Intent data) {

}
Expand All @@ -1121,14 +1123,24 @@ protected void onFragmentResult(int requestCode, int resultCode, Intent data) {
* disable or enable drag back
*
* @return if true open dragBack, otherwise close dragBack
* @deprecated Use {@link #canDragBack(Context, int, int)}
* @deprecated Use {@link #getDragDirection(SwipeBackLayout, SwipeBackLayout.ViewMoveAction, float, float, float, float, float)}
*/
@Deprecated
protected boolean canDragBack() {
return true;
}


/**
* disable or enable drag back
* @param context context
* @param dragDirection gesture direction
* @param moveEdge view move edge
* @return if true open dragBack, otherwise close dragBack
*
* @deprecated Use {@link #getDragDirection(SwipeBackLayout, SwipeBackLayout.ViewMoveAction, float, float, float, float, float)}
*/
@Deprecated
protected boolean canDragBack(Context context, int dragDirection, int moveEdge) {
return canDragBack();
}
Expand Down Expand Up @@ -1162,6 +1174,12 @@ protected int dragBackEdge() {
return EDGE_LEFT;
}

/**
*
* @return
* @deprecated Use {@link #getDragDirection(SwipeBackLayout, SwipeBackLayout.ViewMoveAction, float, float, float, float, float)}
*/
@Deprecated
protected int dragBackDirection() {
int oldEdge = dragBackEdge();
if (oldEdge == EDGE_RIGHT) {
Expand All @@ -1178,19 +1196,33 @@ protected SwipeBackLayout.ViewMoveAction dragViewMoveAction() {
return SwipeBackLayout.MOVE_VIEW_AUTO;
}

protected boolean shouldBeginDrag(SwipeBackLayout swipeBackLayout,
float downX, float downY, int dragDirection) {
protected int getDragDirection(@NonNull SwipeBackLayout swipeBackLayout,
@NonNull SwipeBackLayout.ViewMoveAction viewMoveAction,
float downX, float downY, float dx, float dy, float slopTouch){
int targetDirection = dragBackDirection();
if(!canDragBack(swipeBackLayout.getContext(), targetDirection, viewMoveAction.getEdge(targetDirection))){
return DRAG_DIRECTION_NONE;
}
int edgeSize = QMUIDisplayHelper.dp2px(swipeBackLayout.getContext(), 20);
if (dragDirection == DRAG_DIRECTION_LEFT_TO_RIGHT) {
return downX < edgeSize;
} else if (dragDirection == DRAG_DIRECTION_RIGHT_TO_LEFT) {
return downX > swipeBackLayout.getWidth() - edgeSize;
} else if (dragDirection == DRAG_DIRECTION_TOP_TO_BOTTOM) {
return downY < edgeSize;
} else if (dragDirection == DRAG_DIRECTION_BOTTOM_TO_TOP) {
return downY > swipeBackLayout.getHeight() - edgeSize;
if (targetDirection == DRAG_DIRECTION_LEFT_TO_RIGHT) {
if(downX < edgeSize && dx >= slopTouch){
return targetDirection;
}
} else if (targetDirection == DRAG_DIRECTION_RIGHT_TO_LEFT) {
if(downX > swipeBackLayout.getWidth() - edgeSize && -dx >= slopTouch){
return targetDirection;
}
} else if (targetDirection == DRAG_DIRECTION_TOP_TO_BOTTOM) {
if(downY < edgeSize && dy >= slopTouch){
return targetDirection;
}
} else if (targetDirection == DRAG_DIRECTION_BOTTOM_TO_TOP) {
if(downY > swipeBackLayout.getHeight() - edgeSize && -dy >= slopTouch){
return targetDirection;
}
}
return true;

return DRAG_DIRECTION_NONE;
}

/**
Expand Down
Loading

0 comments on commit 9cb0960

Please sign in to comment.