Commit 91880134 authored by Yacumima's avatar Yacumima

dev

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