[PATCH tooi] EventList: allow existing events to be updated
Export this patch
Add a refresh_event() method to EventListItem which updates the event.
Currently this is only used to update the timestamp when relative
timestamps are enabled, but in future we can use this to update
favourite/boost status as well.
Add refresh_events() to EventList which calls refresh_event() on all
EventListItems, and call it from refresh_timeline() in TimelineTab.
---
tooi/tabs/timeline.py | 3 +++
tooi/widgets/event_list.py | 37 +++++++++++++++++++++++--------------
2 files changed, 26 insertions(+), 14 deletions(-)
diff --git a/tooi/tabs/timeline.py b/tooi/tabs/timeline.py
index 83f090a..183ca44 100644
--- a/tooi/tabs/timeline.py
+++ b/tooi/tabs/timeline.py
@@ -99,6 +99,9 @@ class TimelineTab(TabPane):
self.event_list.prepend_events(newevents)
self.post_message(ShowStatusMessage())
+ # Make sure older events are up to date
+ self.query_one(EventList).refresh_events()
+
async def fetch_timeline(self):
self.generator = self.timeline.fetch()
diff --git a/tooi/widgets/event_list.py b/tooi/widgets/event_list.py
index 8300f34..80bbce7 100644
--- a/tooi/widgets/event_list.py
+++ b/tooi/widgets/event_list.py
@@ -74,6 +74,10 @@ class EventList(ListView):
if item.event.id == event_id:
self.index = i
+ def refresh_events(self):
+ for item in self.query(EventListItem):
+ item.refresh_event()
+
@property
def count(self):
return len(self)
@@ -124,20 +128,7 @@ class EventListItem(ListItem, can_focus=True):
self.ctx = get_context()
def compose(self):
- if self.ctx.config.relative_timestamps:
- diff = datetime.now(timezone.utc) - self.event.created_at
- if (days := diff / timedelta(days=1)) >= 1:
- timestamp = f"{int(days):>2}d"
- elif (hours := diff / timedelta(hours=1)) >= 1:
- timestamp = f"{int(hours):>2}h"
- elif (minutes := diff / timedelta(minutes=1)) >= 1:
- timestamp = f"{int(minutes):>2}m"
- else:
- seconds = diff / timedelta(seconds=1)
- timestamp = f"{int(seconds):>2}s"
- else:
- timestamp = format_datetime(self.event.created_at)
-
+ timestamp = self.format_timestamp()
yield Label(timestamp, classes="event_list_timestamp")
# TODO: These should probably be implemented in a way that doesn't
@@ -156,6 +147,24 @@ class EventListItem(ListItem, can_focus=True):
case _:
yield from self.compose_unknown()
+ def format_timestamp(self):
+ if self.ctx.config.relative_timestamps:
+ diff = datetime.now(timezone.utc) - self.event.created_at
+ if (days := diff / timedelta(days=1)) >= 1:
+ return f"{int(days):>2}d"
+ elif (hours := diff / timedelta(hours=1)) >= 1:
+ return f"{int(hours):>2}h"
+ elif (minutes := diff / timedelta(minutes=1)) >= 1:
+ return f"{int(minutes):>2}m"
+ else:
+ seconds = diff / timedelta(seconds=1)
+ return f"{int(seconds):>2}s"
+ else:
+ return format_datetime(self.event.created_at)
+
+ def refresh_event(self):
+ self.query_one(".event_list_timestamp").update(self.format_timestamp())
+
def _format_account_name(self, account):
ctx = get_context()
acct = account.acct
--
2.43.0