mop-flutter-sdk/ios/Classes/MopPlugin.m

89 lines
3.0 KiB
Dart
Raw Normal View History

2020-02-27 17:21:45 +08:00
#import "MopPlugin.h"
#import "MOPBaseApi.h"
#import "MOPApiRequest.h"
#import "MOPApiConverter.h"
@implementation MopEventStream {
FlutterEventSink _eventSink;
}
- (FlutterError *_Nullable)onListenWithArguments:(id _Nullable)arguments eventSink:(FlutterEventSink)events {
_eventSink = events;
return nil;
}
- (void)send:(NSString *)channel event:(NSString *)event body:(id)body {
if (_eventSink) {
NSDictionary *dictionary = @{@"channel": channel, @"event": event, @"body": body};
_eventSink(dictionary);
}
}
- (FlutterError *_Nullable)onCancelWithArguments:(id _Nullable)arguments {
_eventSink = nil;
return nil;
}
@end
@implementation MopPlugin
2020-04-26 14:57:08 +08:00
static MopPlugin *_instance;
2020-02-27 17:21:45 +08:00
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
FlutterMethodChannel* channel = [FlutterMethodChannel
methodChannelWithName:@"mop"
binaryMessenger:[registrar messenger]];
2020-04-26 14:57:08 +08:00
_instance = [[MopPlugin alloc] init];
[registrar addMethodCallDelegate:_instance channel:channel];
_instance.methodChannel = channel;
2020-02-27 17:21:45 +08:00
2020-04-26 14:57:08 +08:00
FlutterEventChannel *mopEventChannel = [FlutterEventChannel eventChannelWithName:@"plugins.mop.finogeeks.com/mop_event" binaryMessenger:[registrar messenger]];
_instance.mopEventStreamHandler = [[MopEventStream alloc] init];
[mopEventChannel setStreamHandler:_instance.mopEventStreamHandler];
2020-12-12 15:53:11 +08:00
FlutterMethodChannel* shareChannel = [FlutterMethodChannel
methodChannelWithName:@"plugins.finosprite.finogeeks.com/share"
binaryMessenger:[registrar messenger]];
[registrar addMethodCallDelegate:_instance channel:shareChannel];
_instance.shareMethodChannel = shareChannel;
2020-04-26 14:57:08 +08:00
}
2020-02-27 17:21:45 +08:00
2020-04-26 14:57:08 +08:00
+ (instancetype)instance{
return _instance;
2020-02-27 17:21:45 +08:00
}
2020-04-26 14:57:08 +08:00
2020-02-27 17:21:45 +08:00
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"getPlatformVersion" isEqualToString:call.method]) {
result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
} else {
MOPApiRequest* request = [[MOPApiRequest alloc] init];
request.command = call.method;
request.param = (NSDictionary*)call.arguments;
MOPBaseApi* api = [MOPApiConverter apiWithRequest: request];
if (api) {
[api setupApiWithSuccess:^(NSDictionary<NSString *,id> * _Nonnull data) {
result(@{@"retMsg":@"ok",@"success":@(YES),@"data": data});
} failure:^(id _Nullable error) {
if ([error isKindOfClass:[NSDictionary class]]) {
NSDictionary* dict = (NSDictionary*)error;
if (dict != nil) {
result(@{@"retMsg": dict ,@"success":@(NO)});
} else {
result(@{@"retMsg": @"其它错误" ,@"success":@(NO)});
}
} else {
result(@{@"retMsg": error ,@"success":@(NO)});
}
} cancel:^{
}];
} else {
result(FlutterMethodNotImplemented);
}
}
}
@end