2023-05-16 03:46:44 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import Icon from '/imports/ui/components/common/icon/component';
|
|
|
|
import TimerService from '/imports/ui/components/timer/service';
|
|
|
|
import Styled from './styles';
|
|
|
|
|
|
|
|
const CDN = Meteor.settings.public.app.cdn;
|
|
|
|
const BASENAME = Meteor.settings.public.app.basename;
|
|
|
|
const HOST = CDN + BASENAME;
|
2022-02-02 03:05:02 +08:00
|
|
|
const trackName = Meteor.settings.public.timer.music;
|
2023-05-16 03:46:44 +08:00
|
|
|
|
|
|
|
class Indicator extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.timeRef = React.createRef();
|
|
|
|
this.interval = null;
|
|
|
|
|
|
|
|
this.alarm = null;
|
2021-09-03 20:44:16 +08:00
|
|
|
this.music = null;
|
|
|
|
|
2023-05-16 03:46:44 +08:00
|
|
|
// We need to avoid trigger on mount
|
|
|
|
this.triggered = true;
|
|
|
|
|
2021-09-18 04:38:22 +08:00
|
|
|
this.alreadyNotified = false;
|
|
|
|
|
2023-05-16 03:46:44 +08:00
|
|
|
this.updateTime = this.updateTime.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { timer } = this.props;
|
|
|
|
const { running } = timer;
|
|
|
|
|
|
|
|
this.alarm = new Audio(`${HOST}/resources/sounds/alarm.mp3`);
|
2021-09-03 20:44:16 +08:00
|
|
|
this.setUpMusic();
|
|
|
|
|
2023-05-16 03:46:44 +08:00
|
|
|
this.triggered = this.initTriggered();
|
|
|
|
|
|
|
|
const { current } = this.timeRef;
|
|
|
|
if (current && running) {
|
|
|
|
this.interval = setInterval(this.updateTime, TimerService.getInterval());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const { timer } = this.props;
|
|
|
|
const { timer: prevTimer } = prevProps;
|
|
|
|
|
2021-09-14 02:16:07 +08:00
|
|
|
if (this.shouldPlayMusic()) {
|
|
|
|
this.playMusic();
|
|
|
|
}
|
|
|
|
|
2023-05-16 03:46:44 +08:00
|
|
|
this.updateInterval(prevTimer, timer);
|
|
|
|
this.updateAlarmTrigger(prevTimer, timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
clearInterval(this.interval);
|
2022-03-03 02:38:27 +08:00
|
|
|
this.stopMusic();
|
2023-05-16 03:46:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
updateInterval(prevTimer, timer) {
|
|
|
|
const { running } = timer;
|
|
|
|
const { running: prevRunning } = prevTimer;
|
|
|
|
|
|
|
|
if (!prevRunning && running) {
|
|
|
|
this.interval = setInterval(this.updateTime, TimerService.getInterval());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prevRunning && !running) {
|
|
|
|
clearInterval(this.interval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 20:44:16 +08:00
|
|
|
setUpMusic() {
|
2022-02-02 03:05:02 +08:00
|
|
|
const { currentTrack } = this.props;
|
2022-03-04 01:12:53 +08:00
|
|
|
if (trackName[currentTrack] === undefined) return;
|
|
|
|
if (this.music === null) {
|
2022-02-02 03:05:02 +08:00
|
|
|
this.music = new Audio(`${HOST}/resources/sounds/${trackName[currentTrack]}.mp3`);
|
|
|
|
this.music.volume = TimerService.getMusicVolume();
|
|
|
|
this.music.addEventListener('timeupdate', () => {
|
|
|
|
const buffer = 0.19;
|
|
|
|
// Start playing the music before it ends to make the loop gapless
|
|
|
|
if (this.music.currentTime > this.music.duration - buffer) {
|
|
|
|
this.music.currentTime = 0;
|
|
|
|
this.music.play();
|
|
|
|
}
|
|
|
|
});
|
2022-03-04 01:12:53 +08:00
|
|
|
} else {
|
|
|
|
this.music.src = `${HOST}/resources/sounds/${trackName[currentTrack]}.mp3`;
|
2022-02-02 03:05:02 +08:00
|
|
|
}
|
2022-03-04 01:12:53 +08:00
|
|
|
this.music.track = currentTrack;
|
2021-09-03 20:44:16 +08:00
|
|
|
}
|
|
|
|
|
2023-05-16 03:46:44 +08:00
|
|
|
updateAlarmTrigger(prevTimer, timer) {
|
|
|
|
const {
|
|
|
|
accumulated,
|
|
|
|
timestamp,
|
|
|
|
} = timer;
|
|
|
|
|
|
|
|
const { timestamp: prevTimestamp } = prevTimer;
|
|
|
|
|
|
|
|
const reseted = timestamp !== prevTimestamp && accumulated === 0;
|
|
|
|
|
|
|
|
if (reseted) {
|
|
|
|
this.triggered = false;
|
2021-09-18 04:38:22 +08:00
|
|
|
this.alreadyNotified = false;
|
2023-05-16 03:46:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
initTriggered() {
|
|
|
|
const {
|
|
|
|
timer,
|
|
|
|
timeOffset,
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const {
|
|
|
|
stopwatch,
|
|
|
|
running,
|
|
|
|
} = timer;
|
|
|
|
|
|
|
|
if (stopwatch || !running) return false;
|
|
|
|
|
|
|
|
const {
|
|
|
|
time,
|
|
|
|
accumulated,
|
|
|
|
timestamp,
|
|
|
|
} = timer;
|
|
|
|
|
|
|
|
const elapsedTime = TimerService.getElapsedTime(running, timestamp, timeOffset, accumulated);
|
|
|
|
const updatedTime = Math.max(time - elapsedTime, 0);
|
|
|
|
|
|
|
|
if (updatedTime === 0) return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
play() {
|
|
|
|
if (this.alarm && !this.triggered) {
|
|
|
|
this.triggered = true;
|
|
|
|
this.alarm.play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
soundAlarm(time) {
|
|
|
|
const { timer } = this.props;
|
|
|
|
const {
|
|
|
|
running,
|
|
|
|
stopwatch,
|
|
|
|
} = timer;
|
|
|
|
|
|
|
|
const enabled = TimerService.isAlarmEnabled();
|
|
|
|
const zero = time === 0;
|
|
|
|
|
|
|
|
return enabled && running && zero && !stopwatch;
|
|
|
|
}
|
|
|
|
|
2021-09-03 20:44:16 +08:00
|
|
|
playMusic() {
|
2021-09-14 02:16:07 +08:00
|
|
|
const handleUserInteraction = () => {
|
2021-09-03 20:44:16 +08:00
|
|
|
this.music.play();
|
2021-09-14 02:16:07 +08:00
|
|
|
window.removeEventListener('click', handleUserInteraction);
|
|
|
|
window.removeEventListener('auxclick', handleUserInteraction);
|
|
|
|
window.removeEventListener('keydown', handleUserInteraction);
|
|
|
|
window.removeEventListener('touchstart', handleUserInteraction);
|
|
|
|
};
|
|
|
|
|
|
|
|
const playMusicAfterUserInteraction = () => {
|
|
|
|
window.addEventListener('click', handleUserInteraction);
|
|
|
|
window.addEventListener('auxclick', handleUserInteraction);
|
|
|
|
window.addEventListener('keydown', handleUserInteraction);
|
|
|
|
window.addEventListener('touchstart', handleUserInteraction);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.handleUserInteraction = handleUserInteraction;
|
|
|
|
|
|
|
|
if (this.music !== null) {
|
|
|
|
this.music.play()
|
|
|
|
.catch((error) => {
|
|
|
|
if (error.name === 'NotAllowedError') {
|
|
|
|
playMusicAfterUserInteraction();
|
|
|
|
}
|
|
|
|
});
|
2021-09-03 20:44:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stopMusic() {
|
|
|
|
if (this.music !== null) {
|
|
|
|
this.music.pause();
|
|
|
|
this.music.currentTime = 0;
|
2021-09-14 02:16:07 +08:00
|
|
|
window.removeEventListener('click', this.handleUserInteraction);
|
|
|
|
window.removeEventListener('auxclick', this.handleUserInteraction);
|
|
|
|
window.removeEventListener('keydown', this.handleUserInteraction);
|
|
|
|
window.removeEventListener('touchstart', this.handleUserInteraction);
|
2021-09-03 20:44:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 02:16:07 +08:00
|
|
|
shouldPlayMusic() {
|
|
|
|
const {
|
|
|
|
timer,
|
|
|
|
isMusicActive,
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const {
|
|
|
|
running,
|
|
|
|
stopwatch,
|
|
|
|
} = timer;
|
2022-03-04 01:12:53 +08:00
|
|
|
const validMusic = this.music != null;
|
|
|
|
if (!validMusic) return false;
|
2021-09-14 02:16:07 +08:00
|
|
|
|
2022-03-04 01:12:53 +08:00
|
|
|
const musicIsPlaying = !this.music.paused;
|
|
|
|
|
|
|
|
return !musicIsPlaying && isMusicActive && running && !stopwatch;
|
2021-09-14 02:16:07 +08:00
|
|
|
}
|
|
|
|
|
2021-09-03 20:44:16 +08:00
|
|
|
shouldStopMusic(time) {
|
2022-02-02 03:05:02 +08:00
|
|
|
const {
|
|
|
|
timer,
|
2022-03-04 01:12:53 +08:00
|
|
|
isTimerActive,
|
2022-02-02 03:05:02 +08:00
|
|
|
isMusicActive,
|
2022-03-04 01:12:53 +08:00
|
|
|
currentTrack,
|
2022-02-02 03:05:02 +08:00
|
|
|
} = this.props;
|
|
|
|
|
2021-09-03 20:44:16 +08:00
|
|
|
const {
|
|
|
|
running,
|
|
|
|
stopwatch,
|
|
|
|
} = timer;
|
|
|
|
|
|
|
|
const zero = time === 0;
|
|
|
|
const validMusic = this.music != null;
|
2022-03-04 01:12:53 +08:00
|
|
|
const musicIsPlaying = validMusic && !this.music.paused;
|
|
|
|
const trackChanged = this.music?.track !== currentTrack;
|
2021-09-03 20:44:16 +08:00
|
|
|
|
|
|
|
const reachedZeroOrStopped = (running && zero) || (!running);
|
|
|
|
|
2022-03-04 01:12:53 +08:00
|
|
|
return musicIsPlaying
|
|
|
|
&& (!isMusicActive || stopwatch || reachedZeroOrStopped || !isTimerActive || trackChanged);
|
2021-09-03 20:44:16 +08:00
|
|
|
}
|
|
|
|
|
2021-09-18 04:38:22 +08:00
|
|
|
shoulNotifyTimerEnded(time) {
|
|
|
|
const { timer } = this.props;
|
|
|
|
const {
|
|
|
|
running,
|
|
|
|
stopwatch,
|
|
|
|
} = timer;
|
|
|
|
|
|
|
|
if (stopwatch || !running) return false;
|
|
|
|
|
|
|
|
const reachedZero = time === 0;
|
|
|
|
|
|
|
|
if (reachedZero && !this.alreadyNotified) {
|
|
|
|
this.alreadyNotified = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-16 03:46:44 +08:00
|
|
|
getTime() {
|
|
|
|
const {
|
|
|
|
timer,
|
|
|
|
timeOffset,
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const {
|
|
|
|
stopwatch,
|
|
|
|
running,
|
|
|
|
time,
|
|
|
|
accumulated,
|
|
|
|
timestamp,
|
|
|
|
} = timer;
|
|
|
|
|
|
|
|
const elapsedTime = TimerService.getElapsedTime(running, timestamp, timeOffset, accumulated);
|
|
|
|
|
|
|
|
let updatedTime;
|
|
|
|
if (stopwatch) {
|
|
|
|
updatedTime = elapsedTime;
|
|
|
|
} else {
|
|
|
|
updatedTime = Math.max(time - elapsedTime, 0);
|
|
|
|
}
|
|
|
|
|
2021-09-18 04:38:22 +08:00
|
|
|
if (this.shoulNotifyTimerEnded(updatedTime)) {
|
|
|
|
TimerService.timerEnded();
|
|
|
|
}
|
|
|
|
|
2021-09-03 20:44:16 +08:00
|
|
|
if (this.shouldStopMusic(updatedTime)) {
|
|
|
|
this.stopMusic();
|
|
|
|
}
|
|
|
|
|
2023-05-16 03:46:44 +08:00
|
|
|
if (this.soundAlarm(updatedTime)) {
|
|
|
|
this.play();
|
|
|
|
}
|
|
|
|
|
|
|
|
return TimerService.getTimeAsString(updatedTime, stopwatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTime() {
|
|
|
|
const { current } = this.timeRef;
|
|
|
|
if (current) {
|
|
|
|
current.textContent = this.getTime();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2022-03-04 01:12:53 +08:00
|
|
|
const { isTimerActive } = this.props;
|
|
|
|
const time = this.getTime();
|
|
|
|
if (!isTimerActive) return null;
|
2022-02-02 03:05:02 +08:00
|
|
|
|
2023-05-16 03:46:44 +08:00
|
|
|
const {
|
|
|
|
isModerator,
|
|
|
|
timer,
|
2022-02-04 00:49:21 +08:00
|
|
|
sidebarNavigationIsOpen,
|
|
|
|
sidebarContentIsOpen,
|
2022-03-04 01:12:53 +08:00
|
|
|
currentTrack,
|
2023-05-16 03:46:44 +08:00
|
|
|
} = this.props;
|
|
|
|
const { running } = timer;
|
2022-03-04 01:12:53 +08:00
|
|
|
const trackChanged = this.music?.track !== currentTrack;
|
|
|
|
if (trackChanged) {
|
|
|
|
this.setUpMusic();
|
|
|
|
}
|
2023-05-16 03:46:44 +08:00
|
|
|
|
|
|
|
const onClick = running ? TimerService.stopTimer : TimerService.startTimer;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Styled.TimerWrapper>
|
|
|
|
<Styled.Timer>
|
|
|
|
<Styled.TimerIndicator
|
|
|
|
role="button"
|
|
|
|
tabIndex={0}
|
|
|
|
running={running}
|
|
|
|
disabled={!isModerator}
|
2022-02-04 00:49:21 +08:00
|
|
|
hide={sidebarNavigationIsOpen && sidebarContentIsOpen}
|
2023-05-16 03:46:44 +08:00
|
|
|
onClick={isModerator ? onClick : null}
|
|
|
|
>
|
|
|
|
<Styled.TimerContent>
|
|
|
|
<Styled.TimerIcon>
|
|
|
|
<Icon iconName="time" />
|
|
|
|
</Styled.TimerIcon>
|
|
|
|
<Styled.TimerTime
|
|
|
|
aria-hidden
|
|
|
|
ref={this.timeRef}
|
|
|
|
>
|
2022-03-04 01:12:53 +08:00
|
|
|
{time}
|
2023-05-16 03:46:44 +08:00
|
|
|
</Styled.TimerTime>
|
|
|
|
</Styled.TimerContent>
|
|
|
|
</Styled.TimerIndicator>
|
|
|
|
</Styled.Timer>
|
|
|
|
</Styled.TimerWrapper>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Indicator;
|