react-native-visit-rn-sdk 5.0.2 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/README.md +18 -2
  2. package/ios/VisitRnSdkViewManager.h +1 -19
  3. package/ios/VisitRnSdkViewManager.m +8 -1294
  4. package/lib/commonjs/components/VideoCallComponent.js +59 -73
  5. package/lib/commonjs/components/VideoCallComponent.js.map +1 -1
  6. package/lib/commonjs/index.android.js +30 -351
  7. package/lib/commonjs/index.android.js.map +1 -1
  8. package/lib/commonjs/index.ios.js +14 -222
  9. package/lib/commonjs/index.ios.js.map +1 -1
  10. package/lib/module/components/VideoCallComponent.js +57 -69
  11. package/lib/module/components/VideoCallComponent.js.map +1 -1
  12. package/lib/module/index.android.js +27 -343
  13. package/lib/module/index.android.js.map +1 -1
  14. package/lib/module/index.ios.js +13 -219
  15. package/lib/module/index.ios.js.map +1 -1
  16. package/package.json +3 -5
  17. package/src/index.android.js +29 -512
  18. package/src/index.ios.js +30 -317
  19. package/android/bin/build/generated/source/buildConfig/debug/com/visitrnsdk/BuildConfig.class +0 -0
  20. package/android/bin/build/intermediates/aapt_friendly_merged_manifests/debug/aapt/AndroidManifest.xml +0 -7
  21. package/android/bin/build/intermediates/aapt_friendly_merged_manifests/debug/aapt/output-metadata.json +0 -18
  22. package/android/bin/build/intermediates/aar_main_jar/debug/classes.jar +0 -0
  23. package/android/bin/build/intermediates/aar_metadata/debug/aar-metadata.properties +0 -5
  24. package/android/bin/build/intermediates/annotation_processor_list/debug/annotationProcessors.json +0 -1
  25. package/android/bin/build/intermediates/annotations_typedef_file/debug/typedefs.txt +0 -0
  26. package/android/bin/build/intermediates/compile_library_classes_jar/debug/classes.jar +0 -0
  27. package/android/bin/build/intermediates/compile_r_class_jar/debug/R.jar +0 -0
  28. package/android/bin/build/intermediates/compile_symbol_list/debug/R.txt +0 -1954
  29. package/android/bin/build/intermediates/incremental/debug/packageDebugResources/compile-file-map.properties +0 -1
  30. package/android/bin/build/intermediates/incremental/debug/packageDebugResources/merger.xml +0 -2
  31. package/android/bin/build/intermediates/incremental/debug-mergeJavaRes/merge-state +0 -0
  32. package/android/bin/build/intermediates/incremental/mergeDebugJniLibFolders/merger.xml +0 -2
  33. package/android/bin/build/intermediates/incremental/mergeDebugShaders/merger.xml +0 -2
  34. package/android/bin/build/intermediates/incremental/packageDebugAssets/merger.xml +0 -2
  35. package/android/bin/build/intermediates/library_java_res/debug/res.jar +0 -0
  36. package/android/bin/build/intermediates/local_only_symbol_list/debug/R-def.txt +0 -2
  37. package/android/bin/build/intermediates/manifest_merge_blame_file/debug/manifest-merger-blame-debug-report.txt +0 -8
  38. package/android/bin/build/intermediates/merged_java_res/debug/feature-react-native-visit-rn-sdk.jar +0 -0
  39. package/android/bin/build/intermediates/merged_manifest/debug/AndroidManifest.xml +0 -7
  40. package/android/bin/build/intermediates/navigation_json/debug/navigation.json +0 -1
  41. package/android/bin/build/intermediates/packaged_manifests/debug/output-metadata.json +0 -18
  42. package/android/bin/build/intermediates/runtime_library_classes_jar/debug/classes.jar +0 -0
  43. package/android/bin/build/intermediates/symbol_list_with_package_name/debug/package-aware-r.txt +0 -1447
  44. package/android/bin/build/intermediates/variant_model/debug/variant_model.json +0 -0
  45. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab +0 -0
  46. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab.keystream +0 -0
  47. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab.keystream.len +0 -0
  48. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab.len +0 -0
  49. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab.values.at +0 -0
  50. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab_i +0 -0
  51. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab_i.len +0 -0
  52. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab +0 -0
  53. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab.keystream +0 -0
  54. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab.keystream.len +0 -0
  55. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab.len +0 -0
  56. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab.values.at +0 -0
  57. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab_i +0 -0
  58. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab_i.len +0 -0
  59. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab +0 -0
  60. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab.keystream +0 -0
  61. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab.keystream.len +0 -0
  62. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab.len +0 -0
  63. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab.values.at +0 -0
  64. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab_i +0 -0
  65. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab_i.len +0 -0
  66. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab +0 -0
  67. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab.keystream +0 -0
  68. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab.keystream.len +0 -0
  69. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab.len +0 -0
  70. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab.values.at +0 -0
  71. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab_i +0 -0
  72. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab_i.len +0 -0
  73. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab +0 -0
  74. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab.keystream +0 -0
  75. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab.keystream.len +0 -0
  76. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab.len +0 -0
  77. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab.values.at +0 -0
  78. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab_i +0 -0
  79. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab_i.len +0 -0
  80. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab +0 -0
  81. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab.keystream +0 -0
  82. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab.keystream.len +0 -0
  83. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab.len +0 -0
  84. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab.values.at +0 -0
  85. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab_i +0 -0
  86. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab_i.len +0 -0
  87. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab +0 -0
  88. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab.keystream +0 -0
  89. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab.keystream.len +0 -0
  90. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab.len +0 -0
  91. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab.values.at +0 -0
  92. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab_i +0 -0
  93. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab_i.len +0 -0
  94. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab +0 -0
  95. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab.keystream +0 -0
  96. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab.keystream.len +0 -0
  97. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab.len +0 -0
  98. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab.values.at +0 -0
  99. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab_i +0 -0
  100. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab_i.len +0 -0
  101. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/counters.tab +0 -2
  102. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab +0 -0
  103. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab.keystream +0 -0
  104. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab.keystream.len +0 -0
  105. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab.len +0 -0
  106. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab.values.at +0 -0
  107. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab_i +0 -0
  108. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab_i.len +0 -0
  109. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab +0 -0
  110. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab.keystream +0 -0
  111. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab.keystream.len +0 -0
  112. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab.len +0 -0
  113. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab.values.at +0 -0
  114. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab_i +0 -0
  115. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab_i.len +0 -0
  116. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab +0 -0
  117. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab.keystream +0 -0
  118. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab.keystream.len +0 -0
  119. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab.len +0 -0
  120. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab.values.at +0 -0
  121. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab_i +0 -0
  122. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab_i.len +0 -0
  123. package/android/bin/build/kotlin/compileDebugKotlin/cacheable/last-build.bin +0 -0
  124. package/android/bin/build/kotlin/compileDebugKotlin/local-state/build-history.bin +0 -0
  125. package/android/bin/build/outputs/aar/react-native-visit-rn-sdk-debug.aar +0 -0
  126. package/android/bin/build/outputs/logs/manifest-merger-debug-report.txt +0 -25
  127. package/android/bin/build/tmp/compileDebugJavaWithJavac/previous-compilation-data.bin +0 -0
  128. package/android/bin/build/tmp/kotlin-classes/debug/META-INF/react-native-visit-rn-sdk_debug.kotlin_module +0 -0
  129. package/android/bin/local.properties +0 -8
  130. package/lib/commonjs/Services.js +0 -36
  131. package/lib/commonjs/Services.js.map +0 -1
  132. package/lib/commonjs/constants.js +0 -12
  133. package/lib/commonjs/constants.js.map +0 -1
  134. package/lib/module/Services.js +0 -27
  135. package/lib/module/Services.js.map +0 -1
  136. package/lib/module/constants.js +0 -5
  137. package/lib/module/constants.js.map +0 -1
  138. package/src/Services.js +0 -37
  139. package/src/constants.js +0 -4
@@ -1,1309 +1,23 @@
1
1
  #import "VisitRnSdkViewManager.h"
2
- #import <HealthKit/HealthKit.h>
3
2
 
4
3
  @implementation VisitRnSdkViewManager
5
4
 
6
5
  RCT_EXPORT_MODULE(VisitRnSdkViewManager)
7
6
 
8
- // Example method
9
- // See // https://reactnative.dev/docs/native-modules-ios
7
+ + (BOOL)requiresMainQueueSetup
8
+ {
9
+ return YES;
10
+ }
11
+
10
12
  RCT_REMAP_METHOD(multiply,
11
- multiplyWithA:(nonnull NSNumber*)a withB:(nonnull NSNumber*)b
13
+ multiplyWithA:(nonnull NSNumber *)a
14
+ withB:(nonnull NSNumber *)b
12
15
  withResolver:(RCTPromiseResolveBlock)resolve
13
16
  withRejecter:(RCTPromiseRejectBlock)reject)
