From a4cd1c3c24dedde05acc13844ba52115213a927e Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Mon, 9 Dec 2019 20:33:36 +0800 Subject: [PATCH] feat:Navigator add extra --- .../main/java/pub/doric/demo/DemoActivity.java | 2 +- .../main/java/pub/doric/demo/MainActivity.java | 17 ++--------------- .../src/main/java/pub/doric/DoricContext.java | 16 ++++++++++------ .../java/pub/doric/DoricContextManager.java | 4 ++-- doric/src/main/java/pub/doric/DoricPanel.java | 4 ++-- .../java/pub/doric/DoricPanelFragment.java | 6 ++++-- .../pub/doric/navigator/IDoricNavigator.java | 2 +- .../java/pub/doric/plugin/NavigatorPlugin.java | 18 +++++++++++++++++- 8 files changed, 39 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/pub/doric/demo/DemoActivity.java b/app/src/main/java/pub/doric/demo/DemoActivity.java index 1ec7b59a..9f54980b 100644 --- a/app/src/main/java/pub/doric/demo/DemoActivity.java +++ b/app/src/main/java/pub/doric/demo/DemoActivity.java @@ -40,7 +40,7 @@ public class DemoActivity extends AppCompatActivity { DoricPanel doricPanel = new DoricPanel(this); addContentView(doricPanel, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); - doricPanel.config(DoricUtils.readAssetFile("demo/" + source), source); + doricPanel.config(DoricUtils.readAssetFile("demo/" + source), source, ""); doricContext = doricPanel.getDoricContext(); } } diff --git a/app/src/main/java/pub/doric/demo/MainActivity.java b/app/src/main/java/pub/doric/demo/MainActivity.java index b9153c44..0ead1f9a 100644 --- a/app/src/main/java/pub/doric/demo/MainActivity.java +++ b/app/src/main/java/pub/doric/demo/MainActivity.java @@ -33,7 +33,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import pub.doric.DoricActivity; import pub.doric.devkit.ui.DemoDebugActivity; import pub.doric.refresh.DoricSwipeLayout; import pub.doric.utils.DoricUtils; @@ -63,7 +62,6 @@ public class MainActivity extends AppCompatActivity { try { String[] demos = getAssets().list("demo"); List ret = new ArrayList<>(); - ret.add("Test"); for (String str : demos) { if (str.endsWith("js")) { ret.add(str); @@ -107,20 +105,9 @@ public class MainActivity extends AppCompatActivity { tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (data[position].contains("Test")) { - Intent intent = new Intent(tv.getContext(), PullableActivity.class); - tv.getContext().startActivity(intent); - return; - } - if (data[position].contains("NavigatorDemo")) { - Intent intent = new Intent(tv.getContext(), DoricActivity.class); - intent.putExtra("scheme", "assets://demo/" + data[position]); - intent.putExtra("alias", data[position]); - tv.getContext().startActivity(intent); - return; - } Intent intent = new Intent(tv.getContext(), DemoDebugActivity.class); - intent.putExtra("source", data[position]); + intent.putExtra("scheme", "assets://demo/" + data[position]); + intent.putExtra("alias", data[position]); tv.getContext().startActivity(intent); } }); diff --git a/doric/src/main/java/pub/doric/DoricContext.java b/doric/src/main/java/pub/doric/DoricContext.java index 90edf189..de7917da 100644 --- a/doric/src/main/java/pub/doric/DoricContext.java +++ b/doric/src/main/java/pub/doric/DoricContext.java @@ -15,7 +15,6 @@ */ package pub.doric; -import android.animation.Animator; import android.animation.AnimatorSet; import android.content.Context; @@ -49,6 +48,7 @@ public class DoricContext { private RootNode mRootNode = new RootNode(this); private final String source; private String script; + private String extra; private JSONObject initParams; private IDoricDriver doricDriver; private final Map mHeadNodes = new HashMap<>(); @@ -72,10 +72,11 @@ public class DoricContext { return mHeadNodes.get(id); } - protected DoricContext(Context context, String contextId, String source) { + protected DoricContext(Context context, String contextId, String source, String extra) { this.mContext = context; this.mContextId = contextId; this.source = source; + this.extra = extra; } public String getSource() { @@ -86,17 +87,20 @@ public class DoricContext { return script; } - public static DoricContext create(Context context, String script, String source) { - DoricContext doricContext = DoricContextManager.getInstance().createContext(context, script, source); + public static DoricContext create(Context context, String script, String source, String extra) { + DoricContext doricContext = DoricContextManager.getInstance().createContext(context, script, source, extra); doricContext.script = script; + doricContext.extra = extra; return doricContext; } public void init(float width, float height) { this.initParams = new JSONBuilder() .put("width", width) - .put("height", height).toJSONObject(); - callEntity(DoricConstant.DORIC_ENTITY_INIT, this.initParams); + .put("height", height) + .toJSONObject() + ; + callEntity(DoricConstant.DORIC_ENTITY_INIT, this.initParams, extra); callEntity(DoricConstant.DORIC_ENTITY_CREATE); } diff --git a/doric/src/main/java/pub/doric/DoricContextManager.java b/doric/src/main/java/pub/doric/DoricContextManager.java index 3176d4b8..a3046720 100644 --- a/doric/src/main/java/pub/doric/DoricContextManager.java +++ b/doric/src/main/java/pub/doric/DoricContextManager.java @@ -48,9 +48,9 @@ public class DoricContextManager { return Inner.sInstance; } - DoricContext createContext(Context context, final String script, final String source) { + DoricContext createContext(Context context, final String script, final String source, String extra) { final String contextId = String.valueOf(counter.incrementAndGet()); - final DoricContext doricContext = new DoricContext(context, contextId, source); + final DoricContext doricContext = new DoricContext(context, contextId, source, extra); doricContextMap.put(contextId, doricContext); doricContext.getDriver().createContext(contextId, script, source); return doricContext; diff --git a/doric/src/main/java/pub/doric/DoricPanel.java b/doric/src/main/java/pub/doric/DoricPanel.java index a9e578c5..f8893534 100644 --- a/doric/src/main/java/pub/doric/DoricPanel.java +++ b/doric/src/main/java/pub/doric/DoricPanel.java @@ -54,8 +54,8 @@ public class DoricPanel extends FrameLayout implements LifecycleObserver { } - public void config(String script, String alias) { - DoricContext doricContext = DoricContext.create(getContext(), script, alias); + public void config(String script, String alias, String extra) { + DoricContext doricContext = DoricContext.create(getContext(), script, alias, extra); config(doricContext); } diff --git a/doric/src/main/java/pub/doric/DoricPanelFragment.java b/doric/src/main/java/pub/doric/DoricPanelFragment.java index e7cfa6e0..473b0e1b 100644 --- a/doric/src/main/java/pub/doric/DoricPanelFragment.java +++ b/doric/src/main/java/pub/doric/DoricPanelFragment.java @@ -62,10 +62,11 @@ public class DoricPanelFragment extends Fragment implements IDoricNavigator { } final String alias = argument.getString("alias"); String scheme = argument.getString("scheme"); + final String extra = argument.getString("extra"); DoricJSLoaderManager.getInstance().loadJSBundle(scheme).setCallback(new AsyncResult.Callback() { @Override public void onResult(String result) { - doricPanel.config(result, alias); + doricPanel.config(result, alias, extra); DoricContext context = doricPanel.getDoricContext(); context.setDoricNavigator(DoricPanelFragment.this); BaseDoricNavBar navBar = requireActivity().getWindow().getDecorView().findViewById(R.id.doric_nav_bar); @@ -85,10 +86,11 @@ public class DoricPanelFragment extends Fragment implements IDoricNavigator { } @Override - public void push(String scheme, String alias) { + public void push(String scheme, String alias, String extra) { Bundle argument = new Bundle(); argument.putString("scheme", scheme); argument.putString("alias", alias); + argument.putString("extra", extra); getNavController() .navigate(R.id.action_doricPanelFragment_to_doricPanelFragment, argument); } diff --git a/doric/src/main/java/pub/doric/navigator/IDoricNavigator.java b/doric/src/main/java/pub/doric/navigator/IDoricNavigator.java index e260cd5f..a89c6e99 100644 --- a/doric/src/main/java/pub/doric/navigator/IDoricNavigator.java +++ b/doric/src/main/java/pub/doric/navigator/IDoricNavigator.java @@ -21,7 +21,7 @@ package pub.doric.navigator; * @CreateDate: 2019-11-23 */ public interface IDoricNavigator { - void push(String scheme, String alias); + void push(String scheme, String alias, String extra); void pop(); } diff --git a/doric/src/main/java/pub/doric/plugin/NavigatorPlugin.java b/doric/src/main/java/pub/doric/plugin/NavigatorPlugin.java index b28f9251..cb28c162 100644 --- a/doric/src/main/java/pub/doric/plugin/NavigatorPlugin.java +++ b/doric/src/main/java/pub/doric/plugin/NavigatorPlugin.java @@ -18,6 +18,7 @@ package pub.doric.plugin; import com.github.pengfeizhou.jscore.ArchiveException; 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 pub.doric.Doric; @@ -45,8 +46,23 @@ public class NavigatorPlugin extends DoricJavaPlugin { if (navigator != null) { try { JSObject jsObject = jsDecoder.decode().asObject(); + String scheme = jsObject.getProperty("scheme").asString().value(); + String alias = scheme; + String extra = ""; + JSValue config = jsObject.getProperty("config"); + if (config.isObject()) { + JSValue aliasJS = config.asObject().getProperty("alias"); + if (aliasJS.isString()) { + alias = aliasJS.asString().value(); + } + JSValue extraJS = config.asObject().getProperty("extra"); + if (extraJS.isString()) { + extra = extraJS.asString().value(); + } + } navigator.push(jsObject.getProperty("scheme").asString().value(), - jsObject.getProperty("alias").asString().value() + alias, + extra ); promise.resolve(); } catch (ArchiveException e) {