Вы находитесь на странице: 1из 4

'use strict';

angular.module('app.services')
.factory('wfHealthkit', ['$q','dataService','storeLocalData', 'logService',
healthkit]);

function healthkit($q,dataService,storeLocalData, logService) {

var active = false;


var healthConnectionId;

return {
setActive: setActive,
run: run,
isActive: isActive,
setId: setId,
getId: getId
};

function setActive(value) {
if (value) {
active = true;
} else {
active = false;
}
}

function setId(id) {
healthConnectionId = id;
storeLocalData.set('healthConnectionId', JSON.stringify(id));
}

function getId() {
return healthConnectionId;
}

function isActive() {
return active;
}

function getWorkouts() {
//ga('send', 'pageview', '/applehealth/connection/getWorkouts');
var deferred = $q.defer();
if (window.plugins) {
window.plugins.healthkit.findWorkouts({},
function (result) {
deferred.resolve(result);
},
function (error) {
deferred.resolve();
}
);
}

return deferred.promise;
}

function aggregatedSampleType(data) {
var deferred = $q.defer();
if (window.plugins) {
window.plugins.healthkit.checkAuthStatus({
"type": data.value
},
function () {
//have permission
window.plugins.healthkit.querySampleTypeAggregated({
'startDate': new Date(new Date().getTime() - 6 * 24 * 60 *
60 * 1000), // seven days ago
/*'startDate': new Date('2018-03-08'),*/ // hardcode to fix
healthkit failure, revert
'endDate': new Date(), // now
'aggregation': 'day', // 'hour', 'week', 'year' or 'day',
default 'day'
'sampleType': data.value, // any HKQuantityType
'unit': data.unit // make sure this is compatible with the
sampleType
},
function (result) {
deferred.resolve(result);
},
function (error) {
deferred.resolve();
}
);
},
function () {
//don't have permission
deferred.resolve();
}
);
}

return deferred.promise;
}

function updateActivityData(sampleTypeData) {
var params = {
id: getId(), // required key will through an error if missing
workouts: {
workouts: sampleTypeData.workouts || [], // required key will
through an error if missing
daily: sampleTypeData.daily
}
};

var config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
transformRequest: [function (data) {
var query = '';
var dataKeys = Object.keys(data);
for (var i = 0; i < dataKeys.length; i++) {
var dataValue = angular.isObject(data[dataKeys[i]]) ?
JSON.stringify(
data[dataKeys[i]]) : data[dataKeys[i]];
query = query + dataKeys[i] + '=' +
encodeURIComponent(dataValue);
if (i != dataKeys.length - 1) {
query = query + '&';
}
}
return query;
}]
};
if (params.id) {
dataService.post('connections/healthkit_activity', params, config)
.then(function (res) {
})
.finally(function (argument) {
$scope.loading = false;
});
}
}

function run() {
if (isActive()) {
//ga('send', 'pageview', '/applehealth/connection/getData');
var sampleTypeList = [
{
key: 'workouts'
}, {
key: 'steps',
value: 'HKQuantityTypeIdentifierStepCount',
unit: 'count'
}, {
key: 'calories',
value: 'HKQuantityTypeIdentifierActiveEnergyBurned',
unit: 'kcal'
}, {
key: 'distance_walking',
value: 'HKQuantityTypeIdentifierDistanceWalkingRunning',
unit: 'm'
}, {
key: 'distance_biking',
value: 'HKQuantityTypeIdentifierDistanceCycling',
unit: 'm'
}];

var sampleTypeData = {};


var promises = [getWorkouts(), aggregatedSampleType(sampleTypeList[1]),
aggregatedSampleType(sampleTypeList[2]), aggregatedSampleType(sampleTypeList[3]),
aggregatedSampleType(sampleTypeList[4])];

$q.all(promises).then(function (values) {
//logService.log('connection', values, 22);
var dataToSend = {
workouts: values[0],
daily: {}
};
for (var index = 1; index < values.length; index++) {
if (values[index]) {
dataToSend.daily[sampleTypeList[index].key] =
values[index];
}
}
updateActivityData(dataToSend);
}, function (error) {
logService.log('connection', error, 22);
});
} else {
//
}

}
}

Вам также может понравиться