From 894f23cd5b2f04c2e6a203e5da53c37faca1f871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Fri, 10 Jan 2020 15:56:36 +0800 Subject: [PATCH] implement android navbar custom left & right view --- .../src/main/java/pub/doric/DoricContext.java | 7 ++ .../pub/doric/navbar/BaseDoricNavBar.java | 13 +++ .../java/pub/doric/navbar/IDoricNavBar.java | 6 ++ .../java/pub/doric/plugin/NavBarPlugin.java | 97 ++++++++++++++++++- 4 files changed, 121 insertions(+), 2 deletions(-) diff --git a/doric-android/doric/src/main/java/pub/doric/DoricContext.java b/doric-android/doric/src/main/java/pub/doric/DoricContext.java index c11216c6..abb5236b 100644 --- a/doric-android/doric/src/main/java/pub/doric/DoricContext.java +++ b/doric-android/doric/src/main/java/pub/doric/DoricContext.java @@ -77,6 +77,13 @@ public class DoricContext { } } + public void clearHeadNodes(String type) { + Map map = mHeadNodes.get(type); + if (map != null) { + map.clear(); + } + } + public ViewNode targetViewNode(String id) { if (id.equals(mRootNode.getId())) { return mRootNode; diff --git a/doric-android/doric/src/main/java/pub/doric/navbar/BaseDoricNavBar.java b/doric-android/doric/src/main/java/pub/doric/navbar/BaseDoricNavBar.java index 12ec34f6..ceb03377 100644 --- a/doric-android/doric/src/main/java/pub/doric/navbar/BaseDoricNavBar.java +++ b/doric-android/doric/src/main/java/pub/doric/navbar/BaseDoricNavBar.java @@ -72,6 +72,18 @@ public class BaseDoricNavBar extends FrameLayout implements IDoricNavBar { mTvTitle.setText(title); } + @Override + public void setLeft(View view) { + mLeftContainer.removeAllViews(); + mLeftContainer.addView(view); + } + + @Override + public void setRight(View view) { + mRightContainer.removeAllViews(); + mRightContainer.addView(view); + } + private void updateTitleMargins() { try { int width = mRightContainer.getRight() - mLeftContainer.getLeft(); @@ -102,4 +114,5 @@ public class BaseDoricNavBar extends FrameLayout implements IDoricNavBar { super.onLayout(changed, left, top, right, bottom); updateTitleMargins(); } + } \ No newline at end of file diff --git a/doric-android/doric/src/main/java/pub/doric/navbar/IDoricNavBar.java b/doric-android/doric/src/main/java/pub/doric/navbar/IDoricNavBar.java index ff35769d..240c55f6 100644 --- a/doric-android/doric/src/main/java/pub/doric/navbar/IDoricNavBar.java +++ b/doric-android/doric/src/main/java/pub/doric/navbar/IDoricNavBar.java @@ -15,6 +15,8 @@ */ package pub.doric.navbar; +import android.view.View; + /** * @Description: pub.doric.navbar * @Author: pengfei.zhou @@ -28,4 +30,8 @@ public interface IDoricNavBar { void setTitle(String title); void setBackgroundColor(int color); + + void setLeft(View view); + + void setRight(View view); } diff --git a/doric-android/doric/src/main/java/pub/doric/plugin/NavBarPlugin.java b/doric-android/doric/src/main/java/pub/doric/plugin/NavBarPlugin.java index ff21d8d0..96e0edce 100644 --- a/doric-android/doric/src/main/java/pub/doric/plugin/NavBarPlugin.java +++ b/doric-android/doric/src/main/java/pub/doric/plugin/NavBarPlugin.java @@ -15,19 +15,22 @@ */ package pub.doric.plugin; -import android.view.View; -import android.view.ViewGroup; +import android.widget.FrameLayout; import com.github.pengfeizhou.jscore.ArchiveException; import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JavaValue; +import java.util.concurrent.Callable; + 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.navbar.IDoricNavBar; +import pub.doric.shader.ViewNode; import pub.doric.utils.ThreadMode; /** @@ -37,6 +40,10 @@ import pub.doric.utils.ThreadMode; */ @DoricPlugin(name = "navbar") public class NavBarPlugin extends DoricJavaPlugin { + + private static final String TYPE_LEFT = "navbar_left"; + private static final String TYPE_RIGHT = "navbar_right"; + public NavBarPlugin(DoricContext doricContext) { super(doricContext); } @@ -104,4 +111,90 @@ public class NavBarPlugin extends DoricJavaPlugin { } } } + + @DoricMethod(thread = ThreadMode.UI) + public void setLeft(JSDecoder decoder, final DoricPromise promise) { + try { + final JSObject jsObject = decoder.decode().asObject(); + getDoricContext().getDriver().asyncCall(new Callable() { + @Override + public Object call() throws Exception { + String viewId = jsObject.getProperty("id").asString().value(); + String type = jsObject.getProperty("type").asString().value(); + ViewNode node = ViewNode.create(getDoricContext(), type); + node.setId(viewId); + node.init(new FrameLayout.LayoutParams(0, 0)); + node.blend(jsObject.getProperty("props").asObject()); + + getDoricContext().getDoricNavBar().setLeft(node.getNodeView()); + + getDoricContext().clearHeadNodes(TYPE_LEFT); + getDoricContext().addHeadNode(TYPE_LEFT, node); + return null; + } + }, ThreadMode.UI).setCallback(new AsyncResult.Callback() { + @Override + public void onResult(Object result) { + promise.resolve(); + } + + @Override + public void onError(Throwable t) { + t.printStackTrace(); + promise.reject(new JavaValue(t.getLocalizedMessage())); + } + + @Override + public void onFinish() { + + } + }); + } catch (Exception e) { + e.printStackTrace(); + promise.reject(new JavaValue(e.getLocalizedMessage())); + } + } + + @DoricMethod(thread = ThreadMode.UI) + public void setRight(JSDecoder decoder, final DoricPromise promise) { + try { + final JSObject jsObject = decoder.decode().asObject(); + getDoricContext().getDriver().asyncCall(new Callable() { + @Override + public Object call() throws Exception { + String viewId = jsObject.getProperty("id").asString().value(); + String type = jsObject.getProperty("type").asString().value(); + ViewNode node = ViewNode.create(getDoricContext(), type); + node.setId(viewId); + node.init(new FrameLayout.LayoutParams(0, 0)); + node.blend(jsObject.getProperty("props").asObject()); + + getDoricContext().getDoricNavBar().setRight(node.getNodeView()); + + getDoricContext().clearHeadNodes(TYPE_RIGHT); + getDoricContext().addHeadNode(TYPE_RIGHT, node); + return null; + } + }, ThreadMode.UI).setCallback(new AsyncResult.Callback() { + @Override + public void onResult(Object result) { + promise.resolve(); + } + + @Override + public void onError(Throwable t) { + t.printStackTrace(); + promise.reject(new JavaValue(t.getLocalizedMessage())); + } + + @Override + public void onFinish() { + + } + }); + } catch (Exception e) { + e.printStackTrace(); + promise.reject(new JavaValue(e.getLocalizedMessage())); + } + } }