body.js 11.1 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 48 49 50 51
/**
 * 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);
        this._fetchHealthKitUserWeight = this._fetchHealthKitUserWeight.bind(this);
        this._fetchHealthKitUserHeight = this._fetchHealthKitUserHeight.bind(this);
        this._fetchHealthKitUserBmi = this._fetchHealthKitUserBmi.bind(this);
        this._fetchHealthKitStepCountToday = this._fetchHealthKitStepCountToday.bind(this);
52
        this._fetchHealthKitStepCountForDay = this._fetchHealthKitStepCountForDay.bind(this);
53 54
        this._fetchHealthKitBodyFatPercentage = this._fetchHealthKitBodyFatPercentage.bind(this);
        this._fetchHealthKitLeanBodyMass = this._fetchHealthKitLeanBodyMass.bind(this);
55
        this._saveHeight = this._saveHeight.bind(this);
Greg Wilson's avatar
Greg Wilson committed
56
        this._saveBmi = this._saveBmi.bind(this);
57

58 59 60 61 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
        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: {
                read: ["Height", "Weight", "Steps", "DateOfBirth", "BodyMassIndex", "LeanBodyMass", "BodyFatPercentage"],
Greg Wilson's avatar
Greg Wilson committed
94
                write: ["Weight", "Height", "BodyMassIndex"]
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
            }
        };

        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
110

111 112
            self._fetchHealthKitStepCountForDay();

Greg Wilson's avatar
Greg Wilson committed
113 114 115 116 117
            //setTimeout(() => {self._saveBmi(27)}, 1000);
            //setTimeout(() => {self._onactn_addWeight({
            //    weight: 215,
            //})}, 1000);

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        });
    }


    _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
141
            AppleHealthKit.saveWeight({value:weightVal}, (err, res) => {
142 143 144 145 146 147 148 149 150 151 152 153 154 155
                if(this._handleHealthKitError(err, 'saveWeight')){
                    return;
                }
                DATA.weight = weightVal;
                self.trigger({
                    name: 'change:weight',
                    target: null,
                    data: DATA.weight
                });
            });
        }
    }


156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    _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
            });
        });
    }


177 178
    _fetchHealthKitUserWeight() {
        let self = this;
179 180 181 182
        let options = {
            unit: "pound"
        };
        AppleHealthKit.getLatestWeight(options, (err, weight) => {
183
            if(this._handleHealthKitError(err, 'getLatestWeight')){
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
                return;
            }
            weight = _.round(weight,1);

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


    _fetchHealthKitUserHeight() {
        let self = this;
200 201 202 203
        let options = {
            unit: "inch"
        };
        AppleHealthKit.getLatestHeight(options, (err, height) => {
204
            if(this._handleHealthKitError(err, 'getLatestHeight')){
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
                return;
            }
            console.log("HEIGHT: ", height);

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


    _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
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    _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
            });
        });
    }

259 260 261

    _fetchHealthKitBodyFatPercentage() {
        let self = this;
262 263
        AppleHealthKit.getLatestBodyFatPercentage({blah:true}, (err, fatPercentage) => {
            if(this._handleHealthKitError(err, 'getLatestBodyFatPercentage')){
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
                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;
279 280
        AppleHealthKit.getLatestLeanBodyMass({blah:true}, (err, leanMass) => {
            if(this._handleHealthKitError(err, 'getLatestLeanBodyMass')){
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
                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
            });
        });
    }


314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    _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
            //});
        });
    }


337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 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 386 387 388 389 390
    GetHeightValue() {
        return DATA.height;
    }

    GetHeightFormatted() {
        let feet = _.floor((DATA.height / 12));
        let inches = DATA.height % 12;
        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;