diff --git a/js/comms.js b/js/comms.js index 91ae54b68..eb453871d 100644 --- a/js/comms.js +++ b/js/comms.js @@ -8,7 +8,7 @@ reset : (opt) => new Promise((resolve,reject) => { setTimeout(resolve,500); }); }), -uploadApp : (app,skipReset) => { +uploadApp : (app,skipReset) => { // expects an apps.json structure (i.e. with `storage`) Progress.show({title:`Uploading ${app.name}`,sticky:true}); return AppInfo.getFiles(app, httpGet).then(fileContents => { return new Promise((resolve,reject) => { @@ -79,11 +79,11 @@ getInstalledApps : () => { }); }); }, -removeApp : app => { // expects an app structure +removeApp : app => { // expects an appid.info structure (i.e. with `files`) + if (app.files === '') return Promise.resolve(); // nothing to erase Progress.show({title:`Removing ${app.name}`,sticky:true}); - var storage = [{name:app.id+".info"}].concat(app.storage); - var cmds = storage.map(file=>{ - return `\x10require("Storage").erase(${toJS(file.name)});\n`; + var cmds = app.files.split(',').map(file=>{ + return `\x10require("Storage").erase(${toJS(file)});\n`; }).join(""); console.log("removeApp", cmds); return Comms.reset().then(new Promise((resolve,reject) => { diff --git a/js/index.js b/js/index.js index 60b66436a..68b033d1c 100644 --- a/js/index.js +++ b/js/index.js @@ -221,20 +221,7 @@ function refreshLibrary() { // upload icon.classList.remove("icon-upload"); icon.classList.add("loading"); - Comms.uploadApp(app).then((appJSON) => { - Progress.hide({sticky:true}); - if (appJSON) appsInstalled.push(appJSON); - showToast(app.name+" Uploaded!", "success"); - icon.classList.remove("loading"); - icon.classList.add("icon-delete"); - refreshMyApps(); - refreshLibrary(); - }).catch(err => { - Progress.hide({sticky:true}); - showToast("Upload failed, "+err, "error"); - icon.classList.remove("loading"); - icon.classList.add("icon-upload"); - }); + uploadApp(app) } else if (icon.classList.contains("icon-menu")) { // custom HTML update icon.classList.remove("icon-menu"); @@ -261,9 +248,35 @@ refreshFilter(); refreshLibrary(); // =========================================== My Apps +function uploadApp(app) { + return getInstalledApps().then(()=>{ + if (appsInstalled.some(i => i.id === app.id)) { + return updateApp(app) + } + Comms.uploadApp(app).then((appJSON) => { + Progress.hide({ sticky: true }) + if (appJSON) { + appsInstalled.push(appJSON) + } + showToast(app.name + ' Uploaded!', 'success') + }).catch(err => { + Progress.hide({ sticky: true }) + showToast('Upload failed, ' + err, 'error') + }).finally(()=>{ + refreshMyApps(); + refreshLibrary(); + }); + }).catch(err => { + showToast("Device connection failed, "+err,"error"); + }); +} + function removeApp(app) { return showPrompt("Delete","Really remove '"+app.name+"'?").then(() => { - return Comms.removeApp(app); + return getInstalledApps().then(()=>{ + // a = from appid.info, app = from apps.json + return Comms.removeApp(appsInstalled.find(a => a.id === app.id)) + }) }).then(()=>{ appsInstalled = appsInstalled.filter(a=>a.id!=app.id); showToast(app.name+" removed successfully","success"); @@ -289,8 +302,17 @@ function customApp(app) { function updateApp(app) { if (app.custom) return customApp(app); - return Comms.removeApp(app).then(()=>{ - showToast(app.name+" removed successfully. Updating...",); + return getInstalledApps().then(() => { + // a = from appid.info, app = from apps.json + let remove = appsInstalled.find(a => a.id === app.id) + // no need to remove files which will be overwritten anyway + remove.files = remove.files.split(',') + .filter(f => f !== app.id + '.info') + .filter(f => !app.storage.some(s => s.name === f)) + .join(',') + return Comms.removeApp(remove) + }).then(()=>{ + showToast(`Updating ${app.name}...`); appsInstalled = appsInstalled.filter(a=>a.id!=app.id); return Comms.uploadApp(app); }).then((appJSON) => { @@ -369,16 +391,22 @@ return `