From a3f1e67db3652e3e146729826cba79e6f696406d Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sat, 23 Nov 2019 11:18:54 +0800 Subject: [PATCH 01/11] 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__) } From b475097c342964da959dfe2ba80b0bd5e3c4f773 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sat, 23 Nov 2019 11:40:45 +0800 Subject: [PATCH 02/11] feat:DoricPanel observe lifecycle --- .../java/pub/doric/demo/DemoActivity.java | 1 - .../src/main/java/pub/doric/DoricPanel.java | 44 +++++++++++++++---- 2 files changed, 35 insertions(+), 10 deletions(-) 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 c9da789a..0e16a23e 100644 --- a/Android/app/src/main/java/pub/doric/demo/DemoActivity.java +++ b/Android/app/src/main/java/pub/doric/demo/DemoActivity.java @@ -70,7 +70,6 @@ public class DemoActivity extends AppCompatActivity { }); } - @Override public void onAttachedToWindow() { super.onAttachedToWindow(); diff --git a/Android/doric/src/main/java/pub/doric/DoricPanel.java b/Android/doric/src/main/java/pub/doric/DoricPanel.java index a20505aa..a9e578c5 100644 --- a/Android/doric/src/main/java/pub/doric/DoricPanel.java +++ b/Android/doric/src/main/java/pub/doric/DoricPanel.java @@ -16,11 +16,13 @@ package pub.doric; import android.content.Context; -import android.os.Build; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.annotation.RequiresApi; +import androidx.lifecycle.Lifecycle; +import androidx.lifecycle.LifecycleObserver; +import androidx.lifecycle.LifecycleOwner; +import androidx.lifecycle.OnLifecycleEvent; import android.util.AttributeSet; import android.widget.FrameLayout; @@ -32,26 +34,25 @@ import pub.doric.utils.DoricUtils; * @Author: pengfei.zhou * @CreateDate: 2019-07-18 */ -public class DoricPanel extends FrameLayout { +public class DoricPanel extends FrameLayout implements LifecycleObserver { private DoricContext mDoricContext; public DoricPanel(@NonNull Context context) { - super(context); + this(context, null); } public DoricPanel(@NonNull Context context, @Nullable AttributeSet attrs) { - super(context, attrs); + this(context, attrs, 0); } public DoricPanel(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); + if (getContext() instanceof LifecycleOwner) { + ((LifecycleOwner) getContext()).getLifecycle().addObserver(this); + } } - @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) - public DoricPanel(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { - super(context, attrs, defStyleAttr, defStyleRes); - } public void config(String script, String alias) { DoricContext doricContext = DoricContext.create(getContext(), script, alias); @@ -64,6 +65,10 @@ public class DoricPanel extends FrameLayout { if (getMeasuredState() != 0) { mDoricContext.init(DoricUtils.px2dp(getMeasuredWidth()), DoricUtils.px2dp(getMeasuredHeight())); } + if (getContext() instanceof LifecycleOwner + && ((LifecycleOwner) getContext()).getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) { + mDoricContext.onShow(); + } } @Override @@ -84,4 +89,25 @@ public class DoricPanel extends FrameLayout { } } } + + @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) + public void onActivityResume() { + if (mDoricContext != null) { + mDoricContext.onShow(); + } + } + + @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) + public void onActivityPause() { + if (mDoricContext != null) { + mDoricContext.onHidden(); + } + } + + @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) + public void onActivityDestroy() { + if (mDoricContext != null) { + mDoricContext.teardown(); + } + } } From 876fa982544c8e9ff4fd4bddf7ebf74f745d3d08 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sat, 23 Nov 2019 12:07:37 +0800 Subject: [PATCH 03/11] feat:add DoricFragment and doric activity --- .../java/pub/doric/demo/MainActivity.java | 7 +++ Android/doric/src/main/AndroidManifest.xml | 17 ++++++- .../main/java/pub/doric/DoricActivity.java | 45 ++++++++++++++++++ .../main/java/pub/doric/DoricFragment.java | 47 +++++++++++++++++++ .../src/main/res/layout/doric_activity.xml | 5 ++ .../src/main/res/layout/doric_fragment.xml | 6 +++ 6 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 Android/doric/src/main/java/pub/doric/DoricActivity.java create mode 100644 Android/doric/src/main/java/pub/doric/DoricFragment.java create mode 100644 Android/doric/src/main/res/layout/doric_activity.xml create mode 100644 Android/doric/src/main/res/layout/doric_fragment.xml diff --git a/Android/app/src/main/java/pub/doric/demo/MainActivity.java b/Android/app/src/main/java/pub/doric/demo/MainActivity.java index 59a6ce2d..340e94ff 100644 --- a/Android/app/src/main/java/pub/doric/demo/MainActivity.java +++ b/Android/app/src/main/java/pub/doric/demo/MainActivity.java @@ -32,6 +32,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import pub.doric.DoricActivity; import pub.doric.utils.DoricUtils; public class MainActivity extends AppCompatActivity { @@ -46,6 +47,7 @@ public class MainActivity extends AppCompatActivity { try { String[] demos = getAssets().list("demo"); List ret = new ArrayList<>(); + ret.add("Navigator"); for (String str : demos) { if (str.endsWith("js")) { ret.add(str); @@ -89,6 +91,11 @@ public class MainActivity extends AppCompatActivity { tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + if (position == 0) { + Intent intent = new Intent(tv.getContext(), DoricActivity.class); + tv.getContext().startActivity(intent); + return; + } Intent intent = new Intent(tv.getContext(), DemoActivity.class); intent.putExtra("source", data[position]); tv.getContext().startActivity(intent); diff --git a/Android/doric/src/main/AndroidManifest.xml b/Android/doric/src/main/AndroidManifest.xml index cf6aef36..ec029524 100644 --- a/Android/doric/src/main/AndroidManifest.xml +++ b/Android/doric/src/main/AndroidManifest.xml @@ -1 +1,16 @@ - \ No newline at end of file + + + + + + + + + + + + + + + diff --git a/Android/doric/src/main/java/pub/doric/DoricActivity.java b/Android/doric/src/main/java/pub/doric/DoricActivity.java new file mode 100644 index 00000000..ee23f8f3 --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/DoricActivity.java @@ -0,0 +1,45 @@ +/* + * 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; + +import android.os.Bundle; + +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.Fragment; + +/** + * @Description: pub.doric.demo + * @Author: pengfei.zhou + * @CreateDate: 2019-11-19 + */ +public class DoricActivity extends AppCompatActivity { + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.doric_activity); + if (savedInstanceState == null) { + Fragment doricFragment = getSupportFragmentManager().getFragmentFactory().instantiate( + getClassLoader(), + DoricFragment.class.getName() + ); + getSupportFragmentManager().beginTransaction() + .add(R.id.container, doricFragment) + .commit(); + } + } +} diff --git a/Android/doric/src/main/java/pub/doric/DoricFragment.java b/Android/doric/src/main/java/pub/doric/DoricFragment.java new file mode 100644 index 00000000..806c6800 --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/DoricFragment.java @@ -0,0 +1,47 @@ +/* + * 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; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.FrameLayout; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; + +/** + * @Description: pub.doric + * @Author: pengfei.zhou + * @CreateDate: 2019-11-23 + */ +public class DoricFragment extends Fragment { + private FrameLayout root; + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + } + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + root = (FrameLayout) inflater.inflate(R.layout.doric_fragment, container, false); + return root; + } +} diff --git a/Android/doric/src/main/res/layout/doric_activity.xml b/Android/doric/src/main/res/layout/doric_activity.xml new file mode 100644 index 00000000..54b71a1b --- /dev/null +++ b/Android/doric/src/main/res/layout/doric_activity.xml @@ -0,0 +1,5 @@ + + \ No newline at end of file diff --git a/Android/doric/src/main/res/layout/doric_fragment.xml b/Android/doric/src/main/res/layout/doric_fragment.xml new file mode 100644 index 00000000..0f36c22b --- /dev/null +++ b/Android/doric/src/main/res/layout/doric_fragment.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file From e1ac85d18aa2acc6bb1467908604e00605925ded Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sat, 23 Nov 2019 14:54:37 +0800 Subject: [PATCH 04/11] feat:add DoricJSLoader --- .../java/pub/doric/DoricPanelFragment.java | 38 +++++++++++ .../main/java/pub/doric/DoricRegistry.java | 21 +++++- .../java/pub/doric/async/AsyncResult.java | 7 ++ .../pub/doric/loader/DoricAssetJSLoader.java | 36 +++++++++++ .../pub/doric/loader/DoricHttpJSLoader.java | 64 +++++++++++++++++++ .../doric/loader/DoricJSLoaderManager.java | 51 +++++++++++++++ .../java/pub/doric/loader/IDoricJSLoader.java | 29 +++++++++ 7 files changed, 245 insertions(+), 1 deletion(-) create mode 100644 Android/doric/src/main/java/pub/doric/DoricPanelFragment.java create mode 100644 Android/doric/src/main/java/pub/doric/loader/DoricAssetJSLoader.java create mode 100644 Android/doric/src/main/java/pub/doric/loader/DoricHttpJSLoader.java create mode 100644 Android/doric/src/main/java/pub/doric/loader/DoricJSLoaderManager.java create mode 100644 Android/doric/src/main/java/pub/doric/loader/IDoricJSLoader.java diff --git a/Android/doric/src/main/java/pub/doric/DoricPanelFragment.java b/Android/doric/src/main/java/pub/doric/DoricPanelFragment.java new file mode 100644 index 00000000..2acd181e --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/DoricPanelFragment.java @@ -0,0 +1,38 @@ +/* + * 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; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; + +/** + * @Description: pub.doric + * @Author: pengfei.zhou + * @CreateDate: 2019-11-23 + */ +public class DoricPanelFragment extends Fragment { + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + return super.onCreateView(inflater, container, savedInstanceState); + } +} diff --git a/Android/doric/src/main/java/pub/doric/DoricRegistry.java b/Android/doric/src/main/java/pub/doric/DoricRegistry.java index e39d156c..3eb87e6c 100644 --- a/Android/doric/src/main/java/pub/doric/DoricRegistry.java +++ b/Android/doric/src/main/java/pub/doric/DoricRegistry.java @@ -17,6 +17,9 @@ package pub.doric; import android.text.TextUtils; +import pub.doric.loader.DoricAssetJSLoader; +import pub.doric.loader.DoricHttpJSLoader; +import pub.doric.loader.IDoricJSLoader; import pub.doric.plugin.NavigatorPlugin; import pub.doric.plugin.NetworkPlugin; import pub.doric.plugin.ShaderPlugin; @@ -37,6 +40,7 @@ import pub.doric.utils.DoricMetaInfo; import pub.doric.plugin.DoricJavaPlugin; import pub.doric.plugin.ModalPlugin; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -50,9 +54,16 @@ import java.util.concurrent.ConcurrentHashMap; */ public class DoricRegistry { private static Map bundles = new ConcurrentHashMap<>(); + private static Set doricLibraries = new HashSet<>(); + private static Set jsLoaders = new HashSet<>(); + + static { + addJSLoader(new DoricAssetJSLoader()); + addJSLoader(new DoricHttpJSLoader()); + } + private Map> pluginInfoMap = new HashMap<>(); private Map> nodeInfoMap = new HashMap<>(); - private static Set doricLibraries = new HashSet<>(); private static void initRegistry(DoricRegistry doricRegistry) { for (DoricLibrary library : doricLibraries) { @@ -114,4 +125,12 @@ public class DoricRegistry { public String acquireJSBundle(String name) { return bundles.get(name); } + + public static void addJSLoader(IDoricJSLoader jsLoader) { + jsLoaders.add(jsLoader); + } + + public static Collection getJSLoaders() { + return jsLoaders; + } } diff --git a/Android/doric/src/main/java/pub/doric/async/AsyncResult.java b/Android/doric/src/main/java/pub/doric/async/AsyncResult.java index 0b06f9ee..2f6af37a 100644 --- a/Android/doric/src/main/java/pub/doric/async/AsyncResult.java +++ b/Android/doric/src/main/java/pub/doric/async/AsyncResult.java @@ -26,6 +26,13 @@ public class AsyncResult { private Callback callback = null; + public AsyncResult() { + } + + public AsyncResult(R r) { + this.result = r; + } + public void setResult(R result) { this.result = result; if (this.callback != null) { diff --git a/Android/doric/src/main/java/pub/doric/loader/DoricAssetJSLoader.java b/Android/doric/src/main/java/pub/doric/loader/DoricAssetJSLoader.java new file mode 100644 index 00000000..2352d971 --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/loader/DoricAssetJSLoader.java @@ -0,0 +1,36 @@ +/* + * 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.loader; + +import pub.doric.async.AsyncResult; +import pub.doric.utils.DoricUtils; + +/** + * @Description: handle "assets://asset-file-path" + * @Author: pengfei.zhou + * @CreateDate: 2019-11-23 + */ +public class DoricAssetJSLoader implements IDoricJSLoader { + @Override + public boolean filter(String scheme) { + return scheme.startsWith("assets"); + } + + @Override + public AsyncResult request(String scheme) { + return new AsyncResult<>(DoricUtils.readAssetFile(scheme.substring("assets://".length()))); + } +} diff --git a/Android/doric/src/main/java/pub/doric/loader/DoricHttpJSLoader.java b/Android/doric/src/main/java/pub/doric/loader/DoricHttpJSLoader.java new file mode 100644 index 00000000..ca3732a6 --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/loader/DoricHttpJSLoader.java @@ -0,0 +1,64 @@ +/* + * 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.loader; + +import com.bumptech.glide.RequestBuilder; + +import org.jetbrains.annotations.NotNull; + +import java.io.IOException; + +import okhttp3.Call; +import okhttp3.Callback; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import pub.doric.async.AsyncResult; + +/** + * @Description: handle like "https://xxxx.js" + * @Author: pengfei.zhou + * @CreateDate: 2019-11-23 + */ +public class DoricHttpJSLoader implements IDoricJSLoader { + private OkHttpClient okHttpClient = new OkHttpClient(); + + @Override + public boolean filter(String scheme) { + return scheme.startsWith("http"); + } + + @Override + public AsyncResult request(String scheme) { + final AsyncResult ret = new AsyncResult<>(); + okHttpClient.newCall(new Request.Builder().url(scheme).build()).enqueue(new Callback() { + @Override + public void onFailure(@NotNull Call call, @NotNull IOException e) { + ret.setError(e); + } + + @Override + public void onResponse(@NotNull Call call, @NotNull Response response) { + try { + ret.setResult(response.body().string()); + } catch (Exception e) { + ret.setError(e); + } + } + }); + return ret; + } +} diff --git a/Android/doric/src/main/java/pub/doric/loader/DoricJSLoaderManager.java b/Android/doric/src/main/java/pub/doric/loader/DoricJSLoaderManager.java new file mode 100644 index 00000000..f3c01898 --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/loader/DoricJSLoaderManager.java @@ -0,0 +1,51 @@ +/* + * 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.loader; + + +import java.util.Collection; + +import pub.doric.DoricRegistry; +import pub.doric.async.AsyncResult; + +/** + * @Description: pub.doric + * @Author: pengfei.zhou + * @CreateDate: 2019-11-23 + */ +public class DoricJSLoaderManager { + private DoricJSLoaderManager() { + } + + private static class Inner { + private static final DoricJSLoaderManager sInstance = new DoricJSLoaderManager(); + } + + public static DoricJSLoaderManager getInstance() { + return Inner.sInstance; + } + + public AsyncResult loadJSBundle(String scheme) { + Collection jsLoaders = DoricRegistry.getJSLoaders(); + for (IDoricJSLoader jsLoader : jsLoaders) { + if (jsLoader.filter(scheme)) { + return jsLoader.request(scheme); + } + } + return new AsyncResult<>(""); + } + +} diff --git a/Android/doric/src/main/java/pub/doric/loader/IDoricJSLoader.java b/Android/doric/src/main/java/pub/doric/loader/IDoricJSLoader.java new file mode 100644 index 00000000..c26f453f --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/loader/IDoricJSLoader.java @@ -0,0 +1,29 @@ +/* + * 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.loader; + +import pub.doric.async.AsyncResult; + +/** + * @Description: pub.doric + * @Author: pengfei.zhou + * @CreateDate: 2019-11-23 + */ +public interface IDoricJSLoader { + boolean filter(String scheme); + + AsyncResult request(String scheme); +} From 920ca8d41edb98a3959dfcfe6e27bc5eb86f1ebe Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sat, 23 Nov 2019 15:11:11 +0800 Subject: [PATCH 05/11] feat:DoricPanelFragment use DoricJSLoader to load js bundle --- .../main/java/pub/doric/DoricFragment.java | 13 +++-- .../java/pub/doric/DoricPanelFragment.java | 49 ++++++++++++++++++- .../main/java/pub/doric/DoricRegistry.java | 2 +- .../src/main/res/layout/doric_fragment.xml | 1 + .../main/res/layout/doric_framgent_panel.xml | 4 ++ 5 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 Android/doric/src/main/res/layout/doric_framgent_panel.xml diff --git a/Android/doric/src/main/java/pub/doric/DoricFragment.java b/Android/doric/src/main/java/pub/doric/DoricFragment.java index 806c6800..8a7f78ae 100644 --- a/Android/doric/src/main/java/pub/doric/DoricFragment.java +++ b/Android/doric/src/main/java/pub/doric/DoricFragment.java @@ -33,15 +33,18 @@ import androidx.fragment.app.Fragment; public class DoricFragment extends Fragment { private FrameLayout root; - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - } - @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { root = (FrameLayout) inflater.inflate(R.layout.doric_fragment, container, false); return root; } + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + requireFragmentManager().beginTransaction() + .add(R.id.root, DoricPanelFragment.newInstance("assets://demo/Counter.js", "Counter.js")) + .commit(); + } } diff --git a/Android/doric/src/main/java/pub/doric/DoricPanelFragment.java b/Android/doric/src/main/java/pub/doric/DoricPanelFragment.java index 2acd181e..cdc90762 100644 --- a/Android/doric/src/main/java/pub/doric/DoricPanelFragment.java +++ b/Android/doric/src/main/java/pub/doric/DoricPanelFragment.java @@ -16,6 +16,7 @@ package pub.doric; import android.os.Bundle; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -24,15 +25,59 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import pub.doric.async.AsyncResult; +import pub.doric.loader.DoricJSLoaderManager; +import pub.doric.utils.DoricLog; + /** * @Description: pub.doric * @Author: pengfei.zhou * @CreateDate: 2019-11-23 */ public class DoricPanelFragment extends Fragment { - @Nullable + private DoricPanel doricPanel; + + public static DoricPanelFragment newInstance(String scheme, String alias) { + Bundle args = new Bundle(); + args.putString("scheme", scheme); + args.putString("alias", alias); + DoricPanelFragment fragment = new DoricPanelFragment(); + fragment.setArguments(args); + return fragment; + } + @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - return super.onCreateView(inflater, container, savedInstanceState); + doricPanel = (DoricPanel) inflater.inflate(R.layout.doric_framgent_panel, container, false); + return doricPanel; + } + + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + Bundle argument = getArguments(); + if (argument == null) { + DoricLog.e("DoricPanelFragment argument is null"); + return; + } + final String alias = argument.getString("alias"); + String scheme = argument.getString("scheme"); + DoricJSLoaderManager.getInstance().loadJSBundle(scheme).setCallback(new AsyncResult.Callback() { + @Override + public void onResult(String result) { + doricPanel.config(result, alias); + } + + @Override + public void onError(Throwable t) { + DoricLog.e("DoricPanelFragment load JS error:" + t.getLocalizedMessage()); + } + + @Override + public void onFinish() { + + } + }); } } diff --git a/Android/doric/src/main/java/pub/doric/DoricRegistry.java b/Android/doric/src/main/java/pub/doric/DoricRegistry.java index 3eb87e6c..2059d18b 100644 --- a/Android/doric/src/main/java/pub/doric/DoricRegistry.java +++ b/Android/doric/src/main/java/pub/doric/DoricRegistry.java @@ -48,7 +48,7 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; /** - * @Description: com.github.penfeizhou.doric + * @Description: pub.doric * @Author: pengfei.zhou * @CreateDate: 2019-07-20 */ diff --git a/Android/doric/src/main/res/layout/doric_fragment.xml b/Android/doric/src/main/res/layout/doric_fragment.xml index 0f36c22b..7af124c8 100644 --- a/Android/doric/src/main/res/layout/doric_fragment.xml +++ b/Android/doric/src/main/res/layout/doric_fragment.xml @@ -1,5 +1,6 @@ diff --git a/Android/doric/src/main/res/layout/doric_framgent_panel.xml b/Android/doric/src/main/res/layout/doric_framgent_panel.xml new file mode 100644 index 00000000..7e800983 --- /dev/null +++ b/Android/doric/src/main/res/layout/doric_framgent_panel.xml @@ -0,0 +1,4 @@ + + \ No newline at end of file From 885d168d33c4c9f96746e5fcf65b8cdf40ff5558 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sat, 23 Nov 2019 16:23:08 +0800 Subject: [PATCH 06/11] add navigator demo for Android and single activity multi fragment --- .../java/pub/doric/demo/DemoActivity.java | 1 + .../java/pub/doric/demo/MainActivity.java | 5 +- .../main/java/pub/doric/DoricActivity.java | 18 +++++-- .../src/main/java/pub/doric/DoricContext.java | 11 ++++ .../main/java/pub/doric/DoricFragment.java | 48 +++++++++++++++--- .../java/pub/doric/DoricPanelFragment.java | 7 ++- .../pub/doric/navigator/IDoricNavigator.java | 27 ++++++++++ .../java/pub/doric/plugin/ModalPlugin.java | 2 +- .../pub/doric/plugin/NavigatorPlugin.java | 30 +++++++++++ .../main/res/layout/doric_framgent_panel.xml | 3 +- demo/index.ts | 1 + demo/src/NavigatorDemo.ts | 50 +++++++++++++++++++ js-framework/src/util/nativeModules.ts | 13 +++++ 13 files changed, 199 insertions(+), 17 deletions(-) create mode 100644 Android/doric/src/main/java/pub/doric/navigator/IDoricNavigator.java create mode 100644 demo/src/NavigatorDemo.ts 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 0e16a23e..6703c846 100644 --- a/Android/app/src/main/java/pub/doric/demo/DemoActivity.java +++ b/Android/app/src/main/java/pub/doric/demo/DemoActivity.java @@ -30,6 +30,7 @@ import org.greenrobot.eventbus.ThreadMode; import pub.doric.DoricContext; import pub.doric.DoricContextManager; import pub.doric.DoricPanel; +import pub.doric.devkit.DoricContextDebuggable; import pub.doric.devkit.event.EnterDebugEvent; import pub.doric.devkit.event.QuitDebugEvent; import pub.doric.devkit.event.ReloadEvent; diff --git a/Android/app/src/main/java/pub/doric/demo/MainActivity.java b/Android/app/src/main/java/pub/doric/demo/MainActivity.java index 340e94ff..a370254c 100644 --- a/Android/app/src/main/java/pub/doric/demo/MainActivity.java +++ b/Android/app/src/main/java/pub/doric/demo/MainActivity.java @@ -47,7 +47,6 @@ public class MainActivity extends AppCompatActivity { try { String[] demos = getAssets().list("demo"); List ret = new ArrayList<>(); - ret.add("Navigator"); for (String str : demos) { if (str.endsWith("js")) { ret.add(str); @@ -91,8 +90,10 @@ public class MainActivity extends AppCompatActivity { tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (position == 0) { + 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; } diff --git a/Android/doric/src/main/java/pub/doric/DoricActivity.java b/Android/doric/src/main/java/pub/doric/DoricActivity.java index ee23f8f3..ad225b6d 100644 --- a/Android/doric/src/main/java/pub/doric/DoricActivity.java +++ b/Android/doric/src/main/java/pub/doric/DoricActivity.java @@ -19,7 +19,6 @@ import android.os.Bundle; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; -import androidx.fragment.app.Fragment; /** * @Description: pub.doric.demo @@ -27,19 +26,28 @@ import androidx.fragment.app.Fragment; * @CreateDate: 2019-11-19 */ public class DoricActivity extends AppCompatActivity { + private DoricFragment doricFragment; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.doric_activity); if (savedInstanceState == null) { - Fragment doricFragment = getSupportFragmentManager().getFragmentFactory().instantiate( - getClassLoader(), - DoricFragment.class.getName() - ); + String scheme = getIntent().getStringExtra("scheme"); + String alias = getIntent().getStringExtra("alias"); + doricFragment = DoricFragment.newInstance(scheme, alias); getSupportFragmentManager().beginTransaction() .add(R.id.container, doricFragment) .commit(); } } + + @Override + public void onBackPressed() { + if (doricFragment.canPop()) { + doricFragment.pop(); + } else { + super.onBackPressed(); + } + } } diff --git a/Android/doric/src/main/java/pub/doric/DoricContext.java b/Android/doric/src/main/java/pub/doric/DoricContext.java index 4536de7d..63fd5856 100644 --- a/Android/doric/src/main/java/pub/doric/DoricContext.java +++ b/Android/doric/src/main/java/pub/doric/DoricContext.java @@ -26,6 +26,7 @@ import java.util.HashMap; import java.util.Map; import pub.doric.async.AsyncResult; +import pub.doric.navigator.IDoricNavigator; import pub.doric.plugin.DoricJavaPlugin; import pub.doric.shader.RootNode; import pub.doric.utils.DoricConstant; @@ -148,4 +149,14 @@ public class DoricContext { public void onHidden() { callEntity(DoricConstant.DORIC_ENTITY_HIDDEN); } + + private IDoricNavigator doricNavigator; + + public void setDoricNavigator(IDoricNavigator doricNavigator) { + this.doricNavigator = doricNavigator; + } + + public IDoricNavigator getDoricNavigator() { + return this.doricNavigator; + } } diff --git a/Android/doric/src/main/java/pub/doric/DoricFragment.java b/Android/doric/src/main/java/pub/doric/DoricFragment.java index 8a7f78ae..e64117b3 100644 --- a/Android/doric/src/main/java/pub/doric/DoricFragment.java +++ b/Android/doric/src/main/java/pub/doric/DoricFragment.java @@ -19,32 +19,66 @@ import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.FrameLayout; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import pub.doric.navigator.IDoricNavigator; + /** * @Description: pub.doric * @Author: pengfei.zhou * @CreateDate: 2019-11-23 */ -public class DoricFragment extends Fragment { - private FrameLayout root; +public class DoricFragment extends Fragment implements IDoricNavigator { + + public static DoricFragment newInstance(String scheme, String alias) { + Bundle args = new Bundle(); + args.putString("scheme", scheme); + args.putString("alias", alias); + DoricFragment fragment = new DoricFragment(); + fragment.setArguments(args); + return fragment; + } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - root = (FrameLayout) inflater.inflate(R.layout.doric_fragment, container, false); - return root; + return inflater.inflate(R.layout.doric_fragment, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - requireFragmentManager().beginTransaction() - .add(R.id.root, DoricPanelFragment.newInstance("assets://demo/Counter.js", "Counter.js")) + Bundle argument = getArguments(); + if (argument != null) { + String alias = argument.getString("alias"); + String scheme = argument.getString("scheme"); + push(scheme, alias); + } + } + + @Override + public void push(String scheme, String alias) { + getChildFragmentManager().beginTransaction() + .add(R.id.root, DoricPanelFragment.newInstance(scheme, alias)) + .addToBackStack(scheme) .commit(); } + + @Override + public void pop() { + if (canPop()) { + getChildFragmentManager().popBackStack(); + } else { + if (getActivity() != null) { + getActivity().finish(); + } + } + } + + public boolean canPop() { + return getChildFragmentManager().getBackStackEntryCount() > 1; + } } diff --git a/Android/doric/src/main/java/pub/doric/DoricPanelFragment.java b/Android/doric/src/main/java/pub/doric/DoricPanelFragment.java index cdc90762..c25b033f 100644 --- a/Android/doric/src/main/java/pub/doric/DoricPanelFragment.java +++ b/Android/doric/src/main/java/pub/doric/DoricPanelFragment.java @@ -16,7 +16,6 @@ package pub.doric; import android.os.Bundle; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -27,6 +26,7 @@ import androidx.fragment.app.Fragment; import pub.doric.async.AsyncResult; import pub.doric.loader.DoricJSLoaderManager; +import pub.doric.navigator.IDoricNavigator; import pub.doric.utils.DoricLog; /** @@ -67,6 +67,11 @@ public class DoricPanelFragment extends Fragment { @Override public void onResult(String result) { doricPanel.config(result, alias); + DoricContext context = doricPanel.getDoricContext(); + Fragment fragment = getParentFragment(); + if (fragment instanceof IDoricNavigator) { + context.setDoricNavigator((IDoricNavigator) fragment); + } } @Override diff --git a/Android/doric/src/main/java/pub/doric/navigator/IDoricNavigator.java b/Android/doric/src/main/java/pub/doric/navigator/IDoricNavigator.java new file mode 100644 index 00000000..e260cd5f --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/navigator/IDoricNavigator.java @@ -0,0 +1,27 @@ +/* + * 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.navigator; + +/** + * @Description: pub.doric.navigator + * @Author: pengfei.zhou + * @CreateDate: 2019-11-23 + */ +public interface IDoricNavigator { + void push(String scheme, String alias); + + void pop(); +} 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 80ad0700..18dcd2f9 100644 --- a/Android/doric/src/main/java/pub/doric/plugin/ModalPlugin.java +++ b/Android/doric/src/main/java/pub/doric/plugin/ModalPlugin.java @@ -50,7 +50,7 @@ public class ModalPlugin extends DoricJavaPlugin { } @DoricMethod(thread = ThreadMode.UI) - public void toast(JSDecoder decoder, DoricPromise promise) { + public void toast(JSDecoder decoder) { try { JSObject jsObject = decoder.decode().asObject(); String msg = jsObject.getProperty("msg").asString().value(); diff --git a/Android/doric/src/main/java/pub/doric/plugin/NavigatorPlugin.java b/Android/doric/src/main/java/pub/doric/plugin/NavigatorPlugin.java index 48c9d3fc..372529ec 100644 --- a/Android/doric/src/main/java/pub/doric/plugin/NavigatorPlugin.java +++ b/Android/doric/src/main/java/pub/doric/plugin/NavigatorPlugin.java @@ -15,8 +15,15 @@ */ package pub.doric.plugin; +import com.github.pengfeizhou.jscore.ArchiveException; +import com.github.pengfeizhou.jscore.JSDecoder; +import com.github.pengfeizhou.jscore.JSObject; + import pub.doric.DoricContext; +import pub.doric.extension.bridge.DoricMethod; import pub.doric.extension.bridge.DoricPlugin; +import pub.doric.navigator.IDoricNavigator; +import pub.doric.utils.ThreadMode; /** * @Description: pub.doric.plugin @@ -28,4 +35,27 @@ public class NavigatorPlugin extends DoricJavaPlugin { public NavigatorPlugin(DoricContext doricContext) { super(doricContext); } + + @DoricMethod(thread = ThreadMode.UI) + public void push(JSDecoder jsDecoder) { + IDoricNavigator navigator = getDoricContext().getDoricNavigator(); + if (navigator != null) { + try { + JSObject jsObject = jsDecoder.decode().asObject(); + navigator.push(jsObject.getProperty("scheme").asString().value(), + jsObject.getProperty("alias").asString().value() + ); + } catch (ArchiveException e) { + e.printStackTrace(); + } + } + } + + @DoricMethod(thread = ThreadMode.UI) + public void pop() { + IDoricNavigator navigator = getDoricContext().getDoricNavigator(); + if (navigator != null) { + navigator.pop(); + } + } } diff --git a/Android/doric/src/main/res/layout/doric_framgent_panel.xml b/Android/doric/src/main/res/layout/doric_framgent_panel.xml index 7e800983..eab7ed13 100644 --- a/Android/doric/src/main/res/layout/doric_framgent_panel.xml +++ b/Android/doric/src/main/res/layout/doric_framgent_panel.xml @@ -1,4 +1,5 @@ \ No newline at end of file + android:layout_height="match_parent" + android:background="#ffffff" /> \ No newline at end of file diff --git a/demo/index.ts b/demo/index.ts index 36729e73..3b1dace6 100644 --- a/demo/index.ts +++ b/demo/index.ts @@ -10,4 +10,5 @@ export default [ 'src/ModalDemo', 'src/NetworkDemo', 'src/StorageDemo', + 'src/NavigatorDemo', ] \ No newline at end of file diff --git a/demo/src/NavigatorDemo.ts b/demo/src/NavigatorDemo.ts new file mode 100644 index 00000000..62d09ccb --- /dev/null +++ b/demo/src/NavigatorDemo.ts @@ -0,0 +1,50 @@ +import { Panel, scroller, vlayout, text, layoutConfig, LayoutSpec, Color, gravity, IVLayout, Group, IText, navigator } from "doric"; +import { colors, label } from "./utils"; +@Entry +class NaivgatorDemo extends Panel { + build(root: Group) { + scroller(vlayout([ + text({ + text: "Navigator Demo", + layoutConfig: layoutConfig().w(LayoutSpec.AT_MOST), + textSize: 30, + textColor: Color.WHITE, + bgColor: colors[1], + textAlignment: gravity().center(), + height: 50, + }), + ...['Counter', 'EffectsDemo', 'ImageDemo', 'LayoutDemo', + 'ListDemo', 'ModalDemo', 'NavigatorDemo', + 'NetworkDemo', 'ScrollerDemo', 'SliderDemo', 'Snake', 'StorageDemo'].map(e => + label(e).apply({ + height: 50, + bgColor: colors[0], + textSize: 30, + textColor: Color.WHITE, + layoutConfig: layoutConfig().exactly().w(LayoutSpec.AT_MOST), + onClick: () => { + navigator(context).push(`assets://demo/${e}.js`, `${e}.js`) + }, + } as IText) + ), + label('POP').apply({ + width: 200, + height: 50, + bgColor: colors[0], + textSize: 30, + textColor: Color.WHITE, + layoutConfig: layoutConfig().exactly(), + onClick: () => { + navigator(context).pop() + }, + } as IText), + ]).apply({ + layoutConfig: layoutConfig().atmost().h(LayoutSpec.WRAP_CONTENT), + gravity: gravity().center(), + space: 10, + } as IVLayout)).apply({ + layoutConfig: layoutConfig().atmost(), + }).in(root) + } + +} \ No newline at end of file diff --git a/js-framework/src/util/nativeModules.ts b/js-framework/src/util/nativeModules.ts index 9673b4e7..d46ced99 100644 --- a/js-framework/src/util/nativeModules.ts +++ b/js-framework/src/util/nativeModules.ts @@ -166,4 +166,17 @@ export function storage(context: BridgeContext) { return context.storage.clear({ zone }) }, } +} + +export function navigator(context: BridgeContext) { + return { + push: (scheme: string, alias: string) => { + return context.navigator.push({ + scheme, alias + }) + }, + pop: () => { + return context.navigator.pop() + }, + } } \ No newline at end of file From 51042eeba47f84cf8d77427ad13095130c868e40 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sat, 23 Nov 2019 17:01:35 +0800 Subject: [PATCH 07/11] feat:add DoricPanel for iOS --- .DS_Store | Bin 6148 -> 0 bytes iOS/Example/Example/DemoVC.m | 28 ++++++--------------- iOS/Pod/Classes/Doric.h | 3 ++- iOS/Pod/Classes/DoricPanel.h | 28 +++++++++++++++++++++ iOS/Pod/Classes/DoricPanel.m | 46 +++++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 21 deletions(-) delete mode 100644 .DS_Store create mode 100644 iOS/Pod/Classes/DoricPanel.h create mode 100644 iOS/Pod/Classes/DoricPanel.m diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 4dc9640661f590604381a4854336463beb13ecca..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%We}f6g_SNnFJ77AhpZcAh8H2fkK54Qko`HN!@+Dib zEh@@h}stJ(69W|#+up+VssE; zh<*M#=$Y^RnE&-}{Akoaj8e`279FNZe^9M{7c05^nX?7YD|w|0rI*2h90h}LFzh$N z-gEWzC>$I_w*ExQ^tc{IPm|7QyL@R&W`i)vIz647#2r#zJWH}zjv8{9#eLmRU;>_3 z^orZ%wefhpcC+GNzg?YF{PD&{wc@YexHXv+z17RLyX~Fs+jryllMkQu)FHUdX}f6g z2)?kgB+Q$jm!&e>XH4bgwQya?dBMagW3vJm$&*}+1iQFOPmwHSd|b%Lk^2zA0_(>D z>pWhe&mQ;KcZe>RZluR~J)fr+{L{>W7jO@4Y@vk*2+&$!tL2_$7x9?PO*CK3O4O*(|Vd + +#import "DoricContext.h" + +@interface DoricPanel : UIViewController +@property(nonatomic, strong) DoricContext *doricContext; + +- (void)config:(NSString *)script alias:(NSString *)alias; +@end \ No newline at end of file diff --git a/iOS/Pod/Classes/DoricPanel.m b/iOS/Pod/Classes/DoricPanel.m new file mode 100644 index 00000000..d647f1bb --- /dev/null +++ b/iOS/Pod/Classes/DoricPanel.m @@ -0,0 +1,46 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2019/11/23. +// + +#import "DoricPanel.h" +#import "Doric.h" + + +@implementation DoricPanel + +- (void)config:(NSString *)script alias:(NSString *)alias { + self.doricContext = [[[DoricContext alloc] initWithScript:script source:alias] also:^(DoricContext *it) { + [it.rootNode setupRootView:[[DoricStackView new] also:^(DoricStackView *it) { + it.width = self.view.width; + it.height = self.view.height; + [self.view addSubview:it]; + }]]; + [it initContextWithWidth:self.view.width height:self.view.height]; + }]; +} + +- (void)viewDidAppear:(BOOL)animated { + [super viewDidAppear:animated]; + [self.doricContext onShow]; +} + +- (void)viewDidDisappear:(BOOL)animated { + [super viewDidDisappear:animated]; + [self.doricContext onHidden]; +} +@end \ No newline at end of file From 27d601c326c60bf22944486087e82bb2d73d4034 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sat, 23 Nov 2019 17:14:50 +0800 Subject: [PATCH 08/11] feat:add DoricNavigatorProtocol --- iOS/Navigator/DoricNavigatorProtocol.h | 11 +++++++++++ iOS/Pod/Classes/Engine/DoricJSCoreExecutor.h | 4 ++-- iOS/Pod/Classes/Engine/DoricJSEngine.m | 4 ++-- ...JSExecutorProtocal.h => DoricJSExecutorProtocol.h} | 2 +- iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.h | 4 ++-- 5 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 iOS/Navigator/DoricNavigatorProtocol.h rename iOS/Pod/Classes/Engine/{DoricJSExecutorProtocal.h => DoricJSExecutorProtocol.h} (95%) diff --git a/iOS/Navigator/DoricNavigatorProtocol.h b/iOS/Navigator/DoricNavigatorProtocol.h new file mode 100644 index 00000000..dd250290 --- /dev/null +++ b/iOS/Navigator/DoricNavigatorProtocol.h @@ -0,0 +1,11 @@ +// +// Created by pengfei.zhou on 2019/11/23. +// + +#import + +@protocol DoricNavigatorProtocol +- (void)push:(NSString *)scheme alias:(NSString *)alias; + +- (void)pop; +@end \ No newline at end of file diff --git a/iOS/Pod/Classes/Engine/DoricJSCoreExecutor.h b/iOS/Pod/Classes/Engine/DoricJSCoreExecutor.h index 448f8b9e..0718eb3e 100644 --- a/iOS/Pod/Classes/Engine/DoricJSCoreExecutor.h +++ b/iOS/Pod/Classes/Engine/DoricJSCoreExecutor.h @@ -21,11 +21,11 @@ // #import -#import "DoricJSExecutorProtocal.h" +#import "DoricJSExecutorProtocol.h" NS_ASSUME_NONNULL_BEGIN -@interface DoricJSCoreExecutor : NSObject +@interface DoricJSCoreExecutor : NSObject @end diff --git a/iOS/Pod/Classes/Engine/DoricJSEngine.m b/iOS/Pod/Classes/Engine/DoricJSEngine.m index d5a9a25e..3559c0cb 100644 --- a/iOS/Pod/Classes/Engine/DoricJSEngine.m +++ b/iOS/Pod/Classes/Engine/DoricJSEngine.m @@ -21,7 +21,7 @@ // #import "DoricJSEngine.h" -#import "DoricJSExecutorProtocal.h" +#import "DoricJSExecutorProtocol.h" #import "DoricJSCoreExecutor.h" #import "DoricJSRemoteExecutor.h" #import "DoricConstant.h" @@ -29,7 +29,7 @@ #import "DoricBridgeExtension.h" @interface DoricJSEngine () -@property(nonatomic, strong) id jsExecutor; +@property(nonatomic, strong) id jsExecutor; @property(nonatomic, strong) NSMutableDictionary *timers; @property(nonatomic, strong) DoricBridgeExtension *bridgeExtension; @end diff --git a/iOS/Pod/Classes/Engine/DoricJSExecutorProtocal.h b/iOS/Pod/Classes/Engine/DoricJSExecutorProtocol.h similarity index 95% rename from iOS/Pod/Classes/Engine/DoricJSExecutorProtocal.h rename to iOS/Pod/Classes/Engine/DoricJSExecutorProtocol.h index 514a3a88..41f4bd47 100644 --- a/iOS/Pod/Classes/Engine/DoricJSExecutorProtocal.h +++ b/iOS/Pod/Classes/Engine/DoricJSExecutorProtocol.h @@ -25,7 +25,7 @@ NS_ASSUME_NONNULL_BEGIN -@protocol DoricJSExecutorProtocal +@protocol DoricJSExecutorProtocol - (NSString *)loadJSScript:(NSString *)script source:(NSString *)source; diff --git a/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.h b/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.h index b9145867..65a34727 100644 --- a/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.h +++ b/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.h @@ -21,11 +21,11 @@ // #import -#import "DoricJSExecutorProtocal.h" +#import "DoricJSExecutorProtocol.h" NS_ASSUME_NONNULL_BEGIN -@interface DoricJSRemoteExecutor : NSObject +@interface DoricJSRemoteExecutor : NSObject @property(nonatomic, strong) dispatch_semaphore_t semaphore; From 656e72c174042a87c652a15301d1068b3922ce90 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sat, 23 Nov 2019 17:22:27 +0800 Subject: [PATCH 09/11] feat:add NavigatorPlugin for iOS ,only files and protocol --- iOS/Pod/Classes/DoricContext.h | 3 +- iOS/Pod/Classes/DoricRegistry.m | 2 ++ iOS/Pod/Classes/Plugin/DoricNavigatorPlugin.h | 24 ++++++++++++++ iOS/Pod/Classes/Plugin/DoricNavigatorPlugin.m | 31 +++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 iOS/Pod/Classes/Plugin/DoricNavigatorPlugin.h create mode 100644 iOS/Pod/Classes/Plugin/DoricNavigatorPlugin.m diff --git a/iOS/Pod/Classes/DoricContext.h b/iOS/Pod/Classes/DoricContext.h index 6607ba4b..7c1bcefd 100644 --- a/iOS/Pod/Classes/DoricContext.h +++ b/iOS/Pod/Classes/DoricContext.h @@ -22,13 +22,14 @@ #import #import "DoricDriver.h" +#import "DoricNavigatorProtocol.h" NS_ASSUME_NONNULL_BEGIN @class DoricRootNode; @interface DoricContext : NSObject - +@property(nonatomic, weak) id navigator; @property(nonatomic, strong) NSString *contextId; @property(nonatomic, strong) DoricDriver *driver; @property(nonatomic, strong) NSMutableDictionary *pluginInstanceMap; diff --git a/iOS/Pod/Classes/DoricRegistry.m b/iOS/Pod/Classes/DoricRegistry.m index e538d993..99a0ca3b 100644 --- a/iOS/Pod/Classes/DoricRegistry.m +++ b/iOS/Pod/Classes/DoricRegistry.m @@ -35,6 +35,7 @@ #import "DoricSliderNode.h" #import "DoricSlideItemNode.h" #import "DoricStoragePlugin.h" +#import "DoricNavigatorPlugin.h" @interface DoricRegistry () @@ -61,6 +62,7 @@ - (void)innerRegister { [self registerNativePlugin:DoricModalPlugin.class withName:@"modal"]; [self registerNativePlugin:DoricNetworkPlugin.class withName:@"network"]; [self registerNativePlugin:DoricStoragePlugin.class withName:@"storage"]; + [self registerNativePlugin:DoricNavigatorPlugin.class withName:@"navigator"]; [self registerViewNode:DoricStackNode.class withName:@"Stack"]; [self registerViewNode:DoricVLayoutNode.class withName:@"VLayout"]; diff --git a/iOS/Pod/Classes/Plugin/DoricNavigatorPlugin.h b/iOS/Pod/Classes/Plugin/DoricNavigatorPlugin.h new file mode 100644 index 00000000..d5ea4947 --- /dev/null +++ b/iOS/Pod/Classes/Plugin/DoricNavigatorPlugin.h @@ -0,0 +1,24 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2019/11/23. +// + +#import +#import "DoricNativePlugin.h" + +@interface DoricNavigatorPlugin : DoricNativePlugin +@end \ No newline at end of file diff --git a/iOS/Pod/Classes/Plugin/DoricNavigatorPlugin.m b/iOS/Pod/Classes/Plugin/DoricNavigatorPlugin.m new file mode 100644 index 00000000..d711b304 --- /dev/null +++ b/iOS/Pod/Classes/Plugin/DoricNavigatorPlugin.m @@ -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. + */ +// +// Created by pengfei.zhou on 2019/11/23. +// + +#import "DoricNavigatorPlugin.h" + + +@implementation DoricNavigatorPlugin +- (void)push:(NSDictionary *)params { + [self.doricContext.navigator push:params[@"scheme"] alias:params[@"alias"]]; +} + +- (void)pop { + [self.doricContext.navigator pop]; +} +@end \ No newline at end of file From 76bceb084e62715d2ae29af7e6545259724edc64 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sat, 23 Nov 2019 18:01:26 +0800 Subject: [PATCH 10/11] feat:add JSLoader for iOS --- iOS/Loader/DoricHttpJSLoader.h | 25 ++++++++++ iOS/Loader/DoricHttpJSLoader.m | 45 ++++++++++++++++++ iOS/Loader/DoricJSLoaderManager.h | 33 ++++++++++++++ iOS/Loader/DoricJSLoaderManager.m | 68 ++++++++++++++++++++++++++++ iOS/Loader/DoricLoaderProtocol.h | 27 +++++++++++ iOS/Loader/DoricMainBundleJSLoader.h | 25 ++++++++++ iOS/Loader/DoricMainBundleJSLoader.m | 42 +++++++++++++++++ 7 files changed, 265 insertions(+) create mode 100644 iOS/Loader/DoricHttpJSLoader.h create mode 100644 iOS/Loader/DoricHttpJSLoader.m create mode 100644 iOS/Loader/DoricJSLoaderManager.h create mode 100644 iOS/Loader/DoricJSLoaderManager.m create mode 100644 iOS/Loader/DoricLoaderProtocol.h create mode 100644 iOS/Loader/DoricMainBundleJSLoader.h create mode 100644 iOS/Loader/DoricMainBundleJSLoader.m diff --git a/iOS/Loader/DoricHttpJSLoader.h b/iOS/Loader/DoricHttpJSLoader.h new file mode 100644 index 00000000..ce146f58 --- /dev/null +++ b/iOS/Loader/DoricHttpJSLoader.h @@ -0,0 +1,25 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2019/11/23. +// + +#import + +#import "DoricLoaderProtocol.h" + +@interface DoricHttpJSLoader : NSObject +@end \ No newline at end of file diff --git a/iOS/Loader/DoricHttpJSLoader.m b/iOS/Loader/DoricHttpJSLoader.m new file mode 100644 index 00000000..d9bd6a2c --- /dev/null +++ b/iOS/Loader/DoricHttpJSLoader.m @@ -0,0 +1,45 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2019/11/23. +// + +#import "DoricHttpJSLoader.h" + + +@implementation DoricHttpJSLoader + +- (BOOL)filter:(NSString *)scheme { + return [scheme hasPrefix:@"http"]; +} + +- (DoricAsyncResult *)request:(NSString *)scheme { + DoricAsyncResult *ret = [DoricAsyncResult new]; + NSURL *URL = [NSURL URLWithString:scheme]; + NSURLRequest *request = [NSURLRequest requestWithURL:URL]; + [[[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]] + dataTaskWithRequest:request + completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { + if (!error) { + NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + [ret setupResult:dataStr]; + } else { + [ret setupError:[[NSException alloc] initWithName:@"DoricJSLoaderManager Exception" reason:error.description userInfo:nil]]; + } + }] resume]; + return ret; +} +@end \ No newline at end of file diff --git a/iOS/Loader/DoricJSLoaderManager.h b/iOS/Loader/DoricJSLoaderManager.h new file mode 100644 index 00000000..efbf2846 --- /dev/null +++ b/iOS/Loader/DoricJSLoaderManager.h @@ -0,0 +1,33 @@ +/* + * 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. + */ +// +// DoricJSLoaderManager.h +// Doric +// +// Created by pengfei.zhou on 2019/11/23. +// + +#import +#import "DoricLoaderProtocol.h" +#import "DoricAsyncResult.h" + +@interface DoricJSLoaderManager : NSObject ++ (instancetype)instance; + +- (void)addJSLoader:(id )loader; + +- (DoricAsyncResult *)request:(NSString *)scheme; +@end diff --git a/iOS/Loader/DoricJSLoaderManager.m b/iOS/Loader/DoricJSLoaderManager.m new file mode 100644 index 00000000..41cf0fc5 --- /dev/null +++ b/iOS/Loader/DoricJSLoaderManager.m @@ -0,0 +1,68 @@ +/* + * 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. + */ +// +// DoricJSLoaderManager.m +// Doric +// +// Created by pengfei.zhou on 2019/11/23. +// + +#import "DoricJSLoaderManager.h" +#import "DoricMainBundleJSLoader.h" +#import "DoricHttpJSLoader.h" +#import "Doric.h" + +@interface DoricJSLoaderManager () +@property(nonatomic, copy) NSSet > *loaders; +@end + +@implementation DoricJSLoaderManager ++ (instancetype)instance { + static DoricJSLoaderManager *_instance; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + _instance = [DoricJSLoaderManager new]; + }); + return _instance; +} + +- (instancetype)init { + if (self = [super init]) { + _loaders = [[NSSet alloc] initWithArray:@[ + [DoricMainBundleJSLoader new], + [DoricHttpJSLoader new], + ]]; + } + return self; +} + +- (void)addJSLoader:(id )loader { + self.loaders = [[self.loaders mutableCopy] also:^(NSMutableSet *it) { + [it addObject:loader]; + }]; +} + +- (DoricAsyncResult *)request:(NSString *)scheme { + __block DoricAsyncResult *ret; + [self.loaders enumerateObjectsUsingBlock:^(id obj, BOOL *stop) { + if ([obj filter:scheme]) { + ret = [obj request:scheme]; + *stop = YES; + } + }]; + return ret; +} +@end diff --git a/iOS/Loader/DoricLoaderProtocol.h b/iOS/Loader/DoricLoaderProtocol.h new file mode 100644 index 00000000..b3b509f0 --- /dev/null +++ b/iOS/Loader/DoricLoaderProtocol.h @@ -0,0 +1,27 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2019/11/23. +// + +#import +#import "DoricAsyncResult.h" + +@protocol DoricLoaderProtocol +- (BOOL)filter:(NSString *)scheme; + +- (DoricAsyncResult *)request:(NSString *)scheme; +@end \ No newline at end of file diff --git a/iOS/Loader/DoricMainBundleJSLoader.h b/iOS/Loader/DoricMainBundleJSLoader.h new file mode 100644 index 00000000..ff5d79ae --- /dev/null +++ b/iOS/Loader/DoricMainBundleJSLoader.h @@ -0,0 +1,25 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2019/11/23. +// + +#import + +#import "DoricLoaderProtocol.h" + +@interface DoricMainBundleJSLoader : NSObject +@end \ No newline at end of file diff --git a/iOS/Loader/DoricMainBundleJSLoader.m b/iOS/Loader/DoricMainBundleJSLoader.m new file mode 100644 index 00000000..63663098 --- /dev/null +++ b/iOS/Loader/DoricMainBundleJSLoader.m @@ -0,0 +1,42 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2019/11/23. +// + +#import "DoricMainBundleJSLoader.h" + + +@implementation DoricMainBundleJSLoader +- (BOOL)filter:(NSString *)scheme { + return [scheme hasPrefix:@"assets"]; +} + +- (DoricAsyncResult *)request:(NSString *)scheme { + DoricAsyncResult *ret = [DoricAsyncResult new]; + NSString *path = [[NSBundle mainBundle] bundlePath]; + NSString *fullPath = [path stringByAppendingPathComponent:[scheme substringFromIndex:@"assets://".length]]; + NSError *error; + NSString *jsContent = [NSString stringWithContentsOfFile:fullPath encoding:NSUTF8StringEncoding error:&error]; + if (error) { + [ret setupError:[NSException new]]; + } else { + [ret setupResult:jsContent]; + } + return ret; +} + +@end \ No newline at end of file From c666c7b81c8150f7deebdec7f13277e4cc031b38 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sat, 23 Nov 2019 19:44:46 +0800 Subject: [PATCH 11/11] feat:The same VC use diffrent childVC --- iOS/Example/Example/ViewController.m | 12 +++- iOS/Pod/Classes/Doric.h | 5 +- iOS/Pod/Classes/DoricPanel.h | 1 + iOS/Pod/Classes/DoricPanel.m | 7 +- iOS/Pod/Classes/DoricViewController.h | 25 +++++++ iOS/Pod/Classes/DoricViewController.m | 71 +++++++++++++++++++ .../Classes}/Loader/DoricHttpJSLoader.h | 0 .../Classes}/Loader/DoricHttpJSLoader.m | 0 .../Classes}/Loader/DoricJSLoaderManager.h | 0 .../Classes}/Loader/DoricJSLoaderManager.m | 0 .../Classes}/Loader/DoricLoaderProtocol.h | 0 .../Classes}/Loader/DoricMainBundleJSLoader.h | 0 .../Classes}/Loader/DoricMainBundleJSLoader.m | 0 .../Navigator/DoricNavigatorProtocol.h | 0 iOS/Pod/Classes/Util/DoricAsyncResult.h | 12 +--- iOS/Pod/Classes/Util/DoricAsyncResult.m | 10 +-- 16 files changed, 124 insertions(+), 19 deletions(-) create mode 100644 iOS/Pod/Classes/DoricViewController.h create mode 100644 iOS/Pod/Classes/DoricViewController.m rename iOS/{ => Pod/Classes}/Loader/DoricHttpJSLoader.h (100%) rename iOS/{ => Pod/Classes}/Loader/DoricHttpJSLoader.m (100%) rename iOS/{ => Pod/Classes}/Loader/DoricJSLoaderManager.h (100%) rename iOS/{ => Pod/Classes}/Loader/DoricJSLoaderManager.m (100%) rename iOS/{ => Pod/Classes}/Loader/DoricLoaderProtocol.h (100%) rename iOS/{ => Pod/Classes}/Loader/DoricMainBundleJSLoader.h (100%) rename iOS/{ => Pod/Classes}/Loader/DoricMainBundleJSLoader.m (100%) rename iOS/{ => Pod/Classes}/Navigator/DoricNavigatorProtocol.h (100%) diff --git a/iOS/Example/Example/ViewController.m b/iOS/Example/Example/ViewController.m index 8acce53f..6110d39c 100644 --- a/iOS/Example/Example/ViewController.m +++ b/iOS/Example/Example/ViewController.m @@ -58,8 +58,16 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath [self.navigationController pushViewController:[QRScanViewController new] animated:NO]; return; } - DemoVC *demoVC = [[DemoVC alloc] initWithPath:self.demoFilePaths[(NSUInteger) indexPath.row]]; - [self.navigationController pushViewController:demoVC animated:NO]; + NSString *file = self.demoFilePaths[(NSUInteger) indexPath.row]; + if ([file containsString:@"NavigatorDemo"]) { + DoricViewController *doricViewController = [[DoricViewController alloc] + initWithScheme:[NSString stringWithFormat:@"assets://demo/%@", file] + alias:self.demoFilePaths[(NSUInteger) indexPath.row]]; + [self.navigationController pushViewController:doricViewController animated:NO]; + } else { + DemoVC *demoVC = [[DemoVC alloc] initWithPath:file]; + [self.navigationController pushViewController:demoVC animated:NO]; + } } @end diff --git a/iOS/Pod/Classes/Doric.h b/iOS/Pod/Classes/Doric.h index 705cc394..9df6d257 100644 --- a/iOS/Pod/Classes/Doric.h +++ b/iOS/Pod/Classes/Doric.h @@ -20,4 +20,7 @@ #import "DoricRootNode.h" #import "UIView+Doric.h" #import "DoricUtil.h" -#import "DoricPanel.h" \ No newline at end of file +#import "DoricPanel.h" +#import "DoricJSLoaderManager.h" +#import "DoricNavigatorProtocol.h" +#import "DoricViewController.h" \ No newline at end of file diff --git a/iOS/Pod/Classes/DoricPanel.h b/iOS/Pod/Classes/DoricPanel.h index a262ddce..66ad7657 100644 --- a/iOS/Pod/Classes/DoricPanel.h +++ b/iOS/Pod/Classes/DoricPanel.h @@ -20,6 +20,7 @@ #import #import "DoricContext.h" +#import "DoricNavigatorProtocol.h" @interface DoricPanel : UIViewController @property(nonatomic, strong) DoricContext *doricContext; diff --git a/iOS/Pod/Classes/DoricPanel.m b/iOS/Pod/Classes/DoricPanel.m index d647f1bb..b4cc5af1 100644 --- a/iOS/Pod/Classes/DoricPanel.m +++ b/iOS/Pod/Classes/DoricPanel.m @@ -20,11 +20,13 @@ #import "DoricPanel.h" #import "Doric.h" - @implementation DoricPanel - (void)config:(NSString *)script alias:(NSString *)alias { self.doricContext = [[[DoricContext alloc] initWithScript:script source:alias] also:^(DoricContext *it) { + if ([self.parentViewController conformsToProtocol:@protocol(DoricNavigatorProtocol)]) { + it.navigator = (id ) self.parentViewController; + } [it.rootNode setupRootView:[[DoricStackView new] also:^(DoricStackView *it) { it.width = self.view.width; it.height = self.view.height; @@ -43,4 +45,5 @@ - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [self.doricContext onHidden]; } -@end \ No newline at end of file + +@end diff --git a/iOS/Pod/Classes/DoricViewController.h b/iOS/Pod/Classes/DoricViewController.h new file mode 100644 index 00000000..b39c3e4a --- /dev/null +++ b/iOS/Pod/Classes/DoricViewController.h @@ -0,0 +1,25 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2019/11/23. +// + +#import +#import "DoricNavigatorProtocol.h" + +@interface DoricViewController : UIViewController +- (instancetype)initWithScheme:(NSString *)scheme alias:(NSString *)alias; +@end \ No newline at end of file diff --git a/iOS/Pod/Classes/DoricViewController.m b/iOS/Pod/Classes/DoricViewController.m new file mode 100644 index 00000000..49d1068a --- /dev/null +++ b/iOS/Pod/Classes/DoricViewController.m @@ -0,0 +1,71 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2019/11/23. +// + +#import "DoricViewController.h" +#import "DoricAsyncResult.h" +#import "DoricJSLoaderManager.h" +#import "DoricPanel.h" +#import "UIView+Doric.h" +#import "DoricExtensions.h" + +@implementation DoricViewController +- (instancetype)initWithScheme:(NSString *)scheme alias:(NSString *)alias { + if (self = [super init]) { + [self push:scheme alias:alias]; + self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" + style:UIBarButtonItemStylePlain + target:self + action:@selector(pop)]; + } + return self; +} + +- (void)push:(NSString *)scheme alias:(NSString *)alias { + DoricAsyncResult *result = [DoricJSLoaderManager.instance request:scheme]; + result.resultCallback = ^(NSString *result) { + dispatch_async(dispatch_get_main_queue(), ^{ + DoricPanel *panel = [DoricPanel new]; + [panel.view also:^(UIView *it) { + it.backgroundColor = [UIColor whiteColor]; + it.width = self.view.width; + it.height = self.view.height - 88; + it.top = 88; + }]; + [self.view addSubview:panel.view]; + [self addChildViewController:panel]; + [panel config:result alias:alias]; + }); + }; +} + +- (void)pop { + dispatch_async(dispatch_get_main_queue(), ^{ + if (self.childViewControllers.count > 1) { + [self.childViewControllers.lastObject also:^(UIViewController *it) { + [it removeFromParentViewController]; + [it.view removeFromSuperview]; + }]; + } else { + [self.navigationController popViewControllerAnimated:NO]; + } + + }); +} + +@end diff --git a/iOS/Loader/DoricHttpJSLoader.h b/iOS/Pod/Classes/Loader/DoricHttpJSLoader.h similarity index 100% rename from iOS/Loader/DoricHttpJSLoader.h rename to iOS/Pod/Classes/Loader/DoricHttpJSLoader.h diff --git a/iOS/Loader/DoricHttpJSLoader.m b/iOS/Pod/Classes/Loader/DoricHttpJSLoader.m similarity index 100% rename from iOS/Loader/DoricHttpJSLoader.m rename to iOS/Pod/Classes/Loader/DoricHttpJSLoader.m diff --git a/iOS/Loader/DoricJSLoaderManager.h b/iOS/Pod/Classes/Loader/DoricJSLoaderManager.h similarity index 100% rename from iOS/Loader/DoricJSLoaderManager.h rename to iOS/Pod/Classes/Loader/DoricJSLoaderManager.h diff --git a/iOS/Loader/DoricJSLoaderManager.m b/iOS/Pod/Classes/Loader/DoricJSLoaderManager.m similarity index 100% rename from iOS/Loader/DoricJSLoaderManager.m rename to iOS/Pod/Classes/Loader/DoricJSLoaderManager.m diff --git a/iOS/Loader/DoricLoaderProtocol.h b/iOS/Pod/Classes/Loader/DoricLoaderProtocol.h similarity index 100% rename from iOS/Loader/DoricLoaderProtocol.h rename to iOS/Pod/Classes/Loader/DoricLoaderProtocol.h diff --git a/iOS/Loader/DoricMainBundleJSLoader.h b/iOS/Pod/Classes/Loader/DoricMainBundleJSLoader.h similarity index 100% rename from iOS/Loader/DoricMainBundleJSLoader.h rename to iOS/Pod/Classes/Loader/DoricMainBundleJSLoader.h diff --git a/iOS/Loader/DoricMainBundleJSLoader.m b/iOS/Pod/Classes/Loader/DoricMainBundleJSLoader.m similarity index 100% rename from iOS/Loader/DoricMainBundleJSLoader.m rename to iOS/Pod/Classes/Loader/DoricMainBundleJSLoader.m diff --git a/iOS/Navigator/DoricNavigatorProtocol.h b/iOS/Pod/Classes/Navigator/DoricNavigatorProtocol.h similarity index 100% rename from iOS/Navigator/DoricNavigatorProtocol.h rename to iOS/Pod/Classes/Navigator/DoricNavigatorProtocol.h diff --git a/iOS/Pod/Classes/Util/DoricAsyncResult.h b/iOS/Pod/Classes/Util/DoricAsyncResult.h index 9eb813ff..6a5b6a7d 100644 --- a/iOS/Pod/Classes/Util/DoricAsyncResult.h +++ b/iOS/Pod/Classes/Util/DoricAsyncResult.h @@ -26,15 +26,9 @@ NS_ASSUME_NONNULL_BEGIN @interface DoricAsyncResult : NSObject -typedef void(^DoricResultCallback)(R); - -typedef void(^DoricExceptionCallback)(NSException *); - -typedef void(^DoricFinishCallback)(void); - -@property(nonatomic, strong) DoricResultCallback resultCallback; -@property(nonatomic, strong) DoricExceptionCallback exceptionCallback; -@property(nonatomic, strong) DoricFinishCallback finishCallback; +@property(nonatomic, strong) void (^resultCallback)(R result); +@property(nonatomic, strong) void (^exceptionCallback)(NSException *e); +@property(nonatomic, strong) void (^finishCallback)(void); - (void)setupResult:(R)result; diff --git a/iOS/Pod/Classes/Util/DoricAsyncResult.m b/iOS/Pod/Classes/Util/DoricAsyncResult.m index 786f2eff..e70b8805 100644 --- a/iOS/Pod/Classes/Util/DoricAsyncResult.m +++ b/iOS/Pod/Classes/Util/DoricAsyncResult.m @@ -56,24 +56,24 @@ - (id)getResult { return self.result; } -- (void)setResultCallback:(DoricResultCallback)callback { +- (void)setResultCallback:(void (^)(id))callback { _resultCallback = callback; if (self.result && ![self.result isKindOfClass:[NSException class]]) { callback(self.result); } } -- (void)setExceptionCallback:(DoricExceptionCallback)exceptionCallback { +- (void)setExceptionCallback:(void (^)(NSException *))exceptionCallback { _exceptionCallback = exceptionCallback; if ([self.result isKindOfClass:[NSException class]]) { exceptionCallback(self.result); } } -- (void)setFinishCallback:(DoricFinishCallback)callback { - _finishCallback = callback; +- (void)setFinishCallback:(void (^)(void))finishCallback { + _finishCallback = finishCallback; if (self.result) { - callback(); + finishCallback(); } }