Skip to content

Commit 044a4a8

Browse files
committed
Fix work/rest periods
1 parent 299db96 commit 044a4a8

File tree

7 files changed

+76
-50
lines changed

7 files changed

+76
-50
lines changed

src/components/Pomodoro.vue

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
} from '../storage/storage-params';
4343
import { Time } from '../utils/time';
4444
import { logger } from '../utils/logger';
45+
import { useBadge, BadgeColor, BadgeIcon } from '../functions/useBadge';
4546
4647
const { t } = useI18n();
4748
const settingsStorage = injecStorage();
@@ -90,11 +91,26 @@ async function changeStatus() {
9091
StorageParams.POMODORO_INTERVAL_REST,
9192
convertHHMMToSeconds(restTime.value.hours, restTime.value.minutes),
9293
);
93-
await settingsStorage.saveValue(StorageParams.POMODORO_START_TIME, new Date());
94+
await settingsStorage.saveValue(StorageParams.POMODORO_START_TIME, new Date().toString());
9495
await settingsStorage.saveValue(StorageParams.POMODORO_FREQUENCY, frequency.value);
9596
9697
isEnabled.value = !isEnabled.value;
9798
99+
if (isEnabled.value)
100+
await useBadge({
101+
text: null,
102+
color: BadgeColor.none,
103+
icon: BadgeIcon.pomodoroWorkingTime,
104+
});
105+
else {
106+
await settingsStorage.saveValue(StorageParams.POMODORO_START_TIME, null);
107+
await useBadge({
108+
text: null,
109+
color: BadgeColor.none,
110+
icon: BadgeIcon.default,
111+
});
112+
}
113+
98114
logger.log(`Change pomodoro status to ${String(isEnabled.value).toUpperCase()}`);
99115
}
100116
</script>

src/functions/pomodoro.ts

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { addSeconds } from 'date-fns';
22
import { injecStorage } from '../storage/inject-storage';
33
import { StorageParams } from '../storage/storage-params';
4-
import { Time, timeToSeconds } from '../utils/time';
54
import { useBadge, BadgeIcon, BadgeColor } from './useBadge';
5+
import { Settings } from './settings';
6+
import Browser from 'webextension-polyfill';
67

78
export 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
});

src/functions/useBadge.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import Browser from 'webextension-polyfill';
22

33
export interface BadgeState {
4-
text: string;
4+
text: string | null;
55
color: BadgeColor;
66
tabId?: number;
77
icon?: BadgeIcon;
88
}
99

1010
export enum BadgeIcon {
11-
default = '/assets/icons/128x128.png',
11+
default = '/128x128.png',
1212
pomodoroWorkingTime = '/assets/icons/pomodoro.png',
13-
pomodoroRestTime = '/assets/icons/pomodoro-rest-icon.png',
13+
pomodoroRestTime = '/assets/icons/pomodoro-rest.png',
1414
}
1515

1616
export enum BadgeColor {
@@ -21,7 +21,8 @@ export enum BadgeColor {
2121
}
2222

2323
export async function useBadge(badge: BadgeState): Promise<void> {
24-
await Browser.action.setBadgeBackgroundColor({ color: badge.color });
24+
if (badge.color != BadgeColor.none)
25+
await Browser.action.setBadgeBackgroundColor({ color: badge.color });
2526
await Browser.action.setBadgeText({
2627
tabId: badge.tabId,
2728
text: badge.text,
@@ -31,6 +32,7 @@ export async function useBadge(badge: BadgeState): Promise<void> {
3132
path: badge.icon,
3233
});
3334
await Browser.action.setBadgeText({
35+
tabId: badge.tabId,
3436
text: badge.text,
3537
});
3638
} else

src/tracker.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -127,22 +127,22 @@ async function mainTracker(
127127

128128
tab.incSummaryTime();
129129

130-
const viewInBadge =
131-
(await Settings.getInstance().getSetting(StorageParams.VIEW_TIME_IN_BADGE)) &&
132-
(await canChangeBadge());
133-
134-
if (viewInBadge)
135-
await useBadge({
136-
tabId: activeTab?.id,
137-
text: convertSummaryTimeToBadgeString(tab.days.at(-1)!.summary),
138-
color: BadgeColor.blue,
139-
});
140-
else
141-
await useBadge({
142-
tabId: activeTab?.id,
143-
text: '',
144-
color: BadgeColor.red,
145-
});
130+
const viewInBadge = await Settings.getInstance().getSetting(StorageParams.VIEW_TIME_IN_BADGE);
131+
132+
if (await canChangeBadge()) {
133+
if (viewInBadge)
134+
await useBadge({
135+
tabId: activeTab?.id,
136+
text: convertSummaryTimeToBadgeString(tab.days.at(-1)!.summary),
137+
color: BadgeColor.blue,
138+
});
139+
else
140+
await useBadge({
141+
tabId: activeTab?.id,
142+
text: null,
143+
color: BadgeColor.none,
144+
});
145+
}
146146
} else await closeOpenInterval();
147147
}
148148

src/utils/converter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import i18n, { getMessagesFromLocale } from '../plugins/i18n';
22
import { HOUR, HOUR_IN_SECONDS, MINUTE, MINUTE_IN_SECONDS, Time } from './time';
33

44
export function convertHHMMToSeconds(hours: number, minutes: number) {
5-
return hours * 3600 + minutes * 60;
5+
return hours * HOUR_IN_SECONDS + minutes * MINUTE_IN_SECONDS;
66
}
77

88
export function convertHHMMToMilliSeconds(hours: number, minutes: number) {
99
return hours * HOUR + minutes * MINUTE;
1010
}
1111

1212
export function convertSecondsToHHMM(seconds: number): Time {
13-
const hours = Math.floor(seconds / 3600);
14-
const totalSeconds = seconds % 3600;
15-
const mins = Math.floor(totalSeconds / 60);
13+
const hours = Math.floor(seconds / HOUR_IN_SECONDS);
14+
const totalSeconds = seconds % HOUR_IN_SECONDS;
15+
const mins = Math.floor(totalSeconds / MINUTE_IN_SECONDS);
1616

1717
return {
1818
hours: hours,

src/utils/time.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,3 @@ export function daysBetween(startDate: Date, endDate: Date): number {
2929
const millisecondsPerDay = 24 * 60 * 60 * 1000;
3030
return (treatAsUTC(endDate).valueOf() - treatAsUTC(startDate).valueOf()) / millisecondsPerDay + 1;
3131
}
32-
33-
export function timeToSeconds(time: Time) {
34-
return time.hours * HOUR_IN_SECONDS + time.minutes * MINUTE_IN_SECONDS;
35-
}

vite.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ function generateManifest() {
2222
// https://vitejs.dev/config/
2323
export default defineConfig(({ mode }) => ({
2424
build: {
25+
assetsInlineLimit: 1024,
2526
rollupOptions: {
2627
output: {
2728
assetFileNames: assetInfo => {
2829
let extType = assetInfo.name.split('.').at(1);
2930
if (/png|jpe?g|svg|gif|tiff|bmp|ico/i.test(extType)) {
3031
extType = 'icons';
3132
}
32-
return `assets/${extType}/[name]-[hash][extname]`;
33+
return `assets/${extType}/[name][extname]`;
3334
},
3435
},
3536
},

0 commit comments

Comments
 (0)