diff --git a/README.md b/README.md index 9b8a776..94d0893 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,13 @@ In order to make it run, it is necessary to turn on `Health Kit` in the `Capabil ```java compile project(':@ovalmoney_react-native-fitness') ``` - +5. (Optional) In order to better handle the right versions, add in your `android/build.gradle`: + ```ext { + // Other build versions + fitnessPlayServices: "" // default: 17.0.0 + authPlayServices: "" // default: 17.0.0 + } + ``` ## Usage @@ -67,7 +73,7 @@ In order to make it run, it is necessary to turn on `Health Kit` in the `Capabil import Fitness from '@ovalmoney/react-native-fitness'; const permissions = [ - { kind: Fitness.PermissionKind.Step, access: Fitness.PermissionAccess.Write }, + { kind: Fitness.PermissionKinds.Steps, access: Fitness.PermissionAccesses.Write }, ]; Fitness.isAuthorized(permissions) @@ -83,19 +89,27 @@ Fitness.isAuthorized(permissions) - **Fitness.isAuthorized([{ kind: int, access: int }])** Check if permissions are granted or not. It works on Android and iOS >= 12.0, while it returns an error when iOS < 12. It requires an `Array` of `Object` with a mandatory key `kind` and an optional key `access`. -Possible values for the keys can be found in `PermissionKind` and `PermissionAccess` under `Attributes` section. +Possible values for the keys can be found in `PermissionKinds` and `PermissionAccesses` under `Attributes` section. On iOS at least one permissions with `Read` access must be provided, otherwise an `errorEmptyPermissions` will be thrown. - **Fitness.requestPermissions([{ kind: int, access: int }])** Ask permission and return if user granted or not(Android), while, due to Apple's privacy model, always true is returned in iOS. It requires an `Array` of `Object` with a mandatory key `kind` and an optional key `access`. -Possible values for the keys can be found in `PermissionKind` and `PermissionAccess` under `Attributes` section. +Possible values for the keys can be found in `PermissionKinds` and `PermissionAccesses` under `Attributes` section. On iOS at least one permissions with `Read` access must be provided, otherwise an `errorEmptyPermissions` will be thrown. +- **Fitness.logout()** +Available only on android. It performs a logout from google account. +It returns `true` for a successful logout, `false` if user cancel action. + +- **Fitness.disconnect()** +Available only on android. It performs a disconnect action from Google Fit. +It returns `true` for a successful logout, `false` if user cancel action. + - **Fitness.getSteps({ 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` or `minute` otherwise it defaults to `days`. -- **Fitness.getDistance({ startDate: string, endDate: string, interval: string })** +- **Fitness.getDistances({ 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` or `minute` otherwise it defaults to `days`. - **Fitness.getCalories({ startDate: string, endDate: string, interval: string })** @@ -120,31 +134,36 @@ Call this function to get steps and eliminate the need to have Google Fit instal #### Platform Return the used provider. -#### PermissionKind +#### PermissionKinds Return the information of what kind of Permission can be asked. At the moment the list of possible kinds is: - - ***Step***: to required the access for `Step` - - ***Distance***: to required the access for `Distances` + - ***Steps***: to required the access for `Steps` + - ***Distances***: to required the access for `Distances` - ***Calories***: to required the access for `Calories` - ***HeartRate***: to required the access for `Heart rate` - ***Activity***: to required the access for `Activity` (only Android) - ***SleepAnalysis***: to required the access for `Sleep Analysis` -#### PermissionAccess +#### PermissionAccesses Return the information of what kind of Access can be asked. The list of possible kinds is: - ***Read***: to required the access to `Read` - ***Write***: to required the access to `Write` -#### Error (iOS only) +#### Errors Return the list of meaningful errors that can be possible thrown. -On Android it is an empty object. + +##### iOS Possible values are: - ***hkNotAvailable***: thrown if HealthKit is not available - ***methodNotAvailable***: thrown if `isAuthorized` is called on iOS < 12.0 - - ***dateNotCorrect***: thrown if received date is not correct + - ***dateNotCorrect***: thrown if received date is not correct - ***errorEmptyPermissions***: thrown if no read permissions are provided - ***errorNoEvents***: thrown if an error occurs while try to retrieve data +##### Android +Possible values are: + - ***methodNotAvailable***: thrown if `getSleepAnalysis` is called on Android less than N + diff --git a/android/build.gradle b/android/build.gradle index 25d19d8..bdb6970 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -14,9 +14,9 @@ def safeExtGet(prop, fallback) { apply plugin: 'com.android.library' -def DEFAULT_COMPILE_SDK_VERSION = 28 -def DEFAULT_BUILD_TOOLS_VERSION = "28.0.3" -def DEFAULT_TARGET_SDK_VERSION = 28 +def DEFAULT_COMPILE_SDK_VERSION = 29 +def DEFAULT_BUILD_TOOLS_VERSION = "29.0.2" +def DEFAULT_TARGET_SDK_VERSION = 29 android { compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION) @@ -39,6 +39,6 @@ repositories { dependencies { implementation 'com.facebook.react:react-native:+' - implementation "com.google.android.gms:play-services-fitness:${safeExtGet('googlePlayServicesVersion', '17.0.0')}" - implementation "com.google.android.gms:play-services-auth:${safeExtGet('googlePlayServicesVersion', '17.0.0')}" + implementation "com.google.android.gms:play-services-fitness:${safeExtGet('fitnessPlayServices', '17.0.0')}" + implementation "com.google.android.gms:play-services-auth:${safeExtGet('authPlayServices', '17.0.0')}" } diff --git a/android/src/main/java/com/ovalmoney/fitness/RNFitnessModule.java b/android/src/main/java/com/ovalmoney/fitness/RNFitnessModule.java index 88a8322..c89df54 100644 --- a/android/src/main/java/com/ovalmoney/fitness/RNFitnessModule.java +++ b/android/src/main/java/com/ovalmoney/fitness/RNFitnessModule.java @@ -16,7 +16,7 @@ import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.Promise; import com.ovalmoney.fitness.manager.FitnessError; -import com.ovalmoney.fitness.permission.Permission; +import com.ovalmoney.fitness.permission.Permissions; import com.ovalmoney.fitness.permission.Request; public class RNFitnessModule extends ReactContextBaseJavaModule{ @@ -25,21 +25,21 @@ public class RNFitnessModule extends ReactContextBaseJavaModule{ private final static String PLATFORM_KEY = "Platform"; private final static String PLATFORM = "GoogleFit"; - private final static String ERROR_KEY = "Error"; + private final static String ERRORS_KEY = "Errors"; private final static String ERROR_METHOD_NOT_AVAILABLE_KEY = "methodNotAvailable"; - private final static String PERMISSIONS_KEY = "PermissionKind"; - private final static String STEP_KEY = "Step"; + private final static String PERMISSIONS_KEY = "PermissionKinds"; + private final static String STEPS_KEY = "Steps"; private final static String ACTIVITY_KEY = "Activity"; private final static String CALORIES_KEY = "Calories"; - private final static String DISTANCE_KEY = "Distance"; + private final static String DISTANCES_KEY = "Distances"; private final static String HEART_RATE_KEY = "HeartRate"; private final static String SLEEP_ANALYSIS_KEY = "SleepAnalysis"; - private final static String ACCESS_TYPE_KEY = "PermissionAccess"; + private final static String ACCESS_TYPE_KEY = "PermissionAccesses"; - private final static String READ = "READ"; - private final static String WRITE = "WRITE"; + private final static String READ = "Read"; + private final static String WRITE = "Write"; private final static Map PERMISSIONS = new HashMap<>(); private final static Map ACCESSES = new HashMap<>(); @@ -51,17 +51,18 @@ public RNFitnessModule(ReactApplicationContext reactContext) { super(reactContext); feedPermissionsMap(); feedAccessesTypeMap(); + feedErrorsMap(); this.manager = new Manager(); reactContext.addActivityEventListener(this.manager); } private void feedPermissionsMap(){ - PERMISSIONS.put(STEP_KEY, Permission.STEP); - PERMISSIONS.put(DISTANCE_KEY, Permission.DISTANCE); - PERMISSIONS.put(ACTIVITY_KEY, Permission.ACTIVITY); - PERMISSIONS.put(CALORIES_KEY, Permission.CALORIES); - PERMISSIONS.put(HEART_RATE_KEY, Permission.HEART_RATE); - PERMISSIONS.put(SLEEP_ANALYSIS_KEY, Permission.SLEEP_ANALYSIS); + PERMISSIONS.put(STEPS_KEY, Permissions.STEPS); + PERMISSIONS.put(DISTANCES_KEY, Permissions.DISTANCES); + PERMISSIONS.put(ACTIVITY_KEY, Permissions.ACTIVITY); + PERMISSIONS.put(CALORIES_KEY, Permissions.CALORIES); + PERMISSIONS.put(HEART_RATE_KEY, Permissions.HEART_RATE); + PERMISSIONS.put(SLEEP_ANALYSIS_KEY, Permissions.SLEEP_ANALYSIS); } private void feedAccessesTypeMap(){ @@ -84,7 +85,7 @@ public Map getConstants() { constants.put(PLATFORM_KEY, PLATFORM); constants.put(PERMISSIONS_KEY, PERMISSIONS); constants.put(ACCESS_TYPE_KEY, ACCESSES); - constants.put(ERROR_KEY, ERRORS); + constants.put(ERRORS_KEY, ERRORS); return constants; } @@ -103,6 +104,27 @@ public void requestPermissions(ReadableArray permissions, Promise promise){ } } + + @ReactMethod + public void logout(Promise promise){ + final Activity activity = getCurrentActivity(); + if(activity != null) { + manager.logout(activity, promise); + }else{ + promise.reject(new Throwable()); + } + } + + @ReactMethod + public void disconnect(Promise promise){ + final Activity activity = getCurrentActivity(); + if(activity != null) { + manager.disconnect(activity, promise); + }else{ + promise.reject(new Throwable()); + } + } + @ReactMethod public void subscribeToActivity(Promise promise){ try { diff --git a/android/src/main/java/com/ovalmoney/fitness/manager/FitnessError.java b/android/src/main/java/com/ovalmoney/fitness/manager/FitnessError.java index 442cc4c..9cc452f 100644 --- a/android/src/main/java/com/ovalmoney/fitness/manager/FitnessError.java +++ b/android/src/main/java/com/ovalmoney/fitness/manager/FitnessError.java @@ -11,5 +11,5 @@ }) public @interface FitnessError { - int ERROR_METHOD_NOT_AVAILABLE = -101; + int ERROR_METHOD_NOT_AVAILABLE = -99; } \ No newline at end of file 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 5d97155..54a5db4 100644 --- a/android/src/main/java/com/ovalmoney/fitness/manager/Manager.java +++ b/android/src/main/java/com/ovalmoney/fitness/manager/Manager.java @@ -3,9 +3,7 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; -import android.nfc.Tag; import android.os.Build; -import android.os.Bundle; import android.util.Log; import androidx.annotation.NonNull; @@ -18,6 +16,7 @@ import com.facebook.react.bridge.WritableMap; import com.google.android.gms.auth.api.signin.GoogleSignIn; import com.google.android.gms.auth.api.signin.GoogleSignInAccount; +import com.google.android.gms.auth.api.signin.GoogleSignInOptions; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GoogleApiAvailability; import com.google.android.gms.fitness.Fitness; @@ -37,8 +36,8 @@ import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.OnFailureListener; import com.google.android.gms.tasks.OnSuccessListener; +import com.google.android.gms.tasks.OnCanceledListener; import com.google.android.gms.tasks.Task; -import com.google.android.gms.fitness.data.Subscription; import com.ovalmoney.fitness.permission.Request; import java.text.DateFormat; @@ -50,12 +49,12 @@ import java.util.function.Predicate; import java.util.stream.Collectors; -import static com.ovalmoney.fitness.permission.Permission.ACTIVITY; -import static com.ovalmoney.fitness.permission.Permission.CALORIES; -import static com.ovalmoney.fitness.permission.Permission.DISTANCE; -import static com.ovalmoney.fitness.permission.Permission.STEP; -import static com.ovalmoney.fitness.permission.Permission.HEART_RATE; -import static com.ovalmoney.fitness.permission.Permission.SLEEP_ANALYSIS; +import static com.ovalmoney.fitness.permission.Permissions.ACTIVITY; +import static com.ovalmoney.fitness.permission.Permissions.CALORIES; +import static com.ovalmoney.fitness.permission.Permissions.DISTANCES; +import static com.ovalmoney.fitness.permission.Permissions.STEPS; +import static com.ovalmoney.fitness.permission.Permissions.HEART_RATE; +import static com.ovalmoney.fitness.permission.Permissions.SLEEP_ANALYSIS; public class Manager implements ActivityEventListener { @@ -92,12 +91,12 @@ protected FitnessOptions.Builder addPermissionToFitnessOptions(final FitnessOpti for(int i = 0; i < length; i++){ Request currentRequest = permissions.get(i); switch(currentRequest.permissionKind){ - case STEP: + case STEPS: fitnessOptions .addDataType(DataType.TYPE_STEP_COUNT_DELTA, currentRequest.permissionAccess) .addDataType(DataType.TYPE_STEP_COUNT_CUMULATIVE, currentRequest.permissionAccess); break; - case DISTANCE: + case DISTANCES: fitnessOptions.addDataType(DataType.TYPE_DISTANCE_DELTA, currentRequest.permissionAccess); break; case CALORIES: @@ -109,6 +108,7 @@ protected FitnessOptions.Builder addPermissionToFitnessOptions(final FitnessOpti case HEART_RATE: fitnessOptions.addDataType(DataType.TYPE_HEART_RATE_BPM, currentRequest.permissionAccess); break; + case SLEEP_ANALYSIS: default: break; } @@ -119,7 +119,7 @@ protected FitnessOptions.Builder addPermissionToFitnessOptions(final FitnessOpti public boolean isAuthorized(final Activity activity, final ArrayList permissions){ if(isGooglePlayServicesAvailable(activity)) { - FitnessOptions fitnessOptions = addPermissionToFitnessOptions(FitnessOptions.builder(), permissions) + final FitnessOptions fitnessOptions = addPermissionToFitnessOptions(FitnessOptions.builder(), permissions) .build(); return GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(activity), fitnessOptions); } @@ -151,6 +151,63 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode, } } + + public void logout(@NonNull Activity currentActivity, final Promise promise) { + final GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).build(); + GoogleSignIn.getClient(currentActivity, gso) + .revokeAccess() + .addOnCanceledListener(new OnCanceledListener() { + @Override + public void onCanceled() { + promise.resolve(false); + } + }) + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(Void aVoid) { + promise.resolve(true); + } + }) + .addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception e) { + promise.reject(e); + } + }) + .addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception e) { + promise.reject(e); + } + }); + } + + public void disconnect(@NonNull Activity currentActivity, final Promise promise) { + Fitness.getConfigClient( + currentActivity, + GoogleSignIn.getLastSignedInAccount(currentActivity.getApplicationContext() + )) + .disableFit() + .addOnCanceledListener(new OnCanceledListener() { + @Override + public void onCanceled() { + promise.resolve(false); + } + }) + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(Void aVoid) { + promise.resolve(true); + } + }) + .addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception e) { + promise.reject(e); + } + }); + } + @Override public void onNewIntent(Intent intent) { } diff --git a/android/src/main/java/com/ovalmoney/fitness/permission/Permission.java b/android/src/main/java/com/ovalmoney/fitness/permission/Permissions.java similarity index 53% rename from android/src/main/java/com/ovalmoney/fitness/permission/Permission.java rename to android/src/main/java/com/ovalmoney/fitness/permission/Permissions.java index 747b881..ce0a58f 100644 --- a/android/src/main/java/com/ovalmoney/fitness/permission/Permission.java +++ b/android/src/main/java/com/ovalmoney/fitness/permission/Permissions.java @@ -8,16 +8,17 @@ @Retention(SOURCE) @IntDef({ - Permission.STEP, - Permission.DISTANCE, - Permission.ACTIVITY, - Permission.CALORIES, - Permission.SLEEP_ANALYSIS, + Permissions.STEPS, + Permissions.DISTANCES, + Permissions.ACTIVITY, + Permissions.CALORIES, + Permissions.HEART_RATE, + Permissions.SLEEP_ANALYSIS, }) - public @interface Permission { - int STEP = 0; - int DISTANCE = 1; + public @interface Permissions { + int STEPS = 0; + int DISTANCES = 1; int ACTIVITY = 2; int CALORIES = 3; int HEART_RATE = 4; diff --git a/android/src/main/java/com/ovalmoney/fitness/permission/Request.java b/android/src/main/java/com/ovalmoney/fitness/permission/Request.java index 1e0477e..973b6b9 100644 --- a/android/src/main/java/com/ovalmoney/fitness/permission/Request.java +++ b/android/src/main/java/com/ovalmoney/fitness/permission/Request.java @@ -1,10 +1,10 @@ package com.ovalmoney.fitness.permission; public class Request { - public final @Permission int permissionKind; + public final @Permissions int permissionKind; public final int permissionAccess; - public Request(@Permission int permissionKind, int permissionAccess) { + public Request(@Permissions int permissionKind, int permissionAccess) { this.permissionKind = permissionKind; this.permissionAccess = permissionAccess; } diff --git a/ios/RCTFitness/RCTFitness+Permissions.h b/ios/RCTFitness/RCTFitness+Permissions.h index 81b8b58..6d930b7 100644 --- a/ios/RCTFitness/RCTFitness+Permissions.h +++ b/ios/RCTFitness/RCTFitness+Permissions.h @@ -4,8 +4,8 @@ typedef NS_ENUM(NSInteger, RCTFitnessPermissionKind) { - STEP = 0, - DISTANCE, + STEPS = 0, + DISTANCES, ACTIVITY, CALORIES, HEART_RATE, diff --git a/ios/RCTFitness/RCTFitness+Permissions.m b/ios/RCTFitness/RCTFitness+Permissions.m index c7b918c..44bd2af 100644 --- a/ios/RCTFitness/RCTFitness+Permissions.m +++ b/ios/RCTFitness/RCTFitness+Permissions.m @@ -13,9 +13,9 @@ @implementation RCTFitness(Permissions) +(HKObjectType*)getQuantityType:(RCTFitnessPermissionKind)permission { switch (permission) { - case STEP: + case STEPS: return [HKObjectType quantityTypeForIdentifier: HKQuantityTypeIdentifierStepCount]; - case DISTANCE: + case DISTANCES: return [HKObjectType quantityTypeForIdentifier: HKQuantityTypeIdentifierDistanceWalkingRunning]; case ACTIVITY: return nil; diff --git a/ios/RCTFitness/RCTFitness.m b/ios/RCTFitness/RCTFitness.m index 4bed9d8..257a545 100644 --- a/ios/RCTFitness/RCTFitness.m +++ b/ios/RCTFitness/RCTFitness.m @@ -19,13 +19,13 @@ @implementation RCTConvert (Fitness) @end @implementation RCTConvert (Permission) -RCT_ENUM_CONVERTER(RCTFitnessPermissionKind, (@{ @"Step" : @(STEP), - @"Distance" : @(DISTANCE), +RCT_ENUM_CONVERTER(RCTFitnessPermissionKind, (@{ @"Steps" : @(STEPS), + @"Distances" : @(DISTANCES), @"Calories" : @(CALORIES), @"Activity" : @(ACTIVITY), @"HeartRate" : @(HEART_RATE), @"SleepAnalysis" : @(SLEEP_ANALYSIS)}), - STEP, integerValue) + STEPS, integerValue) @end @implementation RCTConvert (PermissionAccess) @@ -80,21 +80,21 @@ - (void) handlePermissions:(NSArray*)permissions returnBlock:(void (^)(NSSet* re - (NSDictionary *)constantsToExport{ return @{ @"Platform" : @"AppleHealth", - @"Error": @{ + @"Errors": @{ @"hkNotAvailable" : @(ErrorHKNotAvailable), @"methodNotAvailable" : @(ErrorMethodNotAvailable), @"dateNotCorrect" : @(ErrorDateNotCorrect), @"emptyPermission" : @(ErrorEmptyPermissions), }, - @"PermissionKind": @{ - @"Step": @(STEP), - @"Distance": @(DISTANCE), + @"PermissionKinds": @{ + @"Steps": @(STEPS), + @"Distances": @(DISTANCES), @"Calories": @(CALORIES), @"Activity": @(ACTIVITY), @"HeartRate": @(HEART_RATE), @"SleepAnalysis" : @(SLEEP_ANALYSIS), }, - @"PermissionAccess": @{ + @"PermissionAccesses": @{ @"Read": @(READ), @"Write": @(WRITE), }, diff --git a/js/index.d.ts b/js/index.d.ts index 4157cab..9e6cc65 100644 --- a/js/index.d.ts +++ b/js/index.d.ts @@ -1,20 +1,35 @@ -export declare enum PermissionKind { - Steps = 0, - Distances = 1, - Calories = 2, - HeartRate = 3, - Activity = 4, - SleepAnalysis = 5, -} +const Errors = { + ErrorHKNotAvailable: -100, + ErrorMethodNotAvailable: -99, + ErrorDateNotCorrect: -98, + ErrorNoEvents: -97, + ErrorEmptyPermissions: -96, +} as const; + +export declare type Errors = typeof Errors[keyof typeof Errors]; + +const PermissionKinds = { + Steps: 0, + Distances: 1, + Calories: 2, + HeartRate: 3, + Activity: 4, + SleepAnalysis: 5, +} as const; + +export declare type PermissionKinds = typeof PermissionKinds[keyof typeof PermissionKinds]; + +const PermissionAccesses = { + Read: 0, + Write: 1, +} as const; + +export declare type PermissionAccesses = typeof PermissionAccesses[keyof typeof PermissionAccesses]; -export declare enum PermissionAccess { - Read = 0, - Write = 1, -} export declare type Permission = { - kind: PermissionKind, - access: PermissionAccess, + kind: PermissionKinds, + access: PermissionAccesses, } /** @@ -34,6 +49,20 @@ export declare function isAuthorized(permissions: Permission[]): Promise +/** + * Disconnect from Google Fit. + * + * @return Promise + */ +export declare function disconnect(): Promise + +/** + * Log out from Google account. + * + * @return Promise + */ +export declare function logout(): Promise + /** * Data interval. * @@ -41,7 +70,7 @@ export declare function requestPermissions(permissions: Permission[]): Promise + * @param request Request + * @return Promise */ -export declare function getSteps(request: StepsRequest): Promise - -export declare interface DistanceRequest { - startDate: string - endDate: string - interval: Interval -} +export declare function getSteps(request: Request): Promise export declare interface DistanceRecord { startDate: string @@ -79,8 +100,6 @@ export declare interface DistanceRecord { quantity: number } -export declare type DistanceResponse = DistanceRecord[] - /** * Fetch distance in meters on a given period of time. * @@ -88,25 +107,17 @@ export declare type DistanceResponse = DistanceRecord[] * If endDate is not provided, the current date will be used. * Set interval to decide how detailed the returned data is, set it to hour or minute otherwise it defaults to days. * - * @param request DistanceRequest - * @return Promise + * @param request Request + * @return Promise */ -export declare function getDistance(request: DistanceRequest): Promise +export declare function getDistances(request: Request): Promise -export declare interface CaloriesRequest { - startDate: string - endDate: string - interval: Interval -} - -export declare interface CalorieRecord { +export declare interface CaloriesRecord { startDate: string endDate: string quantity: number } -export declare type CaloriesResponse = CalorieRecord[] - /** * Fetch calories burnt in kilocalories on a given period of time. * @@ -114,16 +125,10 @@ export declare type CaloriesResponse = CalorieRecord[] * If endDate is not provided, the current date will be used. * Set interval to decide how detailed the returned data is, set it to hour or minute otherwise it defaults to days. * - * @param request CaloriesRequest - * @return Promise + * @param request Request + * @return Promise */ -export declare function getCalories(request: CaloriesRequest): Promise - -export declare interface HeartRateRequest { - startDate: string - endDate: string - interval: Interval -} +export declare function getCalories(request: Request): Promise export declare interface HeartRateRecord { startDate: string @@ -131,8 +136,6 @@ export declare interface HeartRateRecord { quantity: number } -export declare type HeartRateResponse = HeartRateRecord[] - /** * Fetch heart rate bpm on a given period of time. * @@ -140,10 +143,10 @@ export declare type HeartRateResponse = HeartRateRecord[] * If endDate is not provided, the current date will be used. * Set interval to decide how detailed the returned data is, set it to hour or minute otherwise it defaults to days. * - * @param request HeartRateRequest - * @return Promise + * @param request Request + * @return Promise */ -export declare function getHeartRate(request: HeartRateRequest): Promise +export declare function getHeartRate(request: Request): Promise export declare interface SleepAnalysisRequest { startDate: string @@ -158,8 +161,6 @@ export declare interface SleepAnalysisRecord { sourceId: string } -export declare type SleepAnalysisResponse = SleepAnalysisRecord[] - /** * Fetch sleep analysis data on a given period of time. * @@ -167,9 +168,9 @@ export declare type SleepAnalysisResponse = SleepAnalysisRecord[] * If startDate is not provided an error will be thrown. * * @param request SleepAnalysisRequest - * @return Promise + * @return Promise */ -export declare function getSleepAnalysis(request: SleepAnalysisRequest): Promise +export declare function getSleepAnalysis(request: SleepAnalysisRequest): Promise /** * Available only on Android. diff --git a/js/index.js b/js/index.js index bdbe77c..5c7fb12 100644 --- a/js/index.js +++ b/js/index.js @@ -1,4 +1,22 @@ -import { NativeModules } from "react-native"; +import { NativeModules, Platform } from "react-native"; + +/** + * Log out from Google Account + * @returns {*} + */ +const logout = Platform.select({ + ios: () => null, + android: () => NativeModules.Fitness.logout(), +}); + +/** + * Disconnect Google Fit + * @returns {*} + */ +const disconnect = Platform.select({ + ios: () => null, + android: () => NativeModules.Fitness.disconnect(), +}); /** * Get native getStep with parsed Dates @@ -85,6 +103,8 @@ const parseDate = date => { export default { ...NativeModules.Fitness, + logout, + disconnect, getSteps, getDistance, getCalories, diff --git a/package.json b/package.json index b130093..bbaea06 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ovalmoney/react-native-fitness", - "version": "0.4.0", + "version": "0.5.0", "description": "A React Native package that gives you access to Apple kit and Google Fit data.", "main": "./js/index.js", "keywords": [ @@ -16,18 +16,6 @@ { "name": "Francesco Voto", "email": "fv@ovalmoney.com" - }, - { - "name": "Andrea Pacino", - "email": "ap@ovalmoney.com" - }, - { - "name": "Stefano Cerelli", - "email": "stefano.cerelli@ovalmoney.com" - }, - { - "name": "Alessio Occhipinti", - "email": "alessio.occhipinti@ovalmoney.com" } ], "license": "MIT",