From 8c83c3d13e91f9092586e6d73a5e30d6a94f18c3 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Mon, 18 Nov 2019 10:14:33 +0800 Subject: [PATCH] feat:add nativeChannel to let js call native view's method --- .../java/pub/doric/plugin/ShaderPlugin.java | 36 +++++++++++++++++++ demo/src/ListDemo.ts | 7 ++++ js-framework/src/ui/layout.ts | 4 +-- js-framework/src/ui/view.ts | 18 ++++++++++ 4 files changed, 62 insertions(+), 3 deletions(-) diff --git a/Android/doric/src/main/java/pub/doric/plugin/ShaderPlugin.java b/Android/doric/src/main/java/pub/doric/plugin/ShaderPlugin.java index b237f68c..56d86c07 100644 --- a/Android/doric/src/main/java/pub/doric/plugin/ShaderPlugin.java +++ b/Android/doric/src/main/java/pub/doric/plugin/ShaderPlugin.java @@ -19,12 +19,17 @@ import pub.doric.DoricContext; import pub.doric.async.AsyncResult; import pub.doric.extension.bridge.DoricMethod; import pub.doric.extension.bridge.DoricPlugin; +import pub.doric.extension.bridge.DoricPromise; import pub.doric.utils.DoricLog; import pub.doric.utils.ThreadMode; import pub.doric.shader.RootNode; +import com.github.pengfeizhou.jscore.ArchiveException; +import com.github.pengfeizhou.jscore.JSArray; import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSObject; +import com.github.pengfeizhou.jscore.JSValue; +import com.github.pengfeizhou.jscore.JavaValue; import java.util.concurrent.Callable; @@ -71,6 +76,37 @@ public class ShaderPlugin extends DoricJavaPlugin { e.printStackTrace(); DoricLog.e("Shader.render:error%s", e.getLocalizedMessage()); } + } + @DoricMethod + public void command(JSDecoder jsDecoder, final DoricPromise doricPromise) { + try { + final JSObject jsObject = jsDecoder.decode().asObject(); + final JSValue[] viewIds = jsObject.getProperty("viewIds").asArray().toArray(); + final String name = jsObject.getProperty("name").asString().value(); + getDoricContext().getDriver().asyncCall(new Callable() { + @Override + public JavaValue call() throws Exception { + return new JavaValue("called"); + } + }, ThreadMode.UI).setCallback(new AsyncResult.Callback() { + @Override + public void onResult(JavaValue result) { + doricPromise.resolve(result); + } + + @Override + public void onError(Throwable t) { + doricPromise.reject(new JavaValue(t.getLocalizedMessage())); + } + + @Override + public void onFinish() { + + } + }); + } catch (ArchiveException e) { + e.printStackTrace(); + } } } diff --git a/demo/src/ListDemo.ts b/demo/src/ListDemo.ts index 77a692e6..c8451f99 100644 --- a/demo/src/ListDemo.ts +++ b/demo/src/ListDemo.ts @@ -73,6 +73,13 @@ class ListPanel extends Panel { it.onClick = () => { log(`Click item at ${idx}`) it.height += 10 + it.nativeChannel(context, "getWidth")().then( + resolve => { + log(`resolve,${resolve}`) + }, + reject => { + log(`reject,${reject}`) + }) } }) }, diff --git a/js-framework/src/ui/layout.ts b/js-framework/src/ui/layout.ts index 0dd19eac..7b5dbf99 100644 --- a/js-framework/src/ui/layout.ts +++ b/js-framework/src/ui/layout.ts @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { LayoutConfig, Group, Property, IView } from "./view"; +import { Group, Property, IView } from "./view"; import { Gravity } from "../util/gravity"; export interface IStack extends IView { @@ -22,8 +22,6 @@ export interface IStack extends IView { export class Stack extends Group implements IStack { } - - export class Root extends Stack { } diff --git a/js-framework/src/ui/view.ts b/js-framework/src/ui/view.ts index 5e4be06f..2955fb1f 100644 --- a/js-framework/src/ui/view.ts +++ b/js-framework/src/ui/view.ts @@ -255,6 +255,24 @@ export abstract class View implements Modeling, IView { in(group: Group) { group.addChild(this) } + + nativeChannel(context: any, name: string) { + let thisView: View | undefined = this + return function (...args: any) { + const func = context.shader.command + const viewIds = [] + while (thisView != undefined) { + viewIds.push(thisView.viewId) + thisView = thisView.superview + } + const params = { + viewIds: viewIds.reverse(), + name, + args, + } + return Reflect.apply(func, undefined, [params]) as Promise + } + } } export abstract class Superview extends View {