Skip to content

Commit

Permalink
Merge pull request #94 from ZoMinster/module_service_opt
Browse files Browse the repository at this point in the history
一些小修改
  • Loading branch information
SoXeon committed Sep 14, 2017
2 parents 6211166 + 6444998 commit ff7aef5
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 29 deletions.
2 changes: 1 addition & 1 deletion BeeHive/BHAnnotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
class BeeHive; char * k##name##_mod BeeHiveDATA(BeehiveMods) = ""#name"";

#define BeeHiveService(servicename,impl) \
class BeeHive;char * k##servicename##_service BeeHiveDATA(BeehiveServices) = "{ \""#servicename"\" : \""#impl"\"}";
class BeeHive; char * k##servicename##_service BeeHiveDATA(BeehiveServices) = "{ \""#servicename"\" : \""#impl"\"}";

@interface BHAnnotation : NSObject

Expand Down
27 changes: 18 additions & 9 deletions BeeHive/BHAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ typedef void (^BHNotificationCompletionHandler)();
@property (nonatomic, strong) NSError *notificationsError;
@property (nonatomic, strong) NSData *deviceToken;
@property (nonatomic, strong) NSDictionary *userInfo;
@property (nonatomic, weak) BHNotificationResultHandler notificationResultHander;
@property (nonatomic, copy) BHNotificationResultHandler notificationResultHander;
@property (nonatomic, strong) UILocalNotification *localNotification;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
@property (nonatomic, strong) UNNotification *notification;
@property (nonatomic, strong) UNNotificationResponse *notificationResponse;
@property (nonatomic, weak) BHNotificationPresentationOptionsHandler notificationPresentationOptionsHandler;
@property (nonatomic, weak) BHNotificationCompletionHandler notificationCompletionHandler;
@property (nonatomic, copy) BHNotificationPresentationOptionsHandler notificationPresentationOptionsHandler;
@property (nonatomic, copy) BHNotificationCompletionHandler notificationCompletionHandler;
@property (nonatomic, strong) UNUserNotificationCenter *center;
#endif

Expand All @@ -43,32 +43,41 @@ typedef void (^BHNotificationCompletionHandler)();
@interface BHOpenURLItem : NSObject

@property (nonatomic, strong) NSURL *openURL;
@property (nonatomic, strong) NSString *sourceApplication;
@property (nonatomic, copy) NSString *sourceApplication;
@property (nonatomic, strong) id annotation;
@property (nonatomic, strong) NSDictionary *options;

@end

typedef void (^shortcutItemCompletionHandler)(BOOL);
typedef void (^BHShortcutCompletionHandler)(BOOL);

@interface BHShortcutItem : NSObject

#if __IPHONE_OS_VERSION_MAX_ALLOWED > 80400
@property(nonatomic, strong) UIApplicationShortcutItem *shortcutItem;
@property(nonatomic, copy) shortcutItemCompletionHandler scompletionHandler;
@property(nonatomic, copy) BHShortcutCompletionHandler scompletionHandler;
#endif

@end


typedef void (^restorationHandler)(NSArray *);
typedef void (^BHUserActivityRestorationHandler)(NSArray *);

@interface BHUserActivityItem : NSObject

@property (nonatomic, strong) NSString *userActivityType;
@property (nonatomic, copy) NSString *userActivityType;
@property (nonatomic, strong) NSUserActivity *userActivity;
@property (nonatomic, strong) NSError *userActivityError;
@property (nonatomic, strong) restorationHandler restorationHandler;
@property (nonatomic, copy) BHUserActivityRestorationHandler restorationHandler;

@end

typedef void (^BHWatchReplyHandler)(NSDictionary *replyInfo);

@interface BHWatchItem : NSObject

@property (nonatomic, strong) NSDictionary *userInfo;
@property (nonatomic, copy) BHWatchReplyHandler replyHandler;

@end

Expand Down
34 changes: 25 additions & 9 deletions BeeHive/BHAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
#import <UserNotifications/UserNotifications.h>
#endif


#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
@interface BHAppDelegate () <UNUserNotificationCenterDelegate>
#else
@interface BHAppDelegate ()
#endif


@end

Expand All @@ -29,13 +33,17 @@ @implementation BHAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

[[BHModuleManager sharedManager] triggerEvent:BHMSetupEvent];
[[BHModuleManager sharedManager] triggerEvent:BHMInitEvent];

dispatch_async(dispatch_get_main_queue(), ^{
[[BHModuleManager sharedManager] triggerEvent:BHMSplashEvent];
});
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0f) {
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
}
#endif

#ifdef DEBUG
[[BHTimeProfiler sharedTimeProfiler] saveTimeProfileDataIntoFile:@"BeeHiveTimeProfiler"];
Expand All @@ -45,7 +53,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
}


#if __IPHONE_OS_VERSION_MAX_ALLOWED > 80400
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80400

