Skip to content

Commit

Permalink
Update FSM diagram (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeklat authored Nov 13, 2023
1 parent 4449509 commit 69cd390
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 43 deletions.
46 changes: 24 additions & 22 deletions documentation/task state flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@

```mermaid
graph TD
start([START]) --> generic_task{generic_task?}
generic_task -->|None| deleted{Deleted?}
generic_task -->|found| state_in{state in<br/>HIDDEN,<br/>HABITICA_*<br/>?}
deleted -->|False| todoist_task_checked{todoist_task<br/>checked?}
deleted -->|True| initial{initial?}
todoist_task_checked -->|False| todoist_active[TODOIST ACTIVE]
todoist_task_checked -->|True| initial
todoist_active --> habitica_new[HABITICA NEW]
habitica_new --> habitica_created[HABITICA CREATED]
habitica_created --> habitica_finished[HABITICA FINISHED]
habitica_finished --> recurring{recurring?}
recurring -->|False| hidden([HIDDEN])
recurring -->|True| todoist_active
initial -->|True| hidden
initial -->|False| should_score_points{should<br/>score<br/>points?}
should_score_points -->|False| todoist_active
should_score_points -->|True| assigned_to_me{Assigned<br/>to me?}
assigned_to_me -->|True| habitica_new
assigned_to_me -->|False| hidden
state_in -->|True| skip([SKIP])
state_in -->|False| deleted
```
start([START]) --> TodoistNew
TodoistNew --> is_td_deleted{Is TD task\ndeleted?}
is_td_deleted -- No --> is_td_checked{Is TD task\nchecked?}
is_td_deleted -- Yes --> Hidden
is_td_checked -- No --> TodoistActive
is_td_checked -- Yes --> is_td_initial_sync{Initial\nsync?}
is_td_initial_sync -- No --> TodoistActive
is_td_initial_sync -- Yes --> Hidden
TodoistActive --> is_td_deleted_active{Is TD task\ndeleted?}
is_td_deleted_active -- Yes --> Hidden
is_td_deleted_active -- No --> should_td_score_points{Should\nTD task score\npoints?}
should_td_score_points -- Yes --> is_td_owned_by_me{Is TD task\nowned by me?}
should_td_score_points -- No --> TodoistActive
is_td_owned_by_me -- Yes --> HabiticaNew
is_td_owned_by_me -- No --> Hidden
HabiticaNew --> HabiticaCreated
HabiticaCreated --> HabiticaFinished
HabiticaFinished --> is_task_recurring{Is task\nrecurring?}
is_task_recurring -- Yes --> is_task_completed{Is task\ncompleted\nforever?}
is_task_completed -- No --> TodoistActive
is_task_completed -- Yes --> Hidden
is_task_recurring -- No --> Hidden
Hidden --> finish([END])
```
21 changes: 7 additions & 14 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,12 @@ def _owned_by_me(self) -> bool:

class StateTodoistNew(StateTodoist):
def next_state(self) -> None:
if self.todoist_task.is_deleted:
self._set_state(StateHidden)
elif not self.todoist_task.checked:
self._set_state(StateTodoistActive)
elif self.context.initial_sync: # ignore completed tasks during initial sync
if self.todoist_task.is_deleted or (self.todoist_task.checked and self.context.initial_sync):
# Task has been already deleted or checked previously and this is an initial sync
self._set_state(StateHidden)
elif self.todoist_task.checked: # task has been completed since last sync
if self._owned_by_me(): # task is owned by me, it should score points
self._set_state(StateHabiticaNew)
else: # task is owned by someone else, it should not score points
self._set_state(StateHidden)
else:
self._set_state(StateTodoistActive)
# task has not been completed ever or since last sync
self._set_state(StateHabiticaNew)


class StateTodoistActive(StateTodoist):
Expand Down Expand Up @@ -201,7 +194,7 @@ def __init__(self):
settings.sync_delay_seconds, "Next check in {delay:.0f} seconds."
)

def run_forever(self):
def run_forever(self) -> None:
while True:
try:
self._todoist.sync()
Expand All @@ -221,7 +214,7 @@ def set_state(self, state: FSMState) -> None:
state.generic_task.state = new_state
self._task_cache.save_task(state.generic_task)

def create_habitica_task(self, generic_task: GenericTask):
def create_habitica_task(self, generic_task: GenericTask) -> None:
previous_habitica_id = generic_task.habitica_task_id
generic_task.habitica_task_id = self.habitica.create_task(
generic_task.content, TODOIST_PRIORITY_TO_HABITICA_DIFFICULTY[generic_task.priority]
Expand All @@ -236,7 +229,7 @@ def initial_sync(self) -> bool:
def todoist_user_id(self) -> str | None:
return self._todoist_user_id

def _next_tasks_state(self):
def _next_tasks_state(self) -> None:
for todoist_task in self._todoist.state.items.values(): # pylint: disable=no-member
generic_task = self._task_cache.get_task_by_todoist_task_id(todoist_task)

Expand Down
9 changes: 4 additions & 5 deletions src/models/generic_task.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Self

from models.todoist import TodoistTask

Expand All @@ -16,9 +15,9 @@ class GenericTask:
habitica_task_id: str | None = None
completed_at: str | None = None

@staticmethod
def from_todoist_task(todoist_task: TodoistTask, state: str) -> GenericTask:
return GenericTask(
@classmethod
def from_todoist_task(cls, todoist_task: TodoistTask, state: str) -> Self:
return cls(
todoist_task_id=todoist_task.id,
content=todoist_task.content,
priority=todoist_task.priority,
Expand Down
4 changes: 2 additions & 2 deletions src/tasks_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, habitica_dirty_states: set[str]):
self._task_cache = tiny_db.table("tasks_cache")
self._log = logging.getLogger(self.__class__.__name__)

def __len__(self):
def __len__(self) -> int:
return len(self._task_cache)

def get_task_by_todoist_task_id(self, todoist_task: TodoistTask) -> GenericTask | None:
Expand All @@ -34,7 +34,7 @@ def get_task_by_todoist_task_id(self, todoist_task: TodoistTask) -> GenericTask

return GenericTask(**task) if task else None

def save_task(self, generic_task: GenericTask, previous_habitica_id: str | None = ""):
def save_task(self, generic_task: GenericTask, previous_habitica_id: str | None = "") -> None:
if previous_habitica_id != "":
habitica_id = previous_habitica_id
else:
Expand Down

0 comments on commit 69cd390

Please sign in to comment.