From 616277fe5de434ef3f9c42f0f182d726b64b88b4 Mon Sep 17 00:00:00 2001 From: Matrixes <46491408+matrixes@users.noreply.github.com> Date: Wed, 18 May 2022 23:14:37 +0200 Subject: [PATCH] Step count resets around midnight --- apps/barcode/ChangeLog | 3 ++- apps/barcode/barcode.app.js | 38 +++++++++++++++++++++++++++++++++++-- apps/barcode/metadata.json | 2 +- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/apps/barcode/ChangeLog b/apps/barcode/ChangeLog index d97fd8af8..ae89926a5 100644 --- a/apps/barcode/ChangeLog +++ b/apps/barcode/ChangeLog @@ -3,4 +3,5 @@ 0.03: Interaction 0.04: Shows day of week 0.05: Shows day of month -0.06: Updates every 5 minutes when locked, or when unlock occurs. Also shows nr of steps. \ No newline at end of file +0.06: Updates every 5 minutes when locked, or when unlock occurs. Also shows nr of steps. +0.07: Step count resets at midnight \ No newline at end of file diff --git a/apps/barcode/barcode.app.js b/apps/barcode/barcode.app.js index 9f4d31122..0f6684733 100644 --- a/apps/barcode/barcode.app.js +++ b/apps/barcode/barcode.app.js @@ -36,6 +36,8 @@ let leftBarsStartY = upperTextBarLeftOffsetY + textBarHeight; let rightBarsStartX = midBarOffsetX + checkBarWidth; let rightBarsStartY = upperTextBarRightOffsetY + textBarHeight; +/* Utilities */ +let stepCount = 0; let intCaster = num => Number(num); var drawTimeout; @@ -66,7 +68,7 @@ function renderWatch(l) { drawLDigit(chars[2], 2, leftBarsStartY); drawLDigit(chars[3], 3, leftBarsStartY); - g.drawString(Bangle.getStepCount(), startOffsetX + checkBarWidth + 3, startOffsetY + 4); + g.drawString(getStepCount(), startOffsetX + checkBarWidth + 3, startOffsetY + 4); g.drawString(concatTime.substring(0,4), startOffsetX + checkBarWidth + 3, startOffsetY + textBarHeight + digitBarHeight + 6); drawCheckBar(midBarOffsetX, midBarOffsetY); @@ -371,6 +373,36 @@ function calculateChecksum(digits) { return checkSum; } +function storeStepCount() { + stepCount = Bangle.getStepCount(); +} + +function getStepCount() { + let accumulatedSteps = Bangle.getStepCount(); + if(accumulatedSteps <= stepCount) { + return 0; + } + return accumulatedSteps - stepCount; +} + +function resetAtMidnight() { + var now = new Date(); + var night = new Date( + now.getFullYear(), + now.getMonth(), + now.getDate() + 1, // the next day, ... + 23, 58, 0 // ...at 00:00:00 hours + ); + var msToMidnight = night.getTime() - now.getTime(); + + setTimeout(function() { + storeStepCount(); // <-- This is the function being called at midnight. + resetAtMidnight(); // Then, reset again next midnight. + }, msToMidnight); +} + +resetAtMidnight(); + // The layout, referencing the custom renderer var Layout = require("Layout"); var layout = new Layout( { @@ -387,5 +419,7 @@ Bangle.setUI("clock"); layout.render(); Bangle.on('lock', function(locked) { - layout.render(); + if(!locked) { + layout.render(); + } }); \ No newline at end of file diff --git a/apps/barcode/metadata.json b/apps/barcode/metadata.json index d0e5fb448..5c23acada 100644 --- a/apps/barcode/metadata.json +++ b/apps/barcode/metadata.json @@ -2,7 +2,7 @@ "name": "Barcode clock", "shortName":"Barcode clock", "icon": "barcode.icon.png", - "version":"0.06", + "version":"0.07", "description": "EAN-8 compatible barcode clock.", "tags": "barcode,ean,ean-8,watchface,clock,clockface", "type": "clock",