Android: update with js dispatcher & remove PI calculation

This commit is contained in:
王劲鹏 2021-09-23 10:48:03 +08:00 committed by osborn
parent 37ad1eec26
commit 73dd78ca05

View File

@ -23,10 +23,15 @@ import android.view.MotionEvent;
import android.view.ScaleGestureDetector; import android.view.ScaleGestureDetector;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import java.util.concurrent.Callable;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.async.AsyncResult;
import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.utils.DoricJSDispatcher;
import pub.doric.utils.DoricUtils; import pub.doric.utils.DoricUtils;
/** /**
@ -38,6 +43,8 @@ import pub.doric.utils.DoricUtils;
@DoricPlugin(name = "GestureContainer") @DoricPlugin(name = "GestureContainer")
public class GestureContainerNode extends StackNode { public class GestureContainerNode extends StackNode {
private DoricJSDispatcher jsDispatcher = new DoricJSDispatcher();
private enum SwipeOrientation { private enum SwipeOrientation {
LEFT(0), RIGHT(1), TOP(2), BOTTOM(3); LEFT(0), RIGHT(1), TOP(2), BOTTOM(3);
@ -182,7 +189,7 @@ public class GestureContainerNode extends StackNode {
} }
@Override @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { public boolean onScroll(MotionEvent e1, MotionEvent e2, final float distanceX, final float distanceY) {
if (scaleGestureDetector.isInProgress()) { if (scaleGestureDetector.isInProgress()) {
// don't allow scrolling while scaling // don't allow scrolling while scaling
return false; return false;
@ -190,7 +197,12 @@ public class GestureContainerNode extends StackNode {
// handle scrolling // handle scrolling
if (onPan != null) if (onPan != null)
callJSResponse(onPan, DoricUtils.px2dp(distanceX), DoricUtils.px2dp(distanceY)); jsDispatcher.dispatch(new Callable<AsyncResult<JSDecoder>>() {
@Override
public AsyncResult<JSDecoder> call() throws Exception {
return callJSResponse(onPan, DoricUtils.px2dp(distanceX), DoricUtils.px2dp(distanceY));
}
});
return true; return true;
} }
@ -227,9 +239,15 @@ public class GestureContainerNode extends StackNode {
scaleGestureDetector = new ScaleGestureDetector(context, new ScaleGestureDetector.OnScaleGestureListener() { scaleGestureDetector = new ScaleGestureDetector(context, new ScaleGestureDetector.OnScaleGestureListener() {
@Override @Override
public boolean onScale(ScaleGestureDetector scaleGestureDetector) { public boolean onScale(final ScaleGestureDetector scaleGestureDetector) {
if (onPinch != null) if (onPinch != null)
callJSResponse(onPinch, scaleGestureDetector.getScaleFactor()); jsDispatcher.dispatch(new Callable<AsyncResult<JSDecoder>>() {
@Override
public AsyncResult<JSDecoder> call() throws Exception {
return callJSResponse(onPinch, scaleGestureDetector.getScaleFactor());
}
});
return false; return false;
} }
@ -247,9 +265,15 @@ public class GestureContainerNode extends StackNode {
rotateGestureDetector = new RotateGestureDetector(context, new RotateGestureDetector.OnRotateGestureListener() { rotateGestureDetector = new RotateGestureDetector(context, new RotateGestureDetector.OnRotateGestureListener() {
@Override @Override
public boolean onRotate(float degrees, float focusX, float focusY) { public boolean onRotate(final float degrees, float focusX, float focusY) {
if (onRotate != null) if (onRotate != null)
callJSResponse(onRotate, degrees * Math.PI / 180f); jsDispatcher.dispatch(new Callable<AsyncResult<JSDecoder>>() {
@Override
public AsyncResult<JSDecoder> call() throws Exception {
return callJSResponse(onRotate, degrees / 180f);
}
});
return false; return false;
} }
}); });
@ -268,8 +292,16 @@ public class GestureContainerNode extends StackNode {
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
getParent().requestDisallowInterceptTouchEvent(true); getParent().requestDisallowInterceptTouchEvent(true);
final float x = DoricUtils.px2dp(event.getX());
final float y = DoricUtils.px2dp(event.getY());
if (onTouchMove != null) if (onTouchMove != null)
callJSResponse(onTouchMove, DoricUtils.px2dp(event.getX()), DoricUtils.px2dp(event.getY())); jsDispatcher.dispatch(new Callable<AsyncResult<JSDecoder>>() {
@Override
public AsyncResult<JSDecoder> call() throws Exception {
return callJSResponse(onTouchMove, x, y);
}
});
break; break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
if (onTouchUp != null) if (onTouchUp != null)