From 7d5b0f3b03a05f4a3126c1be80f17453b7c62ab2 Mon Sep 17 00:00:00 2001 From: seamlink-aalves <49952868+seamlink-aalves@users.noreply.github.com> Date: Mon, 7 Apr 2025 15:57:40 +0100 Subject: [PATCH 1/2] refactor query method to simplify data processing and improve readability --- www/android/health.js | 101 +++++++++++++----------------------------- 1 file changed, 32 insertions(+), 69 deletions(-) diff --git a/www/android/health.js b/www/android/health.js index cb60d67a..0eefdc00 100755 --- a/www/android/health.js +++ b/www/android/health.js @@ -35,78 +35,41 @@ module.exports = { if (opts.endDate && (typeof opts.endDate == 'object')) opts.endDate = opts.endDate.getTime(); exec((data) => { - // here we use a recursive function instead of a simple loop - // this is to deal with additional queries required for the special case - // of activity with calories and/or distance - finalizeResult = (i) => { - if (i >= data.length) { - // completed, return results - onSuccess(data); - } else { - // iterate - // convert timestamps to date - if (data[i].startDate) data[i].startDate = new Date(data[i].startDate) - if (data[i].endDate) data[i].endDate = new Date(data[i].endDate) - - if (opts.dataType == 'sleep' && opts.sleepSession) { - // convert start and end dates for single stages - for (let stageI = 0; stageI < data[i].value.length; stageI++) { - data[i].value[stageI].startDate = new Date(data[i].value[stageI].startDate) - data[i].value[stageI].endDate = new Date(data[i].value[stageI].endDate) - } - } - - if (opts.dataType == 'activity' && (opts.includeCalories || opts.includeDistance)) { - // we need to also fetch calories and/or distance - - // helper function to get aggregated calories for that activity - getCals = (onDone) => { - this.queryAggregated({ - startDate: data[i].startDate, - endDate: data[i].endDate, - dataType: 'calories.active' - }, (cals) => { - data[i].calories = cals.value - onDone() - }, onError) - } - // helper function to get aggregated distance for that activity - getDist = (onDone) => { - this.queryAggregated({ - startDate: data[i].startDate, - endDate: data[i].endDate, - dataType: 'distance' - }, (dist) => { - data[i].distance = dist.value - onDone() - }, onError) - } - - if (opts.includeCalories) { - // calories are needed, fetch them - getCals(() => { - // now get the distance, if needed - if (opts.includeDistance) { - getDist(() => { - finalizeResult(i + 1) - }) - } else { - // no distance needed, move on - finalizeResult(i + 1) - } - }) - } else { - // distance only is needed - getDist(() => { - finalizeResult(i + 1) - }) - } + data.map((item) => { + if (item.startDate) item.startDate = new Date(item.startDate); + if (item.endDate) item.endDate = new Date(item.endDate); + + if (opts.dataType == 'sleep' && opts.sleepSession) { + // convert start and end dates for single stages + item.value.map((stage) => { + stage.startDate = new Date(stage.startDate); + stage.endDate = new Date(stage.endDate); + }); + } + if (opts.dataType == 'activity' && (opts.includeCalories || opts.includeDistance)) { + // we need to also fetch calories and/or distance + if (opts.includeCalories) { + // calories are needed, fetch them + this.queryAggregated({ + startDate: item.startDate, + endDate: item.endDate, + dataType: 'calories.active' + }, (cals) => { + item.calories = cals.value + }, onError); } else { - finalizeResult(i + 1) + // distance only is needed + this.queryAggregated({ + startDate: item.startDate, + endDate: item.endDate, + dataType: 'distance' + }, (dist) => { + item.distance = dist.value + }, onError); } } - } - finalizeResult(0); + }); + onSuccess(data); }, onError, "health", "query", [opts]) }, From f9d7ea25491ec1c4a5e3ab0a4d74f0cd67bce6b6 Mon Sep 17 00:00:00 2001 From: seamlink-aalves <49952868+seamlink-aalves@users.noreply.github.com> Date: Tue, 7 Oct 2025 14:20:07 +0100 Subject: [PATCH 2/2] refactor query method to use async/await for improved readability and error handling --- www/android/health.js | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/www/android/health.js b/www/android/health.js index 0eefdc00..d128742c 100755 --- a/www/android/health.js +++ b/www/android/health.js @@ -34,8 +34,19 @@ module.exports = { opts.startDate = opts.startDate.getTime() if (opts.endDate && (typeof opts.endDate == 'object')) opts.endDate = opts.endDate.getTime(); + const subQuery = (param, startDate, endDate) => { + return new Promise((resolve, reject) => { + this.queryAggregated({ + startDate: startDate, + endDate: endDate, + dataType: param + }, + (result) => resolve(result.value), + (error) => reject(error)); + }); + } exec((data) => { - data.map((item) => { + data.map(async (item) => { if (item.startDate) item.startDate = new Date(item.startDate); if (item.endDate) item.endDate = new Date(item.endDate); @@ -50,22 +61,18 @@ module.exports = { // we need to also fetch calories and/or distance if (opts.includeCalories) { // calories are needed, fetch them - this.queryAggregated({ - startDate: item.startDate, - endDate: item.endDate, - dataType: 'calories.active' - }, (cals) => { - item.calories = cals.value - }, onError); + try { + item.calories = await subQuery('calories.active', item.startDate, item.endDate) + } catch (error) { + onError(error); + } } else { // distance only is needed - this.queryAggregated({ - startDate: item.startDate, - endDate: item.endDate, - dataType: 'distance' - }, (dist) => { - item.distance = dist.value - }, onError); + try { + item.distance = await subQuery('distance', item.startDate, item.endDate) + } catch (error) { + onError(error); + } } } });