From 69e8823016da19dc07173aa66f1122a84a4420dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20Dengs=C3=B8?= Date: Thu, 12 Dec 2019 14:20:23 +0100 Subject: [PATCH] added interval support (#18) * added interval support * interval * hour interval android * readme --- .gitignore | 3 +- README.md | 8 +++--- .../ovalmoney/fitness/RNFitnessModule.java | 10 +++---- .../ovalmoney/fitness/manager/Manager.java | 27 ++++++++++++++---- ios/RCTFitness/RCTFitness.m | 17 +++++++++-- js/index.js | 28 ++++++++++++------- 6 files changed, 66 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 9cadbfe..305023f 100644 --- a/.gitignore +++ b/.gitignore @@ -43,4 +43,5 @@ local.properties buck-out/ \.buckd/ *.keystore - \ No newline at end of file + +.vscode \ No newline at end of file diff --git a/README.md b/README.md index 5095057..1ec52c7 100644 --- a/README.md +++ b/README.md @@ -75,11 +75,11 @@ Check if permissions are granted or not. It works on Android and iOS >= 12.0, wh - **Fitness.requestPermissions()** Ask permission and return if user granted or not(Android), while, due to Apple's privacy model, always true is returned in iOS. -- **Fitness.getSteps(dates: { startDate: string, endDate: string })** -Fetch steps on a given period of time. It requires an `Object` with `startDate` and `endDate` attributes as string. If startDate is not provided an error will be thrown. +- **Fitness.getSteps(dates: { startDate: string, endDate: string, interval: string })** +Fetch steps on a given period of time. It requires an `Object` with `startDate` and `endDate` attributes as string. If startDate is not provided an error will be thrown. Set `interval` to decide how detailed the returned data is, set it to `hour` otherwise it defaults to `days`. -- **Fitness.getDistance(dates: { startDate: string, endDate: string })** -Fetch distance in meters on a given period of time. It requires an `Object` with `startDate` and `endDate` attributes as string. If startDate is not provided an error will be thrown. +- **Fitness.getDistance(dates: { startDate: string, endDate: string, interval: string })** +Fetch distance in meters on a given period of time. It requires an `Object` with `startDate` and `endDate` attributes as string. If startDate is not provided an error will be thrown. Set `interval` to decide how detailed the returned data is, set it to `hour` otherwise it defaults to `days`. - **Fitness.getCalories(dates: { startDate: string, endDate: string })** Fetch calories burnt in kilocalories on a given period of time. It requires an `Object` with `startDate` and `endDate` attributes as string. If startDate is not provided an error will be thrown. diff --git a/android/src/main/java/com/ovalmoney/fitness/RNFitnessModule.java b/android/src/main/java/com/ovalmoney/fitness/RNFitnessModule.java index a0608ba..ab4c186 100644 --- a/android/src/main/java/com/ovalmoney/fitness/RNFitnessModule.java +++ b/android/src/main/java/com/ovalmoney/fitness/RNFitnessModule.java @@ -69,18 +69,18 @@ public void subscribeToSteps(Promise promise){ } @ReactMethod - public void getSteps(double startDate, double endDate, Promise promise){ + public void getSteps(double startDate, double endDate, String interval, Promise promise){ try { - manager.getSteps(getCurrentActivity(), startDate, endDate, promise); - }catch(Error e){ + manager.getSteps(getCurrentActivity(), startDate, endDate, interval, promise); + }catch(Error e){ promise.reject(e); } } @ReactMethod - public void getDistance(double startDate, double endDate, Promise promise){ + public void getDistance(double startDate, double endDate, String interval, Promise promise){ try { - manager.getDistance(getCurrentActivity(), startDate, endDate, promise); + manager.getDistance(getCurrentActivity(), startDate, endDate, interval, promise); }catch(Error e){ promise.reject(e); } diff --git a/android/src/main/java/com/ovalmoney/fitness/manager/Manager.java b/android/src/main/java/com/ovalmoney/fitness/manager/Manager.java index b5a6c38..9615c3c 100644 --- a/android/src/main/java/com/ovalmoney/fitness/manager/Manager.java +++ b/android/src/main/java/com/ovalmoney/fitness/manager/Manager.java @@ -3,7 +3,7 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; -import android.support.annotation.NonNull; +import androidx.annotation.NonNull; import com.facebook.react.bridge.ActivityEventListener; import com.facebook.react.bridge.Arguments; @@ -139,7 +139,7 @@ public void onFailure(@NonNull Exception e) { } - public void getSteps(Context context, double startDate, double endDate, final Promise promise){ + public void getSteps(Context context, double startDate, double endDate, String customInterval, final Promise promise){ DataSource ESTIMATED_STEP_DELTAS = new DataSource.Builder() .setDataType(DataType.TYPE_STEP_COUNT_DELTA) .setType(DataSource.TYPE_DERIVED) @@ -147,9 +147,16 @@ public void getSteps(Context context, double startDate, double endDate, final Pr .setAppPackageName("com.google.android.gms") .build(); + TimeUnit interval; + if(customInterval == "hour"){ + interval = TimeUnit.HOURS; + }else{ + interval = TimeUnit.DAYS; + } + DataReadRequest readRequest = new DataReadRequest.Builder() .aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_STEP_COUNT_DELTA) - .bucketByTime(1, TimeUnit.DAYS) + .bucketByTime(1, interval) .setTimeRange((long) startDate, (long) endDate, TimeUnit.MILLISECONDS) .build(); @@ -183,10 +190,20 @@ public void onComplete(@NonNull Task task) { }); } - public void getDistance(Context context, double startDate, double endDate, final Promise promise) { + public void getDistance(Context context, double startDate, double endDate, String customInterval,final Promise promise) { + + + TimeUnit interval; + if(customInterval == "hour"){ + interval = TimeUnit.HOURS; + }else{ + interval = TimeUnit.DAYS; + } + + DataReadRequest readRequest = new DataReadRequest.Builder() .aggregate(DataType.TYPE_DISTANCE_DELTA, DataType.AGGREGATE_DISTANCE_DELTA) - .bucketByTime(1, TimeUnit.DAYS) + .bucketByTime(1, interval) .setTimeRange((long) startDate, (long) endDate, TimeUnit.MILLISECONDS) .build(); diff --git a/ios/RCTFitness/RCTFitness.m b/ios/RCTFitness/RCTFitness.m index 4fd98cf..238e783 100644 --- a/ios/RCTFitness/RCTFitness.m +++ b/ios/RCTFitness/RCTFitness.m @@ -97,6 +97,7 @@ - (NSDictionary *)constantsToExport{ RCT_REMAP_METHOD(getSteps, withStartDate: (double) startDate andEndDate: (double) endDate + andInterval: (NSString *) customInterval withStepsResolver:(RCTPromiseResolveBlock)resolve andStepsRejecter:(RCTPromiseRejectBlock)reject){ @@ -111,7 +112,13 @@ - (NSDictionary *)constantsToExport{ NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *interval = [[NSDateComponents alloc] init]; - interval.day = 1; + + if(customInterval == @"hour"){ + interval.hour = 1; + }else{ + interval.day = 1; + } + NSDateComponents *anchorComponents = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]]; @@ -163,6 +170,7 @@ - (NSDictionary *)constantsToExport{ RCT_REMAP_METHOD(getDistance, withStartDate: (double) startDate andEndDate: (double) endDate + andInterval: (NSString *) customInterval withDistanceResolver:(RCTPromiseResolveBlock)resolve andDistanceRejecter:(RCTPromiseRejectBlock)reject){ @@ -177,7 +185,12 @@ - (NSDictionary *)constantsToExport{ NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *interval = [[NSDateComponents alloc] init]; - interval.day = 1; + + if(customInterval == @"hour"){ + interval.hour = 1; + }else{ + interval.day = 1; + } NSDateComponents *anchorComponents = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]]; diff --git a/js/index.js b/js/index.js index aeada98..c546cb6 100644 --- a/js/index.js +++ b/js/index.js @@ -1,4 +1,4 @@ -import { NativeModules } from 'react-native'; +import { NativeModules } from "react-native"; /** * Get native getStep with parsed Dates @@ -6,8 +6,12 @@ import { NativeModules } from 'react-native'; * @param endDate * @returns {*} */ -const getSteps = ({ startDate, endDate }) => - NativeModules.Fitness.getSteps(parseDate(startDate), parseDate(endDate)); +const getSteps = ({ startDate, endDate, interval = "days" }) => + NativeModules.Fitness.getSteps( + parseDate(startDate), + parseDate(endDate), + interval + ); /** * Get native getDistance with parsed Dates @@ -15,8 +19,12 @@ const getSteps = ({ startDate, endDate }) => * @param endDate * @returns {*} */ -const getDistance = ({ startDate, endDate }) => - NativeModules.Fitness.getDistance(parseDate(startDate), parseDate(endDate)); +const getDistance = ({ startDate, endDate, interval = "days" }) => + NativeModules.Fitness.getDistance( + parseDate(startDate), + parseDate(endDate), + interval + ); /** * Get native getCalories with parsed Dates @@ -31,14 +39,14 @@ NativeModules.Fitness.getCalories(parseDate(startDate), parseDate(endDate)); * Check if valid date and parse it * @param date: Date to parse */ -const parseDate = (date) => { - if(!date){ - throw Error('Date not valid'); +const parseDate = date => { + if (!date) { + throw Error("Date not valid"); } const parsed = Date.parse(date); - if(Number.isNaN(parsed)){ - throw Error('Date not valid'); + if (Number.isNaN(parsed)) { + throw Error("Date not valid"); } return parsed; };