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 `
}); } -function getInstalledApps() { +let haveInstalledApps = false; +function getInstalledApps(refresh) { + if (haveInstalledApps && !refresh) { + return Promise.resolve(appsInstalled) + } showLoadingIndicator("myappscontainer"); // Get apps and files return Comms.getInstalledApps() .then(appJSON => { appsInstalled = appJSON; + haveInstalledApps = true; refreshMyApps(); refreshLibrary(); }) .then(() => handleConnectionChange(true)) + .then(() => appsInstalled) .catch(err=>{ return Promise.reject(); }); @@ -392,7 +420,7 @@ function handleConnectionChange(connected) { } htmlToArray(document.querySelectorAll(".btn.refresh")).map(button => button.addEventListener("click", () => { - getInstalledApps().catch(err => { + getInstalledApps(true).catch(err => { showToast("Getting app list failed, "+err,"error"); }); })); @@ -400,7 +428,7 @@ connectMyDeviceBtn.addEventListener("click", () => { if (connectMyDeviceBtn.classList.contains('is-connected')) { Comms.disconnectDevice(); } else { - getInstalledApps().catch(err => { + getInstalledApps(true).catch(err => { showToast("Device connection failed, "+err,"error"); }); } @@ -440,7 +468,7 @@ document.getElementById("removeall").addEventListener("click",event=>{ Progress.hide({sticky:true}); appsInstalled = []; showToast("All apps removed","success"); - return getInstalledApps(); + return getInstalledApps(true); }).catch(err=>{ Progress.hide({sticky:true}); showToast("App removal failed, "+err,"error"); @@ -483,7 +511,7 @@ document.getElementById("installdefault").addEventListener("click",event=>{ return Comms.setTime(); }).then(()=>{ showToast("Default apps successfully installed!","success"); - return getInstalledApps(); + return getInstalledApps(true); }).catch(err=>{ Progress.hide({sticky:true}); showToast("App Install failed, "+err,"error");