Commit 73accf5a authored by Jidong Chen's avatar Jidong Chen Committed by GitHub

Merge pull request #161 from alibaba/result_mediator

Result mediator
parents 7aed07fe 6433ecc8
...@@ -56,6 +56,7 @@ import io.flutter.view.FlutterView; ...@@ -56,6 +56,7 @@ import io.flutter.view.FlutterView;
public class FlutterBoostPlugin implements MethodChannel.MethodCallHandler, Application.ActivityLifecycleCallbacks { public class FlutterBoostPlugin implements MethodChannel.MethodCallHandler, Application.ActivityLifecycleCallbacks {
private static FlutterBoostPlugin sInstance = null; private static FlutterBoostPlugin sInstance = null;
private static int kRid = 0;
public static synchronized void init(IPlatform platform) { public static synchronized void init(IPlatform platform) {
if (sInstance == null) { if (sInstance == null) {
...@@ -148,54 +149,32 @@ public class FlutterBoostPlugin implements MethodChannel.MethodCallHandler, Appl ...@@ -148,54 +149,32 @@ public class FlutterBoostPlugin implements MethodChannel.MethodCallHandler, Appl
ctx = sInstance.mPlatform.getApplication(); ctx = sInstance.mPlatform.getApplication();
} }
//Handling page result. sInstance.mPlatform.startActivity(ctx, concatUrl(url, params), requestCode);
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 static void openPage(Context context, String url, final Map params, int requestCode,PageResultHandler handler) {
public void error(String var1, String var2, Object var3) {
//Doing nothing now.
}
@Override if(handler != null){
public void notImplemented() { String rid = createResultId();
//Doing nothing now. sInstance.mMediator.setHandler(rid,handler);
} params.put("result_id",rid);
}, "no use", key, resultData, params);
}
});
} }
sInstance.mPlatform.startActivity(ctx, concatUrl(url, params), requestCode); openPage(context,url,params,requestCode);
} }
private Boolean needResult(Map params) { private static String createResultId(){
kRid += 2;
if (params == null) return false; return "result_id_" + kRid;
final String key = "needResult";
if (params.containsKey(key)) {
if (params.get(key) instanceof Boolean) {
return (Boolean) params.get(key);
}
}
return false;
} }
public static void onPageResult(String key, Map resultData) { public static void onPageResult(String key , Map resultData, Map params){
if (sInstance == null) { if (sInstance == null) {
throw new RuntimeException("FlutterBoostPlugin not init yet!"); 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) { public static void setHandler(String key, PageResultHandler handler) {
......
...@@ -23,19 +23,69 @@ ...@@ -23,19 +23,69 @@
*/ */
package com.taobao.idlefish.flutterboost; package com.taobao.idlefish.flutterboost;
import com.taobao.idlefish.flutterboost.NavigationService.NavigationService;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import fleamarket.taobao.com.xservicekit.handler.MessageResult;
class PageResultMediator { class PageResultMediator {
private Map<String,PageResultHandler> _handlers = new HashMap<>(); 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(key == null) return;
if(_handlers.containsKey(key)){ if(_handlers.containsKey(key)){
_handlers.get(key).onResult(key,resultData); _handlers.get(key).onResult(key,resultData);
_handlers.remove(key); _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; ...@@ -6,7 +6,10 @@ import android.support.v7.app.AppCompatActivity;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import com.taobao.idlefish.flutterboost.FlutterBoostPlugin;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity implements View.OnClickListener { public class MainActivity extends AppCompatActivity implements View.OnClickListener {
...@@ -46,6 +49,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -46,6 +49,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
PageRouter.openPageByUrl(this, PageRouter.NATIVE_PAGE_URL); PageRouter.openPageByUrl(this, PageRouter.NATIVE_PAGE_URL);
} else if (v == mOpenFlutter) { } else if (v == mOpenFlutter) {
PageRouter.openPageByUrl(this, PageRouter.FLUTTER_PAGE_URL); PageRouter.openPageByUrl(this, PageRouter.FLUTTER_PAGE_URL);
FlutterBoostPlugin.onPageResult("result_id_100",new HashMap(),new HashMap());
} else if (v == mOpenFlutterFragment) { } else if (v == mOpenFlutterFragment) {
PageRouter.openPageByUrl(this, PageRouter.FLUTTER_FRAGMENT_PAGE_URL); 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 @@ ...@@ -29,11 +29,14 @@
if([params[@"present"] boolValue]){ if([params[@"present"] boolValue]){
FLBFlutterViewContainer *vc = FLBFlutterViewContainer.new; FLBFlutterViewContainer *vc = FLBFlutterViewContainer.new;
[vc setName:name params:params]; [vc setName:name params:params];
[self.navigationController presentViewController:vc animated:animated completion:^{}]; [self.navigationController presentViewController:vc animated:animated completion:^{
if(completion) completion(YES);
}];
}else{ }else{
FLBFlutterViewContainer *vc = FLBFlutterViewContainer.new; FLBFlutterViewContainer *vc = FLBFlutterViewContainer.new;
[vc setName:name params:params]; [vc setName:name params:params];
[self.navigationController pushViewController:vc animated:animated]; [self.navigationController pushViewController:vc animated:animated];
if(completion) completion(YES);
} }
} }
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#import "UIViewControllerDemo.h" #import "UIViewControllerDemo.h"
#import <Flutter/Flutter.h> #import <Flutter/Flutter.h>
#import "DemoRouter.h" #import "DemoRouter.h"
#import <flutter_boost/FlutterBoostPlugin.h>
@interface UIViewControllerDemo () @interface UIViewControllerDemo ()
...@@ -25,7 +25,11 @@ ...@@ -25,7 +25,11 @@
- (IBAction)pushFlutterPage:(id)sender { - (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 { - (IBAction)present:(id)sender {
......
...@@ -31,6 +31,7 @@ class _MyAppState extends State<MyApp> { ...@@ -31,6 +31,7 @@ class _MyAppState extends State<MyApp> {
}); });
FlutterBoost.handleOnStartPage(); FlutterBoost.handleOnStartPage();
} }
@override @override
......
...@@ -12,7 +12,11 @@ class FirstRouteWidget extends StatelessWidget { ...@@ -12,7 +12,11 @@ class FirstRouteWidget extends StatelessWidget {
child: RaisedButton( child: RaisedButton(
child: Text('Open second route'), child: Text('Open second route'),
onPressed: () { 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 { ...@@ -31,6 +35,13 @@ class SecondRouteWidget extends StatelessWidget {
child: RaisedButton( child: RaisedButton(
onPressed: () { onPressed: () {
// Navigate back to first route when tapped. // 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); FlutterBoost.singleton.closePageForContext(context);
}, },
child: Text('Go back!'), child: Text('Go back!'),
......
...@@ -30,7 +30,7 @@ typedef void (^FLBPageResultHandler)(NSString *, NSDictionary *); ...@@ -30,7 +30,7 @@ typedef void (^FLBPageResultHandler)(NSString *, NSDictionary *);
@interface FLBResultMediator : NSObject @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)setResultHandler:(FLBPageResultHandler)handler forKey:(NSString *)vcid;
- (void)removeHandlerForKey:(NSString *)vcid; - (void)removeHandlerForKey:(NSString *)vcid;
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
*/ */
#import "FLBResultMediator.h" #import "FLBResultMediator.h"
#import "Service_NavigationService.h"
@interface FLBResultMediator() @interface FLBResultMediator()
@property (nonatomic,strong) NSMutableDictionary *handlers; @property (nonatomic,strong) NSMutableDictionary *handlers;
...@@ -38,16 +39,46 @@ ...@@ -38,16 +39,46 @@
return self; return self;
} }
- (void)onResultForKey:(NSString *)vcId - (void)onResultForKey:(NSString *)rid
resultData:(NSDictionary *)resultData resultData:(NSDictionary *)resultData
params:(nonnull NSDictionary *)params
{ {
if(!vcId) return; if(!rid) return;
NSString *key = vcId; NSString *key = rid;
if(_handlers[key]){ if(_handlers[key]){
FLBPageResultHandler handler = _handlers[key]; FLBPageResultHandler handler = _handlers[key];
handler(key,resultData); handler(key,resultData);
[_handlers removeObjectForKey: key]; [_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 ...@@ -42,7 +42,17 @@ typedef FLBFlutterViewContainer * (^FLBPageBuilder)(NSString *name,NSDictionary
- (FlutterViewController *)currentViewController; - (FlutterViewController *)currentViewController;
#pragma mark - handing vc result. #pragma mark - handing vc result.
- (void)onResultForKey:(NSString *)vcId resultData:(NSDictionary *)resultData; - (void)openPage:(NSString *)name
- (void)setResultHandler:(void (^)(NSString *, NSDictionary *))handler forKey:(NSString *)vcid; 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; - (void)removeHandlerForKey:(NSString *)vcid;
@end @end
...@@ -86,9 +86,21 @@ ...@@ -86,9 +86,21 @@
return [[FLBFlutterApplication sharedApplication] flutterViewController]; 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 - (void)setResultHandler:(void (^)(NSString *, NSDictionary *))handler forKey:(NSString *)vcid
......
...@@ -34,7 +34,8 @@ ...@@ -34,7 +34,8 @@
{ {
//Add your handler code here! //Add your handler code here!
[FlutterBoostPlugin.sharedInstance onResultForKey:key [FlutterBoostPlugin.sharedInstance onResultForKey:key
resultData:resultData]; resultData:resultData
params:params];
} }
#pragma mark - Do not edit these method. #pragma mark - Do not edit these method.
......
...@@ -34,21 +34,6 @@ ...@@ -34,21 +34,6 @@
- (void)onCall:(void (^)(BOOL))result pageName:(NSString *)pageName params:(NSDictionary *)params animated:(NSNumber *)animated - (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 [[FLBFlutterApplication sharedApplication].platform openPage:pageName
params:params params:params
animated:animated.boolValue animated:animated.boolValue
...@@ -57,16 +42,6 @@ ...@@ -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. #pragma mark - Do not edit these method.
- (void)__flutter_p_handler_openPage:(NSDictionary *)args result:(void (^)(BOOL))result { - (void)__flutter_p_handler_openPage:(NSDictionary *)args result:(void (^)(BOOL))result {
[self onCall:result pageName:args[@"pageName"] params:args[@"params"] animated:args[@"animated"]]; [self onCall:result pageName:args[@"pageName"] params:args[@"params"] animated:args[@"animated"]];
......
...@@ -51,6 +51,6 @@ class NavigationService_onNativePageResult extends ServiceCallHandler { ...@@ -51,6 +51,6 @@ class NavigationService_onNativePageResult extends ServiceCallHandler {
//==============================================Do not edit code above! //==============================================Do not edit code above!
Future<bool> onCall(String uniqueId,String key,Map resultData,Map params) async{ 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 { ...@@ -51,11 +51,12 @@ class FlutterBoost {
final GlobalKey<ContainerManagerState> containerManagerKey = final GlobalKey<ContainerManagerState> containerManagerKey =
GlobalKey<ContainerManagerState>(); GlobalKey<ContainerManagerState>();
final Router _router = Router();
final ObserversHolder _observersHolder = ObserversHolder(); final ObserversHolder _observersHolder = ObserversHolder();
final PageResultMediator _resultMediator = PageResultMediator(); final PageResultMediator _resultMediator = PageResultMediator();
final Router _router = Router();
FlutterBoost() { FlutterBoost() {
_router.resultMediator = _resultMediator;
ServiceLoader.load(); ServiceLoader.load();
} }
...@@ -140,10 +141,16 @@ class FlutterBoost { ...@@ -140,10 +141,16 @@ class FlutterBoost {
} }
} }
bool onPageResult(String key, Map<String, dynamic> resultData) {
bool onPageResult(String key, Map resultData, Map params) {
if(_resultMediator.isResultId(key)){
_resultMediator.onPageResult(key, resultData,params);
}else{
containerManager?.containerStateOf(key)?.performOnResult(resultData); containerManager?.containerStateOf(key)?.performOnResult(resultData);
_resultMediator.onPageResult(key, resultData); }
return true; return true;
} }
VoidCallback setPageResultHandler(String key, PageResultHandler handler) { VoidCallback setPageResultHandler(String key, PageResultHandler handler) {
......
...@@ -22,14 +22,27 @@ ...@@ -22,14 +22,27 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
import 'package:flutter_boost/support/logger.dart'; 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<dynamic,dynamic> result);
typedef VoidCallback = void Function(); typedef VoidCallback = void Function();
class PageResultMediator{ 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(); Map<String,PageResultHandler> _handlers = Map();
void onPageResult(String key , Map<dynamic,dynamic> resultData){ void onPageResult(String key , Map<dynamic,dynamic> resultData, Map params){
if(key == null) return; if(key == null) return;
...@@ -38,6 +51,22 @@ class PageResultMediator{ ...@@ -38,6 +51,22 @@ class PageResultMediator{
if(_handlers.containsKey(key)){ if(_handlers.containsKey(key)){
_handlers[key](key,resultData); _handlers[key](key,resultData);
_handlers.remove(key); _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);
}
}
} }
} }
......
...@@ -31,6 +31,8 @@ import 'package:flutter_boost/support/logger.dart'; ...@@ -31,6 +31,8 @@ import 'package:flutter_boost/support/logger.dart';
class Router { class Router {
MessageProxy _msgProxy = MessageProxyImp(); MessageProxy _msgProxy = MessageProxyImp();
PageResultMediator resultMediator = null;
void setMessageProxy(MessageProxy prx) { void setMessageProxy(MessageProxy prx) {
if (prx != null) { if (prx != null) {
...@@ -38,11 +40,13 @@ class Router { ...@@ -38,11 +40,13 @@ class Router {
} }
} }
Future<bool> openPage(String url, Map params, Future<bool> openPage(String url, Map params,
{bool animated = true, PageResultHandler resultHandler}) { {bool animated = true, PageResultHandler resultHandler}) {
if (resultHandler != null) { if (resultHandler != null) {
params["needResult"] = true; String rid = resultMediator.createResultId();
FlutterBoost.singleton.setPageResultHandler(url, params["result_id"] = rid;
FlutterBoost.singleton.setPageResultHandler(rid,
(String key, Map<dynamic, dynamic> result) { (String key, Map<dynamic, dynamic> result) {
Logger.log("Recieved result $result for from page key $key"); Logger.log("Recieved result $result for from page key $key");
if (resultHandler != null) { 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