Skip to content

Commit

Permalink
add support for no Anchor onSwipe (#617)
Browse files Browse the repository at this point in the history
* fix desig time lockout of on click

* fix ConstraintLayout Compose chain

* add support for weights and margins

* resolve comments & clean up spaces

* resolve comments & clean up spaces

* resolve oscar comments

* add support for onSwipe to work with and Anchor
  • Loading branch information
jafu888 authored May 27, 2022
1 parent f68c9aa commit ef01f03
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import androidx.constraintlayout.core.widgets.ConstraintWidgetContainer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

public class Transition implements TypedValues {
Expand Down Expand Up @@ -78,7 +79,7 @@ public boolean hasOnSwipe() {
}

static class OnSwipe {
private String mAnchorId;
String mAnchorId;
private int mAnchorSide;
private StopEngine mEngine;
public static final int ANCHOR_SIDE_TOP = 0;
Expand Down Expand Up @@ -162,6 +163,10 @@ static class OnSwipe {
};
private long mStart;

float getScale() {
return mDragScale;
}

float[] getDirection() {
return TOUCH_DIRECTION[mDragDirection];
}
Expand Down Expand Up @@ -362,9 +367,27 @@ public boolean isNotDone(float progress) {
* @return the change in progress
*/
public float dragToProgress(float currentProgress, int baseW, int baseH, float dx, float dy) {
if (mOnSwipe == null || mOnSwipe.mAnchorId == null) {
WidgetState w = mState.values().stream().findFirst().get();
return -dy / w.mParentHeight;
Collection<WidgetState> widgets = mState.values();
WidgetState childWidget = null;
for (WidgetState widget : widgets) {
childWidget = widget;
break;
}
if (mOnSwipe == null || childWidget == null) {
if (childWidget != null) {
return -dy / childWidget.mParentHeight;
}
return 1.0f;
}
if (mOnSwipe.mAnchorId == null) {

float[] dir = mOnSwipe.getDirection();
float motionDpDtX = childWidget.mParentHeight;
float motionDpDtY = childWidget.mParentHeight;

float drag = (dir[0] != 0) ? dx * Math.abs(dir[0]) / motionDpDtX
: dy * Math.abs(dir[1]) / motionDpDtY;
return drag * mOnSwipe.getScale();
}
WidgetState base = mState.get(mOnSwipe.mAnchorId);
float[] dir = mOnSwipe.getDirection();
Expand All @@ -378,7 +401,7 @@ public float dragToProgress(float currentProgress, int baseW, int baseH, float d
if (DEBUG) {
Utils.log(" drag " + drag);
}
return drag;
return drag * mOnSwipe.getScale();
}

/**
Expand Down Expand Up @@ -412,7 +435,7 @@ public void setTouchUp(float currentProgress,
}

float drag = (dir[0] != 0) ? velocityX / motionDpDt[0] : velocityY / motionDpDt[1];

drag *= mOnSwipe.getScale();
if (DEBUG) {
Utils.log(" >>> velocity " + drag);
Utils.log(" >>> mDuration " + mDuration);
Expand Down Expand Up @@ -825,8 +848,8 @@ static class WidgetState {
MotionWidget mMotionWidgetEnd;
MotionWidget mMotionWidgetInterpolated;
KeyCache mKeyCache = new KeyCache();
private int mParentHeight = -1;
private int mParentWidth = -1;
int mParentHeight = -1;
int mParentWidth = -1;

WidgetState() {
mStart = new WidgetFrame();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import com.google.accompanist.coil.rememberCoilPainter
class MainActivity : AppCompatActivity() {
private var mFrameLayout: FrameLayout? = null
private var composeNum = 0
private val START_NUMBER = 6
private val START_NUMBER = 48
private var demos:ArrayList<CompFunc> = ArrayList()
var map = HashMap<Int, String>();
val linkServer = LinkServer()
Expand Down Expand Up @@ -153,6 +153,8 @@ class MainActivity : AppCompatActivity() {
demos.add(object : CompFunc { @Composable override fun Run() { OnSwipeExperiment() } })
demos.add(object : CompFunc { @Composable override fun Run() { OnSwipeSample1() } })
demos.add(object : CompFunc { @Composable override fun Run() { OnSwipeSample2() } })
demos.add(object : CompFunc { @Composable override fun Run() { OnSwipeSample3() } })

demos.add(object : CompFunc { @Composable override fun Run() { MultiSwipe() } })
demos.add(object : CompFunc { @Composable override fun Run() { MotionArc() } })
demos.add(object : CompFunc { @Composable override fun Run() { MotionEasing() } })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,76 @@ fun OnSwipeSample2() {
.background(Color.White),
motionScene = MotionScene(content = scene),
) {
Text(
text = "on Swipe example \n"+
" onSwipe: {\n" +
" anchor: 'box',\n" +
" direction: 'end',\n" +
" side: 'start',\n" +
" mode: 'spring'\n" +
" }"
)
Box(
modifier = Modifier
.background(Color.Green)
.layoutId("box")
)
}
}
}

@Preview
@Composable
fun OnSwipeSample3() {

var scene =
"""
{
ConstraintSets: {
start: {
box: {
width: 50, height: 50,
bottom: ['parent', 'bottom', 70],
start: ['parent', 'start', 170],
}
},
end: {
box: {
width: 50, height: 50,
top: ['parent', 'top', 170],
end: ['parent', 'end', 170],
}
}
},
Transitions: {
default: {
from: 'start',
to: 'end',
onSwipe: {
direction: 'end',
mode: 'spring',
scale: .3,
}
}
}
}
""".trimIndent()

Column {
MotionLayout(
modifier = Modifier
.fillMaxSize()
.background(Color.White),
motionScene = MotionScene(content = scene),
) {
Text(
text = "on Swipe example \n"+
" onSwipe: {\n" +
" direction: 'end',\n" +
" mode: 'spring'\n" +
" }"
)
Box(
modifier = Modifier
.background(Color.Green)
Expand Down

0 comments on commit ef01f03

Please sign in to comment.