diff --git a/native/ios/Comm.xcodeproj/project.pbxproj b/native/ios/Comm.xcodeproj/project.pbxproj --- a/native/ios/Comm.xcodeproj/project.pbxproj +++ b/native/ios/Comm.xcodeproj/project.pbxproj @@ -203,6 +203,8 @@ CB38F2BE286C6C980010535C /* DeleteEntryMessageSpec.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DeleteEntryMessageSpec.h; path = PersistentStorageUtilities/MessageOperationsUtilities/MessageSpecs/DeleteEntryMessageSpec.h; sourceTree = ""; }; CB38F2BF286C6C980010535C /* UpdateRelationshipMessageSpec.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = UpdateRelationshipMessageSpec.h; path = PersistentStorageUtilities/MessageOperationsUtilities/MessageSpecs/UpdateRelationshipMessageSpec.h; sourceTree = ""; }; CB3C621327CE66540054F24C /* libEXSecureStore.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libEXSecureStore.a; sourceTree = BUILT_PRODUCTS_DIR; }; + CB7EF17B295C580500B17035 /* CommIOSNotificationsBridgeQueue.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = CommIOSNotificationsBridgeQueue.mm; path = Comm/CommIOSNotifications/CommIOSNotificationsBridgeQueue.mm; sourceTree = ""; }; + CB7EF17C295C580500B17035 /* CommIOSNotificationsBridgeQueue.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = CommIOSNotificationsBridgeQueue.h; path = Comm/CommIOSNotifications/CommIOSNotificationsBridgeQueue.h; sourceTree = ""; }; CB90951929531663002F2A7F /* CommIOSNotifications.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CommIOSNotifications.h; path = Comm/CommIOSNotifications/CommIOSNotifications.h; sourceTree = ""; }; CBDEC69928ED859600C17588 /* GlobalDBSingleton.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GlobalDBSingleton.h; sourceTree = ""; }; CBDEC69A28ED867000C17588 /* GlobalDBSingleton.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = GlobalDBSingleton.mm; path = Comm/GlobalDBSingleton.mm; sourceTree = ""; }; @@ -553,6 +555,8 @@ CB90951729531647002F2A7F /* CommIOSNotifications */ = { isa = PBXGroup; children = ( + CB7EF17C295C580500B17035 /* CommIOSNotificationsBridgeQueue.h */, + CB7EF17B295C580500B17035 /* CommIOSNotificationsBridgeQueue.mm */, CB90951929531663002F2A7F /* CommIOSNotifications.h */, ); name = CommIOSNotifications; diff --git a/native/ios/Comm/CommIOSNotifications/CommIOSNotificationsBridgeQueue.h b/native/ios/Comm/CommIOSNotifications/CommIOSNotificationsBridgeQueue.h new file mode 100644 --- /dev/null +++ b/native/ios/Comm/CommIOSNotifications/CommIOSNotificationsBridgeQueue.h @@ -0,0 +1,21 @@ +#import + +@interface CommIOSNotificationsBridgeQueue : NSObject + +@property BOOL jsIsReady; +@property NSDictionary *openedRemoteNotification; +@property NSDictionary *openedLocalNotification; + ++ (nonnull instancetype)sharedInstance; + +- (void)postAction:(NSDictionary *)action + withCompletionKey:(NSString *)completionKey + andCompletionHandler:(void (^)())completionHandler; +- (void)postNotification:(NSDictionary *)notifInfo; + +- (void)consumeActionsQueue:(void (^)(NSDictionary *))block; +- (void)consumeNotificationsQueue:(void (^)(NSDictionary *))block; + +- (void)completeAction:(NSString *)completionKey; + +@end diff --git a/native/ios/Comm/CommIOSNotifications/CommIOSNotificationsBridgeQueue.mm b/native/ios/Comm/CommIOSNotifications/CommIOSNotificationsBridgeQueue.mm new file mode 100644 --- /dev/null +++ b/native/ios/Comm/CommIOSNotifications/CommIOSNotificationsBridgeQueue.mm @@ -0,0 +1,94 @@ +#import "CommIOSNotificationsBridgeQueue.h" + +@implementation CommIOSNotificationsBridgeQueue + +NSMutableArray *commActionsQueue; +NSMutableArray *commNotificationsQueue; +NSMutableDictionary *commActionCompletionHandlers; + ++ (nonnull instancetype)sharedInstance { + static CommIOSNotificationsBridgeQueue *sharedInstance = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedInstance = [self new]; + }); + + return sharedInstance; +} + +- (instancetype)init { + commActionsQueue = [NSMutableArray new]; + commNotificationsQueue = [NSMutableArray new]; + commActionCompletionHandlers = [NSMutableDictionary new]; + self.jsIsReady = NO; + + return self; +} + +- (void)postNotification:(NSDictionary *)notifInfo { + if (!commNotificationsQueue) + return; + [commNotificationsQueue insertObject:notifInfo atIndex:0]; +} + +- (NSDictionary *)dequeueSingleNotification { + if (!commNotificationsQueue || commNotificationsQueue.count == 0) + return nil; + + NSDictionary *notifInfo = [commNotificationsQueue lastObject]; + [commNotificationsQueue removeLastObject]; + + return notifInfo; +} + +- (void)consumeNotificationsQueue:(void (^)(NSDictionary *))block { + NSDictionary *notifInfo; + + while ((notifInfo = [self dequeueSingleNotification]) != nil) { + block(notifInfo); + } + + commNotificationsQueue = nil; +} + +- (void)postAction:(NSDictionary *)action + withCompletionKey:(NSString *)completionKey + andCompletionHandler:(void (^)())completionHandler { + // store completion handler + commActionCompletionHandlers[completionKey] = completionHandler; + + if (!commActionsQueue) + return; + [commActionsQueue insertObject:action atIndex:0]; +} + +- (NSDictionary *)dequeueSingleAction { + if (!commActionsQueue || commActionsQueue.count == 0) + return nil; + + NSDictionary *action = [commActionsQueue lastObject]; + [commActionsQueue removeLastObject]; + + return action; +} + +- (void)consumeActionsQueue:(void (^)(NSDictionary *))block { + NSDictionary *lastActionInfo; + + while ((lastActionInfo = [self dequeueSingleAction]) != nil) { + block(lastActionInfo); + } + + commActionsQueue = nil; +} + +- (void)completeAction:(NSString *)completionKey { + void (^completionHandler)() = + (void (^)())[commActionCompletionHandlers valueForKey:completionKey]; + if (completionHandler) { + completionHandler(); + [commActionCompletionHandlers removeObjectForKey:completionKey]; + } +} + +@end