Skip to content

Commit 6ff61b9

Browse files
committed
Add saveFile function, and ensure it uses data: URI not blobs (because they don't work in Gadgetbridge)
Reduce duplication to make custom/interface use .saveFile where possible (avoiding Blob)
1 parent cc1d369 commit 6ff61b9

File tree

10 files changed

+25
-104
lines changed

10 files changed

+25
-104
lines changed

apps/gpsrec/interface.html

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,7 @@
2323
</Placemark>
2424
</Document>
2525
</kml>`;
26-
var a = document.createElement("a"),
27-
file = new Blob([kml], {type: "application/vnd.google-earth.kml+xml"});
28-
var url = URL.createObjectURL(file);
29-
a.href = url;
30-
a.download = title+".kml";
31-
document.body.appendChild(a);
32-
a.click();
33-
setTimeout(function() {
34-
document.body.removeChild(a);
35-
window.URL.revokeObjectURL(url);
36-
}, 0);
26+
Util.saveFile(title+".kml", "application/vnd.google-earth.kml+xml", kml);
3727
}
3828

3929
function saveGPX(track, title) {
@@ -56,17 +46,7 @@
5646
</trkseg>
5747
</trk>
5848
</gpx>`;
59-
var a = document.createElement("a"),
60-
file = new Blob([gpx], {type: "application/gpx+xml"});
61-
var url = URL.createObjectURL(file);
62-
a.href = url;
63-
a.download = title+".gpx";
64-
document.body.appendChild(a);
65-
a.click();
66-
setTimeout(function() {
67-
document.body.removeChild(a);
68-
window.URL.revokeObjectURL(url);
69-
}, 0);
49+
Util.saveFile(title+".gpx", "application/gpx+xml", gpx);
7050
}
7151

