diff --git a/backup.js b/backup.js index cc4c327bc..a96377a08 100644 --- a/backup.js +++ b/backup.js @@ -7,7 +7,7 @@ function bangleDownload() { Progress.show({title:"Scanning...",sticky:true}); var normalFiles, storageFiles; console.log("Listing normal files..."); - Comms.reset() + return Comms.reset() .then(() => Comms.showMessage("Backing up...")) .then(() => Comms.listFiles({sf:false})) .then(f => { @@ -66,70 +66,74 @@ function bangleDownload() { } function bangleUpload() { - Espruino.Core.Utils.fileOpenDialog({ - id:"backup", - type:"arraybuffer", - mimeType:".zip,application/zip"}, function(data) { - if (data===undefined) return; - var promise = Promise.resolve(); - var zip = new JSZip(); - var cmds = ""; - zip.loadAsync(data).then(function(zip) { - return showPrompt("Restore from ZIP","Are you sure? This will overwrite existing apps"); - }).then(()=>{ - Progress.show({title:`Reading ZIP`}); - zip.forEach(function (path, file){ - console.log("path"); - promise = promise - .then(() => file.async("string")) - .then(data => { - console.log("decoded", path); - if (data.length==0) { // https://github.com/espruino/BangleApps/issues/1593 - console.log("Can't restore files of length 0, ignoring "+path); - } else if (path.startsWith(BACKUP_STORAGEFILE_DIR)) { - path = path.substr(BACKUP_STORAGEFILE_DIR.length+1); - cmds += AppInfo.getStorageFileUploadCommands(path, data)+"\n"; - } else if (!path.includes("/")) { - cmds += AppInfo.getFileUploadCommands(path, data)+"\n"; - } else console.log("Ignoring "+path); + return new Promise(resolve => + Espruino.Core.Utils.fileOpenDialog({ + id:"backup", + type:"arraybuffer", + mimeType:".zip,application/zip"}, function(data) { + if (data===undefined) return; + var promise = Promise.resolve(); + var zip = new JSZip(); + var cmds = ""; + zip.loadAsync(data).then(function(zip) { + return showPrompt("Restore from ZIP","Are you sure? This will overwrite existing apps"); + }).then(()=>{ + Progress.show({title:`Reading ZIP`}); + zip.forEach(function (path, file){ + console.log("path"); + promise = promise + .then(() => file.async("string")) + .then(data => { + console.log("decoded", path); + if (data.length==0) { // https://github.com/espruino/BangleApps/issues/1593 + console.log("Can't restore files of length 0, ignoring "+path); + } else if (path.startsWith(BACKUP_STORAGEFILE_DIR)) { + path = path.substr(BACKUP_STORAGEFILE_DIR.length+1); + cmds += AppInfo.getStorageFileUploadCommands(path, data)+"\n"; + } else if (!path.includes("/")) { + cmds += AppInfo.getFileUploadCommands(path, data)+"\n"; + } else console.log("Ignoring "+path); + }); }); + return promise; + }) + .then(()=>new Promise(resolve => { + showPrompt("Erase Storage","Erase Storage? If restoring a complete backup you should erase storage, but in some cases you may want to upload files from a ZIP while keeping your Bangle's existing data.").then(()=>resolve(true), ()=>resolve(false)); + })) + .then(eraseStorage => { + if (eraseStorage) { + Progress.hide({sticky:true}); + Progress.show({title:`Erasing...`}); + return Comms.removeAllApps(); + }}) + .then(() => { + Progress.hide({sticky:true}); + Progress.show({title:`Restoring...`, sticky:true}); + return Comms.showMessage(`Restoring...`); }) + .then(() => Comms.write("\x10"+Comms.getProgressCmd()+"\n")) + .then(() => Comms.uploadCommandList(cmds, 0, cmds.length)) + .then(() => getInstalledApps(true)) + .then(() => Comms.showMessage(Const.MESSAGE_RELOAD)) + .then(() => { + Progress.hide({sticky:true}); + showToast('Restore complete!', 'success'); + resolve(); + }) + .catch(err => { + Progress.hide({sticky:true}); + showToast('Restore failed, ' + err, 'error'); + resolve(); }); return promise; - }) - .then(()=>new Promise(resolve => { - showPrompt("Erase Storage","Erase Storage? If restoring a complete backup you should erase storage, but in some cases you may want to upload files from a ZIP while keeping your Bangle's existing data.").then(()=>resolve(true), ()=>resolve(false)); - })) - .then(eraseStorage => { - if (eraseStorage) { - Progress.hide({sticky:true}); - Progress.show({title:`Erasing...`}); - return Comms.removeAllApps(); - }}) - .then(() => { - Progress.hide({sticky:true}); - Progress.show({title:`Restoring...`, sticky:true}); - return Comms.showMessage(`Restoring...`); }) - .then(() => Comms.write("\x10"+Comms.getProgressCmd()+"\n")) - .then(() => Comms.uploadCommandList(cmds, 0, cmds.length)) - .then(() => getInstalledApps(true)) - .then(() => Comms.showMessage(Const.MESSAGE_RELOAD)) - .then(() => { - Progress.hide({sticky:true}); - showToast('Restore complete!', 'success'); - }) - .catch(err => { - Progress.hide({sticky:true}); - showToast('Restore failed, ' + err, 'error'); }); - return promise; - }); + } } window.addEventListener('load', (event) => { document.getElementById("downloadallapps").addEventListener("click",event=>{ - bangleDownload(); + startOperation({name:"Backup Apps"}, () => bangleDownload); }); document.getElementById("uploadallapps").addEventListener("click",event=>{ - bangleUpload(); + startOperation({name:"Restore Apps"}, () => bangleUpload); }); }); diff --git a/core b/core index ef99424a9..7e7475ba3 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit ef99424a9fbd01be504841a6c759ba4292a542f7 +Subproject commit 7e7475ba3ab253099481a81e487aaacb9384f974 diff --git a/loader.js b/loader.js index cb25107df..94f2c0b2e 100644 --- a/loader.js +++ b/loader.js @@ -212,7 +212,7 @@ window.addEventListener('load', (event) => { el = document.getElementById("reinstallall"); if (el) el.addEventListener("click",event=>{ var promise = showPrompt("Reinstall","Really re-install all apps?").then(() => { - Comms.reset().then(_ => + startOperation({name:"Reinstall All Apps"}, () => Comms.reset().then(_ => getInstalledApps() ).then(installedapps => { console.log(installedapps); @@ -232,14 +232,14 @@ window.addEventListener('load', (event) => { ).catch(err=>{ Progress.hide({sticky:true}); showToast("App re-install failed, "+err,"error"); - }); + })); }); }); // Button to install all default apps in one go el = document.getElementById("installdefault"); if (el) el.addEventListener("click", event=>{ - getInstalledApps().then(() => { + startOperation({name:"Install Default Apps"}, () => getInstalledApps().then(() => { if (device.id == "BANGLEJS") return httpGet("defaultapps_banglejs1.json"); if (device.id == "BANGLEJS2") @@ -250,15 +250,16 @@ window.addEventListener('load', (event) => { }).catch(err=>{ Progress.hide({sticky:true}); showToast("App Install failed, "+err,"error"); - }); + })); }); // Button to reset the Bangle's settings el = document.getElementById("defaultbanglesettings"); if (el) el.addEventListener("click", event=>{ showPrompt("Reset Settings","Really reset Bangle.js settings?").then(() => { - Comms.write("\x10require('Storage').erase('setting.json');load()\n"); - showToast("Settings reset!", "success"); + startOperation({name:"Reset Settings"}, () => + Comms.write("\x10require('Storage').erase('setting.json');load()\n").then(() => + showToast("Settings reset!", "success"))); }, function() { /* cancelled */ }); });