refactor(ios): 将 ShutoApi 实现移至头文件并改用属性
重构 iOS 端的 ShutoApi 实现,将类定义移至头文件 使用属性替代实例变量以提高代码可读性 移除冗余的 isIonicReady 方法
This commit is contained in:
@@ -22,6 +22,7 @@
|
|||||||
<param name="ios-package" value="ShutoApi" />
|
<param name="ios-package" value="ShutoApi" />
|
||||||
</feature>
|
</feature>
|
||||||
</config-file>
|
</config-file>
|
||||||
|
<source-file src="src/ios/ShutoApi.h" />
|
||||||
<source-file src="src/ios/ShutoApi.m" />
|
<source-file src="src/ios/ShutoApi.m" />
|
||||||
<source-file src="src/ios/ShutoEventManager.h" />
|
<source-file src="src/ios/ShutoEventManager.h" />
|
||||||
<source-file src="src/ios/ShutoEventManager.m" />
|
<source-file src="src/ios/ShutoEventManager.m" />
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#import <Cordova/CDV.h>
|
||||||
|
#import <WebKit/WebKit.h>
|
||||||
|
|
||||||
|
@protocol ShutoApiProtocol <NSObject>
|
||||||
|
- (void)fireEvent:(NSString*)type parameters:(NSDictionary*)parameters;
|
||||||
|
- (void)fireEventWithCallback:(NSString*)eventName parameters:(NSDictionary*)parameters callback:(void (^)(NSDictionary* result, NSError* error))callback;
|
||||||
|
@end
|
||||||
|
|
||||||
|
@interface ShutoApi : CDVPlugin <ShutoApiProtocol>
|
||||||
|
@property (nonatomic, copy) NSString* eventCallbackId;
|
||||||
|
@property (nonatomic, strong) NSDictionary* userInfo;
|
||||||
|
@property (nonatomic, strong) NSMutableDictionary* eventCallbacks;
|
||||||
|
@property (nonatomic, assign) BOOL isIonicReady;
|
||||||
|
|
||||||
|
- (void)close:(CDVInvokedUrlCommand*)command;
|
||||||
|
- (void)registerEvent:(CDVInvokedUrlCommand*)command;
|
||||||
|
- (void)getUserInfo:(CDVInvokedUrlCommand*)command;
|
||||||
|
- (void)setUserInfo:(NSDictionary*)userInfo;
|
||||||
|
- (void)navigateToRoute:(NSString*)route parameters:(NSDictionary*)parameters;
|
||||||
|
- (void)fireEvent:(NSString*)type parameters:(NSDictionary*)parameters;
|
||||||
|
- (void)fireEventWithCallback:(NSString*)eventName parameters:(NSDictionary*)parameters callback:(void (^)(NSDictionary* result, NSError* error))callback;
|
||||||
|
- (void)eventCallback:(CDVInvokedUrlCommand*)command;
|
||||||
|
- (void)ionicReady:(CDVInvokedUrlCommand*)command;
|
||||||
|
@end
|
||||||
+16
-50
@@ -1,42 +1,13 @@
|
|||||||
/********* ShutoApi.m Cordova Plugin Implementation *******/
|
/********* ShutoApi.m Cordova Plugin Implementation *******/
|
||||||
|
|
||||||
#import <Cordova/CDV.h>
|
#import "ShutoApi.h"
|
||||||
#import <WebKit/WebKit.h>
|
|
||||||
#import "SGGC_Log.h"
|
#import "SGGC_Log.h"
|
||||||
#import "ShutoEventManager.h"
|
#import "ShutoEventManager.h"
|
||||||
|
|
||||||
@protocol ShutoApiProtocol <NSObject>
|
|
||||||
- (void)fireEvent:(NSString*)type parameters:(NSDictionary*)parameters;
|
|
||||||
- (void)fireEventWithCallback:(NSString*)eventName parameters:(NSDictionary*)parameters callback:(void (^)(NSDictionary* result, NSError* error))callback;
|
|
||||||
@end
|
|
||||||
|
|
||||||
@interface ShutoApi : CDVPlugin <ShutoApiProtocol> {
|
|
||||||
// 保存事件回调ID
|
|
||||||
NSString* _eventCallbackId;
|
|
||||||
// 保存用户信息
|
|
||||||
NSDictionary* _userInfo;
|
|
||||||
// 保存带回调的事件回调ID映射
|
|
||||||
NSMutableDictionary* _eventCallbacks;
|
|
||||||
// 标识前端是否就绪
|
|
||||||
BOOL _isIonicReady;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)close:(CDVInvokedUrlCommand*)command;
|
|
||||||
- (void)registerEvent:(CDVInvokedUrlCommand*)command;
|
|
||||||
- (void)getUserInfo:(CDVInvokedUrlCommand*)command;
|
|
||||||
- (void)setUserInfo:(CDVInvokedUrlCommand*)command;
|
|
||||||
- (void)navigateToRoute:(NSString*)route parameters:(NSDictionary*)parameters;
|
|
||||||
- (void)fireEvent:(NSString*)type parameters:(NSDictionary*)parameters;
|
|
||||||
- (void)fireEventWithCallback:(NSString*)eventName parameters:(NSDictionary*)parameters callback:(void (^)(NSDictionary* result, NSError* error))callback;
|
|
||||||
- (void)eventCallback:(CDVInvokedUrlCommand*)command;
|
|
||||||
- (void)ionicReady:(CDVInvokedUrlCommand*)command;
|
|
||||||
- (BOOL)isIonicReady;
|
|
||||||
@end
|
|
||||||
|
|
||||||
@implementation ShutoApi
|
@implementation ShutoApi
|
||||||
|
|
||||||
- (void)pluginInitialize {
|
- (void)pluginInitialize {
|
||||||
_eventCallbacks = [[NSMutableDictionary alloc] init];
|
self.eventCallbacks = [[NSMutableDictionary alloc] init];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)close:(CDVInvokedUrlCommand*)command
|
- (void)close:(CDVInvokedUrlCommand*)command
|
||||||
@@ -81,7 +52,7 @@
|
|||||||
- (void)registerEvent:(CDVInvokedUrlCommand*)command
|
- (void)registerEvent:(CDVInvokedUrlCommand*)command
|
||||||
{
|
{
|
||||||
// 保存事件回调ID,以便后续发送事件
|
// 保存事件回调ID,以便后续发送事件
|
||||||
_eventCallbackId = command.callbackId;
|
self.eventCallbackId = command.callbackId;
|
||||||
|
|
||||||
// 返回插件结果,但保持回调通道打开
|
// 返回插件结果,但保持回调通道打开
|
||||||
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
||||||
@@ -93,8 +64,8 @@
|
|||||||
{
|
{
|
||||||
// 返回存储的用户信息
|
// 返回存储的用户信息
|
||||||
CDVPluginResult* pluginResult;
|
CDVPluginResult* pluginResult;
|
||||||
if (_userInfo) {
|
if (self.userInfo) {
|
||||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:_userInfo];
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:self.userInfo];
|
||||||
} else {
|
} else {
|
||||||
// 用户信息为空时返回空对象
|
// 用户信息为空时返回空对象
|
||||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:nil];
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:nil];
|
||||||
@@ -106,7 +77,7 @@
|
|||||||
{
|
{
|
||||||
// 设置用户信息
|
// 设置用户信息
|
||||||
if (userInfo) {
|
if (userInfo) {
|
||||||
_userInfo = userInfo;
|
self.userInfo = userInfo;
|
||||||
[SGGC_Log log:@"SGGC_CDVFile" message:(@"ShutoApi: User info set successfully: %@", userInfo)];
|
[SGGC_Log log:@"SGGC_CDVFile" message:(@"ShutoApi: User info set successfully: %@", userInfo)];
|
||||||
} else {
|
} else {
|
||||||
[SGGC_Log log:@"SGGC_CDVFile" message:(@"ShutoApi: Invalid user info provided")];
|
[SGGC_Log log:@"SGGC_CDVFile" message:(@"ShutoApi: Invalid user info provided")];
|
||||||
@@ -127,7 +98,7 @@
|
|||||||
|
|
||||||
// 触发通用事件
|
// 触发通用事件
|
||||||
- (void)fireEvent:(NSString*)type parameters:(NSDictionary*)parameters {
|
- (void)fireEvent:(NSString*)type parameters:(NSDictionary*)parameters {
|
||||||
if (!_eventCallbackId) {
|
if (!self.eventCallbackId) {
|
||||||
[SGGC_Log log:@"SGGC_CDVFile" message:(@"ShutoApi: No event callback registered")];
|
[SGGC_Log log:@"SGGC_CDVFile" message:(@"ShutoApi: No event callback registered")];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -144,14 +115,14 @@
|
|||||||
// 发送事件到前端
|
// 发送事件到前端
|
||||||
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:eventData];
|
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:eventData];
|
||||||
[pluginResult setKeepCallbackAsBool:YES];
|
[pluginResult setKeepCallbackAsBool:YES];
|
||||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:_eventCallbackId];
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.eventCallbackId];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 触发带回调的事件
|
// 触发带回调的事件
|
||||||
- (void)fireEventWithCallback:(NSString*)eventName parameters:(NSDictionary*)parameters callback:(void (^)(NSDictionary* result, NSError* error))callback {
|
- (void)fireEventWithCallback:(NSString*)eventName parameters:(NSDictionary*)parameters callback:(void (^)(NSDictionary* result, NSError* error))callback {
|
||||||
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: fireEventWithCallback called, eventName=%@, parameters=%@", eventName, parameters]];
|
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: fireEventWithCallback called, eventName=%@, parameters=%@", eventName, parameters]];
|
||||||
|
|
||||||
if (!_eventCallbackId) {
|
if (!self.eventCallbackId) {
|
||||||
[SGGC_Log log:@"SGGC_CDVFile" message:@"ShutoApi: No event callback registered"];
|
[SGGC_Log log:@"SGGC_CDVFile" message:@"ShutoApi: No event callback registered"];
|
||||||
if (callback) callback(nil, [NSError errorWithDomain:@"ShutoApi" code:-1 userInfo:@{NSLocalizedDescriptionKey:@"No event callback registered"}]);
|
if (callback) callback(nil, [NSError errorWithDomain:@"ShutoApi" code:-1 userInfo:@{NSLocalizedDescriptionKey:@"No event callback registered"}]);
|
||||||
return;
|
return;
|
||||||
@@ -161,8 +132,8 @@
|
|||||||
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: Generated callbackId=%@", callbackId]];
|
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: Generated callbackId=%@", callbackId]];
|
||||||
|
|
||||||
if (callback) {
|
if (callback) {
|
||||||
_eventCallbacks[callbackId] = callback;
|
self.eventCallbacks[callbackId] = callback;
|
||||||
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: Callback saved, _eventCallbacks count=%lu", (unsigned long)_eventCallbacks.count]];
|
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: Callback saved, eventCallbacks count=%lu", (unsigned long)self.eventCallbacks.count]];
|
||||||
}
|
}
|
||||||
|
|
||||||
NSMutableDictionary* eventData = [NSMutableDictionary dictionary];
|
NSMutableDictionary* eventData = [NSMutableDictionary dictionary];
|
||||||
@@ -177,7 +148,7 @@
|
|||||||
|
|
||||||
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:eventData];
|
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:eventData];
|
||||||
[pluginResult setKeepCallbackAsBool:YES];
|
[pluginResult setKeepCallbackAsBool:YES];
|
||||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:_eventCallbackId];
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.eventCallbackId];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理前端回调
|
// 处理前端回调
|
||||||
@@ -189,9 +160,9 @@
|
|||||||
NSString* errorMessage = command.arguments[2];
|
NSString* errorMessage = command.arguments[2];
|
||||||
|
|
||||||
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: callbackId=%@, result=%@, errorMessage=%@", callbackId, result, errorMessage]];
|
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: callbackId=%@, result=%@, errorMessage=%@", callbackId, result, errorMessage]];
|
||||||
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: _eventCallbacks count=%lu", (unsigned long)_eventCallbacks.count]];
|
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: eventCallbacks count=%lu", (unsigned long)self.eventCallbacks.count]];
|
||||||
|
|
||||||
void (^callback)(NSDictionary* result, NSError* error) = _eventCallbacks[callbackId];
|
void (^callback)(NSDictionary* result, NSError* error) = self.eventCallbacks[callbackId];
|
||||||
|
|
||||||
if (callback) {
|
if (callback) {
|
||||||
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: Found callback for callbackId=%@", callbackId]];
|
[SGGC_Log log:@"SGGC_CDVFile" message:[NSString stringWithFormat:@"ShutoApi: Found callback for callbackId=%@", callbackId]];
|
||||||
@@ -203,7 +174,7 @@
|
|||||||
|
|
||||||
|
|
||||||
callback(result, error);
|
callback(result, error);
|
||||||
[_eventCallbacks removeObjectForKey:callbackId];
|
[self.eventCallbacks removeObjectForKey:callbackId];
|
||||||
|
|
||||||
[SGGC_Log log:@"SGGC_CDVFile" message:@"ShutoApi: Callback executed and removed"];
|
[SGGC_Log log:@"SGGC_CDVFile" message:@"ShutoApi: Callback executed and removed"];
|
||||||
} else {
|
} else {
|
||||||
@@ -216,7 +187,7 @@
|
|||||||
|
|
||||||
- (void)ionicReady:(CDVInvokedUrlCommand*)command
|
- (void)ionicReady:(CDVInvokedUrlCommand*)command
|
||||||
{
|
{
|
||||||
_isIonicReady = YES;
|
self.isIonicReady = YES;
|
||||||
[SGGC_Log log:@"SGGC_CDVFile" message:@"ShutoApi: Ionic is ready"];
|
[SGGC_Log log:@"SGGC_CDVFile" message:@"ShutoApi: Ionic is ready"];
|
||||||
|
|
||||||
[[ShutoEventManager sharedInstance] fireEvent:@"ionicReady" data:nil];
|
[[ShutoEventManager sharedInstance] fireEvent:@"ionicReady" data:nil];
|
||||||
@@ -225,9 +196,4 @@
|
|||||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)isIonicReady
|
|
||||||
{
|
|
||||||
return _isIonicReady;
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
Reference in New Issue
Block a user