Skip to content

Commit

Permalink
Updated notification test cases, reduced amount of config event emitters
Browse files Browse the repository at this point in the history
  • Loading branch information
ransome1 committed Nov 26, 2023
1 parent 84b01a5 commit 1f09f5f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 67 deletions.
16 changes: 8 additions & 8 deletions src/__tests__/__mock__/recurrence.txt
Original file line number Diff line number Diff line change
@@ -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
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
4 changes: 4 additions & 0 deletions src/__tests__/main/HandleNotification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
44 changes: 8 additions & 36 deletions src/main/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const configStorage = new Store<ConfigData>({
},
}
});

const filtersPath = path.join(userDataDirectory, 'filters.json');
const filterStorage = new Store<{}>({ cwd: userDataDirectory, name: 'filters' });

Expand All @@ -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) {

Expand All @@ -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;
Expand Down
60 changes: 39 additions & 21 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any> = {};
Expand All @@ -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 = () => {
Expand Down Expand Up @@ -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"
}

Expand All @@ -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) {
Expand All @@ -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 => {
Expand All @@ -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 };
4 changes: 3 additions & 1 deletion src/main/modules/File/Watcher.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/HandleNotification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>(notifiedTodoObjectsStorage.get('notifiedTodoObjects', []));
const notifiedTodoObjects = new Set<string>(notifiedTodoObjectsStorage.get('notifiedTodoObjects', []));

if (!notifiedTodoObjects.has(hash)) {
sendNotification(daysUntilDue, body);
Expand Down

0 comments on commit 1f09f5f

Please sign in to comment.