14
17
  {
18
+ (void)reject;
15
19
  NSNumber *result = @([a floatValue] * [b floatValue]);
16
-
17
20
  resolve(result);
18
21
  }
19
22
 
20
- - (instancetype)init
21
- {
22
- self = [super init];
23
- self->calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
24
- self->calendar.timeZone = [NSTimeZone timeZoneWithName:@"IST"];
25
- NSLog(@"Do some stuff here");
26
- return self;
27
- }
28
-
29
- + (BOOL)requiresMainQueueSetup{
30
- return YES;
31
- }
32
-
33
- + (HKHealthStore *)sharedManager {
34
- __strong static HKHealthStore *store = nil;
35
- static dispatch_once_t onceToken;
36
- dispatch_once(&onceToken, ^{
37
- store = [[HKHealthStore alloc] init];
38
- });
39
-
40
- return store;
41
- }
42
-
43
- - (NSArray<NSString *> *)supportedEvents
44
- {
45
- return @[@"EventReminder"];
46
- }
47
-
48
- - (NSString *)readGender
49
- {
50
- NSError *error;
51
- HKBiologicalSexObject *gen=[[VisitRnSdkViewManager sharedManager] biologicalSexWithError:&error];
52
- if (gen.biologicalSex==HKBiologicalSexMale)
53
- {
54
- return(@"Male");
55
- }
56
- else if (gen.biologicalSex==HKBiologicalSexFemale)
57
- {
58
- return (@"Female");
59
- }
60
- else if (gen.biologicalSex==HKBiologicalSexOther)
61
- {
62
- return (@"Other");
63
- }
64
- else{
65
- return (@"Not Set");
66
- }
67
- }
68
-
69
- - (void)fetchQuantitySamplesOfType:(HKQuantityType *)quantityType
70
- unit:(HKUnit *)unit
71
- predicate:(NSPredicate *)predicate
72
- ascending:(BOOL)asc
73
- limit:(NSUInteger)lim
74
- completion:(void (^)(NSArray *, NSError *))completion {
75
-
76
- NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate
77
- ascending:asc];
78
- __block NSTimeInterval totalActivityDuration = 0;
79
- // declare the block
80
- void (^handlerBlock)(HKSampleQuery *query, NSArray *results, NSError *error);
81
- // create and assign the block
82
- handlerBlock = ^(HKSampleQuery *query, NSArray *results, NSError *error) {
83
- if (!results) {
84
- if (completion) {
85
- completion(nil, error);
86
- }
87
- return;
88
- }
89
-
90
- if (completion) {
91
- NSMutableArray *data = [NSMutableArray arrayWithCapacity:1];
92
- NSMutableArray *dataByFrequency = [NSMutableArray new];
93
- dispatch_async(dispatch_get_main_queue(), ^{
94
- for (HKQuantitySample *sample in results) {
95
- HKQuantity *quantity = sample.quantity;
96
- double value = [quantity doubleValueForUnit:unit];
97
- if(value){
98
- NSLog(@"startDate and endDate for fetchQuantitySamplesOfType is %@ & %@", sample.startDate,sample.endDate);
99
- NSNumber* val = [NSNumber numberWithDouble:[sample.endDate timeIntervalSinceDate:sample.startDate]/60];
100
- NSDictionary *element = @{
101
- @"date" : sample.endDate,
102
- @"value" : val,
103
- };
104
- NSMutableDictionary *dict =[NSMutableDictionary dictionaryWithDictionary:element];
105
-
106
- if([dataByFrequency count]>0){
107
- NSMutableDictionary* ele = [dataByFrequency objectAtIndex:[dataByFrequency count]-1];
108
- if([[NSCalendar currentCalendar] isDate:sample.endDate inSameDayAsDate:[ele valueForKey:@"date"]]){
109
- double myValue = [[ele valueForKey:@"value"] doubleValue];
110
- myValue+=[val doubleValue];
111
- [ele setObject:sample.endDate forKey:@"date" ];
112
- [ele setObject: [NSNumber numberWithDouble:myValue] forKey:@"value" ];
113
- }else{
114
- [dataByFrequency addObject:dict];
115
- }
116
- }
117
- else{
118
- [dataByFrequency addObject:dict];
119
- }
120
-
121
- NSTimeInterval duration = [sample.endDate timeIntervalSinceDate:sample.startDate];
122
- totalActivityDuration+=duration;
123
- NSLog(@"fetchQuantitySamplesOfType dict is %@",dict);
124
- }
125
- }
126
- NSLog(@"fetchQuantitySamplesOfType dataByFrequency %@ ",dataByFrequency);
127
- [data addObject:[NSString stringWithFormat:@"%f",totalActivityDuration/60]];
128
- [data addObject:dataByFrequency];
129
- completion(data, error);
130
- });
131
- }
132
- };
133
-
134
- HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType
135
- predicate:predicate
136
- limit:lim
137
- sortDescriptors:@[timeSortDescriptor]
138
- resultsHandler:handlerBlock];
139
-
140
- [[VisitRnSdkViewManager sharedManager] executeQuery:query];
141
- }
142
-
143
-
144
- - (void) getActivityTime:(NSDate*) endDate frequency:(NSString*) frequency days:(NSInteger) days callback:(void(^)(NSMutableArray*))callback{
145
- HKQuantityType *stepCountType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
146
- NSDate *startDate;
147
- NSDate *endDatePeriod;
148
- if([frequency isEqualToString:@"day"]){
149
- NSTimeInterval interval;
150
- [self->calendar rangeOfUnit:NSCalendarUnitDay
151
- startDate:&startDate
152
- interval:&interval
153
- forDate:endDate];
154
- endDatePeriod = [startDate dateByAddingTimeInterval:interval-1];
155
- }else if ([frequency isEqualToString:@"week"]){
156
- NSTimeInterval interval;
157
- [self->calendar rangeOfUnit:NSCalendarUnitWeekOfYear
158
- startDate:&startDate
159
- interval:&interval
160
- forDate:endDate];
161
- endDatePeriod = [startDate dateByAddingTimeInterval:interval-1];
162
- }else if ([frequency isEqualToString:@"month"]){
163
- NSTimeInterval interval;
164
- [self->calendar rangeOfUnit:NSCalendarUnitMonth
165
- startDate:&startDate
166
- interval:&interval
167
- forDate:endDate];
168
- endDatePeriod = [startDate dateByAddingTimeInterval:interval-1];
169
- }else if([frequency isEqualToString:@"custom"]){
170
- endDatePeriod = endDate;
171
- startDate = [self->calendar dateByAddingUnit:NSCalendarUnitDay
172
- value:1-days
173
- toDate:endDatePeriod
174
- options:0];
175
- }
176
- NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDatePeriod options:HKQueryOptionStrictStartDate];
177
- NSPredicate *userEnteredValuePredicate = [HKQuery predicateForObjectsWithMetadataKey:HKMetadataKeyWasUserEntered operatorType: NSNotEqualToPredicateOperatorType value: @YES];
178
-
179
- NSCompoundPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[predicate, userEnteredValuePredicate]];
180
- [self fetchQuantitySamplesOfType:stepCountType unit:[HKUnit countUnit] predicate:compoundPredicate ascending:true limit:HKObjectQueryNoLimit completion:^(NSArray *results, NSError *error) {
181
- if (results) {
182
- NSLog(@"the results of getActivityTime %@",results);
183
- callback([NSMutableArray arrayWithArray:results]);
184
- return;
185
- } else {
186
- NSLog(@"error getting step count samples: %@", error);
187
- return;
188
- }
189
- }];
190
- }
191
-
192
- - (void)fetchHourlySteps:(NSDate*) endDate callback:(void(^)(NSArray*))callback{
193
- HKQuantityType *stepCountType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
194
- NSCalendar *calendar = [NSCalendar currentCalendar];
195
- HKUnit *stepsUnit = [HKUnit countUnit];
196
- NSDateComponents *interval = [[NSDateComponents alloc] init];
197
- interval.hour = 1;
198
- NSDate *startDate = [[NSCalendar calendarWithIdentifier:NSCalendarIdentifierISO8601] startOfDayForDate:endDate];
199
- NSDateComponents *dayInteval = [[NSDateComponents alloc] init];
200
- dayInteval.day = 1;
201
- dayInteval.second = -1;
202
- NSDate* endOfDay = [calendar dateByAddingComponents:dayInteval toDate:startDate options:0];
203
- NSLog(@"endOfDay is, %@",endOfDay);
204
- NSDate *anchorDate = [calendar startOfDayForDate:startDate];
205
- NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endOfDay options:HKQueryOptionStrictStartDate];
206
- NSPredicate *userEnteredValuePredicate = [HKQuery predicateForObjectsWithMetadataKey:HKMetadataKeyWasUserEntered operatorType: NSNotEqualToPredicateOperatorType value: @YES];
207
-
208
- NSCompoundPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[predicate, userEnteredValuePredicate]];
209
-
210
- HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:stepCountType quantitySamplePredicate:compoundPredicate options:HKStatisticsOptionCumulativeSum anchorDate:anchorDate intervalComponents:interval];
211
-
212
- query.initialResultsHandler = ^(HKStatisticsCollectionQuery * _Nonnull query, HKStatisticsCollection * _Nullable result, NSError * _Nullable error) {
213
- if (error) {
214
- NSLog(@"*** An error occurred while calculating the statistics: %@ ***",
215
- error.localizedDescription);
216
- return;
217
- }
218
-
219
- NSMutableArray *data = [NSMutableArray arrayWithCapacity:24];
220
- NSMutableArray *stepsData = [NSMutableArray arrayWithCapacity:24];
221
- NSMutableArray *calorieData = [NSMutableArray arrayWithCapacity:24];
222
- NSLog(@"the startDate is %@ while endOfDay is %@",startDate,endOfDay);
223
- [result enumerateStatisticsFromDate:startDate toDate:endOfDay withBlock:^(HKStatistics * _Nonnull result, BOOL * _Nonnull stop) {
224
- HKQuantity *quantity = result.sumQuantity;
225
-
226
- if (quantity) {
227
- int value = (int)[quantity doubleValueForUnit:stepsUnit];
228
- [data addObject:[NSNumber numberWithInt:value]];
229
- int calories = value/21;
230
- calories+=self->bmrCaloriesPerHour;
231
- [calorieData addObject:[NSNumber numberWithInt:calories]];
232
- } else {
233
- [data addObject:[NSNumber numberWithInt:0]];
234
- [calorieData addObject:[NSNumber numberWithInt:0]];
235
- }
236
- }];
237
- int count = 0;
238
- for (NSNumber* steps in data) {
239
- [stepsData insertObject:steps atIndex:count];
240
- count++;
241
- }
242
- NSArray* finalData = @[stepsData, calorieData];
243
- callback(finalData);
244
- };
245
-
246
- [[VisitRnSdkViewManager sharedManager] executeQuery:query];
247
- }
248
-
249
- -(void) fetchSteps:(NSString*) frequency endDate:(NSDate*) endDate days:(NSInteger) days callback:(void(^)(NSArray*))callback{
250
- NSDateComponents *interval = [[NSDateComponents alloc] init];
251
- NSDate *startDate;
252
- interval.day = 1;
253
- NSDate *endDatePeriod;
254
- if([frequency isEqualToString:@"day"]){
255
- endDatePeriod = endDate;
256
- startDate = [self->calendar dateByAddingUnit:NSCalendarUnitDay
257
- value:0
258
- toDate:endDatePeriod
259
- options:0];
260
- }else if ([frequency isEqualToString:@"week"]){
261
- NSTimeInterval interval;
262
- [self->calendar rangeOfUnit:NSCalendarUnitWeekOfYear
263
- startDate:&startDate
264
- interval:&interval
265
- forDate:endDate];
266
- endDatePeriod = [startDate dateByAddingTimeInterval:interval-1];
267
- }else if ([frequency isEqualToString:@"month"]){
268
- NSTimeInterval interval;
269
- [self->calendar rangeOfUnit:NSCalendarUnitMonth
270
- startDate:&startDate
271
- interval:&interval
272
- forDate:endDate];
273
- endDatePeriod = [startDate dateByAddingTimeInterval:interval-1];
274
- }else if([frequency isEqualToString:@"custom"]){
275
- endDatePeriod = endDate;
276
- startDate = [self->calendar dateByAddingUnit:NSCalendarUnitDay
277
- value:1-days
278
- toDate:endDatePeriod
279
- options:0];
280
- NSLog(@"startDate and endDate in custom fetchSteps is, %@, %@",startDate,endDatePeriod);
281
- }
282
- NSLog(@"startDate and endDate in fetchSteps is, %@, %@",startDate,endDatePeriod);
283
- NSDateComponents *anchorComponents = [self->calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear
284
- fromDate:[NSDate date]];
285
- anchorComponents.hour = 0;
286
- NSLog(@"anchorComponents in fetchSteps are, %@",anchorComponents);
287
- NSDate *anchorDate = [self->calendar dateFromComponents:anchorComponents];
288
- NSLog(@"anchorDate in fetchSteps are, %@",anchorDate);
289
- HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
290
- // Create the query
291
- HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:quantityType
292
- quantitySamplePredicate:nil
293
- options:HKStatisticsOptionCumulativeSum
294
- anchorDate:anchorDate
295
- intervalComponents:interval];
296
-
297
- // Set the results handler
298
- query.initialResultsHandler = ^(HKStatisticsCollectionQuery *query, HKStatisticsCollection *results, NSError *error) {
299
- if (error) {
300
- // Perform proper error handling here
301
- NSLog(@"*** An error occurred while calculating the statistics: %@ ***",error.localizedDescription);
302
- }
303
- NSMutableArray *data = [NSMutableArray arrayWithCapacity:1];
304
- NSMutableArray *calorieData = [NSMutableArray arrayWithCapacity:1];
305
- [results enumerateStatisticsFromDate:startDate
306
- toDate:endDatePeriod
307
- withBlock:^(HKStatistics *result, BOOL *stop) {
308
-
309
- HKQuantity *quantity = result.sumQuantity;
310
- if (quantity) {
311
- int value = [[NSNumber numberWithInt:[quantity doubleValueForUnit:[HKUnit countUnit]]] intValue];
312
- int calories = value/21;
313
- calories+=self->bmrCaloriesPerHour;
314
- [calorieData addObject:[NSNumber numberWithInt:calories]];
315
- [data addObject:[NSNumber numberWithInt:value]];
316
- }else{
317
- [data addObject:[NSNumber numberWithInt:0]];
318
- [calorieData addObject:[NSNumber numberWithInt:0]];
319
- }
320
- }];
321
- NSLog(@"in stepsData and calorieData is %@,%@", data, calorieData);
322
- NSArray* finalData = @[data, calorieData];
323
- callback(finalData);
324
- };
325
-
326
- [[VisitRnSdkViewManager sharedManager] executeQuery:query];
327
- }
328
-
329
- -(void) fetchHourlyDistanceWalkingRunning:(NSDate*) endDate callback:(void(^)(NSArray*))callback{
330
- HKQuantityType *distanceType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning];
331
- NSCalendar *calendar = [NSCalendar currentCalendar];
332
- NSDate *startDate = [[NSCalendar calendarWithIdentifier:NSCalendarIdentifierISO8601] startOfDayForDate:endDate];
333
- HKUnit *distanceUnit = [HKUnit meterUnit];
334
- NSDateComponents *interval = [[NSDateComponents alloc] init];
335
- interval.hour = 1;
336
-
337
- NSDateComponents *dayInteval = [[NSDateComponents alloc] init];
338
- dayInteval.day = 1;
339
- dayInteval.second = -1;
340
- NSDate* endOfDay = [calendar dateByAddingComponents:dayInteval toDate:startDate options:0];
341
-
342
- NSDate *anchorDate = [calendar startOfDayForDate:startDate];
343
- NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endOfDay options:HKQueryOptionStrictStartDate];
344
- NSPredicate *userEnteredValuePredicate = [HKQuery predicateForObjectsWithMetadataKey:HKMetadataKeyWasUserEntered operatorType: NSNotEqualToPredicateOperatorType value: @YES];
345
-
346
- NSCompoundPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[predicate, userEnteredValuePredicate]];
347
-
348
- HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:distanceType quantitySamplePredicate:compoundPredicate options:HKStatisticsOptionCumulativeSum anchorDate:anchorDate intervalComponents:interval];
349
-
350
- query.initialResultsHandler = ^(HKStatisticsCollectionQuery * _Nonnull query, HKStatisticsCollection * _Nullable result, NSError * _Nullable error) {
351
- if (error) {
352
- NSLog(@"*** An error occurred while calculating the statistics: %@ ***",
353
- error.localizedDescription);
354
- return;
355
- }
356
-
357
- NSMutableArray *data = [NSMutableArray arrayWithCapacity:1];
358
- [result enumerateStatisticsFromDate:startDate toDate:endOfDay withBlock:^(HKStatistics * _Nonnull result, BOOL * _Nonnull stop) {
359
- HKQuantity *quantity = result.sumQuantity;
360
- if (quantity) {
361
- int value =(int) [quantity doubleValueForUnit:distanceUnit];
362
- [data addObject:[NSNumber numberWithInt:value]];
363
- } else {
364
- [data addObject:[NSNumber numberWithInt:0]];
365
- }
366
- }];
367
- callback(data);
368
- NSLog(@"fetchDistanceWalkingRunning is,%@",data);
369
- };
370
-
371
- [[VisitRnSdkViewManager sharedManager] executeQuery:query];
372
- }
373
-
374
- -(void) fetchDistanceWalkingRunning:(NSString*) frequency endDate:(NSDate*) endDate days:(NSInteger) days callback:(void(^)(NSArray*))callback{
375
- NSDateComponents *interval = [[NSDateComponents alloc] init];
376
- NSDate *startDate;
377
- interval.day = 1;
378
- NSDate *endDatePeriod;
379
- HKUnit *distanceUnit = [HKUnit meterUnit];
380
- if([frequency isEqualToString:@"day"]){
381
- endDatePeriod = endDate;
382
- startDate = [self->calendar dateByAddingUnit:NSCalendarUnitDay
383
- value:0
384
- toDate:endDatePeriod
385
- options:0];
386
- }else if ([frequency isEqualToString:@"week"]){
387
- NSTimeInterval interval;
388
- [self->calendar rangeOfUnit:NSCalendarUnitWeekOfYear
389
- startDate:&startDate
390
- interval:&interval
391
- forDate:endDate];
392
- endDatePeriod = [startDate dateByAddingTimeInterval:interval-1];
393
- }else if ([frequency isEqualToString:@"month"]){
394
- NSTimeInterval interval;
395
- [self->calendar rangeOfUnit:NSCalendarUnitMonth
396
- startDate:&startDate
397
- interval:&interval
398
- forDate:endDate];
399
- endDatePeriod = [startDate dateByAddingTimeInterval:interval-1];
400
- }else if([frequency isEqualToString:@"custom"]){
401
- endDatePeriod = endDate;
402
- startDate = [self->calendar dateByAddingUnit:NSCalendarUnitDay
403
- value:1-days
404
- toDate:endDatePeriod
405
- options:0];
406
- NSLog(@"startDate and endDate in custom fetchDistanceWalkingRunning is, %@, %@",startDate,endDatePeriod);
407
- }
408
- NSLog(@"startDate and endDate in fetchDistanceWalkingRunning is, %@, %@",startDate,endDatePeriod);
409
- NSDateComponents *anchorComponents = [self->calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear
410
- fromDate:[NSDate date]];
411
- anchorComponents.hour = 0;
412
- NSDate *anchorDate = [self->calendar dateFromComponents:anchorComponents];
413
- HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning];
414
- // Create the query
415
- HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:quantityType
416
- quantitySamplePredicate:nil
417
- options:HKStatisticsOptionCumulativeSum
418
- anchorDate:anchorDate
419
- intervalComponents:interval];
420
-
421
- // Set the results handler
422
- query.initialResultsHandler = ^(HKStatisticsCollectionQuery *query, HKStatisticsCollection *results, NSError *error) {
423
- if (error) {
424
- // Perform proper error handling here
425
- NSLog(@"*** An error occurred while calculating the statistics: %@ ***",error.localizedDescription);
426
- }
427
- NSMutableArray *data = [NSMutableArray arrayWithCapacity:1];
428
-
429
- [results enumerateStatisticsFromDate:startDate
430
- toDate:endDatePeriod
431
- withBlock:^(HKStatistics *result, BOOL *stop) {
432
-
433
- HKQuantity *quantity = result.sumQuantity;
434
- if (quantity) {
435
- int value = [[NSNumber numberWithInt:[quantity doubleValueForUnit:distanceUnit]] intValue];
436
- NSLog(@"in fetchDistanceWalkingRunning %d", value);
437
-
438
- [data addObject:[NSNumber numberWithInt:value]];
439
- }else{
440
- [data addObject:[NSNumber numberWithInt:0]];
441
- }
442
- }];
443
- callback(data);
444
- };
445
-
446
- [[VisitRnSdkViewManager sharedManager] executeQuery:query];
447
- }
448
-
449
- - (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate
450
- limit:(NSUInteger)lim
451
- completion:(void (^)(NSArray *, NSError *))completion {
452
-
453
-
454
- NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate
455
- ascending:true];
456
-
457
-
458
- // declare the block
459
- void (^handlerBlock)(HKSampleQuery *query, NSArray *results, NSError *error);
460
- // create and assign the block
461
- handlerBlock = ^(HKSampleQuery *query, NSArray *results, NSError *error) {
462
- if (!results) {
463
- if (completion) {
464
- completion(nil, error);
465
- }
466
- return;
467
- }
468
- NSLog(@"the results of sleep is, %@", results);
469
-
470
- if (completion) {
471
- NSMutableArray *data = [NSMutableArray arrayWithCapacity:1];
472
-
473
- dispatch_async(dispatch_get_main_queue(), ^{
474
- for (HKCategorySample *sample in results) {
475
-
476
- NSInteger val = sample.value;
477
-
478
- NSString *valueString;
479
-
480
- switch (val) {
481
- case HKCategoryValueSleepAnalysisInBed:
482
- valueString = @"INBED";
483
- break;
484
- case HKCategoryValueSleepAnalysisAsleep:
485
- valueString = @"ASLEEP";
486
- break;
487
- default:
488
- valueString = @"UNKNOWN";
489
- break;
490
- }
491
-
492
- NSDictionary *elem = @{
493
- @"value" : valueString,
494
- @"startDate" : sample.startDate,
495
- @"endDate" : sample.endDate,
496
- };
497
-
498
- [data addObject:elem];
499
- }
500
-
501
- completion(data, error);
502
- });
503
- }
504
- };
505
-
506
- HKCategoryType *categoryType = [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis];
507
- HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:categoryType
508
- predicate:predicate
509
- limit:lim
510
- sortDescriptors:@[timeSortDescriptor]
511
- resultsHandler:handlerBlock];
512
-
513
- [[VisitRnSdkViewManager sharedManager] executeQuery:query];
514
- }
515
-
516
- - (void)requestAuthorization:(void(^)(NSDictionary*))callback {
517
-
518
- if ([HKHealthStore isHealthDataAvailable] == NO) {
519
- // If our device doesn't support HealthKit -> return.
520
- return;
521
- }
522
- NSArray *writeTypes = @[[HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]];
523
- NSArray *readTypes = @[[HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount],
524
- [HKSampleType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis],
525
- [HKSampleType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex],
526
- [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning]];
527
-
528
- [[VisitRnSdkViewManager sharedManager] requestAuthorizationToShareTypes:[NSSet setWithArray:writeTypes] readTypes:[NSSet setWithArray:readTypes]
529
- completion:^(BOOL success, NSError *error) {
530
- NSLog(@"requestAuthorizationToShareTypes executed");
531
- [self canAccessHealthKit:^(BOOL value){
532
- if(value){
533
- NSLog(@"the health kit permission granted");
534
- [self onHealthKitPermissionGranted:^(NSDictionary * data) {
535
- callback(data);
536
- }];
537
- }else{
538
- NSLog(@"the health kit permission not granted");
539
- }
540
- }];
541
- }];
542
- }
543
-
544
- -(void) onHealthKitPermissionGranted:(void(^)(NSDictionary*))callback{
545
- dispatch_group_t loadDetailsGroup=dispatch_group_create();
546
- __block NSString* numberOfSteps = 0;
547
- __block NSTimeInterval totalSleepTime = 0;
548
- NSLog(@"gender is, %@",gender);
549
- for (int i = 0; i<2; i++) {
550
-
551
- dispatch_group_enter(loadDetailsGroup);
552
- if(i==0){
553
- // getting steps for current day
554
- [self fetchSteps:@"day" endDate:[NSDate date] days:0 callback:^(NSArray * result) {
555
- if([[result objectAtIndex:0] count]>0){
556
- numberOfSteps = [[result objectAtIndex:0] objectAtIndex:0];
557
- }
558
- dispatch_group_leave(loadDetailsGroup);
559
- }];
560
- }else if (i==1){
561
- // getting sleep pattern for the day past
562
- [self fetchSleepPattern:[NSDate date] frequency:@"day" days:0 callback:^(NSArray * result) {
563
- NSLog(@"Sleep result is, %@",result);
564
- if([result count]>0){
565
- for (NSDictionary* item in result) {
566
- NSString* sleepValue = [item valueForKey:@"value"];
567
- if([sleepValue isEqualToString:@"INBED"]||[sleepValue isEqualToString:@"ASLEEP"]){
568
- NSDate* startDate = [item valueForKey:@"startDate"];
569
- NSDate* endDate = [item valueForKey:@"endDate"];
570
- NSTimeInterval duration = [endDate timeIntervalSinceDate:startDate] / 60;
571
- totalSleepTime+=duration;
572
- NSLog(@"Sleep value is, %@, while duration is %f",sleepValue,duration);
573
- }
574
- }
575
- }
576
- dispatch_group_leave(loadDetailsGroup);
577
- }];
578
- }
579
- }
580
-
581
- // Now outside the loop wait until everything is done. NOTE: this will
582
- // not block execution, the provided block will be called
583
- // asynchronously at a later point.
584
- dispatch_group_notify(loadDetailsGroup,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
585
- self->gender= [self readGender];
586
- if([self->gender isEqualToString:@"Male"]){
587
- self->bmrCaloriesPerHour = 1662 / 24;
588
- }else{
589
- self->bmrCaloriesPerHour = 1493 / 24;
590
- }
591
- NSLog(@"the steps result is, %@",numberOfSteps);
592
- NSLog(@"total sleep time is %f",totalSleepTime);
593
- // if(!self->hasLoadedOnce){
594
- NSDictionary *element = @{
595
- @"numberOfSteps" : numberOfSteps,
596
- @"sleepTime" : [NSNumber numberWithInt:totalSleepTime]
597
- };
598
- // NSString *javascript = [NSString stringWithFormat:@"updateFitnessPermissions(true,'%@','%ld')",numberOfSteps, sleepTime];
599
- dispatch_async(dispatch_get_main_queue(), ^{
600
- self->hasLoadedOnce = true;
601
- callback(element);
602
- });
603
- // }
604
- });
605
- }
606
-
607
- -(void) canAccessHealthKit: (void(^)(BOOL))callback {
608
- double value = 1;
609
- NSDate *startDate = [NSDate date];
610
- NSDate *endDate = [NSDate date];
611
-
612
- HKUnit *unit = [HKUnit countUnit];
613
- HKQuantity *quantity = [HKQuantity quantityWithUnit:unit doubleValue:value];
614
- HKQuantityType *type = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
615
- HKQuantitySample *sample = [HKQuantitySample quantitySampleWithType:type quantity:quantity startDate:startDate endDate:endDate];
616
-
617
- [[VisitRnSdkViewManager sharedManager] saveObject:sample withCompletion:^(BOOL success, NSError *error) {
618
- if (!success) {
619
- NSLog(@"An error occured saving the step count sample %@. The error was: %@.", sample, error);
620
- callback(NO);
621
- }else{
622
- [[VisitRnSdkViewManager sharedManager] deleteObject:sample withCompletion:^(BOOL success, NSError * _Nullable error) {
623
- if(!success){
624
- callback(NO);
625
- }else{
626
- callback(YES);
627
- }
628
- }];
629
- }
630
- }];
631
- }
632
-
633
- -(void) fetchSleepPattern:(NSDate *) endDate frequency:(NSString*) frequency days:(NSInteger) days callback:(void(^)(NSArray*))callback{
634
- NSDate *startDate;
635
- NSDate *endDatePeriod;
636
- if([frequency isEqualToString:@"day"]){
637
- NSTimeInterval interval;
638
- NSLog(@"startDate and endDate in fetchSleepPattern before is, %@ %@",startDate,endDatePeriod);
639
- [self->calendar rangeOfUnit:NSCalendarUnitDay
640
- startDate:&startDate
641
- interval:&interval
642
- forDate:endDate];
643
- endDatePeriod = [startDate dateByAddingTimeInterval:interval-1];
644
- startDate = [startDate dateByAddingTimeInterval:-3600*2];
645
- NSLog(@"startDate and endDate in fetchSleepPattern is, %@ %@",startDate,endDatePeriod);
646
- }else if ([frequency isEqualToString:@"week"]){
647
- NSTimeInterval interval;
648
- [self->calendar rangeOfUnit:NSCalendarUnitWeekOfYear
649
- startDate:&startDate
650
- interval:&interval
651
- forDate:endDate];
652
- endDatePeriod = [startDate dateByAddingTimeInterval:interval-1];
653
- }else if([frequency isEqualToString:@"custom"]){
654
- endDatePeriod = endDate;
655
- startDate = [self->calendar dateByAddingUnit:NSCalendarUnitDay
656
- value:-days
657
- toDate:endDatePeriod
658
- options:0];
659
- // NSLog(@"startDate and endDate in custom fetchSleepPattern is, %@, %@",startDate,endDatePeriod);
660
- }
661
- // NSLog(@"startDate and endDate in fetchSleepPattern is, %@ %@",startDate,endDatePeriod);
662
- NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDatePeriod options:HKQueryOptionStrictStartDate];
663
- [self fetchSleepCategorySamplesForPredicate:predicate
664
- limit:HKObjectQueryNoLimit
665
- completion:^(NSArray *results, NSError *error) {
666
- if(results){
667
- // NSLog(@"fetchSleepCategorySamplesForPredicate result, %@",results);
668
- callback(results);
669
- return;
670
- } else {
671
- // NSLog(@"error getting sleep samples: %@", error);
672
- return;
673
- }
674
- }];
675
- }
676
-
677
- -(NSMutableArray*) getBlankSleepWeeks:(NSUInteger) currentCount date:(NSDate*) date{
678
- NSMutableArray *result = [[NSMutableArray alloc]init];
679
- NSInteger value = 1;
680
- NSDate *nextDayTime=date;
681
- NSNumber *nextDayTimeStamp;
682
- NSDateComponents *dateComponents;
683
- NSString* day;
684
- NSLog(@"day is, %@",day);
685
- int counter =(int) currentCount;
686
- while(counter<7){
687
- nextDayTime = [self->calendar dateByAddingUnit:NSCalendarUnitDay value:value toDate:nextDayTime options:NSCalendarMatchStrictly];
688
- nextDayTimeStamp = [NSNumber numberWithDouble: [@(floor([nextDayTime timeIntervalSince1970] * 1000)) longLongValue]];
689
- dateComponents = [self->calendar components: NSCalendarUnitWeekday fromDate: nextDayTime];
690
- day = self->calendar.shortWeekdaySymbols[dateComponents.weekday-1];
691
- NSDictionary *element = @{
692
- @"sleepTime" : @0,
693
- @"wakeupTime" : @0,
694
- @"day" : day,
695
- @"startTimestamp" : nextDayTimeStamp,
696
- };
697
- NSLog(@"element is %@",element);
698
- [result addObject:[NSMutableDictionary dictionaryWithDictionary:element]];
699
- counter++;
700
- }
701
- return result;
702
- }
703
-
704
- -(void) evaluateJavascript:(NSArray *) data type:(NSString *) type frequency:(NSString *) frequency activityTime:(NSString *) activityTime callback:(void(^)(NSArray*))callback{
705
- NSString* hoursInDay = @"[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]";
706
- NSString* daysInWeek = @"[1,2,3,4,5,6,7]";
707
- NSString* daysInMonth = @"[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]";
708
- NSString* samples;
709
- NSString *jsonArrayData;
710
- if([frequency isEqualToString:@"day"]){
711
- samples=hoursInDay;
712
- }else if ([frequency isEqualToString:@"week"]){
713
- samples=daysInWeek;
714
- }else if ([frequency isEqualToString:@"month"]){
715
- samples=daysInMonth;
716
- }
717
- if([type isEqualToString:@"steps"] || [type isEqualToString:@"calories"]){
718
- if([type isEqualToString:@"steps"]){
719
- jsonArrayData = [[data objectAtIndex:0] componentsJoinedByString:@","];
720
- }else{
721
- jsonArrayData = [[data objectAtIndex:1] componentsJoinedByString:@","];
722
- }
723
- }else{
724
- jsonArrayData = [data componentsJoinedByString:@","];
725
- }
726
- NSString *javascript = [NSString stringWithFormat:@"DetailedGraph.updateData(%@,[%@],'%@','%@','%@')", samples, jsonArrayData, type,frequency, activityTime];
727
- dispatch_async(dispatch_get_main_queue(), ^{
728
- callback(@[javascript]);
729
- });
730
- }
731
-
732
- -(void) renderGraphData:(NSString *) type frequency:(NSString *) frequency date:(NSDate *) date callback:(void(^)(NSArray*))callback{
733
- if([type isEqualToString:@"steps"] || [type isEqualToString:@"distance"]||[type isEqualToString:@"calories"]){
734
- dispatch_group_t loadDetailsGroup=dispatch_group_create();
735
- __block NSArray* stepsOrDistance = 0;
736
- __block NSString* totalActivityDuration = 0;
737
- for (int i = 0; i<2; i++) {
738
- dispatch_group_enter(loadDetailsGroup);
739
- if(i==0){
740
- [self getActivityTime:date frequency:frequency days:0 callback:^(NSMutableArray * result){
741
- totalActivityDuration = [result objectAtIndex:0];
742
- dispatch_group_leave(loadDetailsGroup);
743
- }];
744
- }else if(i==1){
745
- if([type isEqualToString:@"steps"] || [type isEqualToString:@"calories"]){
746
- if([frequency isEqualToString:@"day"]){
747
- [self fetchHourlySteps:date callback:^(NSArray * result) {
748
- stepsOrDistance = result;
749
- dispatch_group_leave(loadDetailsGroup);
750
- }];
751
- }else{
752
- [self fetchSteps:frequency endDate: date days:0 callback:^(NSArray * result) {
753
- stepsOrDistance = result;
754
- dispatch_group_leave(loadDetailsGroup);
755
- }];
756
- }
757
- }else if ([type isEqualToString:@"distance"]){
758
- if([frequency isEqualToString:@"day"]){
759
- [self fetchHourlyDistanceWalkingRunning:date callback:^(NSArray * result) {
760
- stepsOrDistance = result;
761
- dispatch_group_leave(loadDetailsGroup);
762
- }];
763
- }else{
764
- [self fetchDistanceWalkingRunning:frequency endDate: date days:0 callback:^(NSArray * result) {
765
- stepsOrDistance = result;
766
- dispatch_group_leave(loadDetailsGroup);
767
- }];
768
- }
769
- }
770
- }
771
- }
772
- dispatch_group_notify(loadDetailsGroup,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
773
- [self evaluateJavascript:stepsOrDistance type:type frequency:frequency activityTime:totalActivityDuration callback:^(NSArray * data) {
774
- callback(data);
775
- }];
776
- });
777
- }else if([type isEqualToString:@"sleep"]){
778
- if([frequency isEqualToString:@"day"]){
779
- [self fetchSleepPattern:date frequency:frequency days:0 callback:^(NSArray * results) {
780
- NSNumber* sleepTime = 0;
781
- NSNumber* wakeTime = 0;
782
- int count = 0;
783
- for (NSDictionary *object in results) {
784
- NSString* sleepValue = [object valueForKey:@"value"];
785
- if([sleepValue isEqualToString:@"INBED"]||[sleepValue isEqualToString:@"ASLEEP"]){
786
- if(count==0){
787
- sleepTime =
788
- [NSNumber numberWithDouble: [@(floor([[object valueForKey:@"startDate"] timeIntervalSince1970] * 1000)) longLongValue]];
789
-
790
- }
791
- wakeTime =
792
- [NSNumber numberWithDouble: [@(floor([[object valueForKey:@"endDate"] timeIntervalSince1970] * 1000)) longLongValue]];
793
- count++;
794
- }
795
- }
796
- NSLog(@"sleepTime and wakeTime data, %@ %@",sleepTime, wakeTime);
797
-
798
- if(sleepTime && wakeTime){
799
- NSString *javascript = [NSString stringWithFormat:@"DetailedGraph.updateDailySleep(%@,%@)", sleepTime,wakeTime];
800
- callback(@[javascript]);
801
- }else{
802
- NSString *javascript = [NSString stringWithFormat:@"DetailedGraph.updateDailySleep(0,0)"];
803
- callback(@[javascript]);
804
- }
805
- } ];
806
- }else{
807
- [self fetchSleepPattern:date frequency:frequency days:0 callback:^(NSArray * results) {
808
- NSMutableArray *data = [[NSMutableArray alloc]init];
809
- NSLog(@"weekly sleep results, %@", results);
810
- if([results count]){
811
- for (NSDictionary* item in results) {
812
- NSString* sleepValue = [item valueForKey:@"value"];
813
- if([sleepValue isEqualToString:@"INBED"]||[sleepValue isEqualToString:@"ASLEEP"]){
814
- NSDate* startDate = [item valueForKey:@"startDate"];
815
- NSDate* endDate = [item valueForKey:@"endDate"];
816
- NSTimeInterval interval;
817
- NSNumber* sleepTime =
818
- [NSNumber numberWithDouble: [@(floor([startDate timeIntervalSince1970] * 1000)) longLongValue]];
819
- NSNumber* wakeupTime =
820
- [NSNumber numberWithDouble: [@(floor([endDate timeIntervalSince1970] * 1000)) longLongValue]];
821
- NSLog(@"startDate before calendar function ,%@",startDate);
822
- [self->calendar rangeOfUnit:NSCalendarUnitDay
823
- startDate:&startDate
824
- interval:&interval
825
- forDate:endDate];
826
- NSLog(@"startDate after calendar function ,%@",startDate);
827
- NSNumber* startTimestamp =
828
- [NSNumber numberWithDouble: [@(floor([startDate timeIntervalSince1970] * 1000)) longLongValue]];
829
- NSDateComponents * dateComponents = [self->calendar components: NSCalendarUnitDay | NSCalendarUnitWeekday fromDate: endDate];
830
- NSString* day =self->calendar.shortWeekdaySymbols[dateComponents.weekday - 1];
831
- NSLog(@"Day name: %@", day);
832
- NSDictionary *element = @{
833
- @"sleepTime" : sleepTime,
834
- @"wakeupTime" : wakeupTime,
835
- @"day" : day,
836
- @"startTimestamp" : startTimestamp,
837
- };
838
- NSMutableDictionary *elem = [NSMutableDictionary dictionaryWithDictionary:element];
839
-
840
- NSLog(@"data is, ====>> %@",data);
841
- if([data count]>0){
842
- for (int i=0;i<[data count]; i++) {
843
- NSMutableDictionary* item = [data objectAtIndex:i];
844
- NSString* itemDay = [item objectForKey:@"day"];
845
- NSString* itemSleepTime = [item objectForKey:@"sleepTime"];
846
- if([itemDay isEqualToString:day]){
847
- [elem setValue:itemSleepTime forKey:@"sleepTime"];
848
- [data removeObjectAtIndex:i];
849
- NSLog(@"removed day is, ====>> %@",itemDay);
850
- }
851
- }
852
- [data addObject:elem];
853
- }else{
854
- [data addObject:elem];
855
- }
856
- }
857
- }
858
- }
859
- if([data count]<7 && [data count]>0){
860
- NSMutableDictionary* item = [data objectAtIndex:[data count]-1];
861
- NSNumber* startTimeStamp = [item objectForKey:@"startTimestamp"];
862
- NSTimeInterval unixTimeStamp = [startTimeStamp doubleValue] / 1000.0;
863
- NSMutableArray* newData = [self getBlankSleepWeeks:[data count] date:[NSDate dateWithTimeIntervalSince1970:unixTimeStamp]];
864
- [data addObjectsFromArray:newData];
865
- }
866
- NSLog(@"data is, %@",data);
867
- NSData* jsonArray = [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:nil ];
868
- NSString *jsonString = [[NSString alloc] initWithData:jsonArray encoding:NSUTF8StringEncoding];
869
- NSString *javascript = [NSString stringWithFormat:@"DetailedGraph.updateSleepData(JSON.stringify(%@))", jsonString];
870
- callback(@[javascript]);
871
- }];
872
- }
873
-
874
- }
875
- }
876
-
877
- - (NSDate *)convertStringToDate:(NSString *)date {
878
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
879
- [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
880
- NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
881
- [formatter setLocale:posix];
882
- return [formatter dateFromString:date];
883
- }
884
-
885
- -(void) getDateRanges:(NSDate*) startDate callback:(void(^)(NSMutableArray*))callback{
886
- NSCalendar *calendar = [NSCalendar currentCalendar];
887
- dispatch_group_t loadDetailsGroup=dispatch_group_create();
888
- NSDate *startOfToday = [[NSCalendar calendarWithIdentifier:NSCalendarIdentifierISO8601] startOfDayForDate:[NSDate date]];
889
- NSDate *startOfNextDay = [[NSCalendar calendarWithIdentifier:NSCalendarIdentifierISO8601] dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startOfToday options:0];
890
- NSDate *endOfToday = [[NSCalendar calendarWithIdentifier:NSCalendarIdentifierISO8601] dateByAddingUnit:NSCalendarUnitSecond value:-1 toDate:startOfNextDay options:0];
891
- NSDateComponents *days = [[NSDateComponents alloc] init];
892
- NSInteger dayCount = 0;
893
- NSMutableArray *dates=[NSMutableArray new];
894
- NSDate* startingDate = startDate;
895
- NSDateComponents *component = [calendar components:NSCalendarUnitDay
896
- fromDate:startingDate
897
- toDate:endOfToday
898
- options:0];
899
-
900
- NSInteger numberOfDays =[component day];
901
- if(numberOfDays>30){
902
- [component setDay:-30];
903
- startingDate =[calendar dateByAddingComponents:component toDate:endOfToday options:0];
904
- numberOfDays=30;
905
- }else{
906
- [component setDay:-numberOfDays-1];
907
- startingDate =[calendar dateByAddingComponents:component toDate:endOfToday options:0];
908
- }
909
- NSLog(@"numberOfDays are ,%ld, while startingDate is,%@",(long)numberOfDays,startDate);
910
-
911
- for (NSInteger i=numberOfDays; i>0; i--) {
912
- dispatch_group_enter(loadDetailsGroup);
913
- [days setDay: ++dayCount];
914
- NSDate *date = [calendar dateByAddingComponents: days toDate: startingDate options: 0];
915
- [dates addObject:date];
916
- dispatch_group_leave(loadDetailsGroup);
917
- }
918
-
919
- dispatch_group_notify(loadDetailsGroup,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
920
- NSLog(@"dispatch_group_notify called %@",dates);
921
- callback(dates);
922
- });
923
- }
924
-
925
- -(void) preprocessEmbellishRequest:(NSArray*) steps calories:(NSArray*) calories distance:(NSArray*) distance date:(NSDate*) date callback:(void(^)(NSArray*))callback{
926
- NSLog(@"steps=%@, calories=%@, distance=%@, date=%@",steps, calories, distance, date);
927
- NSMutableArray* embellishData = [NSMutableArray new];
928
- int count=0;
929
- for (NSNumber* step in steps) {
930
- NSDictionary *dict = @{
931
- @"st" : step,
932
- @"c" : [calories objectAtIndex:count],
933
- @"d" : [distance objectAtIndex:count],
934
- @"h" : [NSNumber numberWithInt:count],
935
- @"s" : @"",
936
- };
937
- count++;
938
- [embellishData addObject:dict];
939
- }
940
- NSLog(@"the httpBody is, %lu",(unsigned long)[embellishData count]);
941
- NSTimeInterval unixDate = [date timeIntervalSince1970]*1000;
942
- NSInteger finalDate = unixDate;
943
- NSDictionary *httpBody = @{
944
- @"data" : embellishData,
945
- @"dt" : [NSNumber numberWithLong:finalDate],
946
- };
947
- callback(@[httpBody]);
948
- }
949
-
950
- -(void)callSyncData:(NSInteger) days dates:(NSMutableArray*)dates callback:(void(^)(NSArray*))callback{
951
- dispatch_group_t syncDataGroup=dispatch_group_create();
952
- __block NSArray* steps;
953
- __block NSArray* calorie;
954
- __block NSArray* distanceData;
955
- __block NSArray* activityData;
956
- __block NSArray* sleep;
957
- // NSLog(@"days are %ld",(long)days);
958
- for (int i = 0; i<4; i++) {
959
- dispatch_group_enter(syncDataGroup);
960
- if(i==0){
961
- [self fetchSteps:@"custom" endDate:[NSDate date] days:days callback:^(NSArray * data) {
962
- NSLog(@"steps data for custom range is, %@ length %lu",[data objectAtIndex:0],[[data objectAtIndex:0] count]);
963
- steps = [data objectAtIndex:0];
964
- calorie = [data objectAtIndex:1];
965
- dispatch_group_leave(syncDataGroup);
966
- }];
967
- }else if(i==1){
968
- [self fetchDistanceWalkingRunning:@"custom" endDate:[NSDate date] days:days callback:^(NSArray * distance) {
969
- NSLog(@"distance data for custom range is, %@ length %lu",distance, [distance count]);
970
- distanceData=distance;
971
- dispatch_group_leave(syncDataGroup);
972
- }];
973
- }else if(i==2){
974
- [self getActivityTime:[NSDate date] frequency:@"custom" days:days callback:^(NSMutableArray * activity) {
975
- NSMutableArray* arr = [activity objectAtIndex:1];
976
- NSLog(@"activity data for custom range is, %@ length %lu",activity, [arr count]);
977
- activityData = arr;
978
- dispatch_group_leave(syncDataGroup);
979
- }];
980
- }else if(i==3){
981
- [self fetchSleepPattern:[NSDate date] frequency:@"custom" days:days callback:^(NSArray * sleepData) {
982
- NSMutableArray *data = [[NSMutableArray alloc]init];
983
- for (NSDictionary* item in sleepData) {
984
- NSString* sleepValue = [item valueForKey:@"value"];
985
- if([sleepValue isEqualToString:@"INBED"]||[sleepValue isEqualToString:@"ASLEEP"]){
986
- NSDate* startDate = [item valueForKey:@"startDate"];
987
- NSDate* endDate = [item valueForKey:@"endDate"];
988
- NSTimeInterval interval;
989
- NSNumber* sleepTime =
990
- [NSNumber numberWithDouble: [@(floor([startDate timeIntervalSince1970] * 1000)) longLongValue]];
991
- NSNumber* wakeupTime =
992
- [NSNumber numberWithDouble: [@(floor([endDate timeIntervalSince1970] * 1000)) longLongValue]];
993
- // NSLog(@"startDate before calendar function ,%@",startDate);
994
- [self->calendar rangeOfUnit:NSCalendarUnitDay
995
- startDate:&startDate
996
- interval:&interval
997
- forDate:endDate];
998
- // NSLog(@"startDate after calendar function ,%@",startDate);
999
- NSNumber* startTimestamp =
1000
- [NSNumber numberWithDouble: [@(floor([startDate timeIntervalSince1970] * 1000)) longLongValue]];
1001
- NSDictionary *element = @{
1002
- @"sleepTime" : sleepTime,
1003
- @"wakeupTime" : wakeupTime,
1004
- @"endDate" : endDate,
1005
- @"startTimestamp" : startTimestamp,
1006
- };
1007
- NSMutableDictionary *elem = [NSMutableDictionary dictionaryWithDictionary:element];
1008
-
1009
- if([data count]>0){
1010
- for (int i=0;i<[data count]; i++) {
1011
- NSMutableDictionary* item = [data objectAtIndex:i];
1012
- NSDate* itemEndDate = [item objectForKey:@"endDate"];
1013
- NSString* itemSleepTime = [item objectForKey:@"sleepTime"];
1014
- if([[NSCalendar currentCalendar] isDate:itemEndDate inSameDayAsDate:endDate]){
1015
- [elem setValue:itemSleepTime forKey:@"sleepTime"];
1016
- [data removeObjectAtIndex:i];
1017
- // NSLog(@"removed date is, ====>> %@",endDate);
1018
- }
1019
- }
1020
- [data addObject:elem];
1021
- }else{
1022
- [data addObject:elem];
1023
- }
1024
- }
1025
- }
1026
- sleep = data;
1027
- NSLog(@"fetchSleepPattern data is, ====>> %lu %@",(unsigned long)[data count],data);
1028
- dispatch_group_leave(syncDataGroup);
1029
- }];
1030
- }
1031
- }
1032
- // NSLog(@"callSyncData steps=%@, calories=%@, distance=%@, activity=%@, sleep=%@",steps, calorie, distanceData, activityData, sleep);
1033
-
1034
- dispatch_group_notify(syncDataGroup,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
1035
- if([steps count]>0 && [distanceData count]>0 && [activityData count]>0 && [sleep count]>0)
1036
- {
1037
- NSMutableArray* dailySyncData =[NSMutableArray new];
1038
- int count = 0;
1039
- for (NSDate* date in dates) {
1040
- NSDictionary* dict = @{
1041
- @"steps":[steps objectAtIndex:count],
1042
- @"calories":[calorie objectAtIndex:count],
1043
- @"distance":[distanceData objectAtIndex:count],
1044
- @"date":date
1045
- };
1046
- [dailySyncData addObject:[NSMutableDictionary dictionaryWithDictionary:dict]];
1047
- count++;
1048
- }
1049
- for (NSMutableDictionary* dict in dailySyncData) {
1050
- for(NSMutableDictionary* sleepData in sleep){
1051
- if([[NSCalendar currentCalendar] isDate:[sleepData objectForKey:@"endDate"] inSameDayAsDate:[dict objectForKey:@"date"]]){
1052
- NSString* sleepTime = [sleepData objectForKey:@"sleepTime"];
1053
- NSString* wakeupTime = [sleepData objectForKey:@"wakeupTime"];
1054
- [dict setObject:[NSString stringWithFormat:@"%@-%@",sleepTime,wakeupTime] forKey:@"sleep"];
1055
- }
1056
- }
1057
-
1058
- for(NSMutableDictionary* activity in activityData){
1059
- if([[NSCalendar currentCalendar] isDate:[activity objectForKey:@"date"] inSameDayAsDate:[dict objectForKey:@"date"]]){
1060
- NSString* activityValue = [activity objectForKey:@"value"];
1061
- [dict setObject:activityValue forKey:@"activity"];
1062
- }
1063
- }
1064
-
1065
- [dict setObject: [NSNumber numberWithDouble: [@(floor([[dict objectForKey:@"date"] timeIntervalSince1970] * 1000)) longLongValue]]
1066
- forKey:@"date"];
1067
- }
1068
- NSDictionary *httpBody = @{
1069
- @"fitnessData" : dailySyncData,
1070
- };
1071
- callback(@[httpBody]);
1072
-
1073
- }
1074
- });
1075
- }
1076
-
1077
- -(void)callEmbellishApi:(NSMutableArray*) dates callback:(void(^)(NSArray*))callback{
1078
- for (NSDate* date in dates) {
1079
- dispatch_group_t loadDetailsGroup=dispatch_group_create();
1080
- __block NSArray* steps;
1081
- __block NSArray* calories;
1082
- __block NSArray* distance;
1083
- for(int i = 0; i<2;i++){
1084
- dispatch_group_enter(loadDetailsGroup);
1085
- if(i==0){
1086
- [self fetchHourlySteps:date callback:^(NSArray * data) {
1087
- steps = [data objectAtIndex:0];
1088
- calories = [data objectAtIndex:1];
1089
- dispatch_group_leave(loadDetailsGroup);
1090
- }];
1091
- }else if(i==1){
1092
- [self fetchHourlyDistanceWalkingRunning:date callback:^(NSArray * dist) {
1093
- distance = dist;
1094
- dispatch_group_leave(loadDetailsGroup);
1095
- }];
1096
- }
1097
- }
1098
- dispatch_group_notify(loadDetailsGroup,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
1099
- if([steps count]>0 && [calories count] > 0 && [distance count]>0){
1100
- [self preprocessEmbellishRequest:steps calories:calories distance:distance date:date callback:^(NSArray * data) {
1101
- NSLog(@"preprocessEmbellishRequest data, %@",data);
1102
- callback(data);
1103
- }];
1104
- }
1105
- });
1106
- }
1107
- }
1108
-
1109
-
1110
- RCT_EXPORT_METHOD(connectToAppleHealth:(RCTResponseSenderBlock)callback)
1111
- {
1112
- // [self isHealthKitAvailable:callback];
1113
- [self canAccessHealthKit:^(BOOL value){
1114
- if(value){
1115
- [self onHealthKitPermissionGranted:^(NSDictionary * data) {
1116
- NSArray* finalData = @[data];
1117
- callback(finalData);
1118
- }];
1119
- }else{
1120
- // reject(@"Error", @"Unable to connect to Apple Health", nil);
1121
- [self requestAuthorization:^(NSDictionary * data) {
1122
- NSArray* finalData = @[data];
1123
- callback(finalData);
1124
- }];
1125
- }
1126
- }];
1127
- }
1128
-
1129
- RCT_EXPORT_METHOD(renderGraph:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
1130
- {
1131
- NSString *type = [input objectForKey:@"type"];
1132
- NSString *frequency = [input objectForKey:@"frequency"];
1133
- NSString *timestamp = [input objectForKey:@"timestamp"];
1134
- NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]/1000];
1135
- [self renderGraphData:type frequency:frequency date:date callback:^(NSArray * data) {
1136
- NSLog(@"renderGraphData data is, %@",data);
1137
- callback(@[[NSNull null], data]);
1138
- }];
1139
- }
1140
-
1141
- RCT_EXPORT_METHOD(updateApiUrl:(NSDictionary *)input)
1142
- {
1143
- NSTimeInterval gfHourlyLastSync = [[input objectForKey:@"gfHourlyLastSync"] doubleValue];
1144
- NSTimeInterval googleFitLastSync = [[input objectForKey:@"googleFitLastSync"] doubleValue];
1145
- NSDate* hourlyDataSyncTime = [NSDate dateWithTimeIntervalSince1970:gfHourlyLastSync/1000];
1146
- NSDate* dailyDataSyncTime = [NSDate dateWithTimeIntervalSince1970:googleFitLastSync/1000];
1147
- [self canAccessHealthKit:^(BOOL value){
1148
- if(value){
1149
- dispatch_group_t loadDetailsGroup = dispatch_group_create();
1150
- // NSMutableArray *apiData = [NSMutableArray arrayWithCapacity:2];
1151
- [self getDateRanges:hourlyDataSyncTime callback:^(NSMutableArray * dates) {
1152
- if([dates count]>0){
1153
- [self callEmbellishApi:dates callback:^(NSArray * data) {
1154
- // [apiData addObject:data];
1155
- NSLog(@"callEmbellishApi data is, %@",data);
1156
- [self sendEventWithName:@"EventReminder" body:@{@"callEmbellishApi":data}];
1157
- }];
1158
- }
1159
- }];
1160
- [self getDateRanges:dailyDataSyncTime callback:^(NSMutableArray * dates) {
1161
- if([dates count]>0){
1162
- [self callSyncData:[dates count] dates:dates callback:^(NSArray * data) {
1163
- // [apiData addObject:data];
1164
- [self sendEventWithName:@"EventReminder" body:@{@"callSyncData":data}];
1165
- }];
1166
- }
1167
- }];
1168
- dispatch_group_notify(loadDetailsGroup,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
1169
- // callback([apiData copy]);
1170
- });
1171
- }else{
1172
- [self requestAuthorization:^(NSDictionary * data) {
1173
- // callback(@[data]);
1174
- }];
1175
- }
1176
- }];
1177
- // }
1178
- }
1179
-
1180
-
1181
- RCT_EXPORT_METHOD(fetchHourlyData:(NSInteger)gfHourlyLastSync resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
1182
- NSLog(@"gfHourlyLastSync in fetchHourlyData is %ld",(long)gfHourlyLastSync);
1183
- NSDate* hourlyDataSyncTime = [NSDate dateWithTimeIntervalSince1970:gfHourlyLastSync/1000];
1184
- [self canAccessHealthKit:^(BOOL value){
1185
- if(value){
1186
- dispatch_group_t loadDetailsGroup=dispatch_group_create();
1187
- __block NSArray* steps;
1188
- __block NSArray* calories;
1189
- __block NSArray* distance;
1190
- for(int i = 0; i<2;i++){
1191
- dispatch_group_enter(loadDetailsGroup);
1192
- if(i==0){
1193
- [self fetchHourlySteps:hourlyDataSyncTime callback:^(NSArray * data) {
1194
- steps = [data objectAtIndex:0];
1195
- calories = [data objectAtIndex:1];
1196
- dispatch_group_leave(loadDetailsGroup);
1197
- }];
1198
- }else if(i==1){
1199
- [self fetchHourlyDistanceWalkingRunning:hourlyDataSyncTime callback:^(NSArray * dist) {
1200
- distance = dist;
1201
- dispatch_group_leave(loadDetailsGroup);
1202
- }];
1203
- }
1204
- }
1205
- dispatch_group_notify(loadDetailsGroup,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
1206
- if([steps count]>0 && [calories count] > 0 && [distance count]>0){
1207
- [self preprocessEmbellishRequest:steps calories:calories distance:distance date:hourlyDataSyncTime callback:^(NSArray * data) {
1208
- NSLog(@"fetchHourlyData data, %@",data);
1209
- resolve(data);
1210
- }];
1211
- }
1212
- });
1213
- }else{
1214
- NSError* error;
1215
- reject(@"error", @"Error fetching hourly data", error);
1216
- }
1217
- }];
1218
- }
1219
-
1220
- RCT_EXPORT_METHOD(fetchDailyData:(NSInteger)googleFitLastSync resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
1221
- NSLog(@"gfHourlyLastSync in fetchHourlyData is %ld",(long)googleFitLastSync);
1222
- NSDate* dailyDataSyncTime = [NSDate dateWithTimeIntervalSince1970:googleFitLastSync/1000];
1223
- [self canAccessHealthKit:^(BOOL value){
1224
- if(value){
1225
- [self getDateRanges:dailyDataSyncTime callback:^(NSMutableArray * dates) {
1226
- if([dates count]>0){
1227
- [self callSyncData:[dates count] dates:dates callback:^(NSArray * data) {
1228
- NSLog(@"fetchDailyData data is, %@",data);
1229
- resolve(data);
1230
- }];
1231
- }
1232
- }];
1233
- }else{
1234
- NSError* error;
1235
- reject(@"error", @"Error fetching daily data", error);
1236
- }
1237
- }];
1238
- }
1239
-
1240
- // HealthKit Authorization Check
1241
- RCT_REMAP_METHOD(getHealthKitStatus,
1242
- checkHealthKitAuthorizationWithResolver:(RCTPromiseResolveBlock)resolve
1243
- rejecter:(RCTPromiseRejectBlock)reject)
1244
- {
1245
- if (![HKHealthStore isHealthDataAvailable]) {
1246
- resolve(@(NO));
1247
- return;
1248
- }
1249
-
1250
- HKObjectType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
1251
- HKAuthorizationStatus status = [[VisitRnSdkViewManager sharedManager] authorizationStatusForType:stepCountType];
1252
-
1253
- if (status == HKAuthorizationStatusSharingAuthorized) {
1254
- NSInteger days = 1; // Example: Fetch data for the last 7 days
1255
- dispatch_group_t syncDataGroup = dispatch_group_create();
1256
- __block NSArray *stepsData;
1257
-
1258
- // Enter dispatch group before fetching data
1259
- dispatch_group_enter(syncDataGroup);
1260
- [self fetchSteps:@"custom"
1261
- endDate:[NSDate date]
1262
- days:days
1263
- callback:^(NSArray *data) {
1264
- NSLog(@"Fetched steps data: %@", data);
1265
- stepsData = [data objectAtIndex:0];
1266
- dispatch_group_leave(syncDataGroup);
1267
- }];
1268
-
1269
- // Wait for the fetch to complete
1270
- dispatch_group_notify(syncDataGroup, dispatch_get_main_queue(), ^{
1271
- if (stepsData) {
1272
- NSDictionary *result = @{
1273
- @"steps": stepsData,
1274
- };
1275
- resolve(result);
1276
- } else {
1277
- NSError *error = [NSError errorWithDomain:@"HealthKit"
1278
- code:500
1279
- userInfo:@{NSLocalizedDescriptionKey: @"Failed to fetch step data"}];
1280
- reject(@"fetch_error", @"Error fetching step data", error);
1281
- }
1282
- });
1283
- } else {
1284
- resolve(@(NO)); // HealthKit access not granted
1285
- }
1286
- }
1287
-
1288
- RCT_EXPORT_METHOD(triggerManualSync)
1289
- {
1290
- // Retrieve the stored values from NSUserDefaults
1291
- NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
1292
-
1293
- NSTimeInterval gfHourlyLastSync = [defaults doubleForKey:@"gfHourlyLastSync"];
1294
- NSTimeInterval googleFitLastSync = [defaults doubleForKey:@"googleFitLastSync"];
1295
-
1296
- // Log the retrieved values for debugging
1297
- NSLog(@"Retrieved gfHourlyLastSync: %f and googleFitLastSync: %f from NSUserDefaults", gfHourlyLastSync, googleFitLastSync);
1298
-
1299
- // Create a dictionary to pass the values
1300
- NSDictionary *input = @{
1301
- @"gfHourlyLastSync": @(gfHourlyLastSync),
1302
- @"googleFitLastSync": @(googleFitLastSync)
1303
- };
1304
-
1305
- // Call the existing updateApiUrl method
1306
- [self updateApiUrl:input];
1307
- }
1308
-
1309
23
  @end