1
0
Fork 0
BangleApps/apps/rep/app.ts

217 lines
4.6 KiB
TypeScript
Raw Normal View History

2023-04-29 11:30:23 +00:00
// TODO: buzz on stage change, better setTimeout()
2023-05-17 11:46:44 +00:00
// TODO: fastload: scoping, unregister layout handlers etc
2023-05-18 21:39:11 +00:00
// TODO: spaces vs tabs
// TODO: const colfg=g.theme.fg, colbg=g.theme.bg;
2023-04-29 11:30:23 +00:00
2023-05-17 11:46:44 +00:00
const Layout = require("Layout");
2023-05-18 21:39:11 +00:00
type Rep = {
dur: number,
label: string,
};
const reps: Rep[] = [
// 1 X 4 mins - 4 min recovery
// 2 X 2 mins - 2 min recoveries
// 4 X 1 min - 1 min recoveries (turn for home after the 2nd minute)
// 8 X 30 secs - 30 sec recoveries
2023-04-29 11:30:23 +00:00
// 3 min static recovery
// 1 X 4 mins to finish
2023-05-18 21:39:11 +00:00
{dur:1/60, label:"1st-sec"},
{dur:5/60, label:"5-sec"},
2023-04-29 11:30:23 +00:00
{dur:4, label:"jog"},
2023-05-18 21:39:11 +00:00
{dur:4, label:"recovery"},
2023-04-29 11:30:23 +00:00
{dur:2, label:"jog"},
2023-05-18 21:39:11 +00:00
{dur:2, label:"recovery"},
2023-04-29 11:30:23 +00:00
{dur:2, label:"jog"},
2023-05-18 21:39:11 +00:00
{dur:2, label:"recovery"},
2023-04-29 11:30:23 +00:00
{dur:1, label:"jog"},
2023-05-18 21:39:11 +00:00
{dur:1, label:"recovery"},
2023-04-29 11:30:23 +00:00
{dur:1, label:"jog"},
2023-05-18 21:39:11 +00:00
{dur:1, label:"recovery"},
2023-04-29 11:30:23 +00:00
{dur:1, label:"jog"},
2023-05-18 21:39:11 +00:00
{dur:1, label:"recovery"},
2023-04-29 11:30:23 +00:00
{dur:1, label:"jog"},
2023-05-18 21:39:11 +00:00
{dur:1, label:"recovery"},
2023-04-29 11:30:23 +00:00
2023-05-18 21:39:11 +00:00
{dur:0.5, label:"jog"}, {dur:0.5, label:"recovery"},
{dur:0.5, label:"jog"}, {dur:0.5, label:"recovery"},
{dur:0.5, label:"jog"}, {dur:0.5, label:"recovery"},
{dur:0.5, label:"jog"}, {dur:0.5, label:"recovery"},
{dur:0.5, label:"jog"}, {dur:0.5, label:"recovery"},
{dur:0.5, label:"jog"}, {dur:0.5, label:"recovery"},
{dur:0.5, label:"jog"}, {dur:0.5, label:"recovery"},
{dur:0.5, label:"jog"}, {dur:0.5, label:"recovery"},
2023-04-29 11:30:23 +00:00
{dur:3, label:"static recovery"},
{dur:4, label:"finish"},
];
2023-05-18 21:39:11 +00:00
const fontSzMain = 64;
const fontSzRep = 20;
2023-05-20 07:53:16 +00:00
// FIXME: `Layout_` name
2023-05-17 11:46:44 +00:00
const repLayout = (id: string): (Layout_.Hierarchy & {type:"txt"})[] => [
{
id: `${id}_name`,
type: "txt",
2023-05-18 21:39:11 +00:00
font: `Vector:${fontSzRep}`,
2023-05-17 11:46:44 +00:00
label: "Name PH",
2023-05-20 07:53:16 +00:00
//pad: 4,
fillx: 1,
2023-05-17 11:46:44 +00:00
}, {
id: `${id}_dur`,
type: "txt",
2023-05-18 21:39:11 +00:00
font: `Vector:${fontSzRep}`,
2023-05-17 11:46:44 +00:00
label: "DURATION",
2023-05-20 07:53:16 +00:00
halign: Layout_.Align.Right,
2023-05-17 11:46:44 +00:00
}
];
// top: show current rep: name & duration
// middle: show time left on current rep
// bottom: show next rep
// ctrl: play/pause button. back & forward?
const layout = new Layout({
type: "v",
c: [
{
id: "cur_time",
type: "txt",
2023-05-18 21:39:11 +00:00
font: `Vector:${fontSzMain}` as FontNameWithScaleFactor, // modified in draw
2023-05-17 11:46:44 +00:00
label: "MM:SS", // TODO: empty strings
fillx: 1,
filly: 1,
},
2023-05-18 21:39:11 +00:00
{
type: "h",
c: repLayout("cur"),
filly: 1,
},
2023-05-17 11:46:44 +00:00
{
type: "h",
c: repLayout("next"),
filly: 1,
},
{
type: "h",
c: [{
2023-05-18 21:39:11 +00:00
id: "play",
2023-05-17 11:46:44 +00:00
type: "btn",
label: "Play", // TODO: change
2023-05-18 21:39:11 +00:00
fillx: 1,
2023-05-17 11:46:44 +00:00
cb: () => {
console.log("cb()");
},
filly: 1,
}]
}
]
}, {lazy: true});
2023-04-29 11:30:23 +00:00
let index = 0;
2023-05-18 21:39:11 +00:00
let begin: number | undefined = Date.now(); // TODO: start null
2023-04-29 11:30:23 +00:00
let drawTimeout: number | undefined;
2023-05-18 21:39:11 +00:00
let paused = false;
2023-05-17 11:46:44 +00:00
let positioned = false;
2023-04-29 11:30:23 +00:00
const pad2 = (s: number) => ('0' + s.toFixed(0)).slice(-2);
2023-05-18 21:39:11 +00:00
const currentRepIndex = (elapsedMs: number) => {
let total = 0;
let ent;
for(let i = 0; ent = reps[i]; i++){
total += ent.dur * 60 * 1000;
if(elapsedMs < total)
return i;
}
return null;
};
const repToLabel = (i: number, id: string) => {
const rep = reps[i];
if(rep){
layout[`${id}_name`]!.label = `${i+1}: ${rep.label}`;
2023-05-20 07:53:25 +00:00
// FIXME: display time, i.e. hh:mm
layout[`${id}_dur`]!.label = `${rep.dur.toFixed(0)}m`;
2023-05-18 21:39:11 +00:00
}else{
emptyLabel(id);
}
};
const emptyLabel = (id: string) => {
layout[`${id}_name`]!.label = "<none>";
layout[`${id}_dur`]!.label = `0m`;
};
2023-05-17 11:46:44 +00:00
const drawRep = () => {
2023-05-18 21:39:11 +00:00
if(paused || !begin){ // TODO: separate case for paused
layout.clear();
layout["cur_time"]!.label = "Ready";
if(!positioned){
positioned = true;
layout.update(); // position
}
layout.render(layout["play"]);
layout.render(layout["cur_time"]);
return;
}
2023-05-17 11:46:44 +00:00
// TODO: layout.clear(layout.next_name); layout.render(layout.next_name)
2023-05-18 21:39:11 +00:00
const now = Date.now();
2023-05-17 11:46:44 +00:00
2023-05-18 21:39:11 +00:00
const elapsed = now - begin;
const sec = Math.round(elapsed / 1000);
const min = Math.round(sec / 60);
const timeStr = min.toFixed(0) + ":" + pad2(sec % 60);
layout["cur_time"]!.label = timeStr;
const i = currentRepIndex(elapsed);
if(i !== null){
repToLabel(i, "cur");
repToLabel(i+1, "next");
}else{
emptyLabel("cur");
emptyLabel("next");
}
2023-05-17 11:46:44 +00:00
if(!positioned){
positioned = true;
layout.update(); // position
}
layout.render();
2023-04-29 11:30:23 +00:00
2023-05-18 21:39:11 +00:00
/*
2023-04-29 11:30:23 +00:00
g.reset()
.clearRect(Bangle.appRect)
.setFontAlign(0, 0)
.setFont("Vector", 55)
.drawString(timeStr, x, y)
.setFont("Vector", 33)
.drawString(current ? current.label + " for " + current.dur : "<done>", x, y+48);
2023-05-18 21:39:11 +00:00
// TODO: figure out next rep change time? or every 5s, then countdown from 10-->0
2023-04-29 11:30:23 +00:00
if (drawTimeout) clearTimeout(drawTimeout);
drawTimeout = setTimeout(function() {
drawTimeout = undefined;
2023-05-17 11:46:44 +00:00
drawRep();
2023-04-29 11:30:23 +00:00
}, 1000 - (Date.now() % 1000));
2023-05-17 11:46:44 +00:00
*/
2023-04-29 11:30:23 +00:00
};
2023-05-17 11:46:44 +00:00
g.clear();
2023-05-18 21:39:11 +00:00
var drawInterval = setInterval(drawRep, 1000);
2023-05-17 11:46:44 +00:00
drawRep();
// TODO: widgets