Skip to content

Commit 3b741e3

Browse files
authored
Merge pull request #228 from Nolascoin/bugfix/issue-201
issue-201 possible fix
2 parents 47eda46 + cf3d31e commit 3b741e3

File tree

3 files changed

+64
-30
lines changed

3 files changed

+64
-30
lines changed

RCTAppleHealthKit/RCTAppleHealthKit+Queries.m

+16-11
Original file line numberDiff line numberDiff line change
@@ -1056,11 +1056,12 @@ - (void)setObserverForType:(HKSampleType *)sampleType
10561056
return;
10571057
}
10581058

1059-
[self sendEventWithName:successEvent body:@{}];
1059+
NSLog(@"Emitting event: %@", successEvent);
1060+
[self emitEventWithName:successEvent andPayload:@{}];
10601061

10611062
completionHandler();
10621063

1063-
NSLog(@"[HealthKit] New sample from Apple HealthKit processed - %@", type);
1064+
NSLog(@"[HealthKit] New sample from Apple HealthKit processed (dep) - %@ %@", type, successEvent);
10641065
}];
10651066

10661067

@@ -1077,7 +1078,7 @@ - (void)setObserverForType:(HKSampleType *)sampleType
10771078

10781079
[self.healthStore executeQuery:query];
10791080

1080-
[self sendEventWithName:successEvent body:@{}];
1081+
[self emitEventWithName:successEvent andPayload:@{}];
10811082
}];
10821083
}
10831084

@@ -1110,16 +1111,19 @@ - (void)setObserverForType:(HKSampleType *)sampleType
11101111

11111112
NSLog(@"[HealthKit] An error happened when receiving a new sample - %@", error.localizedDescription);
11121113
if(self.hasListeners) {
1113-
[self sendEventWithName:failureEvent body:@{}];
1114+
[self emitEventWithName:failureEvent andPayload:@{}];
11141115
}
11151116
return;
11161117
}
1118+
11171119
if(self.hasListeners) {
1118-
[self sendEventWithName:successEvent body:@{}];
1120+
[self emitEventWithName:successEvent andPayload:@{}];
1121+
} else {
1122+
NSLog(@"There is no listeners for %@", successEvent);
11191123
}
11201124
completionHandler();
11211125

1122-
NSLog(@"[HealthKit] New sample from Apple HealthKit processed - %@", type);
1126+
NSLog(@"[HealthKit] New sample from Apple HealthKit processed - %@ %@", type, successEvent);
11231127
}];
11241128

11251129

@@ -1132,15 +1136,16 @@ - (void)setObserverForType:(HKSampleType *)sampleType
11321136
if (error) {
11331137
NSLog(@"[HealthKit] An error happened when setting up background observer - %@", error.localizedDescription);
11341138
if(self.hasListeners) {
1135-
[self sendEventWithName:failureEvent body:@{}];
1139+
[self emitEventWithName:failureEvent andPayload:@{}];
11361140
}
11371141
return;
11381142
}
1139-
1143+
NSLog(@"[HealthKit] Background delivery enabled for %@", type);
11401144
[self.healthStore executeQuery:query];
1141-
if(self.hasListeners) {
1142-
[self sendEventWithName:successEvent body:@{}];
1143-
}
1145+
if(self.hasListeners) {
1146+
NSLog(@"[HealthKit] Background observer set up for %@", type);
1147+
[self emitEventWithName:successEvent andPayload:@{}];
1148+
}
11441149
}];
11451150
}
11461151

RCTAppleHealthKit/RCTAppleHealthKit.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
- (HKHealthStore *)_initializeHealthStore;
2424
- (void)isHealthKitAvailable:(RCTResponseSenderBlock)callback;
2525
- (void)initializeHealthKit:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
26-
- (void)checkPermission:(NSString *)input callback:(RCTResponseSenderBlock)callback;
2726
- (void)getModuleInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
2827
- (void)getAuthorizationStatus:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
2928
- (void)initializeBackgroundObservers:(RCTBridge *)bridge;
29+
- (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload;
3030

3131
@end

RCTAppleHealthKit/RCTAppleHealthKit.m

+47-18
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,35 @@
2727
#import <React/RCTBridgeModule.h>
2828
#import <React/RCTEventDispatcher.h>
2929

30-
RCTAppleHealthKit *shared;
3130

3231
@implementation RCTAppleHealthKit
3332

34-
@synthesize bridge = _bridge;
35-
3633
bool hasListeners;
3734

3835
RCT_EXPORT_MODULE();
3936

37+
38+
+ (id)allocWithZone:(NSZone *)zone {
39+
static RCTAppleHealthKit *sharedInstance = nil;
40+
static dispatch_once_t onceToken;
41+
dispatch_once(&onceToken, ^{
42+
sharedInstance = [super allocWithZone:zone];
43+
});
44+
return sharedInstance;
45+
}
46+
47+
+ (RCTCallableJSModules *)sharedJsModule {
48+
static RCTCallableJSModules *sharedJsModule = nil;
49+
static dispatch_once_t onceToken;
50+
dispatch_once(&onceToken, ^{
51+
sharedJsModule = [RCTCallableJSModules new];
52+
});
53+
return sharedJsModule;
54+
}
55+
4056
- (id) init
4157
{
42-
if (shared != nil) {
43-
return shared;
44-
}
45-
46-
self = [super init];
47-
shared = self;
48-
return self;
58+
return [super init];
4959
}
5060

5161
+ (BOOL)requiresMainQueueSetup
@@ -602,7 +612,7 @@ - (void)initializeHealthKit:(NSDictionary *)input callback:(RCTResponseSenderBlo
602612
[self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) {
603613
if (!success) {
604614
NSString *errMsg = [NSString stringWithFormat:@"Error with HealthKit authorization: %@", error];
605-
NSLog(errMsg);
615+
NSLog(@"%@", errMsg);
606616
callback(@[RCTMakeError(errMsg, nil, nil)]);
607617
return;
608618
} else {
@@ -681,8 +691,8 @@ - (void)getAuthorizationStatus:(NSDictionary *)input callback:(RCTResponseSender
681691
if(permissions != nil && [permissions objectForKey:@"read"] != nil && [permissions objectForKey:@"write"] != nil){
682692
NSArray* readPermsNamesArray = [permissions objectForKey:@"read"];
683693
NSArray* writePermsNamesArray = [permissions objectForKey:@"write"];
684-
readPermsArray = [self getReadPermsFromOptions:readPermsNamesArray];
685-
writePermsArray = [self getWritePermsFromOptions:writePermsNamesArray];
694+
readPermsArray = [[self getReadPermsFromOptions:readPermsNamesArray] allObjects];
695+
writePermsArray = [[self getWritePermsFromOptions:writePermsNamesArray] allObjects];
686696
} else {
687697
callback(@[RCTMakeError(@"permissions must be included in permissions object with read and write options", nil, nil)]);
688698
return;
@@ -715,10 +725,8 @@ - (void)getAuthorizationStatus:(NSDictionary *)input callback:(RCTResponseSender
715725
716726
This method must be called at the application:didFinishLaunchingWithOptions: method, in AppDelegate.m
717727
*/
718-
- (void)initializeBackgroundObservers:(RCTBridge *)bridge
728+
- (void)initializeBackgroundObservers:(RCTBridge *)bridge
719729
{
720-
NSLog(@"[HealthKit] Background observers will be added to the app");
721-
722730
[self _initializeHealthStore];
723731

724732
self.bridge = bridge;
@@ -770,14 +778,35 @@ - (void)initializeBackgroundObservers:(RCTBridge *)bridge
770778

771779
// Will be called when this module's first listener is added.
772780
-(void)startObserving {
781+
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
782+
for (NSString *notificationName in [self supportedEvents]) {
783+
[center addObserver:self
784+
selector:@selector(emitEventInternal:)
785+
name:notificationName
786+
object:nil];
787+
}
773788
self.hasListeners = YES;
774-
// Set up any upstream listeners or background tasks as necessary
789+
}
790+
791+
- (void)emitEventInternal:(NSNotification *)notification {
792+
if (self.hasListeners) {
793+
self.callableJSModules = [RCTAppleHealthKit sharedJsModule];
794+
[self.callableJSModules setBridge:self.bridge];
795+
[self sendEventWithName:notification.name
796+
body:notification.userInfo];
797+
}
798+
}
799+
800+
- (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload {
801+
[[NSNotificationCenter defaultCenter] postNotificationName:name
802+
object:self
803+
userInfo:payload];
775804
}
776805

777806
// Will be called when this module's last listener is removed, or on dealloc.
778807
-(void)stopObserving {
779808
self.hasListeners = NO;
780-
// Remove upstream listeners, stop unnecessary background tasks
809+
[[NSNotificationCenter defaultCenter] removeObserver:self];
781810
}
782811

783812
@end

0 commit comments

Comments
 (0)