Skip to content

Commit 14f104b

Browse files
authored
Merge pull request #4008 from kidneyhex/schedclock
[schedclock] New app to schedule changing clock faces
2 parents 451afbb + 2f868f0 commit 14f104b

File tree

10 files changed

+403
-0
lines changed

10 files changed

+403
-0
lines changed

apps/schedclock/ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.01: New App!

apps/schedclock/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Schedule Clock Faces
2+
3+
Change clock faces on a schedule.
4+
5+
For example: a fun clock face for weekends and after work; a detailed clock face for work days.
6+
7+
![Screenshot](screenshot1.png)
8+
9+
## Usage
10+
11+
* Open the `Schedule Clock` app or find it in the `Settings` > `Apps` menu.
12+
* Set `Enabled` to checked
13+
* Select `Add New` to add a new scheduled face change
14+
* Select the `Day`, `Hour`, `Minute`, and what `Clock` to change to
15+
* Select `Save` to save the new (or changed) schedule
16+
17+
![SaveButton](screenshot2.png)
18+
19+
An entry in `Scheduler` will be created for each scheduled clock change.
20+
21+
If the clockface you selected has been uninstalled, the schedule will still exist but won't do anything.
22+
23+
## To Uninstall
24+
Before uninstalling this app, clean up any scheduled alarms by setting the `Enabled` toggle to unchecked.
25+
26+
If you skip this step, orphaned alarms may cause error logs but won't affect functionality.
27+
28+
You can also remove the extra `schedclock` alarms manually with the [Scheduler](/?id=sched) app.
29+
30+
## Creator
31+
32+
[kidneyhex](https://github.com/kidneyhex)
33+
34+
## Attribution
35+
36+
App icon: [Schedule](https://icons8.com/icon/E7VlDozxin8k/schedule) by [Icons8](https://icons8.com/)

apps/schedclock/app-icon.js

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

apps/schedclock/app.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(function () {
2+
3+
// Load the settings page
4+
eval(require("Storage").read("schedclock.settings.js"))(()=>load());
5+
6+
})();

apps/schedclock/app.png

312 Bytes
Loading

apps/schedclock/lib.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const SETTINGS_FILE = "schedclock.settings.json";
2+
const APP_ID = "schedclock";
3+
4+
/**
5+
* Called directly by an alarm to load a specific clock face
6+
* @param {string} faceSrc - Source file of the clock face to load (e.g. "myclock.js")
7+
**/
8+
const setClock = function(faceSrc) {
9+
const settings = require("Storage").readJSON("setting.json", 1) || {};
10+
// Only change the clock if it's different
11+
if (faceSrc && settings.clock !== faceSrc) {
12+
const face = require("Storage").read(faceSrc);
13+
// If the face doesn't exist, do nothing (but log it)
14+
if (!face) {
15+
console.log("schedclock: Invalid clock face", faceSrc);
16+
return;
17+
}
18+
settings.clock = faceSrc;
19+
settings.clockHasWidgets = face.includes("Bangle.loadWidgets");
20+
require("Storage").writeJSON("setting.json", settings);
21+
if(Bangle.CLOCK) load(); // Reload clock if we're on it
22+
}
23+
};
24+
25+
/**
26+
* Handle alarms and resetting them
27+
* @param {number} index Index of the alarm that went off
28+
* @param {string} clock Clockface
29+
*/
30+
exports.onAlarm = function(index, clock) {
31+
const date = new Date();
32+
const Sched = require("sched");
33+
const alarm = Sched.getAlarm(`${APP_ID}.${index}`);
34+
alarm.last = date.getDate(); // prevent second run on the same day
35+
Sched.setAlarm(alarm.id, alarm);
36+
setClock(clock);
37+
};
38+
39+
/**
40+
* Function to sync all alarms in the scheduler with the settings file.
41+
* Called every time settings are changed; maybe a bit excessive, but keeps things simple.
42+
**/
43+
exports.syncAlarms = function() {
44+
const Sched = require("sched");
45+
const settings = require("Storage").readJSON(SETTINGS_FILE, 1) || [];
46+
47+
// Remove all existing alarms from the scheduler library
48+
Sched
49+
.getAlarms()
50+
.filter(a => a.appid && a.appid === APP_ID)
51+
.forEach(a => Sched.setAlarm(a.id, undefined));
52+
53+
// If the app is disabled, we're done.
54+
if (!settings.enabled) return;
55+
56+
// Alarms need "last" set to let sched know they've already ran for the day
57+
// So if an alarm is for before "now", set last to yesterday so it still triggers today
58+
// else set last to today.
59+
const currentDate = new Date();
60+
const currentTime = (currentDate.getHours()*3600000)+(currentDate.getMinutes()*60000)+(currentDate.getSeconds()*1000);
61+
const dayOfMonthToday = currentDate.getDate();
62+
const dayOfMonthYesterday = dayOfMonthToday - 1;
63+
64+
// Add a new alarm for each setting item
65+
settings.sched.forEach((item, index) => {
66+
67+
// Skip invalid records
68+
if (item.hour === undefined || item.minute === undefined) return;
69+
70+
const scheduledTime = (item.hour * 3600000) + (item.minute * 60000);
71+
72+
// Create the new alarm object and save it using a unique ID.
73+
Sched.setAlarm(`${APP_ID}.${index}`, {
74+
t: scheduledTime, // time in milliseconds since midnight
75+
on: true,
76+
rp: true,
77+
last: (scheduledTime > currentTime) ? dayOfMonthYesterday : dayOfMonthToday,
78+
dow: item.dow,
79+
hidden: true,
80+
appid: APP_ID,
81+
js: `require('${APP_ID}.lib.js').onAlarm(${index},'${item.face}')`,
82+
});
83+
});
84+
};

apps/schedclock/metadata.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{ "id": "schedclock",
2+
"name": "Schedule Clock Faces",
3+
"shortName":"Sched Clock",
4+
"version":"0.01",
5+
"author": "kidneyhex",
6+
"description": "Change clock faces on a schedule.",
7+
"icon": "app.png",
8+
"screenshots": [
9+
{"url":"screenshot1.png"},
10+
{"url":"screenshot2.png"}
11+
],
12+
"tags": "tool",
13+
"supports" : ["BANGLEJS2"],
14+
"readme": "README.md",
15+
"dependencies": {"scheduler":"type"},
16+
"storage": [
17+
{"name":"schedclock.app.js","url":"app.js"},
18+
{"name":"schedclock.settings.js","url":"settings.js"},
19+
{"name":"schedclock.lib.js","url":"lib.js"},
20+
{"name":"schedclock.img","url":"app-icon.js","evaluate":true}
21+
],
22+
"data": [{"name":"schedclock.settings.json"}]
23+
}

apps/schedclock/screenshot1.png

1.74 KB
Loading

apps/schedclock/screenshot2.png

1.44 KB
Loading

0 commit comments

Comments
 (0)