Skip to content

Commit

Permalink
Refactoring wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ransome1 committed Apr 4, 2024
1 parent ac7c6f5 commit fd08ecd
Show file tree
Hide file tree
Showing 23 changed files with 297 additions and 323 deletions.
28 changes: 14 additions & 14 deletions src/__tests__/__mock__/recurrence.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

2024-04-02 Line 1 rec:1d due:2024-04-03
2024-04-02 Line 1 rec:w due:2024-04-09
2024-04-02 Line 1 rec:2m due:2024-06-02
2024-04-02 Line 1 rec:+1d due:2024-04-04
2024-04-02 Line 1 rec:7w due:2024-05-21
2024-04-02 Line 1 due:2023-07-24 rec:+1b
2024-04-02 taxes are due in one year t:2022-03-30 due:2022-04-30 rec:+1y
2024-04-02 Water plants @home +quick due:2024-04-09 t:2024-03-30 rec:1w
2024-04-02 Line 1 rec:+1d t:2023-09-20
2024-04-02 Line 1 rec:1d pri:A due:2024-04-03
2024-04-02 (A) Do something rec:d t:2024-04-03 @SomeContext
2024-04-02 Do something rec:0d
2024-04-02 Do something rec:0d due:2024-04-02
2024-04-02 Do something rec:0d due:2024-04-02 t:2024-04-02
2024-04-04 Line 1 rec:1d due:2024-04-05
2024-04-04 Line 1 rec:w due:2024-04-11
2024-04-04 Line 1 rec:2m due:2024-06-04
2024-04-04 Line 1 rec:+1d due:2024-04-06
2024-04-04 Line 1 rec:7w due:2024-05-23
2024-04-04 Line 1 due:2023-07-24 rec:+1b
2024-04-04 taxes are due in one year t:2022-03-30 due:2022-04-30 rec:+1y
2024-04-04 Water plants @home +quick due:2024-04-11 t:2024-04-01 rec:1w
2024-04-04 Line 1 rec:+1d t:2023-09-20
2024-04-04 Line 1 rec:1d pri:A due:2024-04-05
2024-04-04 (A) Do something rec:d t:2024-04-05 @SomeContext
2024-04-04 Do something rec:0d
2024-04-04 Do something rec:0d due:2024-04-04
2024-04-04 Do something rec:0d due:2024-04-04 t:2024-04-04
6 changes: 3 additions & 3 deletions src/main/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { mainWindow } from './main';
import { createFileWatcher } from './modules/File/Watcher';
import { writeToFile } from './modules/File/Write';
import { createTray } from './modules/Tray';
import { processDataRequest, searchString } from './modules/ProcessDataRequest/ProcessDataRequest';
import { dataRequest, searchString } from './modules/DataRequest/DataRequest';
import handleTheme from './modules/Theme';
import { getChannel, handleError } from './util';
import crypto from 'crypto';
Expand Down Expand Up @@ -140,7 +140,7 @@ if(!fs.existsSync(notifiedTodoObjectsPath)) {

filter.onDidChange('attributes', () => {
try {
const requestedData = processDataRequest(searchString);
const requestedData = dataRequest(searchString);
mainWindow!.webContents.send('requestData', requestedData);
} catch(error: Error) {
handleError(error);
Expand All @@ -149,7 +149,7 @@ filter.onDidChange('attributes', () => {

config.onDidAnyChange((settings) => {
try {
const requestedData = processDataRequest(searchString);
const requestedData = dataRequest(searchString);
mainWindow!.webContents.send('requestData', requestedData);
mainWindow!.webContents.send('settingsChanged', settings);
} catch(error: Error) {
Expand Down
9 changes: 4 additions & 5 deletions src/main/modules/Attributes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ let attributes: Attributes = {
completed: {},
};

function incrementCount(countObject: any, key: any | null, notify: boolean, visible: boolean): void {
function incrementCount(countObject: any, key: any | null, notify: boolean): void {
if(key) {
let previousCount: number = parseInt(countObject[key]?.count) || 0;
countObject[key] = {
count: (visible) ? previousCount + 1 : previousCount,
count: previousCount + 1,
notify: notify,
}
}
Expand All @@ -31,19 +31,18 @@ function updateAttributes(todoObjects: TodoObject[], sorting: Sorting[], reset:
todoObjects.forEach((todoObject: TodoObject) => {
const value = todoObject[key as keyof TodoObject];
const notify: boolean = (key === 'due') ? !!todoObject?.notify : false;
const visible: boolean = todoObject.visible;

if(Array.isArray(value)) {
value.forEach((element) => {
if(element !== null) {
const attributeKey = element as keyof Attribute;

incrementCount(attributes[key], attributeKey, notify, visible);
incrementCount(attributes[key], attributeKey, notify);
}
});
} else {
if(value !== null) {
incrementCount(attributes[key], value, notify, visible);
incrementCount(attributes[key], value, notify);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import dayjs from 'dayjs';
let linesInFile: string[];
export const badge: Badge = { count: 0 };

function createTodoObject(index: number, string: string, attributeType?: string, attributeValue?: string): TodoObject {
function createTodoObject(row: number, string: string, attributeType?: string, attributeValue?: string): TodoObject {
let content = string.replaceAll(/[\x10\r\n]/g, ' [LB] ');

let JsTodoTxtObject = new Item(content);
Expand Down Expand Up @@ -43,7 +43,7 @@ function createTodoObject(index: number, string: string, attributeType?: string,
const creation = dayjs(JsTodoTxtObject.created()).isValid() ? dayjs(JsTodoTxtObject.created()).format('YYYY-MM-DD') : null;
const completed = dayjs(JsTodoTxtObject.completed()).isValid() ? dayjs(JsTodoTxtObject.completed()).format('YYYY-MM-DD') : null;
return {
id: index,
row,
body,
created: creation,
complete: JsTodoTxtObject.complete(),
Expand All @@ -59,7 +59,6 @@ function createTodoObject(index: number, string: string, attributeType?: string,
rec,
hidden,
pm,
visible: true,
string: content,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ import { sortAndGroupTodoObjects } from './SortAndGroup';

let searchString: string;
let headers: HeadersObject = {
availableObjects: 0,
visibleObjects: 0
availableObjects: null,
completedObjects: null,
visibleObjects: null
}
let todoObjects: TodoObject[];

function processDataRequest(search?: string): RequestedData {
function dataRequest(search?: string): RequestedData {
searchString = search || '';

const activeFile: FileObject | null = getActiveFile();
if(!activeFile) {
return;
}
const sorting: Sorting[] = config.get('sorting');
const showHidden: boolean = config.get('showHidden');
const fileSorting: boolean = config.get('fileSorting');
const filters: Filters = filter.get('attributes');


const fileContent = readFileContent(activeFile.todoFilePath, activeFile.todoFileBookmark);

const sorting: Sorting[] = config.get('sorting');
const filters: Filters = filter.get('attributes');

todoObjects = createTodoObjects(fileContent);

headers.availableObjects = todoObjects.length;
Expand All @@ -45,33 +45,16 @@ function processDataRequest(search?: string): RequestedData {

updateAttributes(todoObjects, sorting, true);

if(!showHidden) todoObjects = handleHiddenTodoObjects(todoObjects);

if(filters) todoObjects = applyAttributes(todoObjects, filters);

if(searchString) todoObjects = applySearchString(searchString, todoObjects);

updateAttributes(todoObjects, sorting, false);

const visibleTodoObjects: TodoObject[] = todoObjects.filter((todoObject: TodoObject) => {
return todoObject.visible;
});

headers.visibleObjects = visibleTodoObjects.length;

console.log(todoObjects)

todoObjects = sortAndGroupTodoObjects(todoObjects, sorting);

// if(fileSorting) {
// todoObjects = flattenTodoObjects(todoObjects, '');
// } else {
// const sortedAndGroupedTodos: TodoObject[] = sortAndGroupTodoObjects(todoObjects, sorting);
// todoObjects = flattenTodoObjects(sortedAndGroupedTodos, sorting[0].value);
// }
const todoData = sortAndGroupTodoObjects(todoObjects, sorting);

const requestedData: RequestedData = {
todoObjects,
todoData,
attributes,
headers,
filters,
Expand All @@ -80,4 +63,4 @@ function processDataRequest(search?: string): RequestedData {
return requestedData;
}

export { processDataRequest, searchString };
export { dataRequest, searchString };
62 changes: 62 additions & 0 deletions src/main/modules/DataRequest/SortAndGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { config } from '../../config';

function sortAndGroupTodoObjects(todoObjects: TodoObject[], sorting: Sorting[], settings: Settings): TodoObject[] {
let rows;
const fileSorting: boolean = config.get('fileSorting');
const showHidden: boolean = config.get('showHidden');

function compareValues(a: any, b: any, invert: boolean): number {
const comparison = String(a).localeCompare(String(b), undefined, { sensitivity: 'base' });
return invert ? -comparison : comparison;
}

function applySorting(a: TodoObject, b: TodoObject, sorting): number {
for (const { value, invert } of sorting) {
const compareResult = compareValues(a[value], b[value], invert);
if (compareResult !== 0) {
return compareResult;
}
}
return 0;
}

function groupTodoObjectsByKey(todoObjects, attributeKey) {
const grouped = {};
for (const todoObject of todoObjects) {
const groupKey = todoObject[attributeKey] || '';
if (!grouped[groupKey]) {
grouped[groupKey] = {
title: groupKey,
todoObjects: [],
visible: false
};
}
rows++;
todoObject.row = rows;
grouped[groupKey].todoObjects.push(todoObject);
grouped[groupKey].visible = grouped[groupKey].todoObjects.some(todoObject => {
return !todoObject.hidden || (showHidden && todoObject.hidden);
});
}
return Object.values(grouped);
}

function sortTodoObjects(todoObjects: TodoObject[], sorting): any {
const { value } = sorting[0];
const grouped = groupTodoObjectsByKey(todoObjects, value);
return grouped;
}

if(fileSorting) {
return [{
title: null,
todoObjects: todoObjects,
visible: true
}]
}
rows = 0;
const sortedTodoObjects = [...todoObjects].sort((a, b) => applySorting(a, b, sorting));
return sortTodoObjects(sortedTodoObjects, sorting);
}

export { sortAndGroupTodoObjects };
4 changes: 2 additions & 2 deletions src/main/modules/File/Watcher.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chokidar, { FSWatcher } from 'chokidar';
import { processDataRequest, searchString } from '../ProcessDataRequest/ProcessDataRequest';
import { dataRequest, searchString } from '../DataRequest/DataRequest';
import { config } from '../../config';
import { handleError } from '../../util';
import { mainWindow, eventListeners } from '../../main';
Expand All @@ -26,7 +26,7 @@ function createFileWatcher(files: FileObject[]): void {
})
.on('change', (file) => {
try {
const requestedData = processDataRequest(searchString);
const requestedData = dataRequest(searchString);
mainWindow!.webContents.send('requestData', requestedData);
console.log(`${file} has been changed`);
} catch(error: Error) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/File/Write.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { app } from 'electron';
import fs from 'fs';
import { Item } from 'jstodotxt';
import { linesInFile } from '../ProcessDataRequest/CreateTodoObjects';
import { linesInFile } from '../DataRequest/CreateTodoObjects';
import { getActiveFile } from './Active';
import { config } from '../../config';
import { replaceSpeakingDatesWithAbsoluteDates } from '../Date';
Expand Down
19 changes: 2 additions & 17 deletions src/main/modules/Filters/Filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ import { config } from '../../config';
import dayjs from 'dayjs';

function applyAttributes(todoObjects: TodoObject[], filters: Filters | null): TodoObject[] {
return todoObjects.map((todoObject: TodoObject) => {
if (!todoObject.visible) {
return todoObject;
}

todoObject.visible = Object.entries(filters || {}).every(([key, filterArray]: [string, Filter[]]) => {
return todoObjects.filter((todoObject: TodoObject) => {
return Object.entries(filters || {}).every(([key, filterArray]: [string, Filter[]]) => {
if (filterArray.length === 0) {
return true;
}
Expand All @@ -30,8 +26,6 @@ function applyAttributes(todoObjects: TodoObject[], filters: Filters | null): To
return exclude ? !hasMatchingValue : hasMatchingValue;
});
});

return todoObject;
});
}

Expand All @@ -44,20 +38,11 @@ function handleCompletedTodoObjects(todoObjects: TodoObject[]): TodoObject[] {
}
}

function handleHiddenTodoObjects(todoObjects: TodoObject[]): TodoObject[] {
return todoObjects.map((todoObject: TodoObject) => {
if(!todoObject.visible) return todoObject;
todoObject.visible = todoObject.visible && !todoObject.hidden;
return todoObject;
});
}

function handleTodoObjectsDates(todoObjects: TodoObject[]): TodoObject[] {
const thresholdDateInTheFuture: boolean = config.get('thresholdDateInTheFuture');
const dueDateInTheFuture: boolean = config.get('dueDateInTheFuture');

return todoObjects.filter((todoObject: TodoObject) => {
if (!todoObject.visible) return true;

const thresholdDate = dayjs(todoObject?.t);
const dueDate = dayjs(todoObject?.due);
Expand Down
16 changes: 4 additions & 12 deletions src/main/modules/Filters/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
import * as FilterLang from './FilterLang.js';
import { runQuery } from './FilterQuery';
import { createTodoObject } from '../ProcessDataRequest/CreateTodoObjects';
import { createTodoObject } from '../DataRequest/CreateTodoObjects';

function applySearchString(searchString: string, todoObjects: TodoObject[]): TodoObject[] {
try {
const query = FilterLang.parse(searchString);
return todoObjects.map(todoObject => {
if(!todoObject.visible) return todoObject;
todoObject.visible = runQuery(todoObject, query);
return todoObject;
});
return todoObjects.filter(todoObject => runQuery(todoObject, query));
} catch (error) {
const lowerSearchString = searchString.toLowerCase();
return Object.values(todoObjects)
.flat()
.map(todoObject => {
if(!todoObject.visible) return todoObject;
todoObject.visible = todoObject?.string?.toLowerCase().includes(lowerSearchString) || false;
return todoObject;
}) as TodoObject[];
.filter(todoObject => todoObject?.string?.toLowerCase().includes(lowerSearchString));
}
}

function checkForSearchMatches(todoString: string, searchString: string) {
try {
const todoObject = createTodoObject(-1, todoString);
const todoObject = createTodoObject(todoString);
const query = FilterLang.parse(searchString);
return runQuery(todoObject, query);
} catch (error) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/modules/IpcMain.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { ipcMain, app, IpcMainEvent, clipboard, shell } from 'electron';
import { processDataRequest } from './ProcessDataRequest/ProcessDataRequest';
import { changeCompleteState } from './ProcessDataRequest/ChangeCompleteState';
import { dataRequest } from './DataRequest/DataRequest';
import { changeCompleteState } from './DataRequest/ChangeCompleteState';
import { prepareContentForWriting, removeLineFromFile } from './File/Write';
import { archiveTodos, handleRequestArchive } from './File/Archive';
import { config, filter, notifiedTodoObjectsStorage } from '../config';
import { handleError } from '../util';
import { addFile, setFile, removeFile } from './File/File';
import { openFile, createFile } from './File/Dialog';
import { createTodoObject } from './ProcessDataRequest/CreateTodoObjects';
import { createTodoObject } from './DataRequest/CreateTodoObjects';

function handleDataRequest(event: IpcMainEvent, searchString: string) {
try {
const requestedData = processDataRequest(searchString);
const requestedData = dataRequest(searchString);
event.reply('requestData', requestedData);
} catch(error: Error) {
handleError(error);
Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/Notifications.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import crypto from 'crypto';
import { Notification } from 'electron';
import { config, filter, notifiedTodoObjectsStorage } from '../config';
import { createTodoObject } from './ProcessDataRequest/CreateTodoObjects';
import { createTodoObject } from './DataRequest/CreateTodoObjects';
import { checkForSearchMatches } from './Filters/Search';
import dayjs, { Dayjs } from "dayjs";
import isToday from 'dayjs/plugin/isToday';
Expand Down
Loading

0 comments on commit fd08ecd

Please sign in to comment.