Skip to content

Commit 53c491b

Browse files
committed
Refactor
1 parent e5b53f0 commit 53c491b

5 files changed

Lines changed: 44 additions & 27 deletions

File tree

app/preferences-preload.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { exposeElectronApi, exposeGlobal, exposeI18next, exposeProcess, exposeSettings, exposeStretchly } from './utils/context-bridge-exposers.js'
1+
import { exposeElectronApi, exposeGlobal, exposeI18next, exposeProcess, exposeSettings, exposeStretchly, exposeUtils } from './utils/context-bridge-exposers.js'
22

33
exposeElectronApi()
44
exposeGlobal()
55
exposeI18next()
66
exposeProcess()
77
exposeSettings()
88
exposeStretchly()
9+
exposeUtils()

app/preferences-renderer.js

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ window.onload = async (e) => {
8181
const output = range.closest('div').querySelector('output')
8282
range.value = settings[range.name] / divisor
8383
const unit = output.dataset.unit
84-
output.innerHTML = await formatUnitAndValue(unit, range.value)
84+
output.innerHTML = await window.utils.formatUnitAndValue(unit, range.value)
8585
document.querySelector('#longBreakEvery').closest('div').querySelector('output')
8686
.innerHTML = await window.i18next.t('utils.minutes', { count: parseInt(realBreakInterval()) })
8787
})
@@ -205,18 +205,18 @@ window.onload = async (e) => {
205205
const output = range.closest('div').querySelector('output')
206206
range.value = settings[range.name] / divisor
207207
const unit = output.dataset.unit
208-
output.innerHTML = await formatUnitAndValue(unit, range.value)
208+
output.innerHTML = await window.utils.formatUnitAndValue(unit, range.value)
209209
document.querySelector('#longBreakEvery').closest('div').querySelector('output')
210210
.innerHTML = await window.i18next.t('utils.minutes', { count: parseInt(realBreakInterval()) })
211211
if (!eventsAttached) {
212212
range.onchange = async event => {
213-
output.innerHTML = await formatUnitAndValue(unit, range.value)
213+
output.innerHTML = await window.utils.formatUnitAndValue(unit, range.value)
214214
document.querySelector('#longBreakEvery').closest('div').querySelector('output')
215215
.innerHTML = await window.i18next.t('utils.minutes', { count: parseInt(realBreakInterval()) })
216216
window.settings.saveSettings(range.name, range.value * divisor)
217217
}
218218
range.oninput = async event => {
219-
output.innerHTML = await formatUnitAndValue(unit, range.value)
219+
output.innerHTML = await window.utils.formatUnitAndValue(unit, range.value)
220220
document.querySelector('#longBreakEvery').closest('div').querySelector('output')
221221
.innerHTML = await window.i18next.t('utils.minutes', { count: parseInt(realBreakInterval()) })
222222
}
@@ -321,22 +321,4 @@ window.onload = async (e) => {
321321
const breakInterval = document.querySelector('#longBreakEvery').value * 1
322322
return microbreakInterval * (breakInterval + 1)
323323
}
324-
325-
// TODO take out and test
326-
async function formatUnitAndValue (unit, value) {
327-
if (unit === 'seconds') {
328-
if (value < 60) {
329-
return await window.i18next.t('utils.seconds', { count: parseInt(value) })
330-
} else {
331-
const val = parseFloat(value / 60).toFixed(1)
332-
if (val % 1 === 0) {
333-
return await window.i18next.t('utils.minutes', { count: parseInt(val) })
334-
} else {
335-
return await window.i18next.t('utils.minutes', { count: parseFloat(val) })
336-
}
337-
}
338-
} else {
339-
return await window.i18next.t(`utils.${unit}`, { count: parseInt(value) })
340-
}
341-
}
342324
}

app/utils/context-bridge-exposers.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,17 @@ function exposeStretchly () {
9494
}
9595

9696
function exposeUtils () {
97+
const i18n = {
98+
t: (key, options) => ipcRenderer.invoke('i18next-translate', key, options)
99+
}
97100
contextBridge.exposeInMainWorld('utils', {
98101
formatKeyboardShortcut: utils.formatKeyboardShortcut,
99102
formatTimeRemaining: async (milliseconds, locale) => {
100-
const i18n = {
101-
t: (key, options) => ipcRenderer.invoke('i18next-translate', key, options)
102-
}
103103
return utils.formatTimeRemaining(milliseconds, locale, i18n, humanizeDuration)
104104
},
105+
formatUnitAndValue: (unit, value) => {
106+
return utils.formatUnitAndValue(unit, value, i18n)
107+
},
105108
shouldShowNotificationTitle: (platform, systemVersion) => {
106109
return utils.shouldShowNotificationTitle(platform, systemVersion, semver)
107110
},

app/utils/utils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ function formatTimeIn (milliseconds, locale, i18next, humanizeDuration) {
2020
})
2121
}
2222

23+
function formatUnitAndValue (unit, value, i18next) {
24+
if (unit === 'seconds') {
25+
if (value < 60) {
26+
return i18next.t('utils.seconds', { count: parseInt(value) })
27+
} else {
28+
const val = parseFloat(value / 60).toFixed(1)
29+
if (val % 1 === 0) {
30+
return i18next.t('utils.minutes', { count: parseInt(val) })
31+
} else {
32+
return i18next.t('utils.minutes', { count: parseFloat(val) })
33+
}
34+
}
35+
} else {
36+
return i18next.t(`utils.${unit}`, { count: parseInt(value) })
37+
}
38+
}
39+
2340
// does not consider `postponesLimit`
2441
function canPostpone (postpone, passedPercent, postponePercent) {
2542
return postpone && passedPercent <= postponePercent
@@ -56,6 +73,7 @@ function insideFlatpak () {
5673
export {
5774
formatTimeRemaining,
5875
formatTimeIn,
76+
formatUnitAndValue,
5977
canPostpone,
6078
canSkip,
6179
formatKeyboardShortcut,

test/utils.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { formatTimeRemaining, formatTimeIn, canSkip, canPostpone, formatKeyboardShortcut, minutesRemaining, shouldShowNotificationTitle } from '../app/utils/utils'
1+
import { formatTimeRemaining, formatTimeIn, canSkip, canPostpone, formatKeyboardShortcut, minutesRemaining, shouldShowNotificationTitle, formatUnitAndValue } from '../app/utils/utils'
22
import { beforeAll, afterAll, vi } from 'vitest'
33
import 'chai/register-should'
44
import i18next from 'i18next'
@@ -53,6 +53,19 @@ describe('Times formatters', function () {
5353
formatTimeIn(7200000, 'en', i18next, humanizeDuration).should.equal('in about 2 hours')
5454
formatTimeIn(7260000, 'en', i18next, humanizeDuration).should.equal('in about 2 hours 1 minute')
5555
})
56+
57+
it('formats unit and value correctly', function () {
58+
formatUnitAndValue('seconds', 30, i18next).should.equal('30 seconds')
59+
formatUnitAndValue('seconds', 1, i18next).should.equal('1 second')
60+
formatUnitAndValue('seconds', 60, i18next).should.equal('1 minute')
61+
formatUnitAndValue('seconds', 120, i18next).should.equal('2 minutes')
62+
formatUnitAndValue('seconds', 90, i18next).should.equal('1.5 minutes')
63+
formatUnitAndValue('seconds', 150, i18next).should.equal('2.5 minutes')
64+
formatUnitAndValue('minutes', 5, i18next).should.equal('5 minutes')
65+
formatUnitAndValue('minutes', 1, i18next).should.equal('1 minute')
66+
formatUnitAndValue('hours', 2, i18next).should.equal('2 hours')
67+
formatUnitAndValue('hours', 1, i18next).should.equal('1 hour')
68+
})
5669
})
5770

5871
describe('Others', () => {

0 commit comments

Comments
 (0)