From 1f09f5fa922333fe180f507791250f3d9e9092a1 Mon Sep 17 00:00:00 2001 From: ransome1 Date: Sun, 26 Nov 2023 11:11:29 +0100 Subject: [PATCH] Updated notification test cases, reduced amount of config event emitters --- src/__tests__/__mock__/recurrence.txt | 16 +++--- src/__tests__/main/HandleNotification.tsx | 4 ++ src/main/config.tsx | 44 +++-------------- src/main/main.ts | 60 +++++++++++++++-------- src/main/modules/File/Watcher.tsx | 4 +- src/main/modules/HandleNotification.tsx | 2 +- 6 files changed, 63 insertions(+), 67 deletions(-) diff --git a/src/__tests__/__mock__/recurrence.txt b/src/__tests__/__mock__/recurrence.txt index cea00f3c..e44a79cb 100644 --- a/src/__tests__/__mock__/recurrence.txt +++ b/src/__tests__/__mock__/recurrence.txt @@ -1,11 +1,11 @@ -2023-11-23 Line 1 rec:1d due:2023-11-24 -2023-11-23 Line 1 rec:w due:2023-11-30 -2023-11-23 Line 1 rec:2m due:2024-01-23 -2023-11-23 Line 1 rec:+1d due:2023-11-25 -2023-11-23 Line 1 rec:7w due:2024-01-11 +2023-11-26 Line 1 rec:1d due:2023-11-27 +2023-11-26 Line 1 rec:w due:2023-12-03 +2023-11-26 Line 1 rec:2m due:2024-01-26 +2023-11-26 Line 1 rec:+1d due:2023-11-28 +2023-11-26 Line 1 rec:7w due:2024-01-14 2023-07-21 Line 1 due:2023-07-24 rec:+1b 2021-01-01 taxes are due in one year t:2022-03-30 due:2022-04-30 rec:+1y -2023-11-23 Water plants @home +quick due:2023-11-30 t:2023-11-20 rec:1w -2023-11-23 Line 1 rec:+1d t:2023-09-20 due:2023-11-24 -(A) 2023-11-23 Line 1 rec:1d pri:A due:2023-11-24 \ No newline at end of file +2023-11-26 Water plants @home +quick due:2023-12-03 t:2023-11-23 rec:1w +2023-11-26 Line 1 rec:+1d t:2023-09-20 due:2023-11-27 +(A) 2023-11-26 Line 1 rec:1d pri:A due:2023-11-27 \ No newline at end of file diff --git a/src/__tests__/main/HandleNotification.tsx b/src/__tests__/main/HandleNotification.tsx index dc7a4025..dbeec745 100644 --- a/src/__tests__/main/HandleNotification.tsx +++ b/src/__tests__/main/HandleNotification.tsx @@ -23,6 +23,10 @@ jest.mock('electron', () => ({ })); jest.mock('../../main/config', () => ({ + notifiedTodoObjectsStorage: { + get: jest.fn(), + set: jest.fn(), + }, configStorage: { get: jest.fn((key) => { if (key === 'notificationsAllowed') { diff --git a/src/main/config.tsx b/src/main/config.tsx index 1f48ee15..1f2d5230 100644 --- a/src/main/config.tsx +++ b/src/main/config.tsx @@ -77,6 +77,7 @@ const configStorage = new Store({ }, } }); + const filtersPath = path.join(userDataDirectory, 'filters.json'); const filterStorage = new Store<{}>({ cwd: userDataDirectory, name: 'filters' }); @@ -98,19 +99,19 @@ if (!fs.existsSync(customStylesPath)) { } const handleConfigChange = async (key: string, newValue: any) => { - // todo: what if search string was set previously? This would remove it const [todoObjects, attributes, headers, filters] = await processDataRequest(''); mainWindow!.webContents.send('requestData', todoObjects, attributes, headers, filters); - if (key === 'sorting') { - mainWindow!.webContents.send('updateSorting', newValue); - } }; -filterStorage.onDidChange('filters' as never, async () => { - const [todoObjects, attributes, headers, filters] = await processDataRequest(''); - mainWindow!.webContents.send('requestData', todoObjects, attributes, headers, filters); +filterStorage.onDidAnyChange((newValue, oldValue) => { + handleConfigChange(); +}); + +configStorage.onDidAnyChange((newValue, oldValue) => { + handleConfigChange(); }); + configStorage.onDidChange('files', async (files: File[] | undefined) => { if (files) { @@ -134,39 +135,10 @@ configStorage.onDidChange('files', async (files: File[] | undefined) => { } }); -configStorage.onDidChange('showCompleted', () => { - handleConfigChange('showCompleted', configStorage.get('showCompleted')); -}); - -configStorage.onDidChange('showHidden', () => { - handleConfigChange('showHidden', configStorage.get('showHidden')); -}); - -configStorage.onDidChange('thresholdDateInTheFuture', () => { - handleConfigChange('thresholdDateInTheFuture', configStorage.get('thresholdDateInTheFuture')); -}); - -configStorage.onDidChange('dueDateInTheFuture', () => { - handleConfigChange('dueDateInTheFuture', configStorage.get('dueDateInTheFuture')); -}); - configStorage.onDidChange('showFileTabs', () => { mainWindow!.webContents.send('setShowFileTabs'); }); -configStorage.onDidChange('sorting', (sorting) => { - handleConfigChange('sorting', sorting); -}); - -configStorage.onDidChange('notificationThreshold', async () => { - const [todoObjects, attributes, headers, filters] = await processDataRequest(''); - mainWindow!.webContents.send('requestData', todoObjects, attributes, headers, filters); -}); - -configStorage.onDidChange('fileSorting', (fileSorting) => { - handleConfigChange('fileSorting', fileSorting); -}); - configStorage.onDidChange('colorTheme', (colorTheme) => { if (colorTheme === 'system' || colorTheme === 'light' || colorTheme === 'dark') { nativeTheme.themeSource = colorTheme; diff --git a/src/main/main.ts b/src/main/main.ts index efdae242..b35861bb 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -10,8 +10,8 @@ import './modules/Ipc'; import handleTheme from './modules/Theme'; const environment = process.env.NODE_ENV; - const files: File[] = (configStorage.get('files') as File[]) || []; +let tray: boolean = configStorage.get('tray'); let mainWindow: BrowserWindow | null = null; let eventListeners: Record = {}; @@ -32,8 +32,19 @@ const handleCreateWindow = () => { const handleClosed = () => { mainWindow = null; - delete eventListeners.readyToShow; - delete eventListeners.closed; + + delete eventListeners.handleReadyToShow; + delete eventListeners.handleClosed; + delete eventListeners.handleResize; + delete eventListeners.handleMove; + delete eventListeners.handleShow; + delete eventListeners.handleMaximize; + delete eventListeners.handleUnmaximize; + delete eventListeners.handleCreateWindow; + delete eventListeners.handleWindowAllClosed; + delete eventListeners.handleWillQuit; + delete eventListeners.handleBeforeQuit; + delete eventListeners.watcher; } const handleResize = () => { @@ -127,6 +138,16 @@ const createWindow = async() => { .on('closed', handleClosed) .on('maximize', handleMaximize) .on('unmaximize', handleUnmaximize); + + eventListeners + .handleReadyToShow = handleReadyToShow + .handleClosed = handleClosed + .handleResize = handleResize + .handleMove = handleMove + .handleShow = handleShow + .handleMaximize = handleMaximize + .handleUnmaximize = handleUnmaximize; + return "Main window has been created successfully" } @@ -142,7 +163,7 @@ const handleReadyToShow = async () => { } const handleWindowAllClosed = () => { - const tray: boolean = configStorage.get('tray'); + tray = configStorage.get('tray'); if (process.platform !== 'darwin' && !tray) { app.quit(); } else if (process.platform === 'darwin' && tray) { @@ -152,24 +173,16 @@ const handleWindowAllClosed = () => { } } -const handleWillQuit = () => { - delete eventListeners.willQuit; -} - const handleBeforeQuit = () => { app.releaseSingleInstanceLock(); - delete eventListeners.beforeQuit; } app .on('window-all-closed', handleWindowAllClosed) - .on('will-quit', handleWillQuit) .on('before-quit', handleBeforeQuit) + .on('activate', handleCreateWindow) .whenReady() .then(() => { - eventListeners.readyToShow = handleReadyToShow; - eventListeners.closed = handleClosed; - createWindow().then(result => { console.log('main.ts:', result); }).catch(error => { @@ -182,15 +195,20 @@ app console.error('main.ts:', error); }); - createTray().then(result => { - console.log('main.ts:', result); - }).catch(error => { - console.error('main.ts:', error); - }); + if(tray) { + createTray().then(result => { + console.log('main.ts:', result); + }).catch(error => { + console.error('main.ts:', error); + }); + } + + eventListeners + .handleCreateWindow = handleCreateWindow + .handleWindowAllClosed = handleWindowAllClosed + .handleBeforeQuit = handleBeforeQuit; - app.on('activate', handleCreateWindow); - eventListeners.activate = handleCreateWindow; }) .catch(console.error); -export { mainWindow, handleCreateWindow }; +export { mainWindow, handleCreateWindow, eventListeners }; diff --git a/src/main/modules/File/Watcher.tsx b/src/main/modules/File/Watcher.tsx index f8a3f6c4..d19a3d64 100644 --- a/src/main/modules/File/Watcher.tsx +++ b/src/main/modules/File/Watcher.tsx @@ -1,6 +1,6 @@ import chokidar, { FSWatcher } from 'chokidar'; import processDataRequest from '../ProcessDataRequest'; -import { mainWindow } from '../../main'; +import { mainWindow, eventListeners } from '../../main'; import { configStorage } from '../../config'; import { File } from '../../util'; @@ -42,6 +42,8 @@ function createFileWatcher(files: File[]): string { console.log('FileWatcher.ts: Initial scan complete. Ready for changes'); }); + eventListeners.watcher = watcher; + return 'File watchers created'; } catch (error: any) { console.error(error); diff --git a/src/main/modules/HandleNotification.tsx b/src/main/modules/HandleNotification.tsx index 6c60bbd4..1c0b691f 100644 --- a/src/main/modules/HandleNotification.tsx +++ b/src/main/modules/HandleNotification.tsx @@ -47,7 +47,7 @@ export function handleNotification(id: number, due: string | null, body: string, if (dueDate.isBefore(today.add(notificationThreshold, 'day'))) { badge.count += 1; - const notifiedTodoObjects = new Set(notifiedTodoObjectsStorage.get('notifiedTodoObjects', [])); + const notifiedTodoObjects = new Set(notifiedTodoObjectsStorage.get('notifiedTodoObjects', [])); if (!notifiedTodoObjects.has(hash)) { sendNotification(daysUntilDue, body);