-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
Expand Down Expand Up @@ -84,11 +92,12 @@ - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceAppl
{
[[BeeHive shareInstance].context.openURLItem setOpenURL:url];
[[BeeHive shareInstance].context.openURLItem setSourceApplication:sourceApplication];
[[BeeHive shareInstance].context.openURLItem setAnnotation:annotation];
[[BHModuleManager sharedManager] triggerEvent:BHMOpenURLEvent];
return YES;
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED > 80400
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80400
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{

Expand Down Expand Up @@ -136,18 +145,18 @@ - (void)application:(UIApplication *)application didReceiveLocalNotification:(UI
[[BHModuleManager sharedManager] triggerEvent:BHMDidReceiveLocalNotificationEvent];
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED > 80000
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
- (void)application:(UIApplication *)application didUpdateUserActivity:(NSUserActivity *)userActivity
{
if([UIDevice currentDevice].systemVersion.floatValue > 8.0f){
if([UIDevice currentDevice].systemVersion.floatValue >= 8.0f){
[[BeeHive shareInstance].context.userActivityItem setUserActivity: userActivity];
[[BHModuleManager sharedManager] triggerEvent:BHMDidUpdateUserActivityEvent];
}
}

- (void)application:(UIApplication *)application didFailToContinueUserActivityWithType:(NSString *)userActivityType error:(NSError *)error
{
if([UIDevice currentDevice].systemVersion.floatValue > 8.0f){
if([UIDevice currentDevice].systemVersion.floatValue >= 8.0f){
[[BeeHive shareInstance].context.userActivityItem setUserActivityType: userActivityType];
[[BeeHive shareInstance].context.userActivityItem setUserActivityError: error];
[[BHModuleManager sharedManager] triggerEvent:BHMDidFailToContinueUserActivityEvent];
Expand All @@ -156,7 +165,7 @@ - (void)application:(UIApplication *)application didFailToContinueUserActivityWi

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
if([UIDevice currentDevice].systemVersion.floatValue > 8.0f){
if([UIDevice currentDevice].systemVersion.floatValue >= 8.0f){
[[BeeHive shareInstance].context.userActivityItem setUserActivity: userActivity];
[[BeeHive shareInstance].context.userActivityItem setRestorationHandler: restorationHandler];
[[BHModuleManager sharedManager] triggerEvent:BHMContinueUserActivityEvent];
Expand All @@ -166,12 +175,19 @@ - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserAct

- (BOOL)application:(UIApplication *)application willContinueUserActivityWithType:(NSString *)userActivityType
{
if([UIDevice currentDevice].systemVersion.floatValue > 8.0f){
if([UIDevice currentDevice].systemVersion.floatValue >= 8.0f){
[[BeeHive shareInstance].context.userActivityItem setUserActivityType: userActivityType];
[[BHModuleManager sharedManager] triggerEvent:BHMWillContinueUserActivityEvent];
}
return YES;
}
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(nullable NSDictionary *)userInfo reply:(void(^)(NSDictionary * __nullable replyInfo))reply {
if([UIDevice currentDevice].systemVersion.floatValue >= 8.0f){
[BeeHive shareInstance].context.watchItem.userInfo = userInfo;
[BeeHive shareInstance].context.watchItem.replyHandler = reply;
[[BHModuleManager sharedManager] triggerEvent:BHMHandleWatchKitExtensionRequestEvent];
}
}
#endif
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
Expand Down
3 changes: 3 additions & 0 deletions BeeHive/BHContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ typedef enum
//user Activity Model
@property (nonatomic, strong) BHUserActivityItem *userActivityItem;

//watch Model
@property (nonatomic, strong) BHWatchItem *watchItem;

//custom param
@property (nonatomic, copy) NSDictionary *customParam;

Expand Down
1 change: 1 addition & 0 deletions BeeHive/BHModuleManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ typedef NS_ENUM(NSInteger, BHModuleEventType)
BHMContinueUserActivityEvent,
BHMDidFailToContinueUserActivityEvent,
BHMDidUpdateUserActivityEvent,
BHMHandleWatchKitExtensionRequestEvent,
BHMDidCustomEvent = 1000

};
Expand Down
53 changes: 43 additions & 10 deletions BeeHive/BHModuleManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define kModuleInfoNameKey @"moduleClass"
#define kModuleInfoLevelKey @"moduleLevel"
#define kModuleInfoPriorityKey @"modulePriority"
#define kModuleInfoHasInstantiatedKey @"moduleHasInstantiated"

static NSString *kSetupSelector = @"modSetUp:";
static NSString *kInitSelector = @"modInit:";
Expand All @@ -40,6 +41,7 @@
static NSString *kContinueUserActivitySelector = @"modContinueUserActivity:";
static NSString *kDidUpdateContinueUserActivitySelector = @"modDidUpdateContinueUserActivity:";
static NSString *kFailToContinueUserActivitySelector = @"modDidFailToContinueUserActivity:";
static NSString *kHandleWatchKitExtensionRequestSelector = @"modHandleWatchKitExtensionRequest:";
static NSString *kAppCustomSelector = @"modDidCustomEvent:";


Expand Down Expand Up @@ -80,10 +82,16 @@ - (void)loadLocalModules

NSDictionary *moduleList = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

NSArray *modulesArray = [moduleList objectForKey:kModuleArrayKey];

[self.BHModuleInfos addObjectsFromArray:modulesArray];

NSArray<NSDictionary *> *modulesArray = [moduleList objectForKey:kModuleArrayKey];
NSMutableDictionary<NSString *, NSNumber *> *moduleInfoByClass = @{}.mutableCopy;
[self.BHModuleInfos enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[moduleInfoByClass setObject:@1 forKey:[obj objectForKey:kModuleInfoNameKey]];
}];
[modulesArray enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (!moduleInfoByClass[[obj objectForKey:kModuleInfoNameKey]]) {
[self.BHModuleInfos addObject:obj];
}
}];
}

- (void)registerDynamicModule:(Class)moduleClass
Expand Down Expand Up @@ -149,18 +157,18 @@ - (void)registedAllModules
NSString *classStr = [module objectForKey:kModuleInfoNameKey];

Class moduleClass = NSClassFromString(classStr);

if (NSStringFromClass(moduleClass)) {
BOOL hasInstantiated = ((NSNumber *)[module objectForKey:kModuleInfoHasInstantiatedKey]).boolValue;
if (NSStringFromClass(moduleClass) && !hasInstantiated) {
id<BHModuleProtocol> moduleInstance = [[moduleClass alloc] init];
[tmpArray addObject:moduleInstance];
}

}];

[self.BHModules removeAllObjects];
// [self.BHModules removeAllObjects];

[self.BHModules addObjectsFromArray:tmpArray];
[self.BHModulesByEvent removeAllObjects];

[self registerAllSystemEvents];
}

Expand Down Expand Up @@ -260,6 +268,7 @@ - (void)addModuleFromObject:(id)object

id<BHModuleProtocol> moduleInstance = [[class alloc] init];
[self.BHModules addObject:moduleInstance];
[moduleInfo setObject:@(YES) forKey:kModuleInfoHasInstantiatedKey];
[self.BHModules sortUsingComparator:^NSComparisonResult(id<BHModuleProtocol> moduleInstance1, id<BHModuleProtocol> moduleInstance2) {
NSNumber *module1Level = @(BHModuleNormal);
NSNumber *module2Level = @(BHModuleNormal);
Expand Down Expand Up @@ -318,15 +327,38 @@ - (void)registerEvent:(NSInteger)eventType
return;
}
NSNumber *eventTypeNumber = @(eventType);
if (![self.BHSelectorByEvent.allKeys containsObject:eventTypeNumber]) {
if (!self.BHSelectorByEvent[eventTypeNumber]) {
[self.BHSelectorByEvent setObject:selectorStr forKey:eventTypeNumber];
}
if (![self.BHModulesByEvent.allKeys containsObject:eventTypeNumber]) {
if (!self.BHModulesByEvent[eventTypeNumber]) {
[self.BHModulesByEvent setObject:@[].mutableCopy forKey:eventTypeNumber];
}
NSMutableArray *eventModules = [self.BHModulesByEvent objectForKey:eventTypeNumber];
if (![eventModules containsObject:moduleInstance]) {
[eventModules addObject:moduleInstance];
[eventModules sortUsingComparator:^NSComparisonResult(id<BHModuleProtocol> moduleInstance1, id<BHModuleProtocol> moduleInstance2) {
NSNumber *module1Level = @(BHModuleNormal);
NSNumber *module2Level = @(BHModuleNormal);
if ([moduleInstance1 respondsToSelector:@selector(basicModuleLevel)]) {
module1Level = @(BHModuleBasic);
}
if ([moduleInstance2 respondsToSelector:@selector(basicModuleLevel)]) {
module2Level = @(BHModuleBasic);
}
if (module1Level.integerValue != module2Level.integerValue) {
return module1Level.integerValue > module2Level.integerValue;
} else {
NSInteger module1Priority = 0;
NSInteger module2Priority = 0;
if ([moduleInstance1 respondsToSelector:@selector(modulePriority)]) {
module1Priority = [moduleInstance1 modulePriority];
}
if ([moduleInstance2 respondsToSelector:@selector(modulePriority)]) {
module2Priority = [moduleInstance2 modulePriority];
}
return module1Priority < module2Priority;
}
}];
}
}

Expand Down Expand Up @@ -389,6 +421,7 @@ - (NSMutableArray *)BHModules
@(BHMDidUpdateUserActivityEvent):kDidUpdateContinueUserActivitySelector,

@(BHMQuickActionEvent):kQuickActionSelector,
@(BHMHandleWatchKitExtensionRequestEvent):kHandleWatchKitExtensionRequestSelector,
@(BHMDidCustomEvent):kAppCustomSelector,
}.mutableCopy;
}
Expand Down
2 changes: 2 additions & 0 deletions BeeHive/BHModuleProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,7 @@

- (void)modDidUpdateContinueUserActivity:(BHContext *)context;

- (void)modHandleWatchKitExtensionRequest:(BHContext *)context;

- (void)modDidCustomEvent:(BHContext *)context;
@end

0 comments on commit ff7aef5

Please sign in to comment.