Source: validic-cordova-googlefit/www/validic-cordova-googlefit.js




var exec = require('cordova/exec');
var channel = require('cordova/channel')

channel.onCordovaReady.subscribe(function () {
    exec(
        function (recordSummary) {
            cordova.fireDocumentEvent('validic:googlefit:onrecords', recordSummary)
        },
        function (error) {
            cordova.fireDocumentEvent('validic:googlefit:onerror', error)
        },
        "ValidicGoogleFit",
        "registerListener",
        []
    )
})
/**
 * Googe Fit integration with the Validic Platform. Using this plugin an application
 * can request to subscribe to data changes in Google Fit on behalf of the end user.
 * @exports GoogleFit
 */
exports.GoogleFit = {

    /**
     * Permission Result
     * @typedef {Object.<string,boolean>} PermissionResult
     */

    /**
     * @callback PermissionCallback
     * @param {PermissionResult} result
     */
    /**
     * @callback SubscriptionsCallback
     * @param {Array<DataType>} subscriptions
     */
    /**
     * Supported Data Types
     * @enum {string}
     */
    DataType: {
        STEP_COUNT: "com.google.step_count.delta",
        DISTANCE: "com.google.distance.delta",
        NUTRITION: "com.google.nutrition",
        WEIGHT: "com.google.weight",
        HEART_RATE: "com.google.heart_rate.bpm",
        CALORIES_EXPENDED: "com.google.calories.expended",
        MOVE_MINUTES: "com.google.active_minutes"
    },
    /**
     * Support History Types
     * @enum {string}
     */
    HistoryType: {
        SUMMARY: "summary"
    },
    /**
     * Request Permissions for a set of Data Types. If permission has already been granted this will
     * succeed immediately with success. Permission Results will be passed back to the success function.
     * @param {Array<DataType>} dataTypes
     * @param {PermissionCallback} success
     * @param {Function} error
     */
    requestPermissions: function (dataTypes, success, error) {
        exec(success, error, "ValidicGoogleFit", "requestPermissions", [dataTypes])
    },
    /**
     * Check if permissions for a set of datatypes has been granted by the end user
     * @param {Array<DataType>} dataTypes
     * @param {PermissionCallback} success
     * @param {Function} error
     */
    hasPermissions: function (dataTypes, success, error) {
        exec(success, error, "ValidicGoogleFit", "hasPermissions", [dataTypes])
    },
    /**
     * Subscribe to an array of dataTypes. These types will be persisted and subscribed to receive
     * data updates from the Google Fit Platform. Data is automatically uploaded to the Validic Platform.
     * @param {Array<DataType>} dataTypes
     * @param {Function} success
     * @param {Function} error
     */
    subscribe: function (dataTypes, success, error) {
        exec(success, error, "ValidicGoogleFit", "subscribe", [dataTypes])
    },
    /**
     * Unsubscribe from a Google Fit Data type
     * @param {DataType} dataType
     * @param {Function} success
     * @param {Function} error
     */
    unsubscribe: function (dataType, success, error) {
        exec(success, error, "ValidicGoogleFit", "unsubscribe", [dataType])
    },
    /**
     * Remove all subscriptions and logout Google user
     * @param {Function} callback
     */
    disconnect: function (callback) {
        exec(callback, null, "ValidicGoogleFit", "disconnect", [])
    },
    /**
     * Retrive list of current subscriptions
     * @param {SubscriptionsCallback} callback
     */
    subscriptions: function (callback) {
        exec(callback, null, "ValidicGoogleFit", "subscription", [])
    },

    /**
     *
     * @typedef HistoryOptions
     * @param {HistoryType[]} historyTypes - Array of types to fetch data for
     * @param {string} from - ISO 8601 Date String in the format yyyy-MM-dd for the start of the history query. Default is 180 days in the past.
     * @param {string} to - ISO 8601 Date String in the format yyyy-MM-dd for the end of the history query. Default is today.
     *
     */

    /**
     *
     * @param {HistoryOptions | HistoryType[]} historyOptions
     * @param {Function} success
     * @param {Function} error
     */
    fetchHistory: function (historyOptions, success, error) {
        if (Array.isArray(historyOptions)) {
            exec(success, error, "ValidicGoogleFit", "fetchHistory", [{ historyTypes: historyOptions }])
        } else if (typeof historyOptions === 'string' || historyOptions instanceof String) {
            exec(success, error, "ValidicGoogleFit", "fetchHistory", [{ historyTypes: [historyOptions] }])
        } else {
            exec(success, error, "ValidicGoogleFit", "fetchHistory", [historyOptions])
        }
    }

}