Update app.js

Refactoring
master
Andrew Gregory 2022-03-23 16:13:57 +08:00 committed by GitHub
parent 94207739c0
commit b26d341609
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 23 deletions

View File

@ -182,7 +182,7 @@ function updateProgressBar() {
function drawProgressBar() { function drawProgressBar() {
let id = state.id; let id = state.id;
if (id >= 0 && tokens[id].period > 0) { if (id >= 0 && tokens[id].period > 0) {
let rem = Math.floor((state.hotp.next - Date.now()) / 1000); let rem = Math.min(tokens[id].period, Math.floor((state.hotp.next - Date.now()) / 1000));
if (rem >= 0) { if (rem >= 0) {
let y1 = tokenY(id); let y1 = tokenY(id);
let y2 = y1 + TOKEN_HEIGHT - 1; let y2 = y1 + TOKEN_HEIGHT - 1;
@ -192,7 +192,6 @@ function drawProgressBar() {
{ {
// progress bar visible // progress bar visible
y2 = Math.min(y2, AR.y2); y2 = Math.min(y2, AR.y2);
rem = Math.min(rem, tokens[id].period);
let xr = Math.floor(AR.w * rem / tokens[id].period) + AR.x; let xr = Math.floor(AR.w * rem / tokens[id].period) + AR.x;
g.setColor(g.theme.fgH) g.setColor(g.theme.fgH)
.setBgColor(g.theme.bgH) .setBgColor(g.theme.bgH)
@ -209,13 +208,13 @@ function drawProgressBar() {
// id = token ID number (0...) // id = token ID number (0...)
function drawToken(id) { function drawToken(id) {
var x1 = AR.x; let x1 = AR.x;
var y1 = tokenY(id); let y1 = tokenY(id);
var x2 = AR.x2; let x2 = AR.x2;
var y2 = y1 + TOKEN_HEIGHT - 1; let y2 = y1 + TOKEN_HEIGHT - 1;
var adj, lbl; let lbl = (id >= 0 && id < tokens.length) ? tokens[id].label.substr(0, 10) : "";
let adj;
g.setClipRect(x1, Math.max(y1, AR.y), x2, Math.min(y2, AR.y2)); g.setClipRect(x1, Math.max(y1, AR.y), x2, Math.min(y2, AR.y2));
lbl = (id >= 0 && id < tokens.length) ? tokens[id].label.substr(0, 10) : "";
if (id === state.id) { if (id === state.id) {
g.setColor(g.theme.fgH) g.setColor(g.theme.fgH)
.setBgColor(g.theme.bgH); .setBgColor(g.theme.bgH);
@ -281,9 +280,9 @@ function changeId(id) {
function onDrag(e) { function onDrag(e) {
state.cnt = 1; state.cnt = 1;
if (e.b != 0 && e.dy != 0) { if (e.b != 0 && e.dy != 0) {
var y = E.clip(state.listy - E.clip(e.dy, -AR.h, AR.h), 0, Math.max(0, tokens.length * TOKEN_HEIGHT - AR.h)); let y = E.clip(state.listy - E.clip(e.dy, -AR.h, AR.h), 0, Math.max(0, tokens.length * TOKEN_HEIGHT - AR.h));
if (state.listy != y) { if (state.listy != y) {
var id, dy = state.listy - y; let id, dy = state.listy - y;
state.listy = y; state.listy = y;
g.setClipRect(AR.x, AR.y, AR.x2, AR.y2) g.setClipRect(AR.x, AR.y, AR.x2, AR.y2)
.scroll(0, dy); .scroll(0, dy);
@ -315,15 +314,15 @@ function onDrag(e) {
function onTouch(zone, e) { function onTouch(zone, e) {
state.cnt = 1; state.cnt = 1;
if (e) { if (e) {
var id = Math.floor((state.listy + e.y - AR.y) / TOKEN_HEIGHT); let id = Math.floor((state.listy + e.y - AR.y) / TOKEN_HEIGHT);
if (id == state.id || tokens.length == 0 || id >= tokens.length) { if (id == state.id || tokens.length == 0 || id >= tokens.length) {
id = -1; id = -1;
} }
if (state.id != id) { if (state.id != id) {
if (id >= 0) { if (id >= 0) {
// scroll token into view if necessary // scroll token into view if necessary
var dy = 0; let dy = 0;
var y = id * TOKEN_HEIGHT - state.listy; let y = id * TOKEN_HEIGHT - state.listy;
if (y < 0) { if (y < 0) {
dy -= y; dy -= y;
y = 0; y = 0;
@ -357,17 +356,16 @@ function onSwipe(e) {
timerCalc(); timerCalc();
} }
function bangle1Btn(e) { function bangleBtn(e) {
state.cnt = 1; state.cnt = 1;
if (tokens.length > 0) { if (tokens.length > 0) {
var id = state.id; let id = state.id;
switch (e) { switch (e) {
case -1: id--; break; case -1: id--; break;
case 1: id++; break; case 1: id++; break;
} }
id = E.clip(id, 0, tokens.length - 1); id = E.clip(id, 0, tokens.length - 1);
var dy = state.listy - E.clip(id * TOKEN_HEIGHT - half(AR.h - TOKEN_HEIGHT), 0, Math.max(0, tokens.length * TOKEN_HEIGHT - AR.h)); onDrag({b:1, dy:state.listy - E.clip(id * TOKEN_HEIGHT - half(AR.h - TOKEN_HEIGHT), 0, Math.max(0, tokens.length * TOKEN_HEIGHT - AR.h))});
onDrag({b:1, dy:dy});
changeId(id); changeId(id);
drawProgressBar(); drawProgressBar();
} }
@ -384,12 +382,14 @@ function exitApp() {
Bangle.on('touch', onTouch); Bangle.on('touch', onTouch);
Bangle.on('drag' , onDrag ); Bangle.on('drag' , onDrag );
Bangle.on('swipe', onSwipe); Bangle.on('swipe', onSwipe);
if (typeof BTN2 == 'number') { if (typeof BTN1 == 'number') {
setWatch(function(){bangle1Btn(-1);}, BTN1, {edge:"rising" , debounce:50, repeat:true}); if (typeof BTN2 == 'number' && typeof BTN3 == 'number') {
setWatch(function(){exitApp(); }, BTN2, {edge:"falling", debounce:50}); setWatch(()=>bangleBtn(-1), BTN1, {edge:"rising" , debounce:50, repeat:true});
setWatch(function(){bangle1Btn( 1);}, BTN3, {edge:"rising" , debounce:50, repeat:true}); setWatch(()=>exitApp() , BTN2, {edge:"falling", debounce:50});
} else { setWatch(()=>bangleBtn( 1), BTN3, {edge:"rising" , debounce:50, repeat:true});
setWatch(function(){exitApp(); }, BTN1, {edge:"falling", debounce:50}); } else {
setWatch(()=>exitApp() , BTN1, {edge:"falling", debounce:50});
}
} }
Bangle.loadWidgets(); Bangle.loadWidgets();
const AR = Bangle.appRect; const AR = Bangle.appRect;