Skip to content

Commit 2742c4f

Browse files
committed
Play sound after period
1 parent 1d8349c commit 2742c4f

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

src/components/PomodoroSoundsSelector.vue

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export default {
2929
import { ref } from 'vue';
3030
import { useI18n } from 'vue-i18n';
3131
import { PomodoroAudioParams, PomodoroSounds } from '../utils/pomodoro';
32-
import Browser from 'webextension-polyfill';
3332
import { injecStorage } from '../storage/inject-storage';
33+
import { playSound } from '../functions/playSound';
3434
3535
const props = defineProps<{
3636
option: PomodoroAudioParams;
@@ -42,10 +42,7 @@ const settingsStorage = injecStorage();
4242
const audioAfterPeriod = ref<PomodoroSounds>(props.value);
4343
4444
function playAudio() {
45-
const myAudio = new Audio(
46-
Browser.runtime.getURL(`assets/pomodoro-sounds/${audioAfterPeriod.value}`),
47-
);
48-
myAudio.play();
45+
playSound(audioAfterPeriod.value);
4946
}
5047
5148
async function onAudioChange(target: any) {

src/functions/playSound.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Browser from 'webextension-polyfill';
2+
import { PomodoroSounds } from '../utils/pomodoro';
3+
4+
export function playSound(sound: PomodoroSounds) {
5+
const myAudio = new Audio(Browser.runtime.getURL(`assets/pomodoro-sounds/${sound}`));
6+
myAudio.play();
7+
}

src/functions/pomodoro.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ import { useBadge, BadgeIcon, BadgeColor } from './useBadge';
55
import { Settings } from './settings';
66
import Browser from 'webextension-polyfill';
77
import { logger } from '../utils/logger';
8+
import { playSound } from './playSound';
89

910
export async function checkPomodoro() {
10-
function isTargetPeriod() {
11+
type PomodoroPeriod = {
12+
isTargetPeriod: boolean;
13+
isTargetPeriodFinishedNow: boolean;
14+
};
15+
16+
function isTargetPeriod(): PomodoroPeriod {
1117
for (let index = 1; index <= frequency; index++) {
1218
const plusWorkingTime = workTime * (index - 1);
1319
const plusRestTime = restTime * (index - 1);
@@ -20,9 +26,23 @@ export async function checkPomodoro() {
2026
now.getTime() >= isPomodoroTargetPeriodStart.getTime() &&
2127
now.getTime() <= isPomodoroTargetPeriodEnd.getTime();
2228

23-
if (isTargetPeriod) return true;
29+
if (isTargetPeriod)
30+
return {
31+
isTargetPeriod: true,
32+
isTargetPeriodFinishedNow: now.getTime() == isPomodoroTargetPeriodEnd.getTime(),
33+
};
34+
}
35+
return {
36+
isTargetPeriod: false,
37+
isTargetPeriodFinishedNow: false,
38+
};
39+
}
40+
41+
async function play(param: StorageParams) {
42+
if (target.isTargetPeriodFinishedNow) {
43+
const sound = await storage.getValue(param);
44+
playSound(sound);
2445
}
25-
return false;
2646
}
2747

2848
const storage = injecStorage();
@@ -51,7 +71,9 @@ export async function checkPomodoro() {
5171

5272
const activeTab = await Browser.tabs.query({ active: true });
5373

54-
if (now > pomodoroEndTime) {
74+
if (now >= pomodoroEndTime) {
75+
if (now == pomodoroEndTime) await play(StorageParams.POMODORO_AUDIO_AFTER_FINISHED);
76+
5577
await storage.saveValue(StorageParams.IS_POMODORO_ENABLED, false);
5678
await storage.saveValue(StorageParams.POMODORO_START_TIME, null);
5779
await useBadge({
@@ -63,7 +85,8 @@ export async function checkPomodoro() {
6385
return;
6486
}
6587

66-
const isWork = isTargetPeriod();
88+
const target = isTargetPeriod();
89+
const isWork = target.isTargetPeriod;
6790

6891
if (isWork) {
6992
logger.log('[Pomodoro] Work Time');
@@ -73,6 +96,7 @@ export async function checkPomodoro() {
7396
color: BadgeColor.none,
7497
icon: BadgeIcon.pomodoroWorkingTime,
7598
});
99+
await play(StorageParams.POMODORO_AUDIO_AFTER_WORK);
76100
} else {
77101
logger.log('[Pomodoro] Rest Time');
78102
await useBadge({
@@ -81,6 +105,7 @@ export async function checkPomodoro() {
81105
color: BadgeColor.none,
82106
icon: BadgeIcon.pomodoroRestTime,
83107
});
108+
await play(StorageParams.POMODORO_AUDIO_AFTER_REST);
84109
}
85110

86111
return {

0 commit comments

Comments
 (0)