Merge pull request #2524 from Kedlub/backswipe

[backswipe] New app
master
Gordon Williams 2023-01-23 09:05:30 +00:00 committed by GitHub
commit dbc72a12f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 172 additions and 0 deletions

1
apps/backswipe/ChangeLog Normal file
View File

@ -0,0 +1 @@
0.01: New App!

BIN
apps/backswipe/app.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 764 B

50
apps/backswipe/boot.js Normal file
View File

@ -0,0 +1,50 @@
(function () {
var DEFAULTS = {
mode: 0,
apps: [],
};
var settings = require("Storage").readJSON("backswipe.json", 1) || DEFAULTS;
// Overrride the default setUI method, so we can save the back button callback
var setUI = Bangle.setUI;
Bangle.setUI = function (mode, cb) {
var options = {};
if ("object"==typeof mode) {
options = mode;
}
var currentFile = global.__FILE__ || "";
if(global.BACK) delete global.BACK;
if (options && options.back && enabledForApp(currentFile)) {
global.BACK = options.back;
}
setUI(mode, cb);
};
function goBack(lr, ud) {
// if it is a left to right swipe
if (lr === 1) {
// if we're in an app that has a back button, run the callback for it
if (global.BACK) {
global.BACK();
}
}
}
// Check if the back button should be enabled for the current app
// app is the src file of the app
function enabledForApp(app) {
if (!settings) return true;
if (settings.mode === 0) {
return !(settings.apps.filter((a) => a.src === app).length > 0);
} else if (settings.mode === 1) {
return settings.apps.filter((a) => a.src === app).length > 0;
} else {
return settings.mode === 2 ? true : false;
}
}
// Listen to left to right swipe
Bangle.on("swipe", goBack);
})();

View File

@ -0,0 +1,17 @@
{ "id": "backswipe",
"name": "Back Swipe",
"shortName":"BackSwipe",
"version":"0.01",
"description": "Service that allows you to use an app's back button using left to right swipe gesture",
"icon": "app.png",
"tags": "back,gesture,swipe",
"supports" : ["BANGLEJS2"],
"type": "bootloader",
"storage": [
{"name":"backswipe.boot.js","url":"boot.js"},
{"name":"backswipe.settings.js","url":"settings.js"}
],
"data": [
{"name":"backswipe.json"}
]
}

104
apps/backswipe/settings.js Normal file
View File

@ -0,0 +1,104 @@
(function(back) {
var FILE = 'backswipe.json';
// Mode can be 'blacklist', 'whitelist', 'on' or 'disabled'
// Apps is an array of app info objects, where all the apps that are there are either blocked or allowed, depending on the mode
var DEFAULTS = {
'mode': 0,
'apps': []
};
var settings = {};
var loadSettings = function() {
settings = require('Storage').readJSON(FILE, 1) || DEFAULTS;
}
var saveSettings = function(settings) {
require('Storage').write(FILE, settings);
}
// Get all app info files
var getApps = function() {
var apps = require('Storage').list(/\.info$/).map(appInfoFileName => {
var appInfo = require('Storage').readJSON(appInfoFileName, 1);
return appInfo && {
'name': appInfo.name,
'sortorder': appInfo.sortorder,
'src': appInfo.src
};
}).filter(app => app && !!app.src);
apps.sort((a, b) => {
var n = (0 | a.sortorder) - (0 | b.sortorder);
if (n) return n; // do sortorder first
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
});
return apps;
}
var showMenu = function() {
var menu = {
'': { 'title': 'Backswipe' },
'< Back': () => {
back();
},
'Mode': {
value: settings.mode,
min: 0,
max: 3,
format: v => ["Blacklist", "Whitelist", "Always On", "Disabled"][v],
onchange: v => {
settings.mode = v;
saveSettings(settings);
},
},
'App List': () => {
showAppSubMenu();
}
};
E.showMenu(menu);
}
var showAppSubMenu = function() {
var menu = {
'': { 'title': 'Backswipe' },
'< Back': () => {
showMenu();
},
'Add App': () => {
showAppList();
}
};
settings.apps.forEach(app => {
menu[app.name] = () => {
settings.apps.splice(settings.apps.indexOf(app), 1);
saveSettings(settings);
showAppSubMenu();
}
});
E.showMenu(menu);
}
var showAppList = function() {
var apps = getApps();
var menu = {
'': { 'title': 'Backswipe' },
'< Back': () => {
showMenu();
}
};
apps.forEach(app => {
menu[app.name] = () => {
settings.apps.push(app);
saveSettings(settings);
showAppSubMenu();
}
});
E.showMenu(menu);
}
loadSettings();
showMenu();
})