Commit 79258918 authored by Jidong Chen's avatar Jidong Chen

broadcast

1,ios
2,dart
parent b1b34f72
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#import <Flutter/Flutter.h> #import <Flutter/Flutter.h>
#import "FLB2Platform.h" #import "FLB2Platform.h"
#import "FLBBroadcastor.h"
@interface FlutterBoostPlugin : NSObject<FlutterPlugin> @interface FlutterBoostPlugin : NSObject<FlutterPlugin>
...@@ -38,6 +39,14 @@ ...@@ -38,6 +39,14 @@
- (BOOL)isRunning; - (BOOL)isRunning;
- (FlutterViewController *)currentViewController; - (FlutterViewController *)currentViewController;
#pragma mark - broadcast event to/from flutter
- (void)sendEvent:(NSString *)eventName
arguments:(NSDictionary *)arguments
result:(FlutterResult)result;
- (FLBVoidCallback)addEventListener:(FLBEventListener)listner
forName:(NSString *)name;
#pragma mark - handing vc result. #pragma mark - handing vc result.
- (void)openPage:(NSString *)name - (void)openPage:(NSString *)name
params:(NSDictionary *)params params:(NSDictionary *)params
......
...@@ -47,14 +47,18 @@ ...@@ -47,14 +47,18 @@
methodChannelWithName:@"flutter_boost" methodChannelWithName:@"flutter_boost"
binaryMessenger:[registrar messenger]]; binaryMessenger:[registrar messenger]];
FlutterBoostPlugin* instance = [self.class sharedInstance]; FlutterBoostPlugin* instance = [self.class sharedInstance];
[instance registerHandlers];
instance.methodChannel = channel; instance.methodChannel = channel;
instance.broadcastor = [[FLBBroadcastor alloc] initWithMethodChannel:channel];
[registrar addMethodCallDelegate:instance channel:channel]; [registrar addMethodCallDelegate:instance channel:channel];
} }
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"getPlatformVersion" isEqualToString:call.method]) { if ([@"getPlatformVersion" isEqualToString:call.method]) {
result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]); result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
} else { } else if([@"__event__" isEqual: call.method]){
[_broadcastor handleMethodCall:call result:result];
}else{
FLBMessageImp *msg = FLBMessageImp.new; FLBMessageImp *msg = FLBMessageImp.new;
msg.name = call.method; msg.name = call.method;
msg.params = call.arguments; msg.params = call.arguments;
...@@ -94,7 +98,7 @@ ...@@ -94,7 +98,7 @@
{ {
if (self = [super init]) { if (self = [super init]) {
_resultMediator = [FLBResultMediator new]; _resultMediator = [FLBResultMediator new];
[self registerHandlers]; _dispatcher = FLBMessageDispather.new;
} }
return self; return self;
...@@ -172,4 +176,21 @@ ...@@ -172,4 +176,21 @@
[_resultMediator removeHandlerForKey:vcid]; [_resultMediator removeHandlerForKey:vcid];
} }
#pragma mark - broadcast event to/from flutter
- (void)sendEvent:(NSString *)eventName
arguments:(NSDictionary *)arguments
result:(FlutterResult)result
{
[_broadcastor sendEvent:eventName
arguments:arguments
result:result];
}
- (FLBVoidCallback)addEventListener:(FLBEventListener)listner
forName:(NSString *)name
{
[_broadcastor addEventListener:listner
forName:name];
}
@end @end
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
- (id<FLBAbstractFactory>)factory; - (id<FLBAbstractFactory>)factory;
@property (nonatomic,strong) FlutterMethodChannel *methodChannel; @property (nonatomic,strong) FlutterMethodChannel *methodChannel;
@property (nonatomic,strong) FLBBroadcastor *broadcastor;
@property (nonatomic,copy) NSString *fPageId; @property (nonatomic,copy) NSString *fPageId;
@property (nonatomic,copy) NSString *fPagename; @property (nonatomic,copy) NSString *fPagename;
@property (nonatomic,strong) NSDictionary *fParams; @property (nonatomic,strong) NSDictionary *fParams;
......
...@@ -55,6 +55,7 @@ ...@@ -55,6 +55,7 @@
pageName:args[@"pageName"] pageName:args[@"pageName"]
params:args[@"params"] params:args[@"params"]
animated:args[@"animated"]]; animated:args[@"animated"]];
return YES;
} }
- (NSString *)returnType - (NSString *)returnType
...@@ -62,4 +63,10 @@ ...@@ -62,4 +63,10 @@
return @"BOOL"; return @"BOOL";
} }
- (NSArray *)handledMessageNames
{
return @[@"closePage"];
}
@end @end
...@@ -43,8 +43,14 @@ ...@@ -43,8 +43,14 @@
{ {
NSDictionary *args = msg.params; NSDictionary *args = msg.params;
[self onCall:result uniqueId:args[@"uniqueId"] key:args[@"key"] resultData:args[@"resultData"] params:args[@"params"]]; [self onCall:result uniqueId:args[@"uniqueId"] key:args[@"key"] resultData:args[@"resultData"] params:args[@"params"]];
return YES;
} }
- (NSArray *)handledMessageNames
{
return @[@"onFlutterPageResult"];
}
- (NSString *)returnType - (NSString *)returnType
{ {
return @"BOOL"; return @"BOOL";
......
...@@ -42,12 +42,17 @@ ...@@ -42,12 +42,17 @@
{ {
NSDictionary *args = msg.params; NSDictionary *args = msg.params;
[self onCall:result newName:args[@"newName"] oldName:args[@"oldName"] params:args[@"params"]]; [self onCall:result newName:args[@"newName"] oldName:args[@"oldName"] params:args[@"params"]];
return YES;
} }
- (NSString *)returnType - (NSString *)returnType
{ {
return @"BOOL"; return @"BOOL";
} }
- (NSArray *)handledMessageNames
{
return @[@"onShownContainerChanged"];
}
@end @end
...@@ -48,6 +48,7 @@ ...@@ -48,6 +48,7 @@
NSDictionary *args = msg.params; NSDictionary *args = msg.params;
[self onCall:result pageName:args[@"pageName"] params:args[@"params"] animated:args[@"animated"]]; [self onCall:result pageName:args[@"pageName"] params:args[@"params"] animated:args[@"animated"]];
return YES;
} }
- (NSString *)returnType - (NSString *)returnType
...@@ -55,4 +56,9 @@ ...@@ -55,4 +56,9 @@
return @"BOOL"; return @"BOOL";
} }
- (NSArray *)handledMessageNames
{
return @[@"openPage"];
}
@end @end
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
{ {
NSDictionary *args = msg.params; NSDictionary *args = msg.params;
[self onCall:result params:args[@"params"]]; [self onCall:result params:args[@"params"]];
return YES;
} }
- (NSString *)returnType - (NSString *)returnType
...@@ -52,4 +53,9 @@ ...@@ -52,4 +53,9 @@
return @"NSDictionary *"; return @"NSDictionary *";
} }
- (NSArray *)handledMessageNames
{
return @[@"pageOnStart"];
}
@end @end
//
// FLBBroadcastor.h
// flutter_boost
//
// Created by Jidong Chen on 2019/6/13.
//
#import <Foundation/Foundation.h>
#import <Flutter/Flutter.h>
NS_ASSUME_NONNULL_BEGIN
typedef void (^FLBEventListener) (NSString *name ,
NSDictionary *arguments);
typedef void (^FLBVoidCallback)(void);
@interface FLBBroadcastor : NSObject
- (instancetype)initWithMethodChannel:(FlutterMethodChannel *)channel;
- (void)sendEvent:(NSString *)eventName
arguments:(NSDictionary *)arguments
result:(FlutterResult)result;
- (FLBVoidCallback)addEventListener:(FLBEventListener)listner
forName:(NSString *)name;
- (void)handleMethodCall:(FlutterMethodCall *)call
result:(FlutterResult)result;
@end
NS_ASSUME_NONNULL_END
//
// FLBBroadcastor.m
// flutter_boost
//
// Created by Jidong Chen on 2019/6/13.
//
#import "FLBBroadcastor.h"
@interface FLBBroadcastor()
@property (nonatomic,strong) FlutterMethodChannel *channel;
@property (nonatomic,strong) NSMutableDictionary *lists;
@end
@implementation FLBBroadcastor
- (instancetype)initWithMethodChannel:(FlutterMethodChannel *)channel
{
if (self = [super init]) {
_channel = channel;
_lists = NSMutableDictionary.new;
}
return self;
}
- (void)sendEvent:(NSString *)eventName
arguments:(NSDictionary *)arguments
result:(void (^)(id _Nonnull))result
{
if(!eventName) return;
NSMutableDictionary *msg = NSMutableDictionary.new;
msg[@"name"] = eventName;
msg[@"arguments"] = arguments;
[_channel invokeMethod:@"__event__"
arguments:msg
result:result];
}
- (FLBVoidCallback)addEventListener:(FLBEventListener)listner
forName:(NSString *)name
{
if(!name || !listner) return ^{};
NSMutableArray *list = _lists[name];
if(!list){
list = NSMutableArray.new;
_lists[name] = list;
}
[list addObject:listner];
return ^{
[list removeObject:listner];
};
}
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
{
if([call.method isEqual:@"__event__"]){
NSString *name = call.arguments[@"name"];
NSDictionary *arguments = call.arguments[@"arguments"];
if(name){
NSMutableArray *list = _lists[name];
if(list){
for(FLBEventListener l in list){
l(name,arguments);
}
}
}
}
}
@end
...@@ -44,7 +44,8 @@ ...@@ -44,7 +44,8 @@
{ {
if (msg) { if (msg) {
id<FLBMessageHandler> handler = _handlerMap[msg.name]; id<FLBMessageHandler> handler = _handlerMap[msg.name];
return [handler handle:msg result:result]; [handler handle:msg result:result];
return handler != nil;
}else{ }else{
return NO; return NO;
} }
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#import "FLBMessageHandlerImp.h" #import "FLBMessageHandlerImp.h"
#import "FLBMessageImp.h"
typedef void (^SendResult)(NSObject *result); typedef void (^SendResult)(NSObject *result);
...@@ -76,21 +77,28 @@ typedef void (^SendResult)(NSObject *result); ...@@ -76,21 +77,28 @@ typedef void (^SendResult)(NSObject *result);
#pragma clang diagnostic push #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks" #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
SEL method = @selector(call:result:); SEL method = @selector(call:result:);
if (_callHandlers[NSStringFromSelector(method)]) {
return;
}
for(NSString *name in self.handledMessageNames){
if (_callHandlers[name]) {
continue;
}
__weak typeof(self) weakSelf = self; __weak typeof(self) weakSelf = self;
_callHandlers[NSStringFromSelector(method)] = ^(NSDictionary *args,SendResult result){ _callHandlers[name] = ^(NSDictionary *args,SendResult result){
id resultBlock = [weakSelf getHandlerBlockForType:weakSelf.returnType result:result]; id resultBlock = [weakSelf getHandlerBlockForType:weakSelf.returnType result:result];
if (resultBlock && result) { if (resultBlock && result) {
[weakSelf performSelector:method withObject:args withObject:resultBlock]; FLBMessageImp *msg = FLBMessageImp.new;
msg.name = name;
msg.params = args;
[weakSelf performSelector:method withObject:msg withObject:resultBlock];
}else{ }else{
#if DEBUG #if DEBUG
[NSException raise:@"invalid call" format:@"missing handler and result!"]; [NSException raise:@"invalid call" format:@"missing handler and result!"];
#endif #endif
} }
}; };
}
#pragma clang diagnostic pop #pragma clang diagnostic pop
} }
......
...@@ -21,6 +21,7 @@ A new Flutter plugin make flutter better to use! ...@@ -21,6 +21,7 @@ A new Flutter plugin make flutter better to use!
'Classes/Boost/FLB2Platform.h', 'Classes/Boost/FLB2Platform.h',
'Classes/Boost/FLBFlutterContainer.h', 'Classes/Boost/FLBFlutterContainer.h',
'Classes/Boost/FLBFlutterAppDelegate.h', 'Classes/Boost/FLBFlutterAppDelegate.h',
'Classes/Boost/FLBBroadcastor.h',
'Classes/1.0/FLBFlutterViewContainer.h', 'Classes/1.0/FLBFlutterViewContainer.h',
'Classes/1.5/FLB2FlutterViewContainer.h' 'Classes/1.5/FLB2FlutterViewContainer.h'
......
...@@ -39,6 +39,7 @@ import 'messaging/handlers/on_native_page_result_handler.dart'; ...@@ -39,6 +39,7 @@ import 'messaging/handlers/on_native_page_result_handler.dart';
import 'messaging/handlers/will_dealloc_page_container_handler.dart'; import 'messaging/handlers/will_dealloc_page_container_handler.dart';
import 'messaging/handlers/will_show_page_container_handler.dart'; import 'messaging/handlers/will_show_page_container_handler.dart';
import 'messaging/handlers/will_disappear_page_container_handler.dart'; import 'messaging/handlers/will_disappear_page_container_handler.dart';
import 'messaging/base/broadcastor.dart';
import 'observers_holders.dart'; import 'observers_holders.dart';
export 'container/boost_container.dart'; export 'container/boost_container.dart';
...@@ -64,6 +65,7 @@ class FlutterBoost { ...@@ -64,6 +65,7 @@ class FlutterBoost {
final MethodChannel _methodChannel = MethodChannel('flutter_boost'); final MethodChannel _methodChannel = MethodChannel('flutter_boost');
final MessageDispatcher _dispatcher = MessageDispatcher(); final MessageDispatcher _dispatcher = MessageDispatcher();
final Broadcastor _broadcastor = Broadcastor(_methodChannel);
FlutterBoost() { FlutterBoost() {
_router.resultMediator = _resultMediator; _router.resultMediator = _resultMediator;
...@@ -78,7 +80,12 @@ class FlutterBoost { ...@@ -78,7 +80,12 @@ class FlutterBoost {
_dispatcher.registerHandler(WillShowPageContainerHandler()); _dispatcher.registerHandler(WillShowPageContainerHandler());
_dispatcher.registerHandler(WillDisappearPageContainerHandler()); _dispatcher.registerHandler(WillDisappearPageContainerHandler());
_methodChannel.setMethodCallHandler((MethodCall call){ _methodChannel.setMethodCallHandler((MethodCall call){
_dispatcher.dispatch(call); if(call.method == "__event__"){
//Handler broadcast event.
return _broadcastor.handleCall(call);
}else{
return _dispatcher.dispatch(call);
}
}); });
} }
......
import 'package:flutter/services.dart';
typedef void VoidCallback();
typedef Future<dynamic> EventListener(String name , Map arguments);
class Broadcastor{
MethodChannel _channel;
Map<String,List<EventListener>> _lists = Map();
Broadcastor(MethodChannel channel){
_channel = channel;
}
void sendEvent(String name , Map arguments){
if(name == null) {
return;
}
if(arguments == null){
arguments = Map();
}
Map msg = Map();
msg["name"] = name;
msg["arguments"] = arguments;
_channel.invokeMethod("__event__",msg);
}
VoidCallback addEventListener(String name , EventListener listener){
if(name == null || listener == null){
return (){};
}
List<EventListener> list = _lists[name];
if(list == null){
list = List();
_lists[name] = list;
}
list.add(listener);
return (){
list.remove(listener);
};
}
Future<dynamic> handleCall(MethodCall call){
if(!_lists.containsKey(call.method) || call.method != "__event__"){
return Future<dynamic>();
}
String name = call.arguments["name"];
String arg = call.arguments["arguments"];
List<EventListener> list = _lists[call.method];
for(EventListener l in list){
l(name,arg);
}
return Future<dynamic>();
}
}
\ No newline at end of file
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