From a3f1e67db3652e3e146729826cba79e6f696406d Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sat, 23 Nov 2019 11:18:54 +0800 Subject: [PATCH] feat:use DoricPanel instead of DoricContext directly --- .../java/pub/doric/demo/DemoActivity.java | 25 +++------------ .../src/main/java/pub/doric/DoricContext.java | 2 +- .../src/main/java/pub/doric/DoricPanel.java | 16 ++++++++++ .../main/java/pub/doric/DoricRegistry.java | 2 ++ .../java/pub/doric/plugin/ModalPlugin.java | 1 - .../pub/doric/plugin/NavigatorPlugin.java | 31 +++++++++++++++++++ js-framework/src/ui/panel.ts | 1 + 7 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 Android/doric/src/main/java/pub/doric/plugin/NavigatorPlugin.java diff --git a/Android/app/src/main/java/pub/doric/demo/DemoActivity.java b/Android/app/src/main/java/pub/doric/demo/DemoActivity.java index 1265d56d..c9da789a 100644 --- a/Android/app/src/main/java/pub/doric/demo/DemoActivity.java +++ b/Android/app/src/main/java/pub/doric/demo/DemoActivity.java @@ -18,7 +18,6 @@ package pub.doric.demo; import android.os.Bundle; import android.view.KeyEvent; import android.view.ViewGroup; -import android.widget.FrameLayout; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; @@ -30,7 +29,7 @@ import org.greenrobot.eventbus.ThreadMode; import pub.doric.DoricContext; import pub.doric.DoricContextManager; -import pub.doric.devkit.DoricContextDebuggable; +import pub.doric.DoricPanel; import pub.doric.devkit.event.EnterDebugEvent; import pub.doric.devkit.event.QuitDebugEvent; import pub.doric.devkit.event.ReloadEvent; @@ -52,14 +51,12 @@ public class DemoActivity extends AppCompatActivity { protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); String source = getIntent().getStringExtra("source"); - FrameLayout frameLayout = new FrameLayout(this); - addContentView(frameLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + DoricPanel doricPanel = new DoricPanel(this); + addContentView(doricPanel, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); - doricContext = DoricContext.create(this, DoricUtils.readAssetFile("demo/" + source), source); + doricPanel.config(DoricUtils.readAssetFile("demo/" + source), source); + doricContext = doricPanel.getDoricContext(); doricContextDebuggable = new DoricContextDebuggable(doricContext); - doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); - doricContext.getRootNode().setRootView(frameLayout); - sensorHelper = new SensorManagerHelper(this); sensorHelper.setOnShakeListener(new SensorManagerHelper.OnShakeListener() { @Override @@ -73,17 +70,6 @@ public class DemoActivity extends AppCompatActivity { }); } - @Override - protected void onResume() { - super.onResume(); - doricContext.onShow(); - } - - @Override - protected void onPause() { - super.onPause(); - doricContext.onHidden(); - } @Override public void onAttachedToWindow() { @@ -95,7 +81,6 @@ public class DemoActivity extends AppCompatActivity { @Override protected void onDestroy() { super.onDestroy(); - doricContext.teardown(); EventBus.getDefault().unregister(this); sensorHelper.stop(); } diff --git a/Android/doric/src/main/java/pub/doric/DoricContext.java b/Android/doric/src/main/java/pub/doric/DoricContext.java index f9152e21..4536de7d 100644 --- a/Android/doric/src/main/java/pub/doric/DoricContext.java +++ b/Android/doric/src/main/java/pub/doric/DoricContext.java @@ -66,7 +66,7 @@ public class DoricContext { return doricContext; } - public void init(int width, int height) { + public void init(float width, float height) { this.initParams = new JSONBuilder() .put("width", width) .put("height", height).toJSONObject(); diff --git a/Android/doric/src/main/java/pub/doric/DoricPanel.java b/Android/doric/src/main/java/pub/doric/DoricPanel.java index 693c43c4..a20505aa 100644 --- a/Android/doric/src/main/java/pub/doric/DoricPanel.java +++ b/Android/doric/src/main/java/pub/doric/DoricPanel.java @@ -25,6 +25,8 @@ import androidx.annotation.RequiresApi; import android.util.AttributeSet; import android.widget.FrameLayout; +import pub.doric.utils.DoricUtils; + /** * @Description: Doric * @Author: pengfei.zhou @@ -58,6 +60,10 @@ public class DoricPanel extends FrameLayout { public void config(DoricContext doricContext) { mDoricContext = doricContext; + mDoricContext.getRootNode().setRootView(this); + if (getMeasuredState() != 0) { + mDoricContext.init(DoricUtils.px2dp(getMeasuredWidth()), DoricUtils.px2dp(getMeasuredHeight())); + } } @Override @@ -68,4 +74,14 @@ public class DoricPanel extends FrameLayout { public DoricContext getDoricContext() { return mDoricContext; } + + @Override + protected void onSizeChanged(int w, int h, int oldw, int oldh) { + super.onSizeChanged(w, h, oldw, oldh); + if (oldw != w || oldh != h) { + if (mDoricContext != null) { + mDoricContext.init(DoricUtils.px2dp(w), DoricUtils.px2dp(h)); + } + } + } } diff --git a/Android/doric/src/main/java/pub/doric/DoricRegistry.java b/Android/doric/src/main/java/pub/doric/DoricRegistry.java index bbcd374a..e39d156c 100644 --- a/Android/doric/src/main/java/pub/doric/DoricRegistry.java +++ b/Android/doric/src/main/java/pub/doric/DoricRegistry.java @@ -17,6 +17,7 @@ package pub.doric; import android.text.TextUtils; +import pub.doric.plugin.NavigatorPlugin; import pub.doric.plugin.NetworkPlugin; import pub.doric.plugin.ShaderPlugin; import pub.doric.plugin.StoragePlugin; @@ -69,6 +70,7 @@ public class DoricRegistry { this.registerNativePlugin(ModalPlugin.class); this.registerNativePlugin(NetworkPlugin.class); this.registerNativePlugin(StoragePlugin.class); + this.registerNativePlugin(NavigatorPlugin.class); this.registerViewNode(RootNode.class); this.registerViewNode(TextNode.class); this.registerViewNode(ImageNode.class); diff --git a/Android/doric/src/main/java/pub/doric/plugin/ModalPlugin.java b/Android/doric/src/main/java/pub/doric/plugin/ModalPlugin.java index 0d8cfd62..80ad0700 100644 --- a/Android/doric/src/main/java/pub/doric/plugin/ModalPlugin.java +++ b/Android/doric/src/main/java/pub/doric/plugin/ModalPlugin.java @@ -32,7 +32,6 @@ import pub.doric.extension.bridge.DoricPromise; import pub.doric.utils.DoricUtils; import pub.doric.utils.ThreadMode; -import com.github.pengfeizhou.jscore.ArchiveException; import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSValue; diff --git a/Android/doric/src/main/java/pub/doric/plugin/NavigatorPlugin.java b/Android/doric/src/main/java/pub/doric/plugin/NavigatorPlugin.java new file mode 100644 index 00000000..48c9d3fc --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/plugin/NavigatorPlugin.java @@ -0,0 +1,31 @@ +/* + * Copyright [2019] [Doric.Pub] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package pub.doric.plugin; + +import pub.doric.DoricContext; +import pub.doric.extension.bridge.DoricPlugin; + +/** + * @Description: pub.doric.plugin + * @Author: pengfei.zhou + * @CreateDate: 2019-11-23 + */ +@DoricPlugin(name = "navigator") +public class NavigatorPlugin extends DoricJavaPlugin { + public NavigatorPlugin(DoricContext doricContext) { + super(doricContext); + } +} diff --git a/js-framework/src/ui/panel.ts b/js-framework/src/ui/panel.ts index 6d708f54..abc84bda 100644 --- a/js-framework/src/ui/panel.ts +++ b/js-framework/src/ui/panel.ts @@ -56,6 +56,7 @@ export abstract class Panel { this.__data__ = data this.__root__.width = frame.width this.__root__.height = frame.height + this.__root__.children.length = 0 this.build(this.__root__) }