Source: validic-cordova-healthkit/www/HealthKit.js


/**
 * @callback HKCallback
 * @property {string} event - Name of event to fire in the window, 'validicOnHealthKitRecordsProcessed'
 * @property {Object} payload - The payload of the event.
 * @property {number} payload.recordType - Value of ValidicMobile.RecordType indicating type of
 * records processed.
 * @property {number} payload.count - Number of records processed.
 */
/**
 * @callback HKHistoricalFetchCallback
 * @property {string} {RecordType} - Number of records fetched of type ValidicMobile.RecordType. The property name is a string
 *     containing the numeric value from ValidicMobile.RecordType.
 */
// HealthKit related responses
/**
 * Callback response from HealthKit.getSubscriptions
 * @callback HKSampleTypesCallback
 * @property {string[]} sampleTypes - Array of strings representing HealthKit sample types. Values match
 * constants defined in ValidicMobile.HealthKit.SampleType.
 */
/**
 * Callback response from HealthKit.setSubscriptions
 * @callback HKSetSubscriptionsCallback
 * @property {string[]}  unknownIdentifiers - Array of strings representing HealthKit sample types which
 * could not be subscribed to.
 */

/**
 * @exports HealthKit
 * Represents Validic HealthKit functionality.
 */
exports.HealthKit = {
    /**
     *
     * @param {string[]} params - Array of Strings which map to sample types. See {@link HealthKit.SampleType}
     * @param {HKSetSubscriptionsCallback} success - Subscriptions were set successfully
     * @param {FailCallback} error - Invalid parameters were given
     */
    setSubscriptions: function(params, success, error) {
        cordova.exec(success, error, "ValidicHealthKit", "setHealthKitSubscriptions", [params]);
    },

    /**
     * @param {HKSampleTypesCallback} success - Response containing array of currently subscribed sample types.
     */
    getSubscriptions: function(success) {
        cordova.exec(success, null, "ValidicHealthKit", "subscriptions", []);
    },
    /**
     * Indicates the availability of HealthKit
     * @param {AvailableCallback} callback - Callback function including whether healthkit is available for the platform
     *     the application is running on. For Android and iPad this callback will always contain {isAvailable: false}
     */
    isHealthKitAvailable: function(callback) {
        cordova.exec(callback, null, "ValidicHealthKit", "isHealthKitAvailable", []);
    },
    /**
     * Initiate new queries against HealthKit.
     */
    fetchLatestData: function(success) {
        cordova.exec(success, null, "ValidicHealthKit", "fetchLatestData", []);
    },
    /**
     *
     * @typedef HistoryOptions
     * @param {HistoricalSet[]} historicalSets
     * @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.
     *
     */
    /**
      * Fetch historical data for the specified sets across the specified dates.
      * @param {HistoryOptions | HistoricalSet[]} historyOptions - HistoricalSet and Date query options
      * @param {HKHistoricalFetchCallback} success - Response containing count of records for each record type keyed by
            record type values from ValidicMobile.RecordType.
      * @param {FailCallback} error - Error response if invalid historical sets are provided.
      */
    fetchHistoricalSets: function(historyOptions, success, error) {
        if (Array.isArray(historyOptions)) {
            cordova.exec(success, error, "ValidicHealthKit", "fetchHistoricalSets", [{historicalSets: historyOptions}]);
        } else {
            cordova.exec(success, error, "ValidicHealthKit", "fetchHistoricalSets", [historyOptions]);
        }
    },
    /**
     * Returns sample types associated with a subscription set. See ValidicMobile library documentation for description of sets.
     * @param {SubscriptionSet} subscriptionSetID - String representing one of the available subscription sets from SubscriptionSet.
     * @param {HKSampleTypesCallback} success - Response containing array of sample types for given subscription set.
     * @param {FailCallback} error - Error response if invalid subscription set provided.
     */
    getSampleTypesForSubscriptionSet: function(subscriptionSetID, success, error) {
        cordova.exec(success, error, "ValidicHealthKit", "sampleTypesForSubscriptionSet", [subscriptionSetID]);
    },
    /**
     * Returns sample types associated with a record type.
     * @deprecated getSampleTypesForRecordType is deprecated, please use getSampleTypesForSubscriptionSet instead.
     * @param {RecordType} recordType - Number representing one of the available record types from RecordType.
     * @param {HKSampleTypesCallback} success - Response containing array of sample types for given record type.
     * @param {FailCallback} error - Error response is invalid subscription set provided.
     */
    getSampleTypesForRecordType: function(recordType, success, error) {
        console.warn("getSampleTypesForRecordType is deprecated, please use getSampleTypesForSubscriptionSet instead.")
        cordova.exec(success, error, "ValidicHealthKit", "sampleTypesForRecordType", [recordType]);
    },
    /**
     * Registers listener for HealthKit records processed event. It is recommended to use event listeners instead.
     * @param {HKCallback} success - Function invoked when HealthKit records have been processed and submitted.
     */
    setHealthKitListener: function(success) {
        var successful = function(obj) {
            if (success != null && success != 'undefined') {
                success(obj);
            }
            if (obj.hasOwnProperty("event")) {
                cordova.fireWindowEvent(obj.event, obj.payload);
            }
        };
        cordova.exec(successful, null, "ValidicHealthKit", "setHealthKitListener", []);
    },
    /**
     * @enum {string} HealthKit record processed event name.
     */
    EventName: {
        ON_HEALTHKIT_RECORD_PROCESSED: "validicOnHealthKitRecordsProcessed"
    },
    /**
     * Identifiers for available subscription sets.
     * @enum {number}
     */
    SubscriptionSet: {
        ROUTINE: 0,
        DIABETES: 1,
        WEIGHT: 2,
        FITNESS: 3,
        SLEEP: 4,
        BASIC_NUTRITION: 5,
        REPRODUCTIVE_HEALTH: 6,
        BIOMETRICS: 7
    },

    /**
     * Identifiers for available historical fetch sets.
     * @enum {string}
     */
    HistoricalSet: {
        ROUTINE: "routine",
        FITNESS: "fitness"
    },

    /**
        String constants representing HealthKit sample types.
        @enum {string}
     */
    SampleType: {
        HKQuantityTypeIdentifierBodyMassIndex: "HKQuantityTypeIdentifierBodyMassIndex",
        HKQuantityTypeIdentifierBodyFatPercentage: "HKQuantityTypeIdentifierBodyFatPercentage",
        HKQuantityTypeIdentifierHeight: "HKQuantityTypeIdentifierHeight",
        HKQuantityTypeIdentifierBodyMass: "HKQuantityTypeIdentifierBodyMass",
        HKQuantityTypeIdentifierLeanBodyMass: "HKQuantityTypeIdentifierLeanBodyMass",
        HKQuantityTypeIdentifierStepCount: "HKQuantityTypeIdentifierStepCount",
        HKQuantityTypeIdentifierDistanceWalkingRunning: "HKQuantityTypeIdentifierDistanceWalkingRunning",
        HKQuantityTypeIdentifierDistanceCycling: "HKQuantityTypeIdentifierDistanceCycling",
        HKQuantityTypeIdentifierBasalEnergyBurned: "HKQuantityTypeIdentifierBasalEnergyBurned",
        HKQuantityTypeIdentifierActiveEnergyBurned: "HKQuantityTypeIdentifierActiveEnergyBurned",
        HKQuantityTypeIdentifierFlightsClimbed: "HKQuantityTypeIdentifierFlightsClimbed",
        HKQuantityTypeIdentifierNikeFuel: "HKQuantityTypeIdentifierNikeFuel",
        HKQuantityTypeIdentifierAppleExerciseTime: "HKQuantityTypeIdentifierAppleExerciseTime",
        HKQuantityTypeIdentifierHeartRate: "HKQuantityTypeIdentifierHeartRate",
        HKQuantityTypeIdentifierBodyTemperature: "HKQuantityTypeIdentifierBodyTemperature",
        HKQuantityTypeIdentifierBasalBodyTemperature: "HKQuantityTypeIdentifierBasalBodyTemperature",
        HKQuantityTypeIdentifierBloodPressureSystolic: "HKQuantityTypeIdentifierBloodPressureSystolic",
        HKQuantityTypeIdentifierBloodPressureDiastolic: "HKQuantityTypeIdentifierBloodPressureDiastolic",
        HKQuantityTypeIdentifierRespiratoryRate: "HKQuantityTypeIdentifierRespiratoryRate",
        HKQuantityTypeIdentifierOxygenSaturation: "HKQuantityTypeIdentifierOxygenSaturation",
        HKQuantityTypeIdentifierPeripheralPerfusionIndex: "HKQuantityTypeIdentifierPeripheralPerfusionIndex",
        HKQuantityTypeIdentifierBloodGlucose: "HKQuantityTypeIdentifierBloodGlucose",
        HKQuantityTypeIdentifierNumberOfTimesFallen: "HKQuantityTypeIdentifierNumberOfTimesFallen",
        HKQuantityTypeIdentifierElectrodermalActivity: "HKQuantityTypeIdentifierElectrodermalActivity",
        HKQuantityTypeIdentifierInhalerUsage: "HKQuantityTypeIdentifierInhalerUsage",
        HKQuantityTypeIdentifierBloodAlcoholContent: "HKQuantityTypeIdentifierBloodAlcoholContent",
        HKQuantityTypeIdentifierForcedVitalCapacity: "HKQuantityTypeIdentifierForcedVitalCapacity",
        HKQuantityTypeIdentifierForcedExpiratoryVolume1: "HKQuantityTypeIdentifierForcedExpiratoryVolume1",
        HKQuantityTypeIdentifierPeakExpiratoryFlowRate: "HKQuantityTypeIdentifierPeakExpiratoryFlowRate",
        HKQuantityTypeIdentifierDietaryFatTotal: "HKQuantityTypeIdentifierDietaryFatTotal",
        HKQuantityTypeIdentifierDietaryFatPolyunsaturated: "HKQuantityTypeIdentifierDietaryFatPolyunsaturated",
        HKQuantityTypeIdentifierDietaryFatMonounsaturated: "HKQuantityTypeIdentifierDietaryFatMonounsaturated",
        HKQuantityTypeIdentifierDietaryFatSaturated: "HKQuantityTypeIdentifierDietaryFatSaturated",
        HKQuantityTypeIdentifierDietaryCholesterol: "HKQuantityTypeIdentifierDietaryCholesterol",
        HKQuantityTypeIdentifierDietarySodium: "HKQuantityTypeIdentifierDietarySodium",
        HKQuantityTypeIdentifierDietaryCarbohydrates: "HKQuantityTypeIdentifierDietaryCarbohydrates",
        HKQuantityTypeIdentifierDietaryFiber: "HKQuantityTypeIdentifierDietaryFiber",
        HKQuantityTypeIdentifierDietarySugar: "HKQuantityTypeIdentifierDietarySugar",
        HKQuantityTypeIdentifierDietaryEnergyConsumed: "HKQuantityTypeIdentifierDietaryEnergyConsumed",
        HKQuantityTypeIdentifierDietaryProtein: "HKQuantityTypeIdentifierDietaryProtein",
        HKQuantityTypeIdentifierDietaryVitaminA: "HKQuantityTypeIdentifierDietaryVitaminA",
        HKQuantityTypeIdentifierDietaryVitaminB6: "HKQuantityTypeIdentifierDietaryVitaminB6",
        HKQuantityTypeIdentifierDietaryVitaminB12: "HKQuantityTypeIdentifierDietaryVitaminB12",
        HKQuantityTypeIdentifierDietaryVitaminC: "HKQuantityTypeIdentifierDietaryVitaminC",
        HKQuantityTypeIdentifierDietaryVitaminD: "HKQuantityTypeIdentifierDietaryVitaminD",
        HKQuantityTypeIdentifierDietaryVitaminE: "HKQuantityTypeIdentifierDietaryVitaminE",
        HKQuantityTypeIdentifierDietaryVitaminK: "HKQuantityTypeIdentifierDietaryVitaminK",
        HKQuantityTypeIdentifierDietaryCalcium: "HKQuantityTypeIdentifierDietaryCalcium",
        HKQuantityTypeIdentifierDietaryIron: "HKQuantityTypeIdentifierDietaryIron",
        HKQuantityTypeIdentifierDietaryThiamin: "HKQuantityTypeIdentifierDietaryThiamin",
        HKQuantityTypeIdentifierDietaryRiboflavin: "HKQuantityTypeIdentifierDietaryRiboflavin",
        HKQuantityTypeIdentifierDietaryNiacin: "HKQuantityTypeIdentifierDietaryNiacin",
        HKQuantityTypeIdentifierDietaryFolate: "HKQuantityTypeIdentifierDietaryFolate",
        HKQuantityTypeIdentifierDietaryBiotin: "HKQuantityTypeIdentifierDietaryBiotin",
        HKQuantityTypeIdentifierDietaryPantothenicAcid: "HKQuantityTypeIdentifierDietaryPantothenicAcid",
        HKQuantityTypeIdentifierDietaryPhosphorus: "HKQuantityTypeIdentifierDietaryPhosphorus",
        HKQuantityTypeIdentifierDietaryIodine: "HKQuantityTypeIdentifierDietaryIodine",
        HKQuantityTypeIdentifierDietaryMagnesium: "HKQuantityTypeIdentifierDietaryMagnesium",
        HKQuantityTypeIdentifierDietaryZinc: "HKQuantityTypeIdentifierDietaryZinc",
        HKQuantityTypeIdentifierDietarySelenium: "HKQuantityTypeIdentifierDietarySelenium",
        HKQuantityTypeIdentifierDietaryCopper: "HKQuantityTypeIdentifierDietaryCopper",
        HKQuantityTypeIdentifierDietaryManganese: "HKQuantityTypeIdentifierDietaryManganese",
        HKQuantityTypeIdentifierDietaryChromium: "HKQuantityTypeIdentifierDietaryChromium",
        HKQuantityTypeIdentifierDietaryMolybdenum: "HKQuantityTypeIdentifierDietaryMolybdenum",
        HKQuantityTypeIdentifierDietaryChloride: "HKQuantityTypeIdentifierDietaryChloride",
        HKQuantityTypeIdentifierDietaryPotassium: "HKQuantityTypeIdentifierDietaryPotassium",
        HKQuantityTypeIdentifierDietaryCaffeine: "HKQuantityTypeIdentifierDietaryCaffeine",
        HKQuantityTypeIdentifierDietaryWater: "HKQuantityTypeIdentifierDietaryWater",
        HKQuantityTypeIdentifierUVExposure: "HKQuantityTypeIdentifierUVExposure",
        HKCategoryTypeIdentifierSleepAnalysis: "HKCategoryTypeIdentifierSleepAnalysis",
        HKCategoryTypeIdentifierAppleStandHour: "HKCategoryTypeIdentifierAppleStandHour",
        HKCategoryTypeIdentifierCervicalMucusQuality: "HKCategoryTypeIdentifierCervicalMucusQuality",
        HKCategoryTypeIdentifierOvulationTestResult: "HKCategoryTypeIdentifierOvulationTestResult",
        HKCategoryTypeIdentifierMenstrualFlow: "HKCategoryTypeIdentifierMenstrualFlow",
        HKCategoryTypeIdentifierIntermenstrualBleeding: "HKCategoryTypeIdentifierIntermenstrualBleeding",
        HKCategoryTypeIdentifierSexualActivity: "HKCategoryTypeIdentifierSexualActivity",
        HKQuantityTypeIdentifierPushCount: "HKQuantityTypeIdentifierPushCount",
        HKQuantityTypeIdentifierDistanceWheelchair: "HKQuantityTypeIdentifierDistanceWheelchair",
        HKCategoryTypeIdentifierMindfulSession: "HKCategoryTypeIdentifierMindfulSession",
        HKQuantityTypeIdentifierSwimmingStrokeCount: "HKQuantityTypeIdentifierSwimmingStrokeCount",
        HKQuantityTypeIdentifierDistanceSwimming: "HKQuantityTypeIdentifierDistanceSwimming",
        HKCorrelationTypeIdentifierBloodPressure: "HKCorrelationTypeIdentifierBloodPressure",
        HKCorrelationTypeIdentifierFood: "HKCorrelationTypeIdentifierFood",
        HKWorkoutTypeIdentifier: "HKWorkoutTypeIdentifier"
    }
};