Revise event handlers

master
stweedo 2024-09-07 02:37:03 -05:00
parent 69d12f3114
commit 4bf23b4fbb
1 changed files with 92 additions and 88 deletions

View File

@ -23,8 +23,9 @@
let h = g.getHeight(); let h = g.getHeight();
let drawTimeout; let drawTimeout;
// 3. Touch and drag handlers // 3. Event handlers
let touchHandler = function(zone, e) { let eventHandlers = {
touchHandler: function(zone, e) {
let boxTouched = false; let boxTouched = false;
let touchedBox = null; let touchedBox = null;
@ -74,10 +75,9 @@
doubleTapTimer = null; doubleTapTimer = null;
}, 500); }, 500);
} }
}; },
let dragHandler = function(e) { dragHandler: function(e) {
// Check if any box is being dragged
if (!isDragging) return; if (!isDragging) return;
// Stop propagation of the drag event to prevent other handlers // Stop propagation of the drag event to prevent other handlers
@ -101,6 +101,22 @@
} }
draw(); draw();
},
stepHandler: function(up) {
if (boxes.step && !isDragging) {
boxes.step.string = formatStr(boxes.step, Bangle.getHealthStatus("day").steps);
boxes.step.cachedSize = null;
draw();
}
},
lockHandler: function(isLocked) {
if (isLocked) {
deselectAllBoxes();
draw();
}
}
}; };
// 4. Font loading function // 4. Font loading function
@ -396,21 +412,15 @@
// 10. Setup function to configure event handlers // 10. Setup function to configure event handlers
let setup = function() { let setup = function() {
Bangle.on('lock', function(isLocked) { Bangle.on('lock', eventHandlers.lockHandler);
if (isLocked) { Bangle.on('touch', eventHandlers.touchHandler);
// Screen is about to lock, deselect all boxes Bangle.on('drag', eventHandlers.dragHandler);
deselectAllBoxes();
// Redraw to reflect changes
draw();
}
});
Bangle.on('touch', touchHandler);
Bangle.on('drag', dragHandler);
if (boxes.step) { if (boxes.step) {
boxes.step.string = formatStr(boxes.step, Bangle.getHealthStatus("day").steps); boxes.step.string = formatStr(boxes.step, Bangle.getHealthStatus("day").steps);
Bangle.on('step', eventHandlers.stepHandler);
} }
if (boxes.batt) { if (boxes.batt) {
boxes.batt.lastLevel = E.getBattery(); boxes.batt.lastLevel = E.getBattery();
boxes.batt.string = formatStr(boxes.batt, boxes.batt.lastLevel); boxes.batt.string = formatStr(boxes.batt, boxes.batt.lastLevel);
@ -421,10 +431,12 @@
mode: "clock", mode: "clock",
remove: function() { remove: function() {
// Remove event handlers, stop draw timer, remove custom font // Remove event handlers, stop draw timer, remove custom font
Bangle.removeListener('touch', touchHandler); Bangle.removeListener('touch', eventHandlers.touchHandler);
Bangle.removeListener('drag', dragHandler); Bangle.removeListener('drag', eventHandlers.dragHandler);
Bangle.removeListener('step'); Bangle.removeListener('lock', eventHandlers.lockHandler);
Bangle.removeAllListeners('lock'); if (boxes.step) {
Bangle.removeListener('step', eventHandlers.stepHandler);
}
if (drawTimeout) clearTimeout(drawTimeout); if (drawTimeout) clearTimeout(drawTimeout);
drawTimeout = undefined; drawTimeout = undefined;
delete Graphics.prototype.setFontBrunoAce; delete Graphics.prototype.setFontBrunoAce;
@ -434,6 +446,7 @@
widgets.show(); widgets.show();
} }
}); });
loadCustomFont(); loadCustomFont();
draw(); draw();
}; };
@ -443,13 +456,4 @@
widgets.swipeOn(); widgets.swipeOn();
modSetColor(); modSetColor();
setup(); setup();
// Event listener for real-time step updates
Bangle.on('step', function(up) {
if (boxes.step && !isDragging) {
boxes.step.string = formatStr(boxes.step, Bangle.getHealthStatus("day").steps);
boxes.step.cachedSize = null;
draw();
}
});
} }