Skip to content

Commit

Permalink
优化iOS中几个同步的代理方法
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghualei committed Nov 22, 2023
1 parent 6537760 commit ff00307
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 26 deletions.
23 changes: 23 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,26 @@ class _MyAppState extends State<MyApp> {

Mop.instance.registerExtensionApi('getUserProfile', getUserProfile);

Mop.instance.registerExtensionApi('pushNativePage', pushNativePage);

if (!mounted) return;
}

Future<Map<String, dynamic>> pushNativePage(dynamic params) async {
print(params);
Map<String, dynamic> result = {
"userInfo":{
"nickName" : "haley",
"avatarUrl" : "https://www.finclip.com",
"gender" : 1,
"country" : "China",
"province" : "Guangdong",
"city" : "shenzhen",
}
};
return Future.value(result);
}

Future<Map<String, dynamic>> getUserProfile(dynamic params) async {
Map<String, dynamic> result = {
"userInfo":{
Expand Down Expand Up @@ -262,6 +279,12 @@ class MyAppletHandler extends AppletHandler {
return Future.value(result);
}

// @override
// Future<Map<String, dynamic>> getUserInfo() {
// // TODO: implement getUserInfo
// throw UnimplementedError();
// }

@override
Future<void> onCustomMenuClick(String appId, String path, String menuId, String appInfo) {
// TODO: implement onCustomMenuClick
Expand Down
28 changes: 10 additions & 18 deletions ios/Classes/Api/MOPAppletDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,50 +37,42 @@ - (void)forwardAppletWithInfo:(NSDictionary *)contentInfo completion:(void (^)(F
if([result isKindOfClass:[FlutterError class]]|| [result isKindOfClass:[FlutterMethodNotImplemented class] ])
{
completion(FATExtensionCodeFailure,nil);
}else
{
} else {
completion(FATExtensionCodeSuccess,result);
}
}];
}

- (NSDictionary *)getUserInfoWithAppletInfo:(FATAppletInfo *)appletInfo {
NSLog(@"getUserInfoWithAppletInfo");
__block NSDictionary *userInfo;
FlutterMethodChannel *channel = [[MopPlugin instance] methodChannel];
[channel invokeMethod:@"extensionApi:getUserInfo" arguments:nil result:^(id _Nullable result) {
CFRunLoopStop(CFRunLoopGetMain());
userInfo = result;
}];
CFRunLoopRun();
return userInfo;
}

- (BOOL)appletInfo:(FATAppletInfo *)appletInfo didClickMoreBtnAtPath:(NSString *)path {
NSLog(@"appletInfo:didClickMoreBtnAtPath");
__block BOOL flag;
__block BOOL flag = NO;
FlutterMethodChannel *channel = [[MopPlugin instance] methodChannel];
[channel invokeMethod:@"extensionApi:customCapsuleMoreButtonClick" arguments:@{@"appId": appletInfo.appId} result:^(id _Nullable result) {
CFRunLoopStop(CFRunLoopGetMain());
if ([result isKindOfClass:[NSNumber class]]) {
flag = [result boolValue];
}
CFRunLoopStop(CFRunLoopGetMain());
}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
CFRunLoopStop(CFRunLoopGetMain());
});
CFRunLoopRun();

return flag;
}

- (NSArray<id<FATAppletMenuProtocol>> *)customMenusInApplet:(FATAppletInfo *)appletInfo atPath:(NSString *)path {
NSLog(@"customMenusInApplet");
__block NSArray *list;
__block NSArray *list = @[];
FlutterMethodChannel *channel = [[MopPlugin instance] methodChannel];
[channel invokeMethod:@"extensionApi:getCustomMenus" arguments:@{@"appId": appletInfo.appId} result:^(id _Nullable result) {
CFRunLoopStop(CFRunLoopGetMain());
if ([result isKindOfClass:[NSArray class]]) {
list = result;
}
}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
CFRunLoopStop(CFRunLoopGetMain());
});
CFRunLoopRun();

NSMutableArray *models = [NSMutableArray array];
Expand Down
16 changes: 16 additions & 0 deletions ios/Classes/Api/MOPButtonOpenTypeDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ + (instancetype)instance
return _instance;
}

- (BOOL)getUserInfoWithAppletInfo:(FATAppletInfo *)appletInfo bindGetUserInfo:(void (^)(NSDictionary *result))bindGetUserInfo
{
FlutterMethodChannel *channel = [[MopPlugin instance] methodChannel];
NSLog(@"getUserInfoWithAppletInfo:%@", channel);
[channel invokeMethod:@"extensionApi:getUserInfo" arguments:nil result:^(id _Nullable result) {
NSDictionary *userInfo;
if (![result isKindOfClass:[NSDictionary class]]) {
userInfo = @{@"errMsg":@"getUserInfo:fail return value format invalid"};
} else {
userInfo = result;
}
bindGetUserInfo(userInfo);
}];
return YES;
}

- (BOOL)getUserProfileWithAppletInfo:(FATAppletInfo *)appletInfo
bindGetUserProfile:(void (^)(NSDictionary *result))bindGetUserProfile
{
Expand Down
14 changes: 6 additions & 8 deletions ios/Classes/Api/MOP_registerExtensionApi.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ - (void)setupApiWithSuccess:(void (^)(NSDictionary<NSString *,id> * _Nonnull))su
{
NSLog(@"MOP_registerExtensionApi");
FlutterMethodChannel *channel = [[MopPlugin instance] methodChannel];
[[FATClient sharedClient] registerExtensionApi:self.name handle:^(id param, FATExtensionApiCallback callback) {
NSLog(@"invoke ExtensionApi:");
NSLog(@"%@",self.name);
NSLog(@"%@",param);
NSString* api = [@"extensionApi:" stringByAppendingString:self.name];
NSString *name = self.name;
[[FATClient sharedClient] registerExtensionApi:name handler:^(FATAppletInfo *appletInfo, id param, FATExtensionApiCallback callback) {
NSLog(@"channel:%@---invoke ExtensionApi:%@, param:%@", channel, name, param);
NSString *api = [@"extensionApi:" stringByAppendingString:name];
[channel invokeMethod:api arguments:param result:^(id _Nullable result) {
NSLog(@"extensionApi reslut:%@",result);
NSLog(@"extensionApi [%@] reslut:%@", name, result);
// 先判断是否flutter发生错误
// BOOL isFlutterError = [result isKindOfClass:[FlutterError class]] || result == FlutterMethodNotImplemented;
BOOL isValid = [result isKindOfClass:[NSDictionary class]];
if (!isValid) {
NSLog(@"extensionApi reslut is not NSDictionary");
Expand All @@ -34,7 +32,7 @@ - (void)setupApiWithSuccess:(void (^)(NSDictionary<NSString *,id> * _Nonnull))su
BOOL hasError = [[result allKeys] containsObject:@"errMsg"];
if (hasError) {
NSString *errMsg = result[@"errMsg"];
NSString *errPrefix = [NSString stringWithFormat:@"%@:fail", self.name];
NSString *errPrefix = [NSString stringWithFormat:@"%@:fail", name];
BOOL isFail = [errMsg hasPrefix:errPrefix];
if (isFail) {
NSLog(@"extensionApi reslut:fail");
Expand Down

0 comments on commit ff00307

Please sign in to comment.