1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-02-01 19:15:01 +02:00

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
This commit is contained in:
lightray22 2020-01-18 13:46:04 +00:00 committed by Laurent Cozic
parent 8a392e1c06
commit 960d7f84eb
4 changed files with 41 additions and 6 deletions

View File

@ -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);
}));
});

View File

@ -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';
}

View File

@ -26,7 +26,8 @@ class FoldersScreenUtils {
}
if (Setting.value('showNoteCounts')) {
await Folder.addNoteCounts(folders);
await Folder.addNoteCounts(folders,
Setting.value('showCompletedTodos'));
}
return folders;

View File

@ -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`;
${where} GROUP BY folders.id`;
const noteCounts = await this.db().selectAll(sql);
noteCounts.forEach((noteCount) => {
let parentId = noteCount.folder_id;