Commit 91880134 authored by Yacumima's avatar Yacumima

dev

parent 3ef2bfe4
......@@ -40,7 +40,13 @@ public class ContainerRecord implements IContainerRecord {
private MethodChannelProxy mProxy = new MethodChannelProxy();
ContainerRecord(FlutterViewContainerManager manager, IFlutterViewContainer container) {
mUniqueId = System.currentTimeMillis() + "-" + hashCode();
final Map params = container.getContainerUrlParams();
if(params != null && params.containsKey(IContainerRecord.UNIQ_KEY)) {
mUniqueId = String.valueOf(params.get(IContainerRecord.UNIQ_KEY));
}else{
mUniqueId = genUniqueId(this);
}
mManager = manager;
mContainer = container;
}
......@@ -124,6 +130,8 @@ public class ContainerRecord implements IContainerRecord {
mManager.removeRecord(this);
mManager.setContainerResult(this,-1,-1,null);
if (!mManager.hasContainerAppear()) {
mContainer.getBoostFlutterView().onPause();
mContainer.getBoostFlutterView().onStop();
......@@ -164,8 +172,8 @@ public class ContainerRecord implements IContainerRecord {
}
@Override
public void onContainerResult(int requestCode, Map<String, Object> result) {
mManager.setContainerResult(this, requestCode, result);
public void onContainerResult(int requestCode, int resultCode, Map<String, Object> result) {
mManager.setContainerResult(this, requestCode,resultCode, result);
}
@Override
......@@ -252,4 +260,8 @@ public class ContainerRecord implements IContainerRecord {
FlutterBoost.singleton().channel().invokeMethodUnsafe(method, args);
}
}
public static String genUniqueId(Object obj) {
return System.currentTimeMillis() + "-" + obj.hashCode();
}
}
......@@ -67,7 +67,7 @@ public class FlutterBoost {
}
private final IPlatform mPlatform;
private final IContainerManager mManager;
private final FlutterViewContainerManager mManager;
private final IFlutterEngineProvider mEngineProvider;
IStateListener mStateListener;
......@@ -223,7 +223,7 @@ public class FlutterBoost {
Map<String,Object> exts = methodCall.argument("exts");
String url = methodCall.argument("url");
mManager.openContainer(url, params, exts, new IContainerManager.OnResult() {
mManager.openContainer(url, params, exts, new FlutterViewContainerManager.OnResult() {
@Override
public void onResult(Map<String, Object> rlt) {
if (result != null) {
......@@ -232,7 +232,7 @@ public class FlutterBoost {
}
});
}catch (Throwable t){
result.error("open page error",t.getMessage(),t);
}
}
break;
......
......@@ -35,6 +35,7 @@ import com.idlefish.flutterboost.interfaces.IOperateSyncer;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
......@@ -48,8 +49,7 @@ public class FlutterViewContainerManager implements IContainerManager {
private final Set<ContainerRef> mRefs = new HashSet<>();
private final Stack<IContainerRecord> mRecordStack = new Stack<>();
private final AtomicInteger mRequestCode = new AtomicInteger(0);
private final SparseArray<OnResult> mOnResults = new SparseArray<>();
private final Map<String,OnResult> mOnResults = new HashMap<>();
FlutterViewContainerManager() {}
......@@ -86,37 +86,55 @@ public class FlutterViewContainerManager implements IContainerManager {
mRecordMap.remove(record.getContainer());
}
void setContainerResult(IContainerRecord record,int requestCode, Map<String,Object> result) {
void setContainerResult(IContainerRecord record,int requestCode, int resultCode, Map<String,Object> result) {
IFlutterViewContainer target = findContainerById(record.uniqueId());
if(target == null) {
Debuger.exception("setContainerResult error, url="+record.getContainer().getContainerUrl());
}
OnResult onResult = mOnResults.get(requestCode);
if (result == null) {
result = new HashMap<>();
}
result.put("_requestCode__",requestCode);
result.put("_resultCode__",resultCode);
final OnResult onResult = mOnResults.remove(record.uniqueId());
if(onResult != null) {
onResult.onResult(result);
}
mOnResults.remove(requestCode);
}
@Override
public void openContainer(String url, Map<String, Object> urlParams, Map<String, Object> exts,OnResult onResult) {
public IContainerRecord recordOf(IFlutterViewContainer container) {
return mRecordMap.get(container);
}
void openContainer(String url, Map<String, Object> urlParams, Map<String, Object> exts,OnResult onResult) {
Context context = FlutterBoost.singleton().currentActivity();
if(context == null) {
context = FlutterBoost.singleton().platform().getApplication();
}
int requestCode = mRequestCode.addAndGet(3);
if(urlParams == null) {
urlParams = new HashMap<>();
}
int requestCode = 0;
if (urlParams.containsKey("requestCode")) {
requestCode = Integer.valueOf((String)urlParams.remove("requestCode"));
}
final String uniqueId = ContainerRecord.genUniqueId(url);
urlParams.put(IContainerRecord.UNIQ_KEY,uniqueId);
if(onResult != null) {
mOnResults.put(requestCode,onResult);
mOnResults.put(uniqueId,onResult);
}
FlutterBoost.singleton().platform().openContainer(context,url,urlParams,requestCode,exts);
}
@Override
public IContainerRecord closeContainer(String uniqueId, Map<String, Object> result,Map<String,Object> exts) {
IContainerRecord closeContainer(String uniqueId, Map<String, Object> result,Map<String,Object> exts) {
IContainerRecord targetRecord = null;
for (Map.Entry<IFlutterViewContainer, IContainerRecord> entry : mRecordMap.entrySet()) {
if (TextUtils.equals(uniqueId, entry.getValue().uniqueId())) {
......@@ -170,8 +188,7 @@ public class FlutterViewContainerManager implements IContainerManager {
return target;
}
@Override
public void onShownContainerChanged(String old, String now) {
void onShownContainerChanged(String old, String now) {
Utils.assertCallOnMainThread();
IFlutterViewContainer oldContainer = null;
......@@ -220,4 +237,8 @@ public class FlutterViewContainerManager implements IContainerManager {
this.container = new WeakReference<>(container);
}
}
interface OnResult {
void onResult(Map<String,Object> result);
}
}
......@@ -191,7 +191,7 @@ public abstract class BoostFlutterActivity extends Activity implements IFlutterV
}
}
mSyncer.onContainerResult(requestCode,result);
mSyncer.onContainerResult(requestCode,resultCode,result);
}
@Override
......@@ -230,11 +230,11 @@ public abstract class BoostFlutterActivity extends Activity implements IFlutterV
@Override
public void finishContainer(Map<String,Object> result) {
Intent intent = new Intent();
if (result != null) {
intent.putExtra(RESULT_KEY, new HashMap<>(result));
if(result != null) {
FlutterBoost.setBoostResult(this,new HashMap<>(result));
}else{
finish();
}
setResult(Activity.RESULT_OK, intent);
}
@Override
......
......@@ -23,27 +23,15 @@
*/
package com.idlefish.flutterboost.interfaces;
import java.util.Map;
public interface IContainerManager {
IOperateSyncer generateSyncer(IFlutterViewContainer container);
void openContainer(String url,Map<String,Object> urlParams,Map<String,Object> exts,OnResult onResult);
IContainerRecord closeContainer(String uniqueId,Map<String,Object> result,Map<String,Object> exts);
IContainerRecord getCurrentTopRecord();
IContainerRecord getLastGenerateRecord();
IFlutterViewContainer findContainerById(String uniqueId);
void onShownContainerChanged(String oldUniqueId,String nowUniqueId);
boolean hasContainerAppear();
interface OnResult {
void onResult(Map<String,Object> result);
}
}
\ No newline at end of file
......@@ -27,6 +27,9 @@ package com.idlefish.flutterboost.interfaces;
* a container record, which use map a flutter page
*/
public interface IContainerRecord extends IOperateSyncer{
String UNIQ_KEY = "__container_uniqueId_key__";
int STATE_UNKNOW = 0;
int STATE_CREATED = 1;
int STATE_APPEAR = 2;
......
......@@ -22,7 +22,7 @@ public interface IOperateSyncer {
void onActivityResult(int requestCode, int resultCode, Intent data);
void onContainerResult(int requestCode, Map<String,Object> result);
void onContainerResult(int requestCode, int resultCode, Map<String,Object> result);
void onUserLeaveHint();
......
......@@ -39,9 +39,6 @@ enum ContainerLifeCycle {
typedef void BoostContainerLifeCycleObserver(
ContainerLifeCycle state, BoostContainerSettings settings);
typedef void ResultObserver(
int requestCode, int responseCode, Map<dynamic, dynamic> result);
class BoostContainer extends Navigator {
final BoostContainerSettings settings;
......@@ -116,9 +113,7 @@ class BoostContainer extends Navigator {
}
class BoostContainerState extends NavigatorState {
final Set<VoidCallback> _backPressedListeners = Set<VoidCallback>();
final Set<ResultObserver> _resultObservers = Set<ResultObserver>();
VoidCallback backPressedHandler;
String get uniqueId => widget.settings.uniqueId;
......@@ -148,6 +143,12 @@ class BoostContainerState extends NavigatorState {
return null;
}
@override
void initState() {
super.initState();
backPressedHandler = ()=>pop();
}
@override
void didUpdateWidget(Navigator oldWidget) {
super.didUpdateWidget(oldWidget);
......@@ -165,25 +166,7 @@ class BoostContainerState extends NavigatorState {
void performBackPressed() {
Logger.log('performBackPressed');
if (_backPressedListeners.isEmpty) {
pop();
} else {
for (VoidCallback cb in _backPressedListeners) {
cb();
}
}
}
void performOnResult(Map<dynamic, dynamic> data) {
Logger.log('performOnResult ${data.toString()}');
final int requestCode = data['requestCode'];
final int responseCode = data['responseCode'];
final Map result = data['result'];
for (ResultObserver observer in _resultObservers) {
observer(requestCode, responseCode, result);
}
backPressedHandler?.call();
}
@override
......@@ -219,18 +202,6 @@ class BoostContainerState extends NavigatorState {
return future;
}
VoidCallback addBackPressedListener(VoidCallback listener) {
_backPressedListeners.add(listener);
return () => _backPressedListeners.remove(listener);
}
VoidCallback addResultObserver(ResultObserver observer) {
_resultObservers.add(observer);
return () => _resultObservers.remove(observer);
}
VoidCallback addLifeCycleObserver(BoostContainerLifeCycleObserver observer) {
return FlutterBoost.singleton.addBoostContainerLifeCycleObserver(
(ContainerLifeCycle state, BoostContainerSettings settings) {
......
......@@ -37,10 +37,10 @@ export 'container/container_manager.dart';
typedef Widget PageBuilder(String pageName, Map params, String uniqueId);
typedef Route PrePushRoute(
String pageName, String uniqueId, Map params, Route route);
String url, String uniqueId, Map params, Route route);
typedef void PostPushRoute(
String pageName, String uniqueId, Map params, Route route, Future result);
String url, String uniqueId, Map params, Route route, Future result);
class FlutterBoost {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment