11import { addSeconds } from 'date-fns' ;
22import { injecStorage } from '../storage/inject-storage' ;
33import { StorageParams } from '../storage/storage-params' ;
4- import { Time , timeToSeconds } from '../utils/time' ;
54import { useBadge , BadgeIcon , BadgeColor } from './useBadge' ;
5+ import { Settings } from './settings' ;
6+ import Browser from 'webextension-polyfill' ;
67
78export async function checkPomodoro ( ) {
8- function isTargetPeriod ( isRest : boolean ) {
9+ function isTargetPeriod ( ) {
910 for ( let index = 1 ; index <= frequency ; index ++ ) {
10- const plusWorkingTime = timeToSeconds ( workTime ) * ( isRest ? index : index -- ) ;
11- const plusRestTime = timeToSeconds ( restTime ) * index -- ;
11+ const plusWorkingTime = workTime * ( index - 1 ) ;
12+ const plusRestTime = restTime * ( index - 1 ) ;
1213 const isPomodoroTargetPeriodStart = addSeconds ( startTime , plusWorkingTime + plusRestTime ) ;
1314 const isPomodoroTargetPeriodEnd = addSeconds (
1415 startTime ,
15- plusWorkingTime + plusRestTime + timeToSeconds ( workTime ) ,
16+ plusWorkingTime + plusRestTime + workTime ,
1617 ) ;
1718 const isTargetPeriod =
1819 now . getTime ( ) >= isPomodoroTargetPeriodStart . getTime ( ) &&
@@ -24,40 +25,50 @@ export async function checkPomodoro() {
2425 }
2526
2627 const storage = injecStorage ( ) ;
27- const isPomodoroEnabled = ( await storage . getValue ( StorageParams . IS_POMODORO_ENABLED ) ) as boolean ;
28+ const isPomodoroEnabled = ( await Settings . getInstance ( ) . getSetting (
29+ StorageParams . IS_POMODORO_ENABLED ,
30+ ) ) as boolean ;
2831
2932 if ( ! isPomodoroEnabled ) return ;
3033
31- const startTime = ( await storage . getValue ( StorageParams . POMODORO_START_TIME ) ) as Date ;
32- const workTime = ( await storage . getValue ( StorageParams . POMODORO_INTERVAL_WORK ) ) as Time ;
33- const restTime = ( await storage . getValue ( StorageParams . POMODORO_INTERVAL_REST ) ) as Time ;
34- const frequency = ( await storage . getValue ( StorageParams . POMODORO_FREQUENCY ) ) as number ;
34+ const startTime = new Date (
35+ ( await Settings . getInstance ( ) . getSetting ( StorageParams . POMODORO_START_TIME ) ) as string ,
36+ ) ;
37+ const workTime = ( await Settings . getInstance ( ) . getSetting (
38+ StorageParams . POMODORO_INTERVAL_WORK ,
39+ ) ) as number ;
40+ const restTime = ( await Settings . getInstance ( ) . getSetting (
41+ StorageParams . POMODORO_INTERVAL_REST ,
42+ ) ) as number ;
43+ const frequency = ( await Settings . getInstance ( ) . getSetting (
44+ StorageParams . POMODORO_FREQUENCY ,
45+ ) ) as number ;
3546
3647 const now = new Date ( ) ;
3748
38- const pomodoroEndTime = addSeconds (
39- startTime ,
40- timeToSeconds ( workTime ) * frequency + timeToSeconds ( restTime ) * frequency ,
41- ) ;
49+ const pomodoroEndTime = addSeconds ( startTime , workTime * frequency + restTime * frequency ) ;
4250
43- if ( pomodoroEndTime > now ) {
51+ if ( now > pomodoroEndTime ) {
4452 await storage . saveValue ( StorageParams . IS_POMODORO_ENABLED , false ) ;
4553 await storage . saveValue ( StorageParams . POMODORO_START_TIME , null ) ;
4654 return ;
4755 }
4856
49- const isWork = isTargetPeriod ( false ) ;
50- const isRest = isTargetPeriod ( true ) ;
57+ const isWork = isTargetPeriod ( ) ;
58+
59+ const activeTab = await Browser . tabs . query ( { active : true } ) ;
5160
5261 if ( isWork )
5362 await useBadge ( {
54- text : '' ,
63+ tabId : activeTab [ 0 ] . id ,
64+ text : null ,
5565 color : BadgeColor . none ,
5666 icon : BadgeIcon . pomodoroWorkingTime ,
5767 } ) ;
58- if ( isRest )
68+ else
5969 await useBadge ( {
60- text : '' ,
70+ tabId : activeTab [ 0 ] . id ,
71+ text : null ,
6172 color : BadgeColor . none ,
6273 icon : BadgeIcon . pomodoroRestTime ,
6374 } ) ;
0 commit comments