Skip to content

Commit

Permalink
added interval support (#18)
Browse files Browse the repository at this point in the history
* added interval support

* interval

* hour interval android

* readme
  • Loading branch information
CruelMoney authored and scerelli committed Dec 12, 2019
1 parent 9c7f149 commit 69e8823
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ local.properties
buck-out/
\.buckd/
*.keystore


.vscode
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions android/src/main/java/com/ovalmoney/fitness/RNFitnessModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
27 changes: 22 additions & 5 deletions android/src/main/java/com/ovalmoney/fitness/manager/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -139,17 +139,24 @@ 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)
.setStreamName("estimated_steps")
.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();

Expand Down Expand Up @@ -183,10 +190,20 @@ public void onComplete(@NonNull Task<DataReadResponse> 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();

Expand Down
17 changes: 15 additions & 2 deletions ios/RCTFitness/RCTFitness.m
Original file line number Diff line number Diff line change
Expand Up @@ -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){

Expand All @@ -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]];
Expand Down Expand Up @@ -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){

Expand All @@ -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]];
Expand Down
28 changes: 18 additions & 10 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import { NativeModules } from 'react-native';
import { NativeModules } from "react-native";

/**
* Get native getStep with parsed Dates
* @param startDate
* @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
* @param startDate
* @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
Expand All @@ -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;
};
Expand Down

0 comments on commit 69e8823

Please sign in to comment.