body.js 13.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/**
 * Created by greg on 2016-06-27.
 */


var airflux = require( 'airflux' );
var _ = require('lodash');
var moment = require('moment');
var Immutable = require('immutable');
//var actions = require('../actions/actions');

var AppleHealthKit = require('react-native-apple-healthkit');

var DATA = {
    weight: 0,
    height: 0,
    bmi: 0,
    bodyFatPercentage: 0,
    leanBodyMass: 0,
    steps: 0,
};

/**
 * @namespace Stores
 */

/**
 * @class WeightStore
 * @classdesc Airflux store to handle data, actions, and events relating to the WeightStore
 * @memberof Stores
 */
class BodyStore extends airflux.Store {

    /**
     * Initialize the WeightStore, optionally with 'props' object
     * @constructs Stores.TestingEventService
     * @param {object} props - an optional properties object to initialize the store with
     *
     */
    constructor(props) {
        //console.log("WeightStore props --> ", props);
        super(props);
        let self = this;

        //this.listenTo(actions.addWeight, this._onactn_addWeight)

        this._initHealthKit = this._initHealthKit.bind(this);
48 49
        this._fetchHealthKitUserBiologicalSex = this._fetchHealthKitUserBiologicalSex.bind(this);
        this._fetchHealthKitUserDateOfBirth = this._fetchHealthKitUserDateOfBirth.bind(this);
50
        this._fetchHealthKitUserWeight = this._fetchHealthKitUserWeight.bind(this);
51
        this._fetchHealthKitUserWeightSamples = this._fetchHealthKitUserWeightSamples.bind(this);
52
        this._fetchHealthKitUserHeight = this._fetchHealthKitUserHeight.bind(this);
53
        this._fetchHealthKitUserHeightSamples = this._fetchHealthKitUserHeightSamples.bind(this);
54 55
        this._fetchHealthKitUserBmi = this._fetchHealthKitUserBmi.bind(this);
        this._fetchHealthKitStepCountToday = this._fetchHealthKitStepCountToday.bind(this);
56
        this._fetchHealthKitStepCountForDay = this._fetchHealthKitStepCountForDay.bind(this);
57
        this._fetchDailyStepCounts = this._fetchDailyStepCounts.bind(this);
58 59
        this._fetchHealthKitBodyFatPercentage = this._fetchHealthKitBodyFatPercentage.bind(this);
        this._fetchHealthKitLeanBodyMass = this._fetchHealthKitLeanBodyMass.bind(this);
60
        this._saveHeight = this._saveHeight.bind(this);
Greg Wilson's avatar
Greg Wilson committed
61
        this._saveBmi = this._saveBmi.bind(this);
62

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
        this.GetWeightValue = this.GetWeightValue.bind(this);
        this.GetWeightFormatted = this.GetWeightFormatted.bind(this);
        this.GetSteps = this.GetSteps.bind(this);
        this.GetHeightFormatted = this.GetHeightFormatted.bind(this);
        this.GetHeightValue = this.GetHeightValue.bind(this);
        this.GetBMIValue = this.GetBMIValue.bind(this);
        this.GetBMIFormatted = this.GetBMIFormatted.bind(this);
        this.GetBodyFatPercentageValue = this.GetBodyFatPercentageValue.bind(this);
        this.GetBodyFatPercentageFormatted = this.GetBodyFatPercentageFormatted.bind(this);
        this.GetLeanBodyMassValue = this.GetLeanBodyMassValue.bind(this);
        this.GetLeanBodyMassFormatted = this.GetLeanBodyMassFormatted.bind(this);

        AppleHealthKit.isAvailable((err,available) => {
            console.log('AppleHealthKit.isAvailable(): ', available);
            if(available){
                self._initHealthKit();
            }
        });

        //AppleHealthKit.getInfo({init:"true"}, (err,res) => {
        //    if(err) {
        //        console.log("ERROR GETTING HEALTHKIT MODULE INFO");
        //        console.log(err);
        //        return;
        //    }
        //    console.log("HEALTHKIT MODULE INFO: ", res);
        //});
    }


    _initHealthKit() {
        let self = this;

        let healthKitOptions = {
            permissions: {
98
                read: ["Height", "Weight", "Steps", "DateOfBirth", "BodyMassIndex", "LeanBodyMass", "BodyFatPercentage", "BiologicalSex"],
Greg Wilson's avatar
Greg Wilson committed
99
                write: ["Weight", "Height", "BodyMassIndex"]
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
            }
        };

        AppleHealthKit.initHealthKit(healthKitOptions, (err, res) => {
            if(this._handleHealthKitError(err, 'initHealthKit')){
                return;
            }
            console.log("HEALTHKIT INITIALIZED!! ", res);

            self._fetchHealthKitUserWeight();
            self._fetchHealthKitUserBmi();
            self._fetchHealthKitStepCountToday();
            self._fetchHealthKitUserHeight();
            self._fetchHealthKitBodyFatPercentage();
            self._fetchHealthKitLeanBodyMass();
Greg Wilson's avatar
Greg Wilson committed
115

116
            self._fetchHealthKitStepCountForDay();
117
            self._fetchDailyStepCounts();
118

119 120 121 122 123 124
            self._fetchHealthKitUserWeightSamples();
            self._fetchHealthKitUserHeightSamples();

            self._fetchHealthKitUserBiologicalSex();
            self._fetchHealthKitUserDateOfBirth();

Greg Wilson's avatar
Greg Wilson committed
125 126 127 128 129
            //setTimeout(() => {self._saveBmi(27)}, 1000);
            //setTimeout(() => {self._onactn_addWeight({
            //    weight: 215,
            //})}, 1000);

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
        });
    }


