Skip to content

Commit 80173b9

Browse files
committed
Change icon, rename internal timer, display running timer on app restart
Start timer on button press Invert icon, update metadata Update ChangeLog
1 parent ad24e33 commit 80173b9

File tree

4 files changed

+39
-29
lines changed

4 files changed

+39
-29
lines changed

apps/ateatimer/ChangeLog

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
0.01: First release
1+
0.01: First release
2+
0.02: Fix icon, utilize sched, show running timer on app relaunch

apps/ateatimer/app-icon.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/ateatimer/app.js

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,33 @@ function drawTime() {
4444

4545
function startTimer() {
4646
if (timerRunning) return;
47+
if (timeRemaining == 0) return;
4748
timerRunning = true;
4849

4950
// Save the default duration on timer start
5051
timerDuration = timeRemaining;
5152
saveDefaultDuration();
53+
scheduleTimer();
5254

55+
// Start the secondary timer to update the display
56+
setInterval(updateDisplay, 1000);
57+
}
58+
59+
function scheduleTimer() {
5360
// Schedule a new timer using the sched library
54-
require("sched").setAlarm("mytimer", {
61+
require("sched").setAlarm("ateatimer", {
5562
msg: "Tea is ready!",
5663
timer: timeRemaining * 1000, // Convert to milliseconds
5764
vibrate: ".." // Default vibration pattern
5865
});
5966

6067
// Ensure the scheduler updates
6168
require("sched").reload();
62-
63-
// Start the secondary timer to update the display
64-
setInterval(updateDisplay, 1000);
6569
}
6670

6771
function resetTimer() {
6872
// Cancel the existing timer
69-
require("sched").setAlarm("mytimer", undefined);
73+
require("sched").setAlarm("ateatimer", undefined);
7074
require("sched").reload();
7175

7276
timerRunning = false;
@@ -75,25 +79,21 @@ function resetTimer() {
7579
}
7680

7781
function adjustTime(amount) {
82+
if (-amount > timeRemaining) {
83+
// Return if result will be negative
84+
return;
85+
}
7886
timeRemaining += amount;
79-
timeRemaining = Math.max(1, timeRemaining); // Ensure time doesn't go negative
80-
print(timeRemaining);
87+
timeRemaining = Math.max(0, timeRemaining); // Ensure time doesn't go negative
8188
if (timerRunning) {
8289
// Update the existing timer with the new remaining time
83-
let alarm = require("sched").getAlarm("mytimer");
90+
let alarm = require("sched").getAlarm("ateatimer");
8491
if (alarm) {
8592
// Cancel the current alarm
86-
require("sched").setAlarm("mytimer", undefined);
93+
require("sched").setAlarm("ateatimer", undefined);
8794

8895
// Set a new alarm with the updated time
89-
require("sched").setAlarm("mytimer", {
90-
msg: "Tea is ready!",
91-
timer: timeRemaining * 1000, // Convert to milliseconds
92-
vibrate: ".." // Default vibration pattern
93-
});
94-
95-
// Reload the scheduler to apply changes
96-
require("sched").reload();
96+
scheduleTimer();
9797
}
9898
}
9999

@@ -120,13 +120,11 @@ function handleTouch(x, y) {
120120
// Function to update the display every second
121121
function updateDisplay() {
122122
if (timerRunning) {
123-
let alarm = require("sched").getAlarm("mytimer");
124-
if (alarm) {
125-
timeRemaining = Math.ceil(require("sched").getTimeToAlarm(alarm) / 1000);
126-
}
123+
let alarm = require("sched").getAlarm("ateatimer");
124+
timeRemaining = Math.floor(require("sched").getTimeToAlarm(alarm) / 1000);
127125
drawTime();
128126
if (timeRemaining <= 0) {
129-
timeRemaining = 0
127+
timeRemaining = 0;
130128
clearInterval(updateDisplay);
131129
timerRunning = false;
132130
}
@@ -135,13 +133,24 @@ function updateDisplay() {
135133

136134
// Handle physical button press for resetting timer
137135
setWatch(() => {
138-
resetTimer();
136+
if (timerRunning) {
137+
resetTimer();
138+
} else {
139+
startTimer();
140+
}
139141
}, BTN1, { repeat: true, edge: "falling" });
140142

141143
// Handle touch
142144
Bangle.on("touch", (zone, xy) => {
143145
handleTouch(xy.x, xy.y, false);
144146
});
145147

146-
// Draw the initial timer display
147-
drawTime();
148+
let isRunning = require("sched").getAlarm("ateatimer");
149+
if (isRunning) {
150+
timerRunning = true;
151+
// Start the timer to update the display
152+
setInterval(updateDisplay, 1000);
153+
} else {
154+
// Draw the initial timer display
155+
drawTime();
156+
}

apps/ateatimer/metadata.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"name": "A Tea Timer",
33
"shortName":"A Tea Timer",
44
"icon": "app.png",
5-
"version":"0.01",
6-
"description": "Simple app for setting countdown timers for tea. Press up and down to change time, and touch time to start counting. Button will stop countdown and reset counter to last used value.",
5+
"version":"0.02",
6+
"description": "Simple app for setting timers for tea. Touch up and down to change time, and time or button to start counting. When timer is running, button will stop timer and reset counter to last used value.",
77
"tags": "timer",
88
"supports": ["BANGLEJS2"],
99
"storage": [

0 commit comments

Comments
 (0)