|
27 | 27 | #import <React/RCTBridgeModule.h>
|
28 | 28 | #import <React/RCTEventDispatcher.h>
|
29 | 29 |
|
30 |
| -RCTAppleHealthKit *shared; |
31 | 30 |
|
32 | 31 | @implementation RCTAppleHealthKit
|
33 | 32 |
|
34 |
| -@synthesize bridge = _bridge; |
35 |
| - |
36 | 33 | bool hasListeners;
|
37 | 34 |
|
38 | 35 | RCT_EXPORT_MODULE();
|
39 | 36 |
|
| 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 | + |
40 | 56 | - (id) init
|
41 | 57 | {
|
42 |
| - if (shared != nil) { |
43 |
| - return shared; |
44 |
| - } |
45 |
| - |
46 |
| - self = [super init]; |
47 |
| - shared = self; |
48 |
| - return self; |
| 58 | + return [super init]; |
49 | 59 | }
|
50 | 60 |
|
51 | 61 | + (BOOL)requiresMainQueueSetup
|
@@ -583,7 +593,7 @@ - (void)initializeHealthKit:(NSDictionary *)input callback:(RCTResponseSenderBlo
|
583 | 593 | [self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) {
|
584 | 594 | if (!success) {
|
585 | 595 | NSString *errMsg = [NSString stringWithFormat:@"Error with HealthKit authorization: %@", error];
|
586 |
| - NSLog(errMsg); |
| 596 | + NSLog(@"%@", errMsg); |
587 | 597 | callback(@[RCTMakeError(errMsg, nil, nil)]);
|
588 | 598 | return;
|
589 | 599 | } else {
|
@@ -662,8 +672,8 @@ - (void)getAuthorizationStatus:(NSDictionary *)input callback:(RCTResponseSender
|
662 | 672 | if(permissions != nil && [permissions objectForKey:@"read"] != nil && [permissions objectForKey:@"write"] != nil){
|
663 | 673 | NSArray* readPermsNamesArray = [permissions objectForKey:@"read"];
|
664 | 674 | NSArray* writePermsNamesArray = [permissions objectForKey:@"write"];
|
665 |
| - readPermsArray = [self getReadPermsFromOptions:readPermsNamesArray]; |
666 |
| - writePermsArray = [self getWritePermsFromOptions:writePermsNamesArray]; |
| 675 | + readPermsArray = [[self getReadPermsFromOptions:readPermsNamesArray] allObjects]; |
| 676 | + writePermsArray = [[self getWritePermsFromOptions:writePermsNamesArray] allObjects]; |
667 | 677 | } else {
|
668 | 678 | callback(@[RCTMakeError(@"permissions must be included in permissions object with read and write options", nil, nil)]);
|
669 | 679 | return;
|
@@ -696,10 +706,8 @@ - (void)getAuthorizationStatus:(NSDictionary *)input callback:(RCTResponseSender
|
696 | 706 |
|
697 | 707 | This method must be called at the application:didFinishLaunchingWithOptions: method, in AppDelegate.m
|
698 | 708 | */
|
699 |
| -- (void)initializeBackgroundObservers:(RCTBridge *)bridge |
| 709 | + - (void)initializeBackgroundObservers:(RCTBridge *)bridge |
700 | 710 | {
|
701 |
| - NSLog(@"[HealthKit] Background observers will be added to the app"); |
702 |
| - |
703 | 711 | [self _initializeHealthStore];
|
704 | 712 |
|
705 | 713 | self.bridge = bridge;
|
@@ -751,14 +759,35 @@ - (void)initializeBackgroundObservers:(RCTBridge *)bridge
|
751 | 759 |
|
752 | 760 | // Will be called when this module's first listener is added.
|
753 | 761 | -(void)startObserving {
|
| 762 | + NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; |
| 763 | + for (NSString *notificationName in [self supportedEvents]) { |
| 764 | + [center addObserver:self |
| 765 | + selector:@selector(emitEventInternal:) |
| 766 | + name:notificationName |
| 767 | + object:nil]; |
| 768 | + } |
754 | 769 | self.hasListeners = YES;
|
755 |
| - // Set up any upstream listeners or background tasks as necessary |
| 770 | +} |
| 771 | + |
| 772 | +- (void)emitEventInternal:(NSNotification *)notification { |
| 773 | + if (self.hasListeners) { |
| 774 | + self.callableJSModules = [RCTAppleHealthKit sharedJsModule]; |
| 775 | + [self.callableJSModules setBridge:self.bridge]; |
| 776 | + [self sendEventWithName:notification.name |
| 777 | + body:notification.userInfo]; |
| 778 | + } |
| 779 | +} |
| 780 | + |
| 781 | +- (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload { |
| 782 | + [[NSNotificationCenter defaultCenter] postNotificationName:name |
| 783 | + object:self |
| 784 | + userInfo:payload]; |
756 | 785 | }
|
757 | 786 |
|
758 | 787 | // Will be called when this module's last listener is removed, or on dealloc.
|
759 | 788 | -(void)stopObserving {
|
760 | 789 | self.hasListeners = NO;
|
761 |
| - // Remove upstream listeners, stop unnecessary background tasks |
| 790 | + [[NSNotificationCenter defaultCenter] removeObserver:self]; |
762 | 791 | }
|
763 | 792 |
|
764 | 793 | @end
|
0 commit comments