Fix iterative load to not clear and re-draw items.

This commit is contained in:
Sergio Martínez Portela 2022-03-27 23:44:58 +02:00
parent d977290819
commit 5846868f9f
2 changed files with 52 additions and 23 deletions

24
main.py
View File

@ -58,7 +58,10 @@ class MainWindow(Gtk.Window):
self.set_child(self.scrollview) self.set_child(self.scrollview)
self.loading += 1 self.loading += 1
self.task_manager.get_task_list(self.on_task_list_ready) self.task_manager.get_task_list(
self.on_task_list_update,
self.on_task_list_ready,
)
## Rendering ## Rendering
def build_agenda_task_row(self, task): def build_agenda_task_row(self, task):
@ -83,7 +86,7 @@ class MainWindow(Gtk.Window):
row.set_child(hbox) row.set_child(hbox)
return row return row
def on_ready(self): def on_ready(self):
self.loading -= 1 self.loading -= 1
@ -94,21 +97,14 @@ class MainWindow(Gtk.Window):
self.progress_spinner.stop() self.progress_spinner.stop()
## Callbacks ## Callbacks
def on_task_list_ready(self, agenda): def on_task_list_update(self, new_rows):
# TODO: Avoid reconstructing the whole list every time for item in new_rows.with_hour:
i = 0
child = self.task_list.get_first_child()
while child is not None:
was = child
child = child.get_next_sibling()
i += 1
self.task_list.remove(was)
for item in agenda.with_hour:
self.task_list.append(self.build_agenda_task_row(item)) self.task_list.append(self.build_agenda_task_row(item))
for item in agenda.no_hour: for item in new_rows.no_hour:
self.task_list.append(self.build_agenda_task_row(item)) self.task_list.append(self.build_agenda_task_row(item))
def on_task_list_ready(self, success):
self.on_ready() self.on_ready()
## Reactions ## Reactions

View File

@ -91,22 +91,19 @@ class TaskManager:
self.docs = docs self.docs = docs
def get_task_list(self, callback): def get_task_list(self, progress_callback, complete_callback):
def aux(): def aux():
if self.docs is None: if self.docs is None:
last_result = None last_result = None
# No docs read yet, load them iteratively
for doc in self.load(): for doc in self.load():
result = self.get_agenda() result = self.get_agenda_from_doc(doc)
if ((last_result is None) GObject.idle_add(progress_callback, result)
or (len(result.with_hour) != len(last_result.with_hour))
or (len(result.no_hour) != len(last_result.no_hour))):
print("Loaded:", doc._path)
GObject.idle_add(callback, result)
print("Load completed")
else: else:
result = self.get_agenda() result = self.get_agenda()
print("Result", result) print("Result", result)
GObject.idle_add(callback, result) GObject.idle_add(progress_callback, result)
GObject.idle_add(complete_callback, True)
thread = threading.Thread(target=aux) thread = threading.Thread(target=aux)
thread.start() thread.start()
@ -153,3 +150,39 @@ class TaskManager:
with_hour=sorted(items_with_hour, key=lambda x: x.scheduled.time), with_hour=sorted(items_with_hour, key=lambda x: x.scheduled.time),
no_hour=other_items, no_hour=other_items,
) )
def get_agenda_from_doc(self, doc: OrgDoc) -> Agenda:
headline_count = 0
items_in_agenda = []
now = datetime.now()
for hl in doc.getAllHeadlines():
headline_count += 1
if (
hl.scheduled
and isinstance(hl.scheduled, OrgTime)
and hl.scheduled.time.active
):
if is_today(hl.scheduled):
items_in_agenda.append(hl)
elif (hl.scheduled.time.to_datetime() < now) and hl.is_todo:
items_in_agenda.append(hl)
items_with_hour = [
item
for item in items_in_agenda
if item.scheduled and is_today(item.scheduled) and item.scheduled.time.hour
]
other_items = [
item
for item in items_in_agenda
if not (
item.scheduled and is_today(item.scheduled) and item.scheduled.time.hour
)
]
return Agenda(
with_hour=sorted(items_with_hour, key=lambda x: x.scheduled.time),
no_hour=other_items,
)