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

246 lines
9.7 KiB
Dart
Raw Permalink Normal View History

2020-02-27 17:21:45 +08:00
#import "MopPlugin.h"
#import "MOPBaseApi.h"
#import "MOPApiRequest.h"
#import "MOPApiConverter.h"
2022-05-19 14:55:09 +08:00
#import "MOPAppletDelegate.h"
2023-01-03 22:13:59 +08:00
#import <mop/MOPTools.h>
#import "MopShareView.h"
#import <UIView+MOPFATToast.h>
2020-02-27 17:21:45 +08:00
@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];
2021-01-08 15:03:14 +08:00
[registrar addApplicationDelegate:_instance];
2020-04-26 14:57:08 +08:00
[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;
2021-12-31 18:28:14 +08:00
FlutterMethodChannel* appletChannel = [FlutterMethodChannel
methodChannelWithName:@"plugins.finosprite.finogeeks.com/applet"
binaryMessenger:[registrar messenger]];
[registrar addMethodCallDelegate:_instance channel:appletChannel];
_instance.appletMethodChannel = appletChannel;
2023-01-03 22:13:59 +08:00
FlutterMethodChannel* appletShareChannel = [FlutterMethodChannel
methodChannelWithName:@"plugins.finosprite.finogeeks.com/share_applet"
binaryMessenger:[registrar messenger]];
[registrar addMethodCallDelegate:_instance channel:appletShareChannel];
2023-02-09 11:03:50 +08:00
_instance.shareAppletMethodChannel = appletShareChannel;
2023-12-06 13:47:43 +08:00
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]]);
2021-12-31 18:28:14 +08:00
}
else if ([@"getAppletInfo" isEqualToString:call.method]) {
result([self appInfoDictWithAppId:call.arguments[@"appId"]]);
}
2022-02-28 10:22:46 +08:00
else if ([@"getAbsolutePath" isEqualToString:call.method]) {
NSString *path = call.arguments[@"path"];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[@"path"] = [[FATClient sharedClient] fat_absolutePathWithPath:path];
result(dict);
}
2022-12-23 17:20:25 +08:00
else if ([@"copyFileAsFinFile" isEqualToString:call.method]) {
NSString *path = call.arguments[@"path"];
NSString *fileName = [path componentsSeparatedByString:@"/"].lastObject;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[@"path"] = [[FATClient sharedClient] saveFile:[NSData dataWithContentsOfFile:path] fileName:fileName];
result(dict);
}
2023-01-03 22:13:59 +08:00
else if ([@"showShareAppletDialog" isEqualToString:call.method]) {
2023-01-08 22:08:24 +08:00
UIImage *image = [[FATClient sharedClient] getDefaultCurrentAppletImage:440.0f];
2023-01-03 22:13:59 +08:00
MopShareView *view = [MopShareView viewWithData:call.arguments];
view.image = image;
[view show];
[view setDidSelcetTypeBlock:^(NSString *type) {
result(type);
}];
}
else if ([@"showLoading" isEqualToString:call.method]) {
UIViewController *currentVC = [MOPTools topViewController];
[currentVC.view fatMakeToastActivity:CSToastPositionCenter];
result([self appInfoDictWithAppId:call.arguments[@"appId"]]);
}
else if ([@"hideLoading" isEqualToString:call.method]) {
UIViewController *currentVC = [MOPTools topViewController];
[currentVC.view fatHideToastActivity];
[currentVC.view fatHideAllToasts];
result([self appInfoDictWithAppId:call.arguments[@"appId"]]);
}
else if ([@"showToast" isEqualToString:call.method]) {
UIViewController *currentVC = [MOPTools topViewController];
[currentVC.view fatMakeToast:call.arguments[@"msg"]
duration:2.0
position:CSToastPositionCenter];
result([self appInfoDictWithAppId:call.arguments[@"appId"]]);
}
else if ([@"getScreenshot" isEqualToString:call.method]) {
2023-01-08 22:08:24 +08:00
UIImage *image = [[FATClient sharedClient] getDefaultCurrentAppletImage:0.0f];
2023-01-04 22:03:44 +08:00
NSString *filePtah = [[FATClient sharedClient] saveFile:UIImagePNGRepresentation(image) fileName:[NSString stringWithFormat:@"%@",call.arguments[@"appId"]]];
filePtah = [[FATClient sharedClient] fat_absolutePathWithPath:filePtah];
result(filePtah);
2023-01-03 22:13:59 +08:00
}
2022-05-19 14:55:09 +08:00
else if ([@"getPhoneNumberResult" isEqualToString:call.method]) {
2022-10-11 12:54:57 +08:00
if ([MOPAppletDelegate instance].bindGetPhoneNumbers) {
[MOPAppletDelegate instance].bindGetPhoneNumbers(call.arguments);
// NSDictionary *dic = [[NSDictionary alloc] initWithDictionary:call.arguments];
// NSString *jsonString = [dic[@"phone"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
// NSError *error;
// NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
// [MOPAppletDelegate instance].bindGetPhoneNumbers(jsonDic);
2022-05-19 14:55:09 +08:00
}
}
2021-12-31 18:28:14 +08:00
else {
2020-02-27 17:21:45 +08:00
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) {
2022-12-22 14:11:16 +08:00
result(@{@"retMsg":@"ok",@"success":@(YES),@"data": data ? : @{}});
2020-02-27 17:21:45 +08:00
} 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);
}
}
}
2021-03-05 13:12:29 +08:00
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *, id> *)options
{
2022-01-10 17:44:32 +08:00
NSString *string = url.absoluteString;
if ([string containsString:@"finclipWebview/url="]) {
if (![FATClient sharedClient].inited) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
FlutterMethodChannel *channel = [[MopPlugin instance] shareMethodChannel];
[channel invokeMethod:@"shareApi:openURL" arguments:@{@"url":string} result:^(id _Nullable result) {
}];
});
}
else {
FlutterMethodChannel *channel = [[MopPlugin instance] shareMethodChannel];
[channel invokeMethod:@"shareApi:openURL" arguments:@{@"url":string} result:^(id _Nullable result) {
}];
}
2022-01-10 11:32:47 +08:00
2022-01-10 17:44:32 +08:00
return YES;
}
2022-01-10 11:32:47 +08:00
if (![FATClient sharedClient].inited) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[FATClient sharedClient] handleOpenURL:url];
});
}
2021-01-08 15:03:14 +08:00
return [[FATClient sharedClient] handleOpenURL:url];
}
- (BOOL)application:(UIApplication*)application
continueUserActivity:(NSUserActivity*)userActivity
restorationHandler:(void (^)(NSArray*))restorationHandler
2021-03-05 13:12:29 +08:00
{
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
NSURL *url = userActivity.webpageURL;
NSLog(@"url = %@",url.absoluteString);
return [[FATClient sharedClient] handleOpenUniversalLinkURL:url];
2021-03-05 13:12:29 +08:00
}
return YES;
}
2021-12-31 18:28:14 +08:00
- (NSDictionary *)appInfoDictWithAppId:(NSString *)appId {
FATAppletInfo *info = [[FATClient sharedClient] currentApplet];
if ([appId isEqualToString:info.appId]) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[@"appId"] = info.appId;
switch (info.appletVersionType) {
case FATAppletVersionTypeRelease: {
dict[@"appType"] = @"release";
break;
}
case FATAppletVersionTypeTrial: {
dict[@"appType"] = @"trial";
break;
}
case FATAppletVersionTypeTemporary: {
dict[@"appType"] = @"temporary";
break;
}
case FATAppletVersionTypeReview: {
dict[@"appType"] = @"review";
break;
}
case FATAppletVersionTypeDevelopment: {
dict[@"appType"] = @"development";
break;
}
default:
break;
}
return dict;
}
return nil;;
}
2023-01-03 22:13:59 +08:00
2020-02-27 17:21:45 +08:00
@end