add navigator demo for Android and single activity multi fragment
This commit is contained in:
parent
920ca8d41e
commit
885d168d33
@ -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;
|
||||
|
@ -47,7 +47,6 @@ public class MainActivity extends AppCompatActivity {
|
||||
try {
|
||||
String[] demos = getAssets().list("demo");
|
||||
List<String> 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;
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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();
|
||||
}
|
@ -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();
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<pub.doric.DoricPanel xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
android:layout_height="match_parent"
|
||||
android:background="#ffffff" />
|
@ -10,4 +10,5 @@ export default [
|
||||
'src/ModalDemo',
|
||||
'src/NetworkDemo',
|
||||
'src/StorageDemo',
|
||||
'src/NavigatorDemo',
|
||||
]
|
50
demo/src/NavigatorDemo.ts
Normal file
50
demo/src/NavigatorDemo.ts
Normal 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)
|
||||
}
|
||||
|
||||
}
|
@ -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()
|
||||
},
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user