Skip to content

Commit

Permalink
refactor: better sort function
Browse files Browse the repository at this point in the history
  • Loading branch information
kraanzu committed Feb 19, 2023
1 parent 6484fbe commit 0420414
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions dooit/api/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def __init__(
self.workspaces: List[Workspace] = []
self.todos: List[Todo] = []

@property
def kind(self):
return self.__class__.__name__.lower()

@property
def path(self):
"""
Expand All @@ -83,6 +87,8 @@ def _get_children(self, kind: str) -> List:
"""
Get children list (workspace/todo)
"""
if kind not in ["workspace", "todo"]:
raise TypeError(f"Cannot perform this operation for type {kind}")

return self.workspaces if kind.lower() == "workspace" else self.todos

Expand Down Expand Up @@ -225,13 +231,13 @@ def drop(self, kind: str) -> None:
if self.parent:
self.parent.remove_child(kind, self.name)

def sort(self, kind: str, attr: str) -> None:
def sort(self, attr: str) -> None:
"""
Sort the children based on specific attr
"""

if self.parent:
children = self.parent._get_children(kind)
children = self.parent._get_children(self.kind)
children.sort(key=lambda x: getattr(x, f"_{attr}").get_sortable())

def commit(self) -> Dict[str, Any]:
Expand Down
2 changes: 1 addition & 1 deletion dooit/ui/widgets/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ async def toggle_expand_parent(self) -> None:

def sort(self, attr: str) -> None:
curr = self.item.path
self.item.sort(self.model_kind, attr)
self.item.sort(attr)
self._refresh_rows()
self.current = self._rows[curr].index
self.commit()

0 comments on commit 0420414

Please sign in to comment.