From afb3b11a87425b38688564eedf5ff38f2e3e5be2 Mon Sep 17 00:00:00 2001 From: Roman Melnyk Date: Wed, 12 May 2021 14:02:31 +0300 Subject: [PATCH 1/4] #86 Fix singletone class initialisation. Handle bundle stob start observing issue. --- .../RNVoipPushNotificationManager.m | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m index d84e59a..8797825 100644 --- a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m +++ b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m @@ -30,27 +30,32 @@ @implementation RNVoipPushNotificationManager static NSString *_lastVoipToken = @""; static NSMutableDictionary *completionHandlers = nil; - // ===== // ===== RN Module Configure and Override ===== // ===== +-(instancetype) init { + return [[self class] sharedInstance]; +} -- (instancetype)init -{ - if (self = [super init]) { +-(instancetype) initPrivate { + self = [super init]; + if (self) { + // Initialization code here. _delayedEvents = [NSMutableArray array]; } return self; } -+ (id)allocWithZone:(NSZone *)zone { - static RNVoipPushNotificationManager *sharedInstance = nil; +// Singletone implementation based on https://stackoverflow.com/q/5720029/3686678 and https://stackoverflow.com/a/7035136/3686678 ++(instancetype) sharedInstance { + static RNVoipPushNotificationManager *sharedVoipPushManager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - sharedInstance = [super allocWithZone:zone]; + sharedVoipPushManager = [[self alloc] initPrivate]; }); - return sharedInstance; + + return sharedVoipPushManager; } // --- clean observer and completionHandlers when app close @@ -90,12 +95,18 @@ - (void)startObserving { _hasListeners = YES; if ([_delayedEvents count] > 0) { + // RCTLog(@"[RNVoipPushNotificationManager] sendEventWithName _hasListeners = %d name = %@", _hasListeners, RNVoipPushDidLoadWithEvents); [self sendEventWithName:RNVoipPushDidLoadWithEvents body:_delayedEvents]; } } - (void)stopObserving { +#ifdef DEBUG + // RCTLog(@"[RNVoipPushNotificationManager] skip stopObserving because in debug mode startObserving is not called on reload, but stopObserving is called before reload"); + return; +#endif + _hasListeners = NO; } @@ -107,6 +118,7 @@ - (void)stopObserving // --- send directly if has listeners, cache it otherwise - (void)sendEventWithNameWrapper:(NSString *)name body:(id)body { + // RCTLog(@"[RNVoipPushNotificationManager] sendEventWithNameWrapper _hasListeners = %d name = %@", _hasListeners, name); if (_hasListeners) { [self sendEventWithName:name body:body]; } else { @@ -126,7 +138,7 @@ + (void)voipRegistration #ifdef DEBUG RCTLog(@"[RNVoipPushNotificationManager] voipRegistration is already registered. return _lastVoipToken = %@", _lastVoipToken); #endif - RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager allocWithZone: nil]; + RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager sharedInstance]; [voipPushManager sendEventWithNameWrapper:RNVoipPushRemoteNotificationsRegisteredEvent body:_lastVoipToken]; } else { _isVoipRegistered = YES; @@ -164,7 +176,7 @@ + (void)didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSStr _lastVoipToken = [hexString copy]; - RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager allocWithZone: nil]; + RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager sharedInstance]; [voipPushManager sendEventWithNameWrapper:RNVoipPushRemoteNotificationsRegisteredEvent body:_lastVoipToken]; } @@ -175,7 +187,7 @@ + (void)didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSSt RCTLog(@"[RNVoipPushNotificationManager] didReceiveIncomingPushWithPayload payload.dictionaryPayload = %@, type = %@", payload.dictionaryPayload, type); #endif - RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager allocWithZone: nil]; + RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager sharedInstance]; [voipPushManager sendEventWithNameWrapper:RNVoipPushRemoteNotificationReceivedEvent body:payload.dictionaryPayload]; } From 27a11075d43e3f53eccbc22335618e96f6c361a7 Mon Sep 17 00:00:00 2001 From: Roman Melnyk Date: Wed, 19 May 2021 10:22:58 +0300 Subject: [PATCH 2/4] #86 Applying review changes. --- .../RNVoipPushNotificationManager.m | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m index 8797825..a1ba330 100644 --- a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m +++ b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m @@ -39,12 +39,8 @@ -(instancetype) init { } -(instancetype) initPrivate { - self = [super init]; - if (self) { - // Initialization code here. - _delayedEvents = [NSMutableArray array]; - } - return self; + _delayedEvents = [NSMutableArray array]; + return [super init]; } // Singletone implementation based on https://stackoverflow.com/q/5720029/3686678 and https://stackoverflow.com/a/7035136/3686678 @@ -95,18 +91,12 @@ - (void)startObserving { _hasListeners = YES; if ([_delayedEvents count] > 0) { - // RCTLog(@"[RNVoipPushNotificationManager] sendEventWithName _hasListeners = %d name = %@", _hasListeners, RNVoipPushDidLoadWithEvents); [self sendEventWithName:RNVoipPushDidLoadWithEvents body:_delayedEvents]; } } - (void)stopObserving { -#ifdef DEBUG - // RCTLog(@"[RNVoipPushNotificationManager] skip stopObserving because in debug mode startObserving is not called on reload, but stopObserving is called before reload"); - return; -#endif - _hasListeners = NO; } @@ -118,7 +108,6 @@ - (void)stopObserving // --- send directly if has listeners, cache it otherwise - (void)sendEventWithNameWrapper:(NSString *)name body:(id)body { - // RCTLog(@"[RNVoipPushNotificationManager] sendEventWithNameWrapper _hasListeners = %d name = %@", _hasListeners, name); if (_hasListeners) { [self sendEventWithName:name body:body]; } else { From b88f6f8a6d8d8575b6f03682cb471be0bbb1801b Mon Sep 17 00:00:00 2001 From: Roman Melnyk Date: Wed, 19 May 2021 11:13:50 +0300 Subject: [PATCH 3/4] #86 Applying review changes. --- .../RNVoipPushNotificationManager.m | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m index a1ba330..7a0dfa0 100644 --- a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m +++ b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m @@ -39,8 +39,12 @@ -(instancetype) init { } -(instancetype) initPrivate { - _delayedEvents = [NSMutableArray array]; - return [super init]; + self = [super init]; + if (self) { + _delayedEvents = [NSMutableArray array]; + } + + return self; } // Singletone implementation based on https://stackoverflow.com/q/5720029/3686678 and https://stackoverflow.com/a/7035136/3686678 From 5c8c40c1299d1381263952bec4b227b461cc2602 Mon Sep 17 00:00:00 2001 From: zxcpoiu Date: Wed, 19 May 2021 01:35:00 -0700 Subject: [PATCH 4/4] fix style to match current code base --- .../RNVoipPushNotificationManager.m | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m index 7a0dfa0..31256d2 100644 --- a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m +++ b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m @@ -34,13 +34,14 @@ @implementation RNVoipPushNotificationManager // ===== RN Module Configure and Override ===== // ===== --(instancetype) init { +-(instancetype) init +{ return [[self class] sharedInstance]; } --(instancetype) initPrivate { - self = [super init]; - if (self) { +-(instancetype) initPrivate +{ + if (self = [super init]) { _delayedEvents = [NSMutableArray array]; } @@ -48,7 +49,8 @@ -(instancetype) initPrivate { } // Singletone implementation based on https://stackoverflow.com/q/5720029/3686678 and https://stackoverflow.com/a/7035136/3686678 -+(instancetype) sharedInstance { ++(instancetype) sharedInstance +{ static RNVoipPushNotificationManager *sharedVoipPushManager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{