|
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
|
@@ -602,7 +612,7 @@ - (void)initializeHealthKit:(NSDictionary *)input callback:(RCTResponseSenderBlo
|
602 | 612 | [self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) {
|
603 | 613 | if (!success) {
|
604 | 614 | NSString *errMsg = [NSString stringWithFormat:@"Error with HealthKit authorization: %@", error];
|
605 |
| - NSLog(errMsg); |
| 615 | + NSLog(@"%@", errMsg); |
606 | 616 | callback(@[RCTMakeError(errMsg, nil, nil)]);
|
607 | 617 | return;
|
608 | 618 | } else {
|
@@ -681,8 +691,8 @@ - (void)getAuthorizationStatus:(NSDictionary *)input callback:(RCTResponseSender
|
681 | 691 | if(permissions != nil && [permissions objectForKey:@"read"] != nil && [permissions objectForKey:@"write"] != nil){
|
682 | 692 | NSArray* readPermsNamesArray = [permissions objectForKey:@"read"];
|
683 | 693 | 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]; |
686 | 696 | } else {
|
687 | 697 | callback(@[RCTMakeError(@"permissions must be included in permissions object with read and write options", nil, nil)]);
|
688 | 698 | return;
|
@@ -715,10 +725,8 @@ - (void)getAuthorizationStatus:(NSDictionary *)input callback:(RCTResponseSender
|
715 | 725 |
|
716 | 726 | This method must be called at the application:didFinishLaunchingWithOptions: method, in AppDelegate.m
|
717 | 727 | */
|
718 |
| -- (void)initializeBackgroundObservers:(RCTBridge *)bridge |
| 728 | + - (void)initializeBackgroundObservers:(RCTBridge *)bridge |
719 | 729 | {
|
720 |
| - NSLog(@"[HealthKit] Background observers will be added to the app"); |
721 |
| - |
722 | 730 | [self _initializeHealthStore];
|
723 | 731 |
|
724 | 732 | self.bridge = bridge;
|
@@ -770,14 +778,35 @@ - (void)initializeBackgroundObservers:(RCTBridge *)bridge
|
770 | 778 |
|
771 | 779 | // Will be called when this module's first listener is added.
|
772 | 780 | -(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 | + } |
773 | 788 | 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]; |
775 | 804 | }
|
776 | 805 |
|
777 | 806 | // Will be called when this module's last listener is removed, or on dealloc.
|
778 | 807 | -(void)stopObserving {
|
779 | 808 | self.hasListeners = NO;
|
780 |
| - // Remove upstream listeners, stop unnecessary background tasks |
| 809 | + [[NSNotificationCenter defaultCenter] removeObserver:self]; |
781 | 810 | }
|
782 | 811 |
|
783 | 812 | @end
|
0 commit comments