2023-09-16 11:35:50 +00:00
g . clear ( ) ;
2022-11-02 19:06:17 +00:00
Bangle . POMOPLUS _ACTIVE = true ; //Prevent the boot code from running. To avoid having to reload on every interaction, we'll control the vibrations from here when the user is in the app.
const storage = require ( "Storage" ) ;
const common = require ( "pomoplus-com.js" ) ;
//Expire the state if necessary
if (
common . settings . pausedTimerExpireTime != 0 &&
! common . state . running &&
( new Date ( ) ) . getTime ( ) - common . state . pausedTime > common . settings . pausedTimerExpireTime
) {
common . state = common . STATE _DEFAULT ;
}
function drawButtons ( ) {
2023-09-16 11:35:50 +00:00
let w = g . getWidth ( ) ;
let h = g . getHeight ( ) ;
2022-11-02 19:06:17 +00:00
//Draw the backdrop
2023-09-16 11:35:50 +00:00
const BAR _TOP = h - 24 ;
2022-11-02 19:06:17 +00:00
g . setColor ( 0 , 0 , 1 ) . setFontAlign ( 0 , - 1 )
2023-09-16 11:35:50 +00:00
. clearRect ( 0 , BAR _TOP , w , h )
. fillRect ( 0 , BAR _TOP , w , h )
2022-11-02 19:06:17 +00:00
. setColor ( 1 , 1 , 1 ) ;
if ( ! common . state . wasRunning ) { //If the timer was never started, only show a play button
2023-09-16 11:35:50 +00:00
g . drawImage ( common . BUTTON _ICONS . play , w / 2 , BAR _TOP ) ;
2022-11-02 19:06:17 +00:00
} else {
2023-09-16 11:35:50 +00:00
g . drawLine ( w / 2 , BAR _TOP , w / 2 , h ) ;
2022-11-02 19:06:17 +00:00
if ( common . state . running ) {
2023-09-16 11:35:50 +00:00
g . drawImage ( common . BUTTON _ICONS . pause , w / 4 , BAR _TOP )
. drawImage ( common . BUTTON _ICONS . skip , w * 3 / 4 , BAR _TOP ) ;
2022-11-02 19:06:17 +00:00
} else {
2023-09-16 11:35:50 +00:00
g . drawImage ( common . BUTTON _ICONS . reset , w / 4 , BAR _TOP )
. drawImage ( common . BUTTON _ICONS . play , w * 3 / 4 , BAR _TOP ) ;
2022-11-02 19:06:17 +00:00
}
}
}
function drawTimerAndMessage ( ) {
2023-09-16 11:35:50 +00:00
let w = g . getWidth ( ) ;
let h = g . getHeight ( ) ;
2022-11-02 19:06:17 +00:00
g . reset ( )
. setFontAlign ( 0 , 0 )
. setFont ( "Vector" , 36 )
2023-09-16 11:35:50 +00:00
. clearRect ( w / 2 - 60 , h / 2 - 34 , w / 2 + 60 , h / 2 + 34 )
2022-11-02 19:06:17 +00:00
//Draw the timer
. drawString ( ( ( ) => {
let timeLeft = common . getTimeLeft ( ) ;
let hours = timeLeft / 3600000 ;
let minutes = ( timeLeft % 3600000 ) / 60000 ;
let seconds = ( timeLeft % 60000 ) / 1000 ;
function pad ( number ) {
return ( '00' + parseInt ( number ) ) . slice ( - 2 ) ;
}
if ( hours >= 1 ) return ` ${ parseInt ( hours ) } : ${ pad ( minutes ) } : ${ pad ( seconds ) } ` ;
else return ` ${ parseInt ( minutes ) } : ${ pad ( seconds ) } ` ;
2023-09-16 11:35:50 +00:00
} ) ( ) , w / 2 , h / 2 )
2022-11-02 19:06:17 +00:00
//Draw the phase label
. setFont ( "Vector" , 12 )
. drawString ( ( ( currentPhase , numShortBreaks ) => {
if ( ! common . state . wasRunning ) return "Not started" ;
else if ( currentPhase == common . PHASE _WORKING ) return ` Work ${ numShortBreaks + 1 } / ${ common . settings . numShortBreaks + 1 } `
else if ( currentPhase == common . PHASE _SHORT _BREAK ) return ` Short break ${ numShortBreaks + 1 } / ${ common . settings . numShortBreaks } ` ;
else return "Long break!" ;
} ) ( common . state . phase , common . state . numShortBreaks ) ,
2023-09-16 11:35:50 +00:00
w / 2 , h / 2 + 18 ) ;
2022-11-02 19:06:17 +00:00
//Update phase with vibation if needed
if ( common . getTimeLeft ( ) <= 0 ) {
common . nextPhase ( true ) ;
}
}
drawButtons ( ) ;
Bangle . on ( "touch" , ( button , xy ) => {
//If we support full touch and we're not touching the keys, ignore.
//If we don't support full touch, we can't tell so just assume we are.
if ( xy !== undefined && xy . y <= g . getHeight ( ) - 24 ) return ;
if ( ! common . state . wasRunning ) {
//If we were never running, there is only one button: the start button
let now = ( new Date ( ) ) . getTime ( ) ;
common . state = {
wasRunning : true ,
running : true ,
startTime : now ,
pausedTime : now ,
elapsedTime : 0 ,
phase : common . PHASE _WORKING ,
numShortBreaks : 0
} ;
setupTimerInterval ( ) ;
drawButtons ( ) ;
2023-09-16 11:35:50 +00:00
if ( common . settings . showClock ) Bangle . showClock ( ) ;
2022-11-02 19:06:17 +00:00
} else if ( common . state . running ) {
//If we are running, there are two buttons: pause and skip
if ( button == 1 ) {
//Record the exact moment that we paused
let now = ( new Date ( ) ) . getTime ( ) ;
common . state . pausedTime = now ;
//Stop the timer
common . state . running = false ;
clearInterval ( timerInterval ) ;
timerInterval = undefined ;
drawTimerAndMessage ( ) ;
drawButtons ( ) ;
} else {
common . nextPhase ( false ) ;
}
} else {
//If we are stopped, there are two buttons: Reset and continue
if ( button == 1 ) {
//Reset the timer
common . state = common . STATE _DEFAULT ;
drawTimerAndMessage ( ) ;
drawButtons ( ) ;
} else {
//Start the timer and record old elapsed time and when we started
let now = ( new Date ( ) ) . getTime ( ) ;
common . state . elapsedTime += common . state . pausedTime - common . state . startTime ;
common . state . startTime = now ;
common . state . running = true ;
drawTimerAndMessage ( ) ;
setupTimerInterval ( ) ;
drawButtons ( ) ;
2023-09-16 11:35:50 +00:00
if ( common . settings . showClock ) Bangle . showClock ( ) ;
2022-11-02 19:06:17 +00:00
}
}
} ) ;
let timerInterval ;
function setupTimerInterval ( ) {
if ( timerInterval !== undefined ) {
clearInterval ( timerInterval ) ;
}
setTimeout ( ( ) => {
timerInterval = setInterval ( drawTimerAndMessage , 1000 ) ;
drawTimerAndMessage ( ) ;
} , common . timeLeft % 1000 ) ;
}
drawTimerAndMessage ( ) ;
if ( common . state . running ) {
setupTimerInterval ( ) ;
}
//Save our state when the app is closed
E . on ( 'kill' , ( ) => {
storage . writeJSON ( common . STATE _PATH , common . state ) ;
} ) ;
Bangle . loadWidgets ( ) ;
2023-09-16 11:35:50 +00:00
Bangle . drawWidgets ( ) ;