From 960d7f84eb5434a1583961cc115cd7285979c16a Mon Sep 17 00:00:00 2001 From: lightray22 <4316805+lightray22@users.noreply.github.com> Date: Sat, 18 Jan 2020 13:46:04 +0000 Subject: [PATCH] Desktop: Don't count completed to-dos in note counts when they are not shown (#2288) * Desktop: don't count completed to-dos in note counts when they are not shown * Desktop: review comments for commit 0383dcc * Desktop: fix remaining lint issues with commit 1fe4685 --- CliClient/tests/models_Folder.js | 27 +++++++++++++++++++ ReactNativeClient/lib/BaseApplication.js | 7 +++-- ReactNativeClient/lib/folders-screen-utils.js | 3 ++- ReactNativeClient/lib/models/Folder.js | 10 ++++--- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/CliClient/tests/models_Folder.js b/CliClient/tests/models_Folder.js index 30d2bd8a4..16be5b148 100644 --- a/CliClient/tests/models_Folder.js +++ b/CliClient/tests/models_Folder.js @@ -150,4 +150,31 @@ describe('models_Folder', function() { expect(foldersById[f4.id].note_count).toBe(0); })); + it('should not count completed to-dos', asyncTest(async () => { + + 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: '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 }); + + const folders = await Folder.all(); + await Folder.addNoteCounts(folders, false); + + const foldersById = {}; + folders.forEach((f) => { foldersById[f.id] = f; }); + + expect(folders.length).toBe(4); + 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); + })); + }); diff --git a/ReactNativeClient/lib/BaseApplication.js b/ReactNativeClient/lib/BaseApplication.js index 371ae41ce..706e02e45 100644 --- a/ReactNativeClient/lib/BaseApplication.js +++ b/ReactNativeClient/lib/BaseApplication.js @@ -450,11 +450,14 @@ 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'; } diff --git a/ReactNativeClient/lib/folders-screen-utils.js b/ReactNativeClient/lib/folders-screen-utils.js index eb1728e24..964a68fa2 100644 --- a/ReactNativeClient/lib/folders-screen-utils.js +++ b/ReactNativeClient/lib/folders-screen-utils.js @@ -26,7 +26,8 @@ class FoldersScreenUtils { } if (Setting.value('showNoteCounts')) { - await Folder.addNoteCounts(folders); + await Folder.addNoteCounts(folders, + Setting.value('showCompletedTodos')); } return folders; diff --git a/ReactNativeClient/lib/models/Folder.js b/ReactNativeClient/lib/models/Folder.js index eb692427c..9b586eef4 100644 --- a/ReactNativeClient/lib/models/Folder.js +++ b/ReactNativeClient/lib/models/Folder.js @@ -107,15 +107,19 @@ 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, includeCompletedTodos = true) { const foldersById = {}; folders.forEach((f) => { foldersById[f.id] = f; + f.note_count = 0; }); + const where = !includeCompletedTodos ? 'WHERE (notes.is_todo = 0 OR notes.todo_completed = 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 + ${where} GROUP BY folders.id`; + const noteCounts = await this.db().selectAll(sql); noteCounts.forEach((noteCount) => { let parentId = noteCount.folder_id;