Merge pull request #688 from ollycross/master

feat(app): Update "Lazy Clock" app to v0.02
master
Gordon Williams 2021-03-17 09:32:05 +00:00 committed by GitHub
commit cc4cea3a30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 97 additions and 79 deletions

View File

@ -2623,7 +2623,7 @@
{ "id": "lazyclock", { "id": "lazyclock",
"name": "Lazy Clock", "name": "Lazy Clock",
"icon": "lazyclock.png", "icon": "lazyclock.png",
"version":"0.01", "version":"0.02",
"readme": "README.md", "readme": "README.md",
"description": "Tells the time, roughly", "description": "Tells the time, roughly",
"tags": "clock", "tags": "clock",

View File

@ -1 +1,2 @@
0.01: Launch app 0.01: Launch app
0.02: Fix bug with the elusive one o'clock monster; Only change template when going over boundaries; Re-jig wording options

View File

@ -1,5 +1,6 @@
let secondInterval; let secondInterval;
let showRealTime = false; let showRealTime = false;
let currentFormatter;
const utils = { const utils = {
random: function(items) { random: function(items) {
@ -68,13 +69,14 @@ const words = {
approx: ['\'Bout', 'About', 'Around', `Summat\nlike`, 'Near', 'Close to'], approx: ['\'Bout', 'About', 'Around', `Summat\nlike`, 'Near', 'Close to'],
approach: ['Nearly', `Coming\nup to`, 'Approaching', `A touch\nbefore`], approach: ['Nearly', `Coming\nup to`, 'Approaching', `A touch\nbefore`],
past: [`A shade\nafter`, `A whisker\nafter`, 'Just gone'], past: [`A shade\nafter`, `A whisker\nafter`, 'Just gone'],
quarter: ['Quarter', `Fifteen\nminutes`], quarter: ['A quarter', 'Quarter'],
half: ['Half', 'Half past'], half: ['Half', 'Half past'],
exactly: ['exactly', 'on the dot', 'o\' clock'], exactly: ['exactly', 'on the dot', 'o\' clock'],
ish: ['-ish', `\n(ish)`] ish: ['-ish', `\n(ish)`, `\nand change`, `\nand some`, `\nor`, `\nor\nthereabouts`]
}; };
function switchMode() { function switchMode() {
currentFormatter = null;
showRealTime = !showRealTime; showRealTime = !showRealTime;
refreshTime(); refreshTime();
} }
@ -94,12 +96,7 @@ function drawRealTime(date) {
} }
function drawDumbTime(time) { const makeApprox = (str, template) => {
const hours = time.getHours();
const minutes = time.getMinutes();
function formatTime(hours, minutes) {
const makeApprox = (str, template) => {
let _template = template || 'approx'; let _template = template || 'approx';
if (utils.oneIn(2)) { if (utils.oneIn(2)) {
_template = 'approx'; _template = 'approx';
@ -113,15 +110,15 @@ function drawDumbTime(time) {
const approx = `${utils.random(words[_template])} `; const approx = `${utils.random(words[_template])} `;
return `${approx}\n${str.toLowerCase()}`; return `${approx}\n${str.toLowerCase()}`;
}; };
const formatters = { const formatters = {
'onTheHour': (hoursAsWord) => { 'onTheHour': (hoursAsWord) => {
const exactly = utils.random(words.exactly); const exactly = utils.random(words.exactly);
return `${hoursAsWord}\n${exactly}`; return `${hoursAsWord}\n${exactly}`;
}, },
'nearTheHour': (hoursAsWord) => { 'nearTheHour': (hoursAsWord, minutes) => {
const template = (minutes < 10) ? 'past' : 'approach'; const template = (minutes < 10) ? 'past' : 'approach';
return makeApprox(hoursAsWord, template); return makeApprox(hoursAsWord, template);
@ -129,41 +126,39 @@ function drawDumbTime(time) {
'nearQuarter': (hoursAsWord, minutes) => { 'nearQuarter': (hoursAsWord, minutes) => {
const direction = (minutes > 30) ? 'to' : 'past'; const direction = (minutes > 30) ? 'to' : 'past';
const quarter = utils.random(words.quarter); const quarter = utils.random(words.quarter);
const formatted = `${quarter} ${direction}\n${hoursAsWord}`; const formatted = `${quarter} ${direction}\n${hoursAsWord}`;
return (minutes === 15 || minutes === 45) ? formatted : makeApprox(formatted); return (minutes === 15 || minutes === 45) ? formatted : makeApprox(formatted);
}, },
'nearHalf': (hoursAsWord, minutes) => { 'nearHalf': (hoursAsWord, minutes) => {
const half = utils.random(words.half); const half = utils.random(words.half);
const formatted = `${half}\n${hoursAsWord}`; const formatted = `${half}\n${hoursAsWord}`;
const template = (minutes > 30) ? 'past' : 'approach'; const template = (minutes > 30) ? 'past' : 'approach';
return (minutes === 30) ? formatted : makeApprox(formatted, template); return (minutes === 30) ? formatted : makeApprox(formatted, template);
}, },
}; };
function getFormatter(hours, minutes) { function getFormatter(hours, minutes) {
if (minutes === 0) { if (minutes === 0) {
return formatters.onTheHour; return 'onTheHour';
} else if (minutes > 50 || minutes < 10) { } else if (minutes > 50 || minutes < 10) {
return formatters.nearTheHour; return 'nearTheHour';
} else if (minutes > 40|| minutes < 20) { } else if (minutes > 40|| minutes < 20) {
return formatters.nearQuarter; return 'nearQuarter';
} else {
return formatters.nearHalf;
}
} }
return 'nearHalf';
}
function drawDumbTime(hours, minutes, formatter) {
function formatTime(hours, minutes, formatter) {
const hoursAsWord = utils.hours2Word(hours, minutes); const hoursAsWord = utils.hours2Word(hours, minutes);
const formatter = getFormatter(hours, minutes);
return formatter(hoursAsWord, minutes); return formatter(hoursAsWord, minutes);
} }
utils.print(formatTime(hours, minutes)); utils.print(formatTime(hours, minutes, formatter));
} }
function cancelTimeout() { function cancelTimeout() {
@ -174,22 +169,42 @@ function cancelTimeout() {
secondInterval = undefined; secondInterval = undefined;
} }
function refreshTime() { function refreshTime(force) {
cancelTimeout(); function clearForRefresh() {
g.clearRect(0, 24, g.getWidth(), g.getHeight()-24); g.clearRect(0, 24, g.getWidth(), g.getHeight()-24);
g.reset(); g.reset();
g.setFontAlign(0,0); g.setFontAlign(0,0);
}
function setRefreshInterval(time) {
const secondsTillRefresh = 60 - time.getSeconds();
secondInterval = setTimeout(refreshTime, secondsTillRefresh * 1000);
return secondInterval;
}
force = force === true;
cancelTimeout();
const time = new Date(); const time = new Date();
const method = showRealTime ? drawRealTime : drawDumbTime; if (showRealTime) {
clearForRefresh();
drawRealTime(time);
return setRefreshInterval(time);
}
method(time); const hours = time.getHours();
const minutes = time.getMinutes();
const formatter = getFormatter(hours, minutes);
const secondsTillRefresh = 60 - time.getSeconds(); if (formatter !== currentFormatter) {
clearForRefresh();
currentFormatter = formatter;
drawDumbTime(hours, minutes, formatters[formatter]);
}
secondInterval = setTimeout(refreshTime, secondsTillRefresh * 1000); return setRefreshInterval(time);
} }
@ -200,6 +215,7 @@ function startClock() {
function addEvents() { function addEvents() {
Bangle.on('lcdPower', (on) => { Bangle.on('lcdPower', (on) => {
cancelTimeout(); cancelTimeout();
currentFormatter = null;
if (on) { if (on) {
startClock(); startClock();
} }
@ -215,8 +231,10 @@ function addEvents() {
edge: "falling" edge: "falling"
}); });
setWatch(() => {
setWatch(refreshTime, BTN3, { currentFormatter = null;
refreshTime();
}, BTN3, {
repeat: true, repeat: true,
edge: "falling" edge: "falling"
}); });
@ -232,5 +250,4 @@ function init() {
addEvents(); addEvents();
} }
init(); init();