diff --git a/apps.json b/apps.json index d502b37c4..18d20b9da 100644 --- a/apps.json +++ b/apps.json @@ -392,7 +392,8 @@ { "id": "swatch", "name": "Stopwatch", "icon": "stopwatch.png", - "version":"0.03", + "version":"0.04", + "interface": "interface.html", "description": "Simple stopwatch with Lap Time logging to a JSON file", "tags": "health", "allow_emulator":true, diff --git a/apps/heart/interface.html b/apps/heart/interface.html index 177e2cdfb..4a21d2e27 100644 --- a/apps/heart/interface.html +++ b/apps/heart/interface.html @@ -11,17 +11,7 @@ var domRecords = document.getElementById("records"); function saveRecord(record,name) { var csv = `${record.map(rec=>[rec.time, rec.bpm, rec.confidence].join(",")).join("\n")}`; - var a = document.createElement("a"), - file = new Blob([csv], {type: "Comma-separated value file"}); - var url = URL.createObjectURL(file); - a.href = url; - a.download = name+".csv"; - document.body.appendChild(a); - a.click(); - setTimeout(function() { - document.body.removeChild(a); - window.URL.revokeObjectURL(url); - }, 0); + Util.saveCSV(name, csv); } diff --git a/apps/swatch/ChangeLog b/apps/swatch/ChangeLog index 86a782585..2900c9aa1 100644 --- a/apps/swatch/ChangeLog +++ b/apps/swatch/ChangeLog @@ -3,3 +3,4 @@ Lap log now scrolls into 2nd column after 18th entry, able to display 36 entries before going off screen 0.03: Added ability to save Lap log as a date named JSON file into memory Fixed bug from 0.01 where BN1 (reset) could clear the lap log when timer is running +0.04: Changed save file filename, add interface.html to allow laps to be loaded diff --git a/apps/swatch/interface.html b/apps/swatch/interface.html new file mode 100644 index 000000000..928c5fe39 --- /dev/null +++ b/apps/swatch/interface.html @@ -0,0 +1,90 @@ + +
+ + + + + + + + + diff --git a/apps/swatch/stopwatch.js b/apps/swatch/stopwatch.js index d4136d8ed..2ebd8198b 100644 --- a/apps/swatch/stopwatch.js +++ b/apps/swatch/stopwatch.js @@ -4,7 +4,6 @@ var started = false; var timeY = 60; var hsXPos = 0; var lapTimes = []; -var saveTimes = []; var displayInterval; function timeToText(t) { @@ -25,7 +24,7 @@ function updateLabels() { for (var i in lapTimes) { if (i<18) {g.drawString(lapTimes.length-i+": "+timeToText(lapTimes[i]),35,timeY + 30 + i*8);} - else + else {g.drawString(lapTimes.length-i+": "+timeToText(lapTimes[i]),125,timeY + 30 + (i-18)*8);} } drawsecs(); @@ -51,10 +50,8 @@ function drawms() { g.clearRect(hsXPos,timeY,220,timeY+20); g.drawString("."+("0"+hs).substr(-2),hsXPos,timeY+10); } -function saveconvert() { - for (var v in lapTimes){ - saveTimes[v]=v+1+"-"+timeToText(lapTimes[(lapTimes.length-1)-v]); - } +function getLapTimesArray() { + return lapTimes.map(timeToText).reverse(); } setWatch(function() { // Start/stop @@ -80,16 +77,21 @@ setWatch(function() { // Start/stop }, BTN2, {repeat:true}); setWatch(function() { // Lap Bangle.beep(); - if (started) tCurrent = Date.now(); - lapTimes.unshift(tCurrent-tStart); - tStart = tCurrent; - if (!started) - { - var timenow= Date(); - saveconvert(); - require("Storage").writeJSON("StpWch-"+timenow.toString(), saveTimes); + if (started) { + tCurrent = Date.now(); + lapTimes.unshift(tCurrent-tStart); + } + tStart = tCurrent; + if (!started) { // save + var timenow= Date(); + var filename = "swatch-"+(new Date()).toISOString().substr(0,16).replace("T","_")+".json"; + // this maxes out the 28 char maximum + require("Storage").writeJSON(filename, getLapTimesArray()); + E.showMessage("Laps Saved","Stopwatch"); + setTimeout(updateLabels, 1000); + } else { + updateLabels(); } - updateLabels(); }, BTN1, {repeat:true}); setWatch(function() { // Reset if (!started) { diff --git a/lib/interface.js b/lib/interface.js index 414c9d7fb..7e8be4fd9 100644 --- a/lib/interface.js +++ b/lib/interface.js @@ -39,7 +39,10 @@ var Util = { window.postMessage({type:"readstoragefile",data:filename,id:__id}); }, eraseStorageFile : function(filename,callback) { - Puck.write(`\x10require("Storage").open(${JSON.stringify(filename)}","r").erase()\n`,callback); + Puck.write(`\x10require("Storage").open(${JSON.stringify(filename)},"r").erase()\n`,callback); + }, + eraseStorage : function(filename,callback) { + Puck.write(`\x10require("Storage").erase(${JSON.stringify(filename)})\n`,callback); }, showModal : function(title) { if (!Util.domModal) { @@ -66,6 +69,19 @@ var Util = { hideModal : function() { if (!Util.domModal) return; Util.domModal.classList.remove("active"); + }, + saveCSV : function(filename, csvData) { + var a = document.createElement("a"), + file = new Blob([csvData], {type: "Comma-separated value file"}); + var url = URL.createObjectURL(file); + a.href = url; + a.download = filename+".csv"; + document.body.appendChild(a); + a.click(); + setTimeout(function() { + document.body.removeChild(a); + window.URL.revokeObjectURL(url); + }, 0); } }; window.addEventListener("message", function(event) {