    _handleHealthKitError(err, method) : boolean {
        if(err){
            let errStr = 'HealthKit_ERROR['+method+'] : ';
            if(typeof err === 'string'){
                errStr += err;
            } else if (typeof err === 'object' && err.message){
                errStr += err.message;
            }
            console.log(errStr);
            return true;
        }
        return false;
    }

    _onactn_addWeight(options) {
        console.log('_onactn_addWeight() --> ', options);
        if(options && options.weight){
            let weightVal = parseFloat(options.weight);
            let self = this;
Greg Wilson's avatar
Greg Wilson committed
153
            AppleHealthKit.saveWeight({value:weightVal}, (err, res) => {
154 155 156 157 158 159 160 161 162 163 164 165 166 167
                if(this._handleHealthKitError(err, 'saveWeight')){
                    return;
                }
                DATA.weight = weightVal;
                self.trigger({
                    name: 'change:weight',
                    target: null,
                    data: DATA.weight
                });
            });
        }
    }


168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    _fetchHealthKitUserBiologicalSex() {
        let self = this;
        AppleHealthKit.getBiologicalSex(null, (err, sex) => {
            if(this._handleHealthKitError(err, 'getBiologicalSex')){
                return;
            }
            console.log('BiologicalSex: ', sex);
        });
    }

    _fetchHealthKitUserDateOfBirth() {
        let self = this;
        AppleHealthKit.getDateOfBirth(null, (err, dob) => {
            if(this._handleHealthKitError(err, 'getDateOfBirth')){
                return;
            }
            console.log('DateOfBirth: ', dob);
        });
    }



190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    _saveHeight(height_inches) {
        let self = this;
        let options = {
            value: height_inches
        };

        AppleHealthKit.saveHeight(options, (err, res) => {
            if(this._handleHealthKitError(err, 'saveHeight')){
                return;
            }
            console.log('Height Saved Successfully...');
            DATA.height = height_inches;
            self.trigger({
                name: 'change:height',
                target: null,
                data: DATA.height
            });
        });
    }


211 212
    _fetchHealthKitUserWeight() {
        let self = this;
213 214 215 216
        let options = {
            unit: "pound"
        };
        AppleHealthKit.getLatestWeight(options, (err, weight) => {
217
            if(this._handleHealthKitError(err, 'getLatestWeight')){
218 219 220 221 222 223 224 225 226 227 228 229 230 231
                return;
            }
            weight = _.round(weight,1);

            DATA.weight = weight;
            self.trigger({
                name: 'change:weight',
                target: null,
                data: weight
            });
        });
    }


232 233 234 235 236

    _fetchHealthKitUserWeightSamples() {
        let self = this;
        let d = new Date(2016,4,27);
        let options = {
Greg Wilson's avatar
Greg Wilson committed
237
            unit: "gram",
238 239
            startDate: d.toISOString(),
            ascending: false,
Greg Wilson's avatar
Greg Wilson committed
240
            limit:3,
241 242 243 244 245 246 247 248 249 250
        };
        AppleHealthKit.getWeightSamples(options, (err, samples) => {
            if(this._handleHealthKitError(err, 'getWeightSamples')){
                return;
            }
            console.log('weight samples: ', samples);
        });
    }


251 252
    _fetchHealthKitUserHeight() {
        let self = this;
253 254 255 256
        let options = {
            unit: "inch"
        };
        AppleHealthKit.getLatestHeight(options, (err, height) => {
257
            if(this._handleHealthKitError(err, 'getLatestHeight')){
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
                return;
            }
            console.log("HEIGHT: ", height);

            if(typeof height === "number" && height > 0){
                DATA.height = height;
                self.trigger({
                    name: 'change:height',
                    target: null,
                    data: height
                });
            }
        });
    }


274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    _fetchHealthKitUserHeightSamples() {
        let self = this;
        let d = new Date(2016,1,1);
        let options = {
            unit: "inch",
            startDate: d.toISOString(),
            //ascending: false,
            //limit:2,
        };
        AppleHealthKit.getHeightSamples(options, (err, samples) => {
            if(this._handleHealthKitError(err, 'getHeightSamples')){
                return;
            }
            console.log('height samples: ', samples);
        });
    }



293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    _fetchHealthKitUserBmi() {
        let self = this;
        AppleHealthKit.getLatestBmi({blah:true}, (err, bmi) => {
            if(this._handleHealthKitError(err, 'getLatestBmi')){
                return;
            }
            console.log("LATEST BMI: ", bmi);
            if(bmi && bmi.value){
                DATA.bmi = _.round(bmi.value,1);
                self.trigger({
                    name: 'change:bmi',
                    target: null,
                    data: DATA.bmi
                });
            }
        });
    }

Greg Wilson's avatar
Greg Wilson committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    _saveBmi(bmi_value) {
        let self = this;
        let options = {
            value: bmi_value
        };

        AppleHealthKit.saveBmi(options, (err, res) => {
            if(this._handleHealthKitError(err, 'saveBmi')){
                return;
            }
            console.log('BMI Saved Successfully...');
            DATA.bmi = bmi_value;
            self.trigger({
                name: 'change:bmi',
                target: null,
                data: DATA.bmi
            });
        });
    }

331 332 333