7252
function trackLineToObject(l, hasTrackNumber) {

apps/hrmaccevents/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ The CSV data contains the following columns:
2828
* PPG_o - `e.vcPPGoffs` from the `Bangle.on("HRM-raw"` event. This is the PPG offset used to map `e.vcPPG` to `e.raw` so there are no glitches when the exposure values in the sensor change.
2929
* BTHRM - BPM figure from external Bluetooth HRM device (this is our reference BPM)
3030

31+
## FIXME
32+
33+
The `custom.html` for the app uses the Puck.js lib directly when it should just use `customize.js` - it won't work well under Gadgetbridge and may fail on other platforms too
34+
3135
## Creator
3236

3337
[halemmerich](https://github.com/halemmerich)

apps/hrmaccevents/custom.html

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<link rel="stylesheet" href="../../css/spectre.min.css">
55
</head>
66
<body>
7+
<script src="../../core/lib/customize.js"></script>
78
<script src="https://www.puck-js.com/puck.js"></script>
89
<p>
910
<div class="form-group">
@@ -23,20 +24,6 @@
2324
<p id="result"></p>
2425
<script>
2526

26-
function saveCSV(filename, csvData) {
27-
let a = document.createElement("a"),
28-
file = new Blob([csvData], {type: "Comma-separated value file"});
29-
let url = URL.createObjectURL(file);
30-
a.href = url;
31-
a.download = filename+".csv";
32-
document.body.appendChild(a);
33-
a.click();
34-
setTimeout(function() {
35-
document.body.removeChild(a);
36-
window.URL.revokeObjectURL(url);
37-
}, 0);
38-
}
39-
4027
function createCode(){
4128
//modes: 1 BT, 2 File
4229
return "var method=" + (document.getElementById("chkLocal").checked ? 2 : 1) + ";\n" + String.raw`
@@ -48,7 +35,7 @@
4835
var gotBTHRM = false;
4936
var gotHRM = false;
5037
var gotAcc = false;
51-
38+
5239
var events = -1;
5340
var hrmRaw,hrmPulse,bthrmPulse
5441
@@ -59,9 +46,9 @@
5946
let bthrmSettings = (require("Storage").readJSON("bthrm.json",1) || {});
6047
6148
Bangle.setHRMPower(1);
62-
49+
6350
if (bthrmSettings.replace) Bangle.origSetHRMPower(1);
64-
51+
6552
if (Bangle.setBTHRMPower){
6653
Bangle.setBTHRMPower(1);
6754
} else {
@@ -166,7 +153,7 @@
166153
drawStatus(free>0.25*process.env.STORAGE, h++, Math.floor(free/1024) + "K");
167154
}
168155
}
169-
156+
170157
var intervalId = -1;
171158
172159
g.setFont12x20();
@@ -181,7 +168,7 @@
181168
drawStatusText("Events", h++);
182169
if (method == 2) drawStatusText("Storage", h++);
183170
updateStatus();
184-
171+
185172
intervalId = setInterval(()=>{
186173
updateStatus();
187174
}, 1000);
@@ -221,8 +208,8 @@
221208

222209
window.addEventListener("message", function(event) {
223210
let msg = event.data;
224-
if (msg.type=="readstoragefilersp") {
225-
saveCSV("log.csv", msg.data);
211+
if (msg.type=="readstoragefilersp") {
212+
Util.saveCSV("log", msg.data);
226213
}
227214
}, false);
228215

@@ -240,7 +227,7 @@
240227
});
241228

242229
document.getElementById("btnSave").addEventListener("click", function() {
243-
saveCSV("log.csv", localStorage.getItem("data"));
230+
Util.saveCSV("log", localStorage.getItem("data"));
244231
});
245232

246233
function reset(){
@@ -289,7 +276,7 @@
289276
l.forEach(onLine);
290277
});
291278
connection.write("reset();\n", function() {
292-
setTimeout(function() {
279+
setTimeout(function() {
293280
connection.write("\x03\x10if(1){"+createCode()+"}\n",
294281
function() { console.log("Ready..."); });
295282
}, 1500);

apps/medicalinfo/interface.html

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,7 @@
9595
Util.showModal("Downloading...");
9696
let medicalInfoJson = getEditableContent();
9797
if (isJsonString(medicalInfoJson)) {
98-
var a = document.createElement("a"),
99-
file = new Blob([medicalInfoJson], { type: "application/json" });
100-
var url = URL.createObjectURL(file);
101-
a.href = url;
102-
a.download = medicalInfoFile;
103-
document.body.appendChild(a);
104-
a.click();
105-
setTimeout(function () {
106-
document.body.removeChild(a);
107-
window.URL.revokeObjectURL(url);
108-
}, 0);
98+
Util.saveFile(medicalInfoFile, "application/json", medicalInfoJson);
10999
} else {
110100
document.getElementById("info").innerHTML = errorFormat();
111101
}

apps/presentation_timer/interface.html

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,7 @@
9696
Util.showModal("Downloading...");
9797
let csvTimes = getEditableContent();
9898
if (isCorrectCsvString(csvTimes)) {
99-
var a = document.createElement("a"),
100-
file = new Blob([csvTimes], { type: "text/csv" });
101-
var url = URL.createObjectURL(file);
102-
a.href = url;
103-
a.download = filePresentationTimer;
104-
document.body.appendChild(a);
105-
a.click();
106-
setTimeout(function () {
107-
document.body.removeChild(a);
108-
window.URL.revokeObjectURL(url);
109-
}, 0);
99+
Util.saveFile(filePresentationTimer, "text/csv", csvTimes);
110100
} else {
111101
document.getElementById("info").innerHTML = errorFormat();
112102
}

apps/recorder/interface.html

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,7 @@
7575
</Folder>
7676
</Document>
7777
</kml>`;
78-
var a = document.createElement("a"),
79-
file = new Blob([kml], {type: "application/vnd.google-earth.kml+xml"});
80-
var url = URL.createObjectURL(file);
81-
a.href = url;
82-
a.download = title+".kml";
83-
document.body.appendChild(a);
84-
a.click();
85-
setTimeout(function() {
86-
document.body.removeChild(a);
87-
window.URL.revokeObjectURL(url);
88-
}, 0);
78+
Util.saveFile(title+".kml", "application/vnd.google-earth.kml+xml", kml);
8979
showToast("Download finished.", "success");
9080
}
9181

@@ -122,17 +112,7 @@
122112
</trkseg>
123113
</trk>
124114
</gpx>`;
125-
var a = document.createElement("a"),
126-
file = new Blob([gpx], {type: "application/gpx+xml"});
127-
var url = URL.createObjectURL(file);
128-
a.href = url;
129-
a.download = title+".gpx";
130-
document.body.appendChild(a);
131-
a.click();
132-
setTimeout(function() {
133-
document.body.removeChild(a);
134-
window.URL.revokeObjectURL(url);
135-
}, 0);
115+
Util.saveFile(title+".gpx", "application/gpx+xml", gpx);
136116
showToast("Download finished.", "success");
137117
}
138118

apps/swatch/interface.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
});
7676
}
7777
if (task=="download") {
78-
Util.saveCSV(lap.n.slice(0,-5)+".csv", lap.d.map((d,n)=>[n+1,d].join(",")).join("\n"));
78+
Util.saveCSV(lap.n.slice(0,-5), lap.d.map((d,n)=>[n+1,d].join(",")).join("\n"));
7979
}
8080
});
8181
}

apps/tempmonitor/interface.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<th>Month</th>
3333
<th>Day</th>
3434
<th>Time HH:mm</th>
35-
<th>temperature</th>
35+
<th>temperature</th>
3636
</tr>`+data.trim().split("\n").map(l=>{
3737
l = l.split(";");
3838
return `<tr>
@@ -47,7 +47,7 @@
4747

4848
// You can call a utility function to save the data
4949
document.getElementById("btnSave").addEventListener("click", function() {
50-
Util.saveCSV("temphistory.csv", csvData);
50+
Util.saveCSV("temphistory", csvData);
5151
});
5252
// Or you can also delete the file
5353
document.getElementById("btnDelete").addEventListener("click", function() {

apps/todolist/interface.html

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,7 @@
9595
Util.showModal("Downloading...");
9696
let jsonTodos = getEditableContent();
9797
if (isJsonString(jsonTodos)) {
98-
var a = document.createElement("a"),
99-
file = new Blob([jsonTodos], { type: "application/json" });
100-
var url = URL.createObjectURL(file);
101-
a.href = url;
102-
a.download = fileTodoList;
103-
document.body.appendChild(a);
104-
a.click();
105-
setTimeout(function () {
106-
document.body.removeChild(a);
107-
window.URL.revokeObjectURL(url);
108-
}, 0);
98+
Util.saveFile(fileTodoList, "application/json", jsonTodos);
10999
} else {
110100
document.getElementById("info").innerHTML = errorFormat();
111101
}

core

0 commit comments

Comments
 (0)