61 lines
1.7 KiB
JavaScript
61 lines
1.7 KiB
JavaScript
import { useTimerStore } from '~/stores/timer'
|
|
|
|
export const useTimerControls = () => {
|
|
const timer = useTimerStore()
|
|
const interval = ref({})
|
|
|
|
const toggleTimer = () => {
|
|
timer.toggleTimer()
|
|
if (timer.isStarted) {
|
|
interval.value = setInterval(startTimer, 1000)
|
|
} else { clearInterval(interval.value) }
|
|
}
|
|
|
|
const startTimer = () => {
|
|
if (timer.getTimeRemaining <= 0) {
|
|
const currentSession = timer.settings.timer[timer.currentSession].text
|
|
|
|
timer.nextSession()
|
|
if (timer.playSessionEndSound) { playAlarm() }
|
|
const nextSession = timer.settings.timer[timer.currentSession].text
|
|
|
|
// if (timer.showNotification) { showNotification(currentSession, nextSession) }
|
|
|
|
if (timer.settings.autoStart) { return }
|
|
return toggleTimer()
|
|
}
|
|
timer.setTimeRemaining(timer.getTimeRemaining - 1)
|
|
}
|
|
|
|
const clearTimer = () => {
|
|
timer.started = false
|
|
timer.clearTimeRemaining()
|
|
clearInterval(interval.value)
|
|
}
|
|
|
|
// const showNotification = (currentSession, nextSession) => {
|
|
// const {
|
|
// isSupported,
|
|
// notification,
|
|
// show,
|
|
// onError
|
|
// } = useWebNotification({
|
|
// title: `${currentSession} is done.`,
|
|
// body: `You've just finished a ${String(currentSession).toLowerCase()} session. Now get ready to start the next session: ${nextSession}`,
|
|
// renotify: true,
|
|
// tag: currentSession
|
|
// })
|
|
// if (isSupported.value) { show() }
|
|
// }
|
|
|
|
const playAlarm = async () => {
|
|
const audio = await import(`../assets/sounds/${useTimerStore().alarmAudio}.mp3`)
|
|
const alarm = new Audio(audio.default)
|
|
alarm.play()
|
|
}
|
|
return {
|
|
toggleTimer,
|
|
clearTimer
|
|
}
|
|
}
|