    _fetchHealthKitBodyFatPercentage() {
        let self = this;
334 335
        AppleHealthKit.getLatestBodyFatPercentage({blah:true}, (err, fatPercentage) => {
            if(this._handleHealthKitError(err, 'getLatestBodyFatPercentage')){
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
                return;
            }
            console.log("BODY FAT PERCENTAGE: ", fatPercentage);
            DATA.bodyFatPercentage = fatPercentage;
            self.trigger({
                name: 'change:body_fat_percentage',
                target: null,
                data: DATA.bodyFatPercentage
            });
        });
    }


    _fetchHealthKitLeanBodyMass() {
        let self = this;
351 352
        AppleHealthKit.getLatestLeanBodyMass({blah:true}, (err, leanMass) => {
            if(this._handleHealthKitError(err, 'getLatestLeanBodyMass')){
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
                return;
            }
            console.log("LEAN BODY MASS: ", leanMass);
            DATA.leanBodyMass = _.round(leanMass,0);
            self.trigger({
                name: 'change:lean_body_mass',
                target: null,
                data: DATA.leanBodyMass
            });
        });
    }



    _fetchHealthKitStepCountToday() {
        let self = this;
        AppleHealthKit.getStepCountForToday({options:"true"}, (err, steps) => {
            if(this._handleHealthKitError(err, 'getStepCountForToday')){
                return;
            }
            console.log("STEPS : ", steps);
            steps = _.round(steps,0);

            DATA.steps = steps;
            self.trigger({
                name: 'change:steps',
                target: null,
                data: steps
            });
        });
    }


386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
    _fetchHealthKitStepCountForDay() {
        let self = this;
        let d = new Date(2016,5,27);
        let options = {
            date: d.toISOString()
        };
        AppleHealthKit.getStepCountForDay(options, (err, steps) => {
            if(this._handleHealthKitError(err, 'getStepCountForDay')){
                return;
            }
            console.log("STEPS FOR DAY : ", steps);
            //steps = _.round(steps,0);
            //
            //DATA.steps = steps;
            //self.trigger({
            //    name: 'change:steps',
            //    target: null,
            //    data: steps
            //});
        });
    }


409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    _fetchDailyStepCounts() {
        let self = this;
        let d = new Date(2016,4,1);
        let options = {
            startDate: d.toISOString()
        };
        AppleHealthKit.getMultiDayStepCounts(options, (err, res) => {
            if(this._handleHealthKitError(err, 'getMultiDayStepCounts')){
                return;
            }
            console.log("DAILY STEP COUNTS: ", res);
        });
    }




426 427 428 429 430 431
    GetHeightValue() {
        return DATA.height;
    }

    GetHeightFormatted() {
        let feet = _.floor((DATA.height / 12));
432
        let inches = _.floor((DATA.height % 12));
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
        let formatted = '' + feet + '\'' + inches + '"';
        return formatted;
    }

    GetWeightValue() {
        return DATA.weight;
    }

    GetWeightFormatted() {
        return DATA.weight + ' lbs';
    }

    GetBMIValue() {
        return DATA.bmi;
    }

    GetBMIFormatted() {
        return '' + DATA.bmi;
    }

    GetBodyFatPercentageValue() {
        return DATA.bodyFatPercentage;
    }

    GetBodyFatPercentageFormatted() {
        return '' + DATA.bodyFatPercentage + '%';
    }

    GetLeanBodyMassValue() {
        return DATA.leanBodyMass;
    }

    GetLeanBodyMassFormatted() {
        return '' + DATA.leanBodyMass + ' lbs';
    }


    GetSteps() {
        return DATA.steps;
    }

}


let storeInstance = new BodyStore();
export default storeInstance;
module.exports = storeInstance;