@@ -5,9 +5,15 @@ import { useBadge, BadgeIcon, BadgeColor } from './useBadge';
55import { Settings } from './settings' ;
66import Browser from 'webextension-polyfill' ;
77import { logger } from '../utils/logger' ;
8+ import { playSound } from './playSound' ;
89
910export 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