add navigator demo for Android and single activity multi fragment

This commit is contained in:
pengfei.zhou 2019-11-23 16:23:08 +08:00
parent 920ca8d41e
commit 885d168d33
13 changed files with 199 additions and 17 deletions

View File

@ -30,6 +30,7 @@ import org.greenrobot.eventbus.ThreadMode;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.DoricContextManager; import pub.doric.DoricContextManager;
import pub.doric.DoricPanel; import pub.doric.DoricPanel;
import pub.doric.devkit.DoricContextDebuggable;
import pub.doric.devkit.event.EnterDebugEvent; import pub.doric.devkit.event.EnterDebugEvent;
import pub.doric.devkit.event.QuitDebugEvent; import pub.doric.devkit.event.QuitDebugEvent;
import pub.doric.devkit.event.ReloadEvent; import pub.doric.devkit.event.ReloadEvent;

View File

@ -47,7 +47,6 @@ public class MainActivity extends AppCompatActivity {
try { try {
String[] demos = getAssets().list("demo"); String[] demos = getAssets().list("demo");
List<String> ret = new ArrayList<>(); List<String> ret = new ArrayList<>();
ret.add("Navigator");
for (String str : demos) { for (String str : demos) {
if (str.endsWith("js")) { if (str.endsWith("js")) {
ret.add(str); ret.add(str);
@ -91,8 +90,10 @@ public class MainActivity extends AppCompatActivity {
tv.setOnClickListener(new View.OnClickListener() { tv.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (position == 0) { if (data[position].contains("NavigatorDemo")) {
Intent intent = new Intent(tv.getContext(), DoricActivity.class); Intent intent = new Intent(tv.getContext(), DoricActivity.class);
intent.putExtra("scheme", "assets://demo/" + data[position]);
intent.putExtra("alias", data[position]);
tv.getContext().startActivity(intent); tv.getContext().startActivity(intent);
return; return;
} }

View File

@ -19,7 +19,6 @@ import android.os.Bundle;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
/** /**
* @Description: pub.doric.demo * @Description: pub.doric.demo
@ -27,19 +26,28 @@ import androidx.fragment.app.Fragment;
* @CreateDate: 2019-11-19 * @CreateDate: 2019-11-19
*/ */
public class DoricActivity extends AppCompatActivity { public class DoricActivity extends AppCompatActivity {
private DoricFragment doricFragment;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.doric_activity); setContentView(R.layout.doric_activity);
if (savedInstanceState == null) { if (savedInstanceState == null) {
Fragment doricFragment = getSupportFragmentManager().getFragmentFactory().instantiate( String scheme = getIntent().getStringExtra("scheme");
getClassLoader(), String alias = getIntent().getStringExtra("alias");
DoricFragment.class.getName() doricFragment = DoricFragment.newInstance(scheme, alias);
);
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.add(R.id.container, doricFragment) .add(R.id.container, doricFragment)
.commit(); .commit();
} }
} }
@Override
public void onBackPressed() {
if (doricFragment.canPop()) {
doricFragment.pop();
} else {
super.onBackPressed();
}
}
} }

View File

@ -26,6 +26,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import pub.doric.async.AsyncResult; import pub.doric.async.AsyncResult;
import pub.doric.navigator.IDoricNavigator;
import pub.doric.plugin.DoricJavaPlugin; import pub.doric.plugin.DoricJavaPlugin;
import pub.doric.shader.RootNode; import pub.doric.shader.RootNode;
import pub.doric.utils.DoricConstant; import pub.doric.utils.DoricConstant;
@ -148,4 +149,14 @@ public class DoricContext {
public void onHidden() { public void onHidden() {
callEntity(DoricConstant.DORIC_ENTITY_HIDDEN); callEntity(DoricConstant.DORIC_ENTITY_HIDDEN);
} }
private IDoricNavigator doricNavigator;
public void setDoricNavigator(IDoricNavigator doricNavigator) {
this.doricNavigator = doricNavigator;
}
public IDoricNavigator getDoricNavigator() {
return this.doricNavigator;
}
} }

View File

@ -19,32 +19,66 @@ import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.FrameLayout;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import pub.doric.navigator.IDoricNavigator;
/** /**
* @Description: pub.doric * @Description: pub.doric
* @Author: pengfei.zhou * @Author: pengfei.zhou
* @CreateDate: 2019-11-23 * @CreateDate: 2019-11-23
*/ */
public class DoricFragment extends Fragment { public class DoricFragment extends Fragment implements IDoricNavigator {
private FrameLayout root;
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 @Nullable
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
root = (FrameLayout) inflater.inflate(R.layout.doric_fragment, container, false); return inflater.inflate(R.layout.doric_fragment, container, false);
return root;
} }
@Override @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
requireFragmentManager().beginTransaction() Bundle argument = getArguments();
.add(R.id.root, DoricPanelFragment.newInstance("assets://demo/Counter.js", "Counter.js")) 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(); .commit();
} }
@Override
public void pop() {
if (canPop()) {
getChildFragmentManager().popBackStack();
} else {
if (getActivity() != null) {
getActivity().finish();
}
}
}
public boolean canPop() {
return getChildFragmentManager().getBackStackEntryCount() > 1;
}
} }

View File

@ -16,7 +16,6 @@
package pub.doric; package pub.doric;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -27,6 +26,7 @@ import androidx.fragment.app.Fragment;
import pub.doric.async.AsyncResult; import pub.doric.async.AsyncResult;
import pub.doric.loader.DoricJSLoaderManager; import pub.doric.loader.DoricJSLoaderManager;
import pub.doric.navigator.IDoricNavigator;
import pub.doric.utils.DoricLog; import pub.doric.utils.DoricLog;
/** /**
@ -67,6 +67,11 @@ public class DoricPanelFragment extends Fragment {
@Override @Override
public void onResult(String result) { public void onResult(String result) {
doricPanel.config(result, alias); doricPanel.config(result, alias);
DoricContext context = doricPanel.getDoricContext();
Fragment fragment = getParentFragment();
if (fragment instanceof IDoricNavigator) {
context.setDoricNavigator((IDoricNavigator) fragment);
}
} }
@Override @Override

View File

@ -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();
}

View File

@ -50,7 +50,7 @@ public class ModalPlugin extends DoricJavaPlugin {
} }
@DoricMethod(thread = ThreadMode.UI) @DoricMethod(thread = ThreadMode.UI)
public void toast(JSDecoder decoder, DoricPromise promise) { public void toast(JSDecoder decoder) {
try { try {
JSObject jsObject = decoder.decode().asObject(); JSObject jsObject = decoder.decode().asObject();
String msg = jsObject.getProperty("msg").asString().value(); String msg = jsObject.getProperty("msg").asString().value();

View File

@ -15,8 +15,15 @@
*/ */
package pub.doric.plugin; 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.DoricContext;
import pub.doric.extension.bridge.DoricMethod;
import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.navigator.IDoricNavigator;
import pub.doric.utils.ThreadMode;
/** /**
* @Description: pub.doric.plugin * @Description: pub.doric.plugin
@ -28,4 +35,27 @@ public class NavigatorPlugin extends DoricJavaPlugin {
public NavigatorPlugin(DoricContext doricContext) { public NavigatorPlugin(DoricContext doricContext) {
super(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();
}
}
} }

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<pub.doric.DoricPanel xmlns:android="http://schemas.android.com/apk/res/android" <pub.doric.DoricPanel xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent"
android:background="#ffffff" />

View File

@ -10,4 +10,5 @@ export default [
'src/ModalDemo', 'src/ModalDemo',
'src/NetworkDemo', 'src/NetworkDemo',
'src/StorageDemo', 'src/StorageDemo',
'src/NavigatorDemo',
] ]

50
demo/src/NavigatorDemo.ts Normal file
View File

@ -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)
}
}

View File

@ -167,3 +167,16 @@ export function storage(context: BridgeContext) {
}, },
} }
} }
export function navigator(context: BridgeContext) {
return {
push: (scheme: string, alias: string) => {
return context.navigator.push({
scheme, alias
})
},
pop: () => {
return context.navigator.pop()
},
}
}