mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-22 15:39:11 +00:00
playback + timer
timer not working yet
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
import { useEffect, useState } from 'react';
|
|
||||||
import styles from './Countdown.module.css';
|
import styles from './Countdown.module.css';
|
||||||
|
|
||||||
function display(seconds) {
|
function display(seconds) {
|
||||||
@@ -11,20 +10,9 @@ function display(seconds) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function Countdown({ time, small }) {
|
export default function Countdown({ time, small }) {
|
||||||
const [counter, setCounter] = useState(time);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
console.log('setting time here');
|
|
||||||
setCounter(time);
|
|
||||||
}, [time]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
counter > 0 && setTimeout(() => setCounter(counter - 1), 1000);
|
|
||||||
}, [counter]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={small ? styles.countdownClockSmall : styles.countdownClock}>
|
<div className={small ? styles.countdownClockSmall : styles.countdownClock}>
|
||||||
{display(counter)}
|
{time === null ? '- -:- -' : display(time * 60)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ export default function PlaybackControl(props) {
|
|||||||
colorScheme='green'
|
colorScheme='green'
|
||||||
className={style.start}
|
className={style.start}
|
||||||
disabled={roll}
|
disabled={roll}
|
||||||
onClick={() => props.playbackControl('start')}
|
onClick={() => props.playbackControl('play')}
|
||||||
>
|
>
|
||||||
Start
|
Start
|
||||||
</Button>
|
</Button>
|
||||||
@@ -67,6 +67,7 @@ export default function PlaybackControl(props) {
|
|||||||
colorScheme='blue'
|
colorScheme='blue'
|
||||||
className={style.reset}
|
className={style.reset}
|
||||||
onClick={() => props.playbackControl('roll')}
|
onClick={() => props.playbackControl('roll')}
|
||||||
|
disabled
|
||||||
>
|
>
|
||||||
Roll
|
Roll
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { IconButton } from '@chakra-ui/button';
|
|||||||
import { SettingsIcon } from '@chakra-ui/icons';
|
import { SettingsIcon } from '@chakra-ui/icons';
|
||||||
import { Grid, GridItem, Heading } from '@chakra-ui/layout';
|
import { Grid, GridItem, Heading } from '@chakra-ui/layout';
|
||||||
import { Box } from '@chakra-ui/layout';
|
import { Box } from '@chakra-ui/layout';
|
||||||
import { getHours, getMinutes } from 'date-fns';
|
import { addSeconds, getHours, getMinutes, getSeconds } from 'date-fns';
|
||||||
import { differenceInSeconds } from 'date-fns/esm';
|
import { differenceInSeconds } from 'date-fns/esm';
|
||||||
import { useContext } from 'react';
|
import { useContext } from 'react';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
@@ -28,10 +28,113 @@ export default function Editor() {
|
|||||||
prevState: 'pause',
|
prevState: 'pause',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Timer stuff
|
||||||
|
const [timer, setTimer] = useState({
|
||||||
|
TIMER_UPDATE_INTERVAL: 250,
|
||||||
|
currentTime: null,
|
||||||
|
currentTimeSeconds: null,
|
||||||
|
|
||||||
|
playMode: 'stop',
|
||||||
|
|
||||||
|
isStarted: false,
|
||||||
|
isRunning: false,
|
||||||
|
|
||||||
|
startTime: null,
|
||||||
|
pauseTime: null,
|
||||||
|
|
||||||
|
lastRun: null,
|
||||||
|
elapsedTime: 0,
|
||||||
|
|
||||||
|
elapsedStartedTime: 0,
|
||||||
|
elapsedRunningTime: 0,
|
||||||
|
|
||||||
|
totalElapsedPausedTime: 0,
|
||||||
|
periodElapsedPausedTime: 0,
|
||||||
|
|
||||||
|
elapsedResumeTime: 0,
|
||||||
|
|
||||||
|
targetTime: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
// update timer object
|
||||||
|
const updateTimer = (vals) => {
|
||||||
|
setTimer({ ...timer, ...vals });
|
||||||
|
};
|
||||||
|
|
||||||
|
// set timer target
|
||||||
|
const setTimerTargetinSeconds = (target) => {
|
||||||
|
const t = addSeconds(new Date(), target);
|
||||||
|
updateTimer({ currentTime: t, currentTimeSeconds: target, targetTime: t });
|
||||||
|
};
|
||||||
|
|
||||||
|
// timer playback control
|
||||||
|
const setTimerState = (state) => {
|
||||||
|
const now = new Date();
|
||||||
|
|
||||||
|
switch (state) {
|
||||||
|
case 'play': {
|
||||||
|
updateTimer({ playMode: state, isStarted: true, startTime: now, lastRun: now });
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'pause': {
|
||||||
|
updateTimer({ playMode: state, pauseTime: now, isRunning: false });
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// update
|
||||||
|
const updateTime = () => {
|
||||||
|
// exit if we are not ready
|
||||||
|
if (timer.startTime == null) return;
|
||||||
|
|
||||||
|
// aux
|
||||||
|
const now = new Date();
|
||||||
|
|
||||||
|
// how long has the time been running
|
||||||
|
const elapsedTime = now - timer.startTime;
|
||||||
|
|
||||||
|
if (timer.playMode === 'play') {
|
||||||
|
// current time here
|
||||||
|
const currentTime = timer.targetTime - elapsedTime;
|
||||||
|
updateTimer({
|
||||||
|
currentTime: currentTime,
|
||||||
|
currentTimeSeconds: getSeconds(currentTime),
|
||||||
|
elapsedTime: elapsedTime,
|
||||||
|
lastRun: now,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (timer.playMode === 'pause') {
|
||||||
|
const pausedTime = now - timer.pauseTime;
|
||||||
|
updateTimer({
|
||||||
|
totalElapsedPausedTime: timer.totalElapsedPausedTime + pausedTime,
|
||||||
|
periodElapsedPausedTime: timer.periodElapsedPausedTime + now,
|
||||||
|
elapsedTime: elapsedTime,
|
||||||
|
lastRun: now,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// call again
|
||||||
|
setTimeout(updateTime, timer.TIMER_UPDATE_INTERVAL);
|
||||||
|
};
|
||||||
|
|
||||||
|
// when playmode changes, we might schedule a timer
|
||||||
|
// is this enough for change or should i check prevstate?
|
||||||
|
useEffect(() => {
|
||||||
|
if (playback.state !== 'stop' || playback.state !== null) {
|
||||||
|
updateTime();
|
||||||
|
}
|
||||||
|
}, [playback.state]);
|
||||||
|
|
||||||
const updatePlayback = (vals) => {
|
const updatePlayback = (vals) => {
|
||||||
setPlayback({ ...playback, ...vals });
|
setPlayback({ ...playback, ...vals });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// gets timer on current
|
||||||
// ?? I have already done this a few times,
|
// ?? I have already done this a few times,
|
||||||
// maybe loop once and get all data?
|
// maybe loop once and get all data?
|
||||||
const getCurrentTime = (target) => {
|
const getCurrentTime = (target) => {
|
||||||
@@ -50,23 +153,24 @@ export default function Editor() {
|
|||||||
let minEnd = getHours(curEvent.timeEnd) * 60;
|
let minEnd = getHours(curEvent.timeEnd) * 60;
|
||||||
minEnd = minEnd + getMinutes(curEvent.timeEnd);
|
minEnd = minEnd + getMinutes(curEvent.timeEnd);
|
||||||
|
|
||||||
|
|
||||||
// return time in seconds
|
// return time in seconds
|
||||||
return ((minEnd - minStart) * 60);
|
return (minEnd - minStart) * 60;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const playbackControl = (action, payload) => {
|
const playbackControl = (action, payload) => {
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case 'start': {
|
case 'play': {
|
||||||
if (playback.state !== 'play') {
|
if (playback.state !== 'play') {
|
||||||
updatePlayback({ state: 'play', prevState: playback.state });
|
updatePlayback({ state: 'play', prevState: playback.state });
|
||||||
|
setTimerState('play');
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'pause': {
|
case 'pause': {
|
||||||
if (playback.state !== 'pause') {
|
if (playback.state !== 'pause') {
|
||||||
updatePlayback({ state: 'pause', prevState: playback.state });
|
updatePlayback({ state: 'pause', prevState: playback.state });
|
||||||
|
setTimerState('pause');
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -94,6 +198,8 @@ export default function Editor() {
|
|||||||
let time = getCurrentTime(cur);
|
let time = getCurrentTime(cur);
|
||||||
// update playback
|
// update playback
|
||||||
updatePlayback({ current: cur, next: nxt, currentTimer: time });
|
updatePlayback({ current: cur, next: nxt, currentTimer: time });
|
||||||
|
// set timer
|
||||||
|
setTimerTargetinSeconds(time);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -113,6 +219,8 @@ export default function Editor() {
|
|||||||
let time = getCurrentTime(cur);
|
let time = getCurrentTime(cur);
|
||||||
// update playback
|
// update playback
|
||||||
updatePlayback({ current: cur, next: nxt, currentTimer: time });
|
updatePlayback({ current: cur, next: nxt, currentTimer: time });
|
||||||
|
// set timer
|
||||||
|
setTimerTargetinSeconds(time);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -122,6 +230,7 @@ export default function Editor() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
console.log('playback here', playback);
|
console.log('playback here', playback);
|
||||||
|
console.log('timer here', timer);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Grid
|
<Grid
|
||||||
@@ -184,7 +293,7 @@ export default function Editor() {
|
|||||||
<PlaybackControl
|
<PlaybackControl
|
||||||
playback={playback}
|
playback={playback}
|
||||||
playbackControl={playbackControl}
|
playbackControl={playbackControl}
|
||||||
time={playback.currentTimer}
|
time={timer.currentTimeSeconds}
|
||||||
roll={playback.state === 'roll'}
|
roll={playback.state === 'roll'}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user