Skip to content

Desktop: don't count completed to-dos in note counts when they are not shown #2288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions CliClient/tests/models_Folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,42 @@ describe('models_Folder', function() {
}));

it('should add node counts', asyncTest(async () => {
let folders, foldersById;

let f1 = await Folder.save({ title: 'folder1' });
let f2 = await Folder.save({ title: 'folder2', parent_id: f1.id });
let f3 = await Folder.save({ title: 'folder3', parent_id: f2.id });
let f4 = await Folder.save({ title: 'folder4' });

let n1 = await Note.save({ title: 'note1', parent_id: f3.id });
let n2 = await Note.save({ title: 'note1', parent_id: f3.id });
let n3 = await Note.save({ title: 'note1', parent_id: f1.id });
let n2 = await Note.save({ title: 'note2', parent_id: f3.id });
let n3 = await Note.save({ title: 'note3', parent_id: f1.id });
let n4 = await Note.save({ title: 'note4', parent_id: f3.id, is_todo: true, todo_completed: 0 });
let n5 = await Note.save({ title: 'note5', parent_id: f3.id, is_todo: true, todo_completed: 999 });
let n6 = await Note.save({ title: 'note6', parent_id: f3.id, is_todo: true, todo_completed: 999 });

folders = await Folder.all();
await Folder.addNoteCounts(folders, true); // count completed

foldersById = {};
folders.forEach((f) => { foldersById[f.id] = f; });

expect(folders.length).toBe(4);
expect(foldersById[f1.id].note_count).toBe(6);
expect(foldersById[f2.id].note_count).toBe(5);
expect(foldersById[f3.id].note_count).toBe(5);
expect(foldersById[f4.id].note_count).toBe(0);

const folders = await Folder.all();
await Folder.addNoteCounts(folders);
folders = await Folder.all();
await Folder.addNoteCounts(folders, false); // don't count completed

const foldersById = {};
foldersById = {};
folders.forEach((f) => { foldersById[f.id] = f; });

expect(folders.length).toBe(4);
expect(foldersById[f1.id].note_count).toBe(3);
expect(foldersById[f2.id].note_count).toBe(2);
expect(foldersById[f3.id].note_count).toBe(2);
expect(foldersById[f1.id].note_count).toBe(4);
expect(foldersById[f2.id].note_count).toBe(3);
expect(foldersById[f3.id].note_count).toBe(3);
expect(foldersById[f4.id].note_count).toBe(0);
}));

Expand Down
9 changes: 7 additions & 2 deletions ReactNativeClient/lib/BaseApplication.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,16 @@ class BaseApplication {
refreshFolders = true;
}

if (this.hasGui() && ((action.type == 'SETTING_UPDATE_ONE' && action.key.indexOf('folders.sortOrder') === 0) || action.type == 'SETTING_UPDATE_ALL')) {
if (this.hasGui() && action.type == 'SETTING_UPDATE_ALL')
{
refreshFolders = 'now';
}

if (this.hasGui() && ((action.type == 'SETTING_UPDATE_ONE' && action.key == 'showNoteCounts') || action.type == 'SETTING_UPDATE_ALL')) {
if (this.hasGui() && action.type == 'SETTING_UPDATE_ONE' && (
action.key.indexOf('folders.sortOrder') === 0 ||
action.key == 'showNoteCounts' ||
action.key == 'showCompletedTodos' ))
{
refreshFolders = 'now';
}

Expand Down
3 changes: 2 additions & 1 deletion ReactNativeClient/lib/folders-screen-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class FoldersScreenUtils {
}

if (Setting.value('showNoteCounts')) {
await Folder.addNoteCounts(folders);
await Folder.addNoteCounts(folders,
Setting.value('showCompletedTodos'));
}

return folders;
Expand Down
9 changes: 6 additions & 3 deletions ReactNativeClient/lib/models/Folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,18 @@ class Folder extends BaseItem {

// Calculates note counts for all folders and adds the note_count attribute to each folder
// Note: this only calculates the overall number of nodes for this folder and all its descendants
static async addNoteCounts(folders) {
static async addNoteCounts(folders, showDone) {
const foldersById = {};
folders.forEach((f) => {
foldersById[f.id] = f;
f.note_count = 0;
});

const sql = `SELECT folders.id as folder_id, count(notes.parent_id) as note_count
FROM folders LEFT JOIN notes ON notes.parent_id = folders.id
GROUP BY folders.id`;
FROM folders LEFT JOIN notes ON notes.parent_id = folders.id`
+ (!showDone ? ' WHERE (notes.is_todo = 0 OR notes.todo_completed = 0)' : '')
+ ` GROUP BY folders.id`;

const noteCounts = await this.db().selectAll(sql);
noteCounts.forEach((noteCount) => {
let parentId = noteCount.folder_id;
Expand Down