Skip to content

Commit

Permalink
Properly count tasks for tags
Browse files Browse the repository at this point in the history
Consider the ancestors of a tag when calculating task counts.
Refresh task counts after drag and drop.

This fixes GitHub issue #404
  • Loading branch information
gycsaba96 authored and diegogangl committed Mar 15, 2024
1 parent 087f90c commit 133d67c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
13 changes: 12 additions & 1 deletion GTG/core/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def count_tasks(count: dict, tasklist: list):
if not task.tags:
count['untagged'] += 1

for tag in task.tags:
for tag in { t for owned_tag in task.tags for t in [owned_tag] + owned_tag.get_ancestors() }:
val = count.get(tag.name, 0)
count[tag.name] = val + 1

Expand All @@ -246,6 +246,17 @@ def count_tasks(count: dict, tasklist: list):
self.tasks.filter(Filter.ACTIONABLE))


def refresh_tag_stats(self) -> None:
"""
Refresh the number of tasks for each tag.
"""
self.refresh_task_count()
for tag_name in self.tags.get_all_tag_names():
tag = self.tags.find(tag_name)
self.refresh_task_for_tag(tag)
self.notify_tag_change(tag)


def notify_tag_change(self, tag) -> None:
"""Notify tasks that this tag has changed."""

Expand Down
15 changes: 14 additions & 1 deletion GTG/core/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import re

from lxml.etree import Element, SubElement
from typing import Any, Dict, Set
from typing import Any, Dict, List, Set

from GTG.core.base_store import BaseStore

Expand Down Expand Up @@ -163,6 +163,15 @@ def set_task_count_closed(self, value: int) -> None:
self._task_count_closed = value


def get_ancestors(self) -> List['Tag']:
"""Return all ancestors of this tag"""
ancestors = []
here = self
while here.parent:
here = here.parent
ancestors.append(here)
return ancestors

def __hash__(self):
return id(self)

Expand Down Expand Up @@ -207,6 +216,10 @@ def __str__(self) -> str:

return f'Tag Store. Holds {len(self.lookup)} tag(s)'

def get_all_tag_names(self) -> List[str]:
"""Return all tag names."""
return list(self.lookup_names.keys())


def find(self, name: str) -> Tag:
"""Get a tag by name."""
Expand Down
7 changes: 5 additions & 2 deletions GTG/gtk/browser/sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,6 @@ def check_parent(self, value, target) -> bool:

def drag_drop(self, target, value, x, y):
"""Callback when dropping onto a target"""

dropped = target.get_widget().props.tag

if not self.check_parent(value, dropped):
Expand All @@ -596,7 +595,10 @@ def drag_drop(self, target, value, x, y):
self.ds.tags.unparent(value.id, value.parent.id)

self.ds.tags.parent(value.id, dropped.id)
self.ds.refresh_tag_stats()
self.ds.tags.tree_model.emit('items-changed', 0, 0, 0)
self.refresh_tags()



def drop_enter(self, target, x, y, user_data=None):
Expand Down Expand Up @@ -646,7 +648,8 @@ def notify_task(self, task: Task) -> None:
def on_toplevel_tag_drop(self, drop_target, tag, x, y):
if tag.parent:
self.ds.tags.unparent(tag.id, tag.parent.id)

self.ds.refresh_tag_stats()
self.refresh_tags()
try:
for expander in self.expanders:
expander.activate_action('listitem.toggle-expand')
Expand Down

0 comments on commit 133d67c

Please sign in to comment.