Skip to content

Commit

Permalink
Merge pull request #228 from Nolascoin/bugfix/issue-201
Browse files Browse the repository at this point in the history
issue-201 possible fix
  • Loading branch information
GGGava committed Jan 13, 2023
2 parents 47eda46 + cf3d31e commit 3b741e3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 30 deletions.
27 changes: 16 additions & 11 deletions RCTAppleHealthKit/RCTAppleHealthKit+Queries.m
Original file line number Diff line number Diff line change
Expand Up @@ -1056,11 +1056,12 @@ - (void)setObserverForType:(HKSampleType *)sampleType
return;
}

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

completionHandler();

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


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

[self.healthStore executeQuery:query];

[self sendEventWithName:successEvent body:@{}];
[self emitEventWithName:successEvent andPayload:@{}];
}];
}

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

NSLog(@"[HealthKit] An error happened when receiving a new sample - %@", error.localizedDescription);
if(self.hasListeners) {
[self sendEventWithName:failureEvent body:@{}];
[self emitEventWithName:failureEvent andPayload:@{}];
}
return;
}

if(self.hasListeners) {
[self sendEventWithName:successEvent body:@{}];
[self emitEventWithName:successEvent andPayload:@{}];
} else {
NSLog(@"There is no listeners for %@", successEvent);
}
completionHandler();

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


Expand All @@ -1132,15 +1136,16 @@ - (void)setObserverForType:(HKSampleType *)sampleType
if (error) {
NSLog(@"[HealthKit] An error happened when setting up background observer - %@", error.localizedDescription);
if(self.hasListeners) {
[self sendEventWithName:failureEvent body:@{}];
[self emitEventWithName:failureEvent andPayload:@{}];
}
return;
}

NSLog(@"[HealthKit] Background delivery enabled for %@", type);
[self.healthStore executeQuery:query];
if(self.hasListeners) {
[self sendEventWithName:successEvent body:@{}];
}
if(self.hasListeners) {
NSLog(@"[HealthKit] Background observer set up for %@", type);
[self emitEventWithName:successEvent andPayload:@{}];
}
}];
}

Expand Down
2 changes: 1 addition & 1 deletion RCTAppleHealthKit/RCTAppleHealthKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
- (HKHealthStore *)_initializeHealthStore;
- (void)isHealthKitAvailable:(RCTResponseSenderBlock)callback;
- (void)initializeHealthKit:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)checkPermission:(NSString *)input callback:(RCTResponseSenderBlock)callback;
- (void)getModuleInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)getAuthorizationStatus:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)initializeBackgroundObservers:(RCTBridge *)bridge;
- (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload;

@end
65 changes: 47 additions & 18 deletions RCTAppleHealthKit/RCTAppleHealthKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,35 @@
#import <React/RCTBridgeModule.h>
#import <React/RCTEventDispatcher.h>

RCTAppleHealthKit *shared;

@implementation RCTAppleHealthKit

@synthesize bridge = _bridge;

bool hasListeners;

RCT_EXPORT_MODULE();


+ (id)allocWithZone:(NSZone *)zone {
static RCTAppleHealthKit *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [super allocWithZone:zone];
});
return sharedInstance;
}

+ (RCTCallableJSModules *)sharedJsModule {
static RCTCallableJSModules *sharedJsModule = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedJsModule = [RCTCallableJSModules new];
});
return sharedJsModule;
}

- (id) init
{
if (shared != nil) {
return shared;
}

self = [super init];
shared = self;
return self;
return [super init];
}

+ (BOOL)requiresMainQueueSetup
Expand Down Expand Up @@ -602,7 +612,7 @@ - (void)initializeHealthKit:(NSDictionary *)input callback:(RCTResponseSenderBlo
[self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) {
if (!success) {
NSString *errMsg = [NSString stringWithFormat:@"Error with HealthKit authorization: %@", error];
NSLog(errMsg);
NSLog(@"%@", errMsg);
callback(@[RCTMakeError(errMsg, nil, nil)]);
return;
} else {
Expand Down Expand Up @@ -681,8 +691,8 @@ - (void)getAuthorizationStatus:(NSDictionary *)input callback:(RCTResponseSender
if(permissions != nil && [permissions objectForKey:@"read"] != nil && [permissions objectForKey:@"write"] != nil){
NSArray* readPermsNamesArray = [permissions objectForKey:@"read"];
NSArray* writePermsNamesArray = [permissions objectForKey:@"write"];
readPermsArray = [self getReadPermsFromOptions:readPermsNamesArray];
writePermsArray = [self getWritePermsFromOptions:writePermsNamesArray];
readPermsArray = [[self getReadPermsFromOptions:readPermsNamesArray] allObjects];
writePermsArray = [[self getWritePermsFromOptions:writePermsNamesArray] allObjects];
} else {
callback(@[RCTMakeError(@"permissions must be included in permissions object with read and write options", nil, nil)]);
return;
Expand Down Expand Up @@ -715,10 +725,8 @@ - (void)getAuthorizationStatus:(NSDictionary *)input callback:(RCTResponseSender
This method must be called at the application:didFinishLaunchingWithOptions: method, in AppDelegate.m
*/
- (void)initializeBackgroundObservers:(RCTBridge *)bridge
- (void)initializeBackgroundObservers:(RCTBridge *)bridge
{
NSLog(@"[HealthKit] Background observers will be added to the app");

[self _initializeHealthStore];

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

// Will be called when this module's first listener is added.
-(void)startObserving {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
for (NSString *notificationName in [self supportedEvents]) {
[center addObserver:self
selector:@selector(emitEventInternal:)
name:notificationName
object:nil];
}
self.hasListeners = YES;
// Set up any upstream listeners or background tasks as necessary
}

- (void)emitEventInternal:(NSNotification *)notification {
if (self.hasListeners) {
self.callableJSModules = [RCTAppleHealthKit sharedJsModule];
[self.callableJSModules setBridge:self.bridge];
[self sendEventWithName:notification.name
body:notification.userInfo];
}
}

- (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload {
[[NSNotificationCenter defaultCenter] postNotificationName:name
object:self
userInfo:payload];
}

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

@end

0 comments on commit 3b741e3

Please sign in to comment.