Merge pull request #319 from rigrig/files_data

App Manager: Add support for data files
master
Gordon Williams 2020-04-20 08:34:53 +01:00 committed by GitHub
commit 7164b0a8be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 23 deletions

View File

@ -319,7 +319,7 @@
{ "id": "files", { "id": "files",
"name": "App Manager", "name": "App Manager",
"icon": "files.png", "icon": "files.png",
"version":"0.02", "version":"0.03",
"description": "Show currently installed apps, free space, and allow their deletion from the watch", "description": "Show currently installed apps, free space, and allow their deletion from the watch",
"tags": "tool,system,files", "tags": "tool,system,files",
"storage": [ "storage": [

View File

@ -1 +1,2 @@
0.02: Fix deletion of apps - now use files list in app.info (fix #262) 0.02: Fix deletion of apps - now use files list in app.info (fix #262)
0.03: Add support for data files

View File

@ -30,29 +30,80 @@ function showMainMenu() {
return E.showMenu(mainmenu); return E.showMenu(mainmenu);
} }
function eraseApp(app) { function isGlob(f) {return /[?*]/.test(f)}
E.showMessage('Erasing\n' + app.name + '...'); function globToRegex(pattern) {
const ESCAPE = '.*+-?^${}()|[]\\';
const regex = pattern.replace(/./g, c => {
switch (c) {
case '?': return '.';
case '*': return '.*';
default: return ESCAPE.includes(c) ? ('\\' + c) : c;
}
});
return new RegExp('^'+regex+'$');
}
function eraseFiles(app) {
app.files.split(",").forEach(f=>storage.erase(f)); app.files.split(",").forEach(f=>storage.erase(f));
} }
function eraseData(app) {
if(!app.data) return;
const d=app.data.split(';'),
files=d[0].split(','),
sFiles=(d[1]||'').split(',');
let erase = f=>storage.erase(f);
files.forEach(f=>{
if (!isGlob(f)) erase(f);
else storage.list(globToRegex(f)).forEach(erase);
})
erase = sf=>storage.open(sf,'r').erase();
sFiles.forEach(sf=>{
if (!isGlob(sf)) erase(sf);
else storage.list(globToRegex(sf+'\u0001'))
.forEach(fs=>erase(fs.substring(0,fs.length-1)));
})
}
function eraseApp(app, files,data) {
E.showMessage('Erasing\n' + app.name + '...');
if (files) eraseFiles(app)
if (data) eraseData(app)
}
function eraseOne(app, files,data){
E.showPrompt('Erase\n'+app.name+'?').then((v) => {
if (v) {
Bangle.buzz(100, 1);
eraseApp(app, files,data)
showApps();
} else {
showAppMenu(app)
}
})
}
function eraseAll(apps, files,data) {
E.showPrompt('Erase all?').then((v) => {
if (v) {
Bangle.buzz(100, 1);
for(var n = 0; n<apps.length; n++)
eraseApp(apps[n], files,data);
}
showApps();
})
}
function showAppMenu(app) { function showAppMenu(app) {
const appmenu = { let appmenu = {
'': { '': {
'title': app.name, 'title': app.name,
}, },
'< Back': () => m = showApps(), '< Back': () => m = showApps(),
'Erase': () => { }
E.showPrompt('Erase\n' + app.name + '?').then((v) => { if (app.data) {
if (v) { appmenu['Erase Completely'] = () => eraseOne(app, true, true)
Bangle.buzz(100, 1); appmenu['Erase App,Keep Data'] = () => eraseOne(app,true, false)
eraseApp(app); appmenu['Only Erase Data'] = () => eraseOne(app,false, true)
m = showApps(); } else {
} else { appmenu['Erase'] = () => eraseOne(app,true, false)
m = showAppMenu(app) }
}
});
}
};
return E.showMenu(appmenu); return E.showMenu(appmenu);
} }
@ -78,13 +129,12 @@ function showApps() {
return menu; return menu;
}, appsmenu); }, appsmenu);
appsmenu['Erase All'] = () => { appsmenu['Erase All'] = () => {
E.showPrompt('Erase all?').then((v) => { E.showMenu({
if (v) { '': {'title': 'Erase All'},
Bangle.buzz(100, 1); 'Erase Everything': () => eraseAll(list, true, true),
for (var n = 0; n < list.length; n++) 'Erase Apps,Keep Data': () => eraseAll(list, true, false),
eraseApp(list[n]); 'Only Erase Data': () => eraseAll(list, false, true),
} '< Back': () => showApps(),
m = showApps();
}); });
}; };
} else { } else {