diff --git a/apps/counter2/ChangeLog b/apps/counter2/ChangeLog index d952c505b..76543fa12 100644 --- a/apps/counter2/ChangeLog +++ b/apps/counter2/ChangeLog @@ -3,3 +3,4 @@ 0.03: Fix lint warnings 0.04: Fix lint warnings 0.05: Fix on not reading counter defaults in Settings +0.06: Added ability to display only one counter and fast-scrolling diff --git a/apps/counter2/README.md b/apps/counter2/README.md index d57844aae..b2f48e384 100644 --- a/apps/counter2/README.md +++ b/apps/counter2/README.md @@ -11,10 +11,13 @@ The counter state gets saved. Best to use this with pattern launcher or ClockCal ## Howto - Tap top side or swipe up to increase counter - Tap bottom side or swipe down to decrease counter - - Hold (600ms) to reset to default value (configurable) - - Press button to exit + - Hold either side to quickly increase or decrease the counter. + - If the counters are not on their presets, then press the button to reset them + - If the counters are on their presets, then press the button to leave the screen + - Long press the button to leave the screen regardless of the counter values ## Configurable Features +- Display both Counters or only Counter 1 - Default value Counter 1 - Default value Counter 2 - Buzz on interact diff --git a/apps/counter2/app.js b/apps/counter2/app.js index 42b59cf5d..01b16a72a 100644 --- a/apps/counter2/app.js +++ b/apps/counter2/app.js @@ -1,20 +1,25 @@ -Bangle.loadWidgets(); - var s = Object.assign({ + display2:true, counter0:10, counter1:20, max0:15, max1:25, + fullscreen:true, buzz: true, colortext: true, }, require('Storage').readJSON("counter2.json", true) || {}); +var sGlob = Object.assign({ + timeout: 10, +}, require('Storage').readJSON("setting.json", true) || {}); + const f1 = (s.colortext) ? "#f00" : "#fff"; const f2 = (s.colortext) ? "#00f" : "#fff"; const b1 = (s.colortext) ? g.theme.bg : "#f00"; const b2 = (s.colortext) ? g.theme.bg : "#00f"; var drag; +var dragCurr = {x:0 ,y:0 ,dx:0 ,dy:0 ,b:false}; const screenwidth = g.getWidth(); const screenheight = g.getHeight(); @@ -37,51 +42,111 @@ function saveSettings() { } let ignoreonce = false; +let fastupdateoccurring = false; var dragtimeout; function updateScreen() { - g.setBgColor(b1); - g.clearRect(0, 0, halfwidth, screenheight); - g.setBgColor(b2); - g.clearRect(halfwidth, 0, screenwidth, screenheight); - g.setFont("Vector", 60).setFontAlign(0, 0); - g.setColor(f1); - g.drawString(Math.floor(counter[0]), halfwidth * 0.5, halfheight); - g.setColor(f2); - g.drawString(Math.floor(counter[1]), halfwidth * 1.5, halfheight); + if (s.display2) { + g.setBgColor(b1); + g.clearRect(0, 0, halfwidth, screenheight); + g.setBgColor(b2); + g.clearRect(halfwidth, 0, screenwidth, screenheight); + g.setFont("Vector", 60).setFontAlign(0, 0); + g.setColor(f1); + g.drawString(Math.floor(counter[0]), halfwidth * 0.5, halfheight); + g.setColor(f2); + g.drawString(Math.floor(counter[1]), halfwidth * 1.5, halfheight); + } + else { + g.setBgColor(b2); // Using right counter's colors b/c blue looks nicer than red + g.clearRect(0, 0, screenwidth, screenheight); + g.setFont("Vector", 90).setFontAlign(0, 0); + g.setColor(f2); + g.drawString(Math.floor(counter[0]), halfwidth, halfheight); + } saveSettings(); if (s.buzz) Bangle.buzz(50,.5); - Bangle.drawWidgets(); + + Bangle.loadWidgets(); + if (s.fullscreen) { + require("widget_utils").hide(); + } + else { + Bangle.drawWidgets(); + } } +// Clearing the timer on lock is likely uneeded, but just in case +Bangle.on('lock', e => { + drag = undefined; + var timeOutTimer = sGlob.timeout * 1000; + Bangle.setOptions({backlightTimeout: timeOutTimer, lockTimeout: timeOutTimer}); + if (dragtimeout) clearTimeout(dragtimeout); + fastupdateoccurring = false; +}); + Bangle.on("drag", e => { - const c = (e.x < halfwidth) ? 0 : 1; + const c = (e.x >= halfwidth && s.display2) ? 1 : 0; + dragCurr = e; if (!drag) { if (ignoreonce) { ignoreonce = false; return; } drag = { x: e.x, y: e.y }; - dragtimeout = setTimeout(function () { resetcounter(c); }, 600); //if dragging for 500ms, reset counter + dragtimeout = setTimeout(function () { fastupdatecounter(c); }, 600); //if dragging for 500ms, reset counter } else if (drag && !e.b) { // released - let adjust = 0; - const dx = e.x - drag.x, dy = e.y - drag.y; - if (Math.abs(dy) > Math.abs(dx) + 30) { - adjust = (dy > 0) ? -1 : 1; - } else { - adjust = (e.y > halfwidth) ? -1 : 1; - } - counter[c] += adjust; - updateScreen(); - drag = undefined; + if (!fastupdateoccurring) + updatecounter(c); + drag = undefined; + if (dragtimeout) { + let timeOutTimer = 1000; + Bangle.setOptions({backlightTimeout: timeOutTimer, lockTimeout: timeOutTimer}); clearTimeout(dragtimeout); } + fastupdateoccurring = false; + } }); +function updatecounter(which) { + let adjust = 0; + const dx = dragCurr.x - drag.x, dy = dragCurr.y - drag.y; + if (Math.abs(dy) > Math.abs(dx) + 30) { + adjust = (dy > 0) ? -1 : 1; + } else { + adjust = (dragCurr.y > halfheight) ? -1 : 1; + } + counter[which] += adjust; + updateScreen(); +} + +function fastupdatecounter(which) { + fastupdateoccurring = true; + updatecounter(which); + Bangle.setOptions({backlightTimeout: 0, lockTimeout: 0}); + dragtimeout = setTimeout(function () { fastupdatecounter(which); }, 10); +} + + function resetcounter(which) { - counter[which] = defaults[which]; - console.log("resetting counter ", which); + // If which is null, reset all + fastupdateoccurring = false; + if (dragtimeout) { + let timeOutTimer = 1000; + Bangle.setOptions({backlightTimeout: timeOutTimer, lockTimeout: timeOutTimer}); + clearTimeout(dragtimeout); + } + if (which == null) { + for (let iter = 0; iter < defaults.length; iter++) { + counter[iter] = defaults[iter]; + } + console.log("resetting all counters"); + } + else { + counter[which] = defaults[which]; + console.log("resetting counter ", which); + } updateScreen(); drag = undefined; ignoreonce = true; @@ -91,5 +156,13 @@ function resetcounter(which) { updateScreen(); setWatch(function() { + for (let which = 0; which < defaults.length; which++) { + if(counter[which] != defaults[which]) { + resetcounter(null); + return; + } + } + var timeOutTimer = sGlob.timeout * 1000; + Bangle.setOptions({backlightTimeout: timeOutTimer, lockTimeout: timeOutTimer}); load(); }, BTN1, {repeat:true, edge:"falling"}); diff --git a/apps/counter2/metadata.json b/apps/counter2/metadata.json index 04b00487f..11cc9776f 100644 --- a/apps/counter2/metadata.json +++ b/apps/counter2/metadata.json @@ -1,7 +1,7 @@ { "id": "counter2", "name": "Counter2", - "version": "0.05", + "version": "0.06", "description": "Dual Counter", "readme":"README.md", "icon": "counter2-icon.png", diff --git a/apps/counter2/settings.js b/apps/counter2/settings.js index d74d9269f..32c6a2009 100644 --- a/apps/counter2/settings.js +++ b/apps/counter2/settings.js @@ -1,10 +1,12 @@ (function (back) { var FILE = "counter2.json"; const defaults={ + display2:true, counter0:12, counter1:0, max0:12, max1:0, + fullscreen: true, buzz: true, colortext: true, }; @@ -14,26 +16,40 @@ require('Storage').writeJSON(FILE, settings); } - const menu = { - "": { "title": "Counter2" }, - "< Back": () => back(), - 'Default C1': { - value: settings.max0, - min: -99, max: 99, - onchange: v => { - settings.max0 = v; - writeSettings(); - } - }, - 'Default C2': { - value: settings.max1, - min: -99, max: 99, - onchange: v => { - settings.max1 = v; - writeSettings(); - } - }, - 'Color': { + function showMainMenu() { + let appMenu = { + "": { "title": "Counter2" }, + "< Back": () => back(), + 'Counters to Display ': { + value: settings.display2, + format: v => v?"2":"1", + onchange: v => { + settings.display2 = v; + writeSettings(); + // redisplay the menu with/without C2 setting + setTimeout(showMainMenu, 0); + } + }, + 'Default C1': { + value: settings.max0, + min: -99, max: 99, + onchange: v => { + settings.max0 = v; + writeSettings(); + } + }, + }; + if (settings.display2) { + appMenu['Default C2'] = { + value: settings.max1, + min: -99, max: 99, + onchange: v => { + settings.max1 = v; + writeSettings(); + } + }; + } + appMenu['Color'] = { value: settings.colortext, format: v => v?"Text":"Backg", onchange: v => { @@ -41,15 +57,23 @@ console.log("Color",v); writeSettings(); } - }, - 'Vibrate': { + }; + appMenu['Fullscreen'] = { + value: settings.fullscreen, + onchange: v => { + settings.fullscreen = v; + writeSettings(); + } + }; + appMenu['Vibrate'] = { value: settings.buzz, onchange: v => { settings.buzz = v; writeSettings(); - } - } - }; - // Show the menu - E.showMenu(menu); -}) + }, + }; + E.showMenu(appMenu); + } + + showMainMenu(); + }) \ No newline at end of file