Skip to content

Commit 6305818

Browse files
committed
Fix pomodoro timer
1 parent 4b4ddee commit 6305818

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

src/background.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,24 @@ import { Settings } from './functions/settings';
66
import { StorageParams } from './storage/storage-params';
77
import { injecStorage } from './storage/inject-storage';
88
import { todayLocalDate } from './utils/date';
9+
import { checkPomodoro } from './functions/pomodoro';
910

1011
logger.log('Start background script');
12+
let pomodoroTimer: number;
1113

1214
self.onerror = err => {
1315
console.error('Unhandled error:', err);
1416
};
1517

16-
Browser.storage.onChanged.addListener((changes, namespace) => {
18+
Browser.storage.onChanged.addListener(async (changes, namespace) => {
1719
for (var key in changes) {
1820
if (Object.values(StorageParams).includes(key as StorageParams))
19-
Settings.getInstance().reloadSetting(key as StorageParams);
21+
await Settings.getInstance().reloadSetting(key as StorageParams);
22+
23+
if (key == StorageParams.IS_POMODORO_ENABLED) {
24+
const value = changes[StorageParams.IS_POMODORO_ENABLED].newValue;
25+
pomodoro(value);
26+
}
2027
}
2128
});
2229

@@ -54,5 +61,15 @@ Browser.windows.onFocusChanged.addListener(() => {
5461
logger.log('onFocusChanged');
5562
});
5663

64+
async function pomodoro(value?: boolean) {
65+
if (value == undefined) {
66+
const settingsStorage = injecStorage();
67+
value = await settingsStorage.getValue(StorageParams.IS_POMODORO_ENABLED);
68+
}
69+
if (value == true) pomodoroTimer = setInterval(checkPomodoro, 1000);
70+
else clearInterval(pomodoroTimer);
71+
}
72+
73+
pomodoro();
5774
scheduleJobs();
5875
initTracker();

src/functions/pomodoro.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { StorageParams } from '../storage/storage-params';
44
import { useBadge, BadgeIcon, BadgeColor } from './useBadge';
55
import { Settings } from './settings';
66
import Browser from 'webextension-polyfill';
7+
import { logger } from '../utils/logger';
78

89
export async function checkPomodoro() {
910
function isTargetPeriod() {
@@ -48,28 +49,41 @@ export async function checkPomodoro() {
4849

4950
const pomodoroEndTime = addSeconds(startTime, workTime * frequency + restTime * frequency);
5051

52+
const activeTab = await Browser.tabs.query({ active: true });
53+
5154
if (now > pomodoroEndTime) {
5255
await storage.saveValue(StorageParams.IS_POMODORO_ENABLED, false);
5356
await storage.saveValue(StorageParams.POMODORO_START_TIME, null);
57+
await useBadge({
58+
tabId: activeTab[0].id,
59+
text: null,
60+
color: BadgeColor.none,
61+
icon: BadgeIcon.default,
62+
});
5463
return;
5564
}
5665

5766
const isWork = isTargetPeriod();
5867

59-
const activeTab = await Browser.tabs.query({ active: true });
60-
61-
if (isWork)
68+
if (isWork) {
69+
logger.log('[Pomodoro] Work Time');
6270
await useBadge({
6371
tabId: activeTab[0].id,
6472
text: null,
6573
color: BadgeColor.none,
6674
icon: BadgeIcon.pomodoroWorkingTime,
6775
});
68-
else
76+
} else {
77+
logger.log('[Pomodoro] Rest Time');
6978
await useBadge({
7079
tabId: activeTab[0].id,
7180
text: null,
7281
color: BadgeColor.none,
7382
icon: BadgeIcon.pomodoroRestTime,
7483
});
84+
}
85+
86+
return {
87+
isWork,
88+
};
7589
}

src/tracker.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { Settings } from './functions/settings';
1616
import { useNotificationList } from './functions/useNotificationList';
1717
import { NotificationType, useNotification } from './functions/useNotification';
1818
import { Messages } from './utils/messages';
19-
import { checkPomodoro } from './functions/pomodoro';
2019

2120
const activeTabInstance = ActiveTab.getInstance();
2221

@@ -40,8 +39,6 @@ async function trackTime() {
4039
if (isValidPage(activeTab)) {
4140
const activeDomain = extractHostname(activeTab!.url);
4241

43-
await checkPomodoro();
44-
4542
if ((await isInBlackList(activeDomain)) && (await canChangeBadge())) {
4643
await useBadge({
4744
tabId: activeTab?.id,

0 commit comments

Comments
 (0)