Commit cd39f6ab authored by Jidong Chen's avatar Jidong Chen

Merge branch 'no_query' into flutter_1.5_upgrade_opt

# Conflicts:
#	android/src/main/java/com/taobao/idlefish/flutterboost/BoostFlutterNativeView.java
parents 6da5f686 ac1f1d46
......@@ -32,12 +32,15 @@ public class BoostFlutterNativeView extends FlutterNativeView {
public BoostFlutterNativeView(Context context) {
super(context);
}
@Override
public void detachFromFlutterView() {
//do nothing...
}
public void detach() {
//do nothing...
}
@Override
public void destroy() {
//do nothing...
......
......@@ -56,6 +56,7 @@ import io.flutter.view.FlutterView;
public class FlutterBoostPlugin implements MethodChannel.MethodCallHandler, Application.ActivityLifecycleCallbacks {
private static FlutterBoostPlugin sInstance = null;
private static int kRid = 0;
public static synchronized void init(IPlatform platform) {
if (sInstance == null) {
......@@ -147,55 +148,33 @@ public class FlutterBoostPlugin implements MethodChannel.MethodCallHandler, Appl
if (ctx == null) {
ctx = sInstance.mPlatform.getApplication();
}
//Handling page result.
if (sInstance.needResult(params)) {
sInstance.mMediator.setHandler(url, new PageResultHandler() {
@Override
public void onResult(String key, Map resultData) {
NavigationService.onNativePageResult(new MessageResult<Boolean>() {
@Override
public void success(Boolean var1) {
//Doing nothing now.
}
@Override
public void error(String var1, String var2, Object var3) {
//Doing nothing now.
}
@Override
public void notImplemented() {
//Doing nothing now.
}
}, "no use", key, resultData, params);
}
});
}
sInstance.mPlatform.startActivity(ctx, concatUrl(url, params), requestCode);
}
private Boolean needResult(Map params) {
public static void openPage(Context context, String url, final Map params, int requestCode,PageResultHandler handler) {
if (params == null) return false;
final String key = "needResult";
if (params.containsKey(key)) {
if (params.get(key) instanceof Boolean) {
return (Boolean) params.get(key);
}
if(handler != null){
String rid = createResultId();
sInstance.mMediator.setHandler(rid,handler);
params.put("result_id",rid);
}
return false;
openPage(context,url,params,requestCode);
}
public static void onPageResult(String key, Map resultData) {
private static String createResultId(){
kRid += 2;
return "result_id_" + kRid;
}
public static void onPageResult(String key , Map resultData, Map params){
if (sInstance == null) {
throw new RuntimeException("FlutterBoostPlugin not init yet!");
}
sInstance.mMediator.onPageResult(key, resultData);
sInstance.mMediator.onPageResult(key,resultData,params);
}
public static void setHandler(String key, PageResultHandler handler) {
......
......@@ -38,20 +38,10 @@
private boolean onCall(MessageResult<Boolean> result,String pageName,Map params,Boolean animated){
Map pageParams = null;
int requestCode = 0;
if(params != null && params.get("query") != null) {
pageParams = (Map)params.get("query");
}
if(params != null && params.get("requestCode") != null) {
requestCode = (int)params.get("requestCode");
FlutterBoostPlugin.openPage(null,pageName,params,0);
if(result != null){
result.success(true);
}
FlutterBoostPlugin.openPage(null,pageName,pageParams,requestCode);
result.success(true);
return true;
}
......
......@@ -23,19 +23,69 @@
*/
package com.taobao.idlefish.flutterboost;
import com.taobao.idlefish.flutterboost.NavigationService.NavigationService;
import java.util.HashMap;
import java.util.Map;
import fleamarket.taobao.com.xservicekit.handler.MessageResult;
class PageResultMediator {
private Map<String,PageResultHandler> _handlers = new HashMap<>();
void onPageResult(String key , Map resultData){
void onPageResult(String key , Map resultData,Map params){
if(key == null) return;
if(_handlers.containsKey(key)){
_handlers.get(key).onResult(key,resultData);
_handlers.remove(key);
}else{
if(params == null || !params.containsKey("forward")){
if(params == null){
params = new HashMap();
}
params.put("forward",1);
NavigationService.onNativePageResult(new MessageResult<Boolean>() {
@Override
public void success(Boolean var1) {
}
@Override
public void error(String var1, String var2, Object var3) {
}
@Override
public void notImplemented() {
}
},key,key,resultData,params);
}else{
int forward = (Integer) params.get("forward");
params.put("forward",++forward);
if(forward <= 2){
NavigationService.onNativePageResult(new MessageResult<Boolean>() {
@Override
public void success(Boolean var1) {
}
@Override
public void error(String var1, String var2, Object var3) {
}
@Override
public void notImplemented() {
}
},key,key,resultData,params);
}
}
}
}
......
......@@ -6,7 +6,10 @@ import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import com.taobao.idlefish.flutterboost.FlutterBoostPlugin;
import java.lang.ref.WeakReference;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
......@@ -46,6 +49,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
PageRouter.openPageByUrl(this, PageRouter.NATIVE_PAGE_URL);
} else if (v == mOpenFlutter) {
PageRouter.openPageByUrl(this, PageRouter.FLUTTER_PAGE_URL);
FlutterBoostPlugin.onPageResult("result_id_100",new HashMap(),new HashMap());
} else if (v == mOpenFlutterFragment) {
PageRouter.openPageByUrl(this, PageRouter.FLUTTER_FRAGMENT_PAGE_URL);
}
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BuildLocationStyle</key>
<string>UseTargetSettings</string>
</dict>
</plist>
......@@ -29,11 +29,14 @@
if([params[@"present"] boolValue]){
FLBFlutterViewContainer *vc = FLBFlutterViewContainer.new;
[vc setName:name params:params];
[self.navigationController presentViewController:vc animated:animated completion:^{}];
[self.navigationController presentViewController:vc animated:animated completion:^{
if(completion) completion(YES);
}];
}else{
FLBFlutterViewContainer *vc = FLBFlutterViewContainer.new;
[vc setName:name params:params];
[self.navigationController pushViewController:vc animated:animated];
if(completion) completion(YES);
}
}
......
......@@ -9,7 +9,7 @@
#import "UIViewControllerDemo.h"
#import <Flutter/Flutter.h>
#import "DemoRouter.h"
#import <flutter_boost/FlutterBoostPlugin.h>
@interface UIViewControllerDemo ()
......@@ -25,7 +25,11 @@
- (IBAction)pushFlutterPage:(id)sender {
[DemoRouter.sharedRouter openPage:@"first" params:@{} animated:YES completion:^(BOOL f){}];
[DemoRouter.sharedRouter openPage:@"first" params:@{} animated:YES completion:^(BOOL f){
[FlutterBoostPlugin.sharedInstance onResultForKey:@"result_id_100" resultData:@{} params:@{}];
}];
}
- (IBAction)present:(id)sender {
......
......@@ -31,6 +31,7 @@ class _MyAppState extends State<MyApp> {
});
FlutterBoost.handleOnStartPage();
}
@override
......
......@@ -12,7 +12,11 @@ class FirstRouteWidget extends StatelessWidget {
child: RaisedButton(
child: Text('Open second route'),
onPressed: () {
FlutterBoost.singleton.openPage("second", {}, animated: true);
FlutterBoost.singleton.openPage("second", {}, animated: true, resultHandler:(String key , Map<dynamic,dynamic> result){
print("did recieve second route result $key $result");
});
},
),
),
......@@ -31,6 +35,13 @@ class SecondRouteWidget extends StatelessWidget {
child: RaisedButton(
onPressed: () {
// Navigate back to first route when tapped.
BoostContainerSettings settings = BoostContainer.of(context).settings;
if(settings.params.containsKey("result_id")){
String rid = settings.params["result_id"];
FlutterBoost.singleton.onPageResult(rid, {"data":"works"},{});
}
FlutterBoost.singleton.closePageForContext(context);
},
child: Text('Go back!'),
......
......@@ -30,7 +30,7 @@ typedef void (^FLBPageResultHandler)(NSString *, NSDictionary *);
@interface FLBResultMediator : NSObject
- (void)onResultForKey:(NSString *)vcId resultData:(NSDictionary *)resultData;
- (void)onResultForKey:(NSString *)resultId resultData:(NSDictionary *)resultData params:(NSDictionary *)params;
- (void)setResultHandler:(FLBPageResultHandler)handler forKey:(NSString *)vcid;
- (void)removeHandlerForKey:(NSString *)vcid;
......
......@@ -23,6 +23,7 @@
*/
#import "FLBResultMediator.h"
#import "Service_NavigationService.h"
@interface FLBResultMediator()
@property (nonatomic,strong) NSMutableDictionary *handlers;
......@@ -38,16 +39,46 @@
return self;
}
- (void)onResultForKey:(NSString *)vcId
- (void)onResultForKey:(NSString *)rid
resultData:(NSDictionary *)resultData
params:(nonnull NSDictionary *)params
{
if(!vcId) return;
if(!rid) return;
NSString *key = vcId;
NSString *key = rid;
if(_handlers[key]){
FLBPageResultHandler handler = _handlers[key];
handler(key,resultData);
[_handlers removeObjectForKey: key];
}else{
//Cannot find handler here. Try to forward message to flutter.
//Use forward to avoid circle.
if(!params || !params[@"forward"]){
NSMutableDictionary *tmp = params.mutableCopy;
if(!tmp){
tmp = NSMutableDictionary.new;
}
tmp[@"forward"] = @(1);
params = tmp;
[Service_NavigationService onNativePageResult:^(NSNumber *r) {}
uniqueId:rid
key:rid
resultData:resultData
params:params];
}else{
NSMutableDictionary *tmp = params.mutableCopy;
tmp[@"forward"] = @([params[@"forward"] intValue] + 1);
params = tmp;
if([params[@"forward"] intValue] <= 2){
[Service_NavigationService onNativePageResult:^(NSNumber *r) {}
uniqueId:rid
key:rid
resultData:resultData
params:params];
}
}
}
}
......
......@@ -42,7 +42,17 @@ typedef FLBFlutterViewContainer * (^FLBPageBuilder)(NSString *name,NSDictionary
- (FlutterViewController *)currentViewController;
#pragma mark - handing vc result.
- (void)onResultForKey:(NSString *)vcId resultData:(NSDictionary *)resultData;
- (void)setResultHandler:(void (^)(NSString *, NSDictionary *))handler forKey:(NSString *)vcid;
- (void)openPage:(NSString *)name
params:(NSDictionary *)params
animated:(BOOL)animated
completion:(void (^)(BOOL finished))completion
resultHandler:(void (^)(NSString *resultId,NSDictionary *rData))resultHandler;
- (void)onResultForKey:(NSString *)vcId
resultData:(NSDictionary *)resultData
params:(NSDictionary *)params;
- (void)setResultHandler:(void (^)(NSString *, NSDictionary *))handler
forKey:(NSString *)result_id;
- (void)removeHandlerForKey:(NSString *)vcid;
@end
......@@ -86,9 +86,21 @@
return [[FLBFlutterApplication sharedApplication] flutterViewController];
}
- (void)onResultForKey:(NSString *)vcId resultData:(NSDictionary *)resultData
- (void)openPage:(NSString *)name
params:(NSDictionary *)params animated:(BOOL)animated
completion:(void (^)(BOOL))completion
resultHandler:(void (^)(NSString *, NSDictionary *))resultHandler
{
[_resultMediator onResultForKey:vcId resultData:resultData];
static int kRid = 0;
NSString *resultId = [NSString stringWithFormat:@"result_id_%d",kRid++];
[_resultMediator setResultHandler:^(NSString * _Nonnull resultId, NSDictionary * _Nonnull resultData) {
if(resultHandler) resultHandler(resultId,resultData);
} forKey:resultId];
}
- (void)onResultForKey:(NSString *)vcId resultData:(NSDictionary *)resultData params:(NSDictionary *)params
{
[_resultMediator onResultForKey:vcId resultData:resultData params:params];
}
- (void)setResultHandler:(void (^)(NSString *, NSDictionary *))handler forKey:(NSString *)vcid
......
......@@ -34,7 +34,8 @@
{
//Add your handler code here!
[FlutterBoostPlugin.sharedInstance onResultForKey:key
resultData:resultData];
resultData:resultData
params:params];
}
#pragma mark - Do not edit these method.
......
......@@ -34,21 +34,6 @@
- (void)onCall:(void (^)(BOOL))result pageName:(NSString *)pageName params:(NSDictionary *)params animated:(NSNumber *)animated
{
NSString *url = pageName;
if(pageName == nil){
pageName = params[@"url"];
}
if([self needResult:params]){
[FlutterBoostPlugin.sharedInstance setResultHandler:^(NSString *key , NSDictionary *resultData) {
[Service_NavigationService onNativePageResult:^(NSNumber *) {}
uniqueId:@"no use"
key:url
resultData:resultData
params:@{}];
} forKey:url];
}
[[FLBFlutterApplication sharedApplication].platform openPage:pageName
params:params
animated:animated.boolValue
......@@ -57,16 +42,6 @@
}];
}
- (BOOL)needResult:(NSDictionary *)params
{
NSString *const key = @"needResult";
NSNumber *val = params[key];
if (val && [val isKindOfClass:NSNumber.class]) {
return val.boolValue;
}
return NO;
}
#pragma mark - Do not edit these method.
- (void)__flutter_p_handler_openPage:(NSDictionary *)args result:(void (^)(BOOL))result {
[self onCall:result pageName:args[@"pageName"] params:args[@"params"] animated:args[@"animated"]];
......
......@@ -51,6 +51,6 @@ class NavigationService_onNativePageResult extends ServiceCallHandler {
//==============================================Do not edit code above!
Future<bool> onCall(String uniqueId,String key,Map resultData,Map params) async{
return FlutterBoost.singleton.onPageResult(key, resultData);
return FlutterBoost.singleton.onPageResult(key, resultData, params);
}
}
......@@ -51,11 +51,12 @@ class FlutterBoost {
final GlobalKey<ContainerManagerState> containerManagerKey =
GlobalKey<ContainerManagerState>();
final Router _router = Router();
final ObserversHolder _observersHolder = ObserversHolder();
final PageResultMediator _resultMediator = PageResultMediator();
final Router _router = Router();
FlutterBoost() {
_router.resultMediator = _resultMediator;
ServiceLoader.load();
}
......@@ -140,10 +141,16 @@ class FlutterBoost {
}
}
bool onPageResult(String key, Map<String, dynamic> resultData) {
containerManager?.containerStateOf(key)?.performOnResult(resultData);
_resultMediator.onPageResult(key, resultData);
bool onPageResult(String key, Map resultData, Map params) {
if(_resultMediator.isResultId(key)){
_resultMediator.onPageResult(key, resultData,params);
}else{
containerManager?.containerStateOf(key)?.performOnResult(resultData);
}
return true;
}
VoidCallback setPageResultHandler(String key, PageResultHandler handler) {
......
......@@ -22,14 +22,27 @@
* THE SOFTWARE.
*/
import 'package:flutter_boost/support/logger.dart';
import 'package:flutter_boost/AIOService/NavigationService/service/NavigationService.dart';
typedef void PageResultHandler(String key , Map<dynamic,dynamic> result);
typedef void PageResultHandler(String key , Map<String,dynamic> result);
typedef VoidCallback = void Function();
class PageResultMediator{
static int _resultId = 0;
String createResultId(){
_resultId++;
return "result_id_$_resultId";
}
bool isResultId(String rid){
if(rid == null) return false;
return rid.contains("result_id");
}
Map<String,PageResultHandler> _handlers = Map();
void onPageResult(String key , Map<dynamic,dynamic> resultData){
void onPageResult(String key , Map<String,dynamic> resultData, Map params){
if(key == null) return;
......@@ -38,6 +51,22 @@ class PageResultMediator{
if(_handlers.containsKey(key)){
_handlers[key](key,resultData);
_handlers.remove(key);
}else{
//Cannot find handler consider forward to native.
//Use forward to avoid circle.
if(params == null || !params.containsKey("forward")){
if(params == null){
params = new Map();
}
params["forward"] = 1;
NavigationService.onFlutterPageResult(key, key , resultData, params);
}else{
params["forward"] = params["forward"]+1;
if(params["forward"] <= 2){
NavigationService.onFlutterPageResult(key, key , resultData, params);
}
}
}
}
......@@ -49,4 +78,4 @@ class PageResultMediator{
};
}
}
\ No newline at end of file
}
......@@ -31,6 +31,8 @@ import 'package:flutter_boost/support/logger.dart';
class Router {
MessageProxy _msgProxy = MessageProxyImp();
PageResultMediator resultMediator = null;
void setMessageProxy(MessageProxy prx) {
if (prx != null) {
......@@ -38,11 +40,13 @@ class Router {
}
}
Future<bool> openPage(String url, Map params,
{bool animated = true, PageResultHandler resultHandler}) {
if (resultHandler != null) {
params["needResult"] = true;
FlutterBoost.singleton.setPageResultHandler(url,
String rid = resultMediator.createResultId();
params["result_id"] = rid;
FlutterBoost.singleton.setPageResultHandler(rid,
(String key, Map<dynamic, dynamic> result) {
Logger.log("Recieved result $result for from page key $key");
if (resultHandler != null) {
......
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