Imporvment

This commit is contained in:
tr1x_em committed 2025-08-14 16:05:29 +05:30
1 parent ac484bc682
commit 630d26d6f0
1 file changed
+49 -24
+49 -24
View File
@@ -294,7 +294,7 @@ class CachedNotifications(Notifications):
self.clear_all.emit() self.clear_all.emit()
def on_notification_added(self, service, notification_id: int) -> None: def on_notification_added(self, service, notification_id: int) -> None:
"""Handle notification added and cache it with enhanced metadata""" """Handle notification added and cache it with enhanced metadata - GUARANTEED STORAGE"""
# Don't call super() - we're handling this ourselves # Don't call super() - we're handling this ourselves
# Import logger at the top of the function # Import logger at the top of the function
@@ -302,7 +302,10 @@ class CachedNotifications(Notifications):
notification = self.get_notification_from_id(notification_id) notification = self.get_notification_from_id(notification_id)
if notification: if not notification:
logger.error(f"CRITICAL: Failed to get notification with ID {notification_id}")
return
# Import here to avoid circular imports # Import here to avoid circular imports
from config import data from config import data
from modules.notification.notification import ( from modules.notification.notification import (
@@ -332,8 +335,7 @@ class CachedNotifications(Notifications):
logger.debug(f"Caching new notification: ID={notification.id}, App={notification.app_name}, Summary={notification.summary[:50]}...") logger.debug(f"Caching new notification: ID={notification.id}, App={notification.app_name}, Summary={notification.summary[:50]}...")
# Cache notifications (except for ignored apps) # GUARANTEED STORAGE: Always create and store notification to history first
# DND only affects popup display, not caching
cache_id = self._next_cache_id cache_id = self._next_cache_id
self._next_cache_id += 1 self._next_cache_id += 1
self._count += 1 self._count += 1
@@ -344,6 +346,31 @@ class CachedNotifications(Notifications):
# Set cache_id directly since it's read-only property # Set cache_id directly since it's read-only property
cached_notification._cache_id = cache_id cached_notification._cache_id = cache_id
# Initialize cache metadata (will be populated below)
cached_notification.cache_metadata = {
"app_icon_cache_key": None,
"notification_image_cache_key": None,
"has_cached_image": False,
"cache_timestamp": int(time.time())
}
# IMMEDIATELY store to history before attempting any caching operations
handler_id = cached_notification.connect(
"removed-from-cache",
lambda *args: self.remove_cached_notification(notification_id=cache_id),
)
self._signal_handlers[cache_id] = handler_id
self._cached_notifications[cache_id] = cached_notification
# Save to JSON file immediately - GUARANTEED STORAGE
try:
self.cache_notifications()
logger.debug(f"GUARANTEED: Notification {cache_id} stored to history")
except Exception as e:
logger.error(f"CRITICAL: Failed to save notification {cache_id} to history: {e}")
# Now attempt asset caching (failures here won't affect history storage)
try:
# Preload assets and store cache metadata # Preload assets and store cache metadata
preload_notification_assets(notification) preload_notification_assets(notification)
@@ -352,8 +379,13 @@ class CachedNotifications(Notifications):
notification_image_cache_key = None notification_image_cache_key = None
if notification.app_icon: if notification.app_icon:
try:
app_icon_cache_key = get_cache_key(notification.app_icon, (35, 35), notification.app_name) app_icon_cache_key = get_cache_key(notification.app_icon, (35, 35), notification.app_name)
cache_notification_icon(notification.app_icon, (35, 35), notification.app_name) cache_notification_icon(notification.app_icon, (35, 35), notification.app_name)
cached_notification.cache_metadata["app_icon_cache_key"] = app_icon_cache_key
logger.debug(f"Cached app icon for notification {cache_id}")
except Exception as e:
logger.warning(f"Failed to cache app icon for notification {cache_id}: {e}")
if hasattr(notification, 'image_pixbuf'): if hasattr(notification, 'image_pixbuf'):
try: try:
@@ -364,33 +396,26 @@ class CachedNotifications(Notifications):
notification.id, image_pixbuf notification.id, image_pixbuf
) )
cache_notification_image(notification.id, image_pixbuf, (35, 35)) cache_notification_image(notification.id, image_pixbuf, (35, 35))
except (AttributeError, OSError, Exception): cached_notification.cache_metadata["notification_image_cache_key"] = notification_image_cache_key
# If temp file is gone or any other error, skip caching cached_notification.cache_metadata["has_cached_image"] = True
pass logger.debug(f"Cached notification image for notification {cache_id}")
except (AttributeError, OSError, Exception) as e:
logger.warning(f"Failed to cache notification image for notification {cache_id}: {e}")
# Store metadata in cached notification # Update cached notification with final metadata
cached_notification.cache_metadata = {
"app_icon_cache_key": app_icon_cache_key,
"notification_image_cache_key": notification_image_cache_key,
"has_cached_image": notification_image_cache_key is not None,
"cache_timestamp": int(time.time())
}
handler_id = cached_notification.connect(
"removed-from-cache",
lambda *args: self.remove_cached_notification(notification_id=cache_id),
)
self._signal_handlers[cache_id] = handler_id
self._cached_notifications[cache_id] = cached_notification self._cached_notifications[cache_id] = cached_notification
# Save updated metadata to JSON
self.cache_notifications() self.cache_notifications()
except Exception as e:
logger.error(f"Asset caching failed for notification {cache_id}, but notification is still stored: {e}")
# Always emit signals regardless of caching success
self.notify("count") self.notify("count")
self.emit("cached-notification-added", cached_notification) self.emit("cached-notification-added", cached_notification)
logger.debug(f"Successfully cached notification: Cache ID={cache_id}, Total cached={len(self._cached_notifications)}") logger.debug(f"Successfully processed notification: Cache ID={cache_id}, Total cached={len(self._cached_notifications)}")
else:
print(f"WARNING: Failed to get notification with ID {notification_id}")
def remove_cached_notification(self, notification_id: int): def remove_cached_notification(self, notification_id: int):
"""Remove the notification of given id with enhanced cache cleanup""" """Remove the notification of given id with enhanced cache cleanup"""