changeset 406:03554ad70846

common (jidSelector): replace implitict_update mechanism by real-time update: instead of having the possibility to (de)activate implicit update, real time update of items is now done. For now only the notifications and opened chat items are updated in real time.
author Goffi <goffi@goffi.org>
date Wed, 12 Feb 2020 20:02:58 +0100
parents 84ff5c917064
children 364d2c8eb476
files cagou/core/common.py cagou/plugins/plugin_wid_chat.kv cagou/plugins/plugin_wid_chat.py
diffstat 3 files changed, 48 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/core/common.py	Wed Feb 12 20:02:58 2020 +0100
+++ b/cagou/core/common.py	Wed Feb 12 20:02:58 2020 +0100
@@ -211,9 +211,9 @@
     #      common_widgets.CategorySeparator)
     #    - a callable, which must return an iterable of kwargs for ContactButton
     to_show = properties.ListProperty(['roster'])
-    # if True, update() is called automatically when widget is created
-    # if False, you'll have to call update() at least once manually
-    implicit_update = properties.ObjectProperty(True)
+
+    # TODO: roster and bookmarks must be updated in real time, like for opened_chats
+
 
     def __init__(self, **kwargs):
         self.register_event_type('on_select')
@@ -224,23 +224,63 @@
         super().__init__(**kwargs)
 
     def on_kv_post(self, wid):
-        if self.implicit_update:
-            self.update()
+        self.update()
 
     def on_select(self, wid):
         pass
 
     def on_parent(self, wid, parent):
         if parent is None:
-            log.debug("removing contactsFilled listener")
+            log.debug("removing listeners")
             G.host.removeListener("contactsFilled", self.onContactsFilled)
+            G.host.removeListener("notification", self.onNotification)
+            G.host.removeListener("notificationsClear", self.onNotificationsClear)
+            G.host.removeListener(
+                "widgetNew", self.onWidgetNew, ignore_missing=True)
+            G.host.removeListener(
+                "widgetDeleted", self.onWidgetDeleted, ignore_missing=True)
         else:
+            log.debug("adding listeners")
             G.host.addListener("contactsFilled", self.onContactsFilled)
+            G.host.addListener("notification", self.onNotification)
+            G.host.addListener("notificationsClear", self.onNotificationsClear)
 
     def onContactsFilled(self, profile):
         log.debug("onContactsFilled event received")
         self.update()
 
+    def onNotification(self, entity, notification_data, profile):
+        for item in self.items_map.get(entity.bare, []):
+            notifs = list(G.host.getNotifs(entity.bare, profile=profile))
+            item.badge_text = str(len(notifs))
+
+    def onNotificationsClear(self, entity, type_, profile):
+        for item in self.items_map.get(entity.bare, []):
+            item.badge_text = ''
+
+    def onWidgetNew(self, wid):
+        if not isinstance(wid, quick_chat.QuickChat):
+            return
+        item = self.getItemFromWid(wid)
+        if item is None:
+            return
+        idx = 0
+        for child in self.opened_chats.children:
+            if isinstance(child, self.item_class) and child < item:
+                break
+            idx+=1
+        self.opened_chats.add_widget(item, index=idx)
+
+    def onWidgetDeleted(self, wid):
+        if not isinstance(wid, quick_chat.QuickChat):
+            return
+
+        for child in self.opened_chats.children:
+            if not isinstance(child, self.item_class):
+                continue
+            if child.jid.bare == wid.target.bare:
+                self.opened_chats.remove_widget(child)
+                break
 
     def _createItem(self, **kwargs):
         item = self.item_class(**kwargs)
@@ -302,6 +342,8 @@
         return item
 
     def addOpenedChatsItems(self):
+        G.host.addListener("widgetNew", self.onWidgetNew)
+        G.host.addListener("widgetDeleted", self.onWidgetDeleted)
         self.opened_chats = category_layout = self.addCategoryLayout(_("Opened chats"))
         widgets = sorted(G.host.widgets.getWidgets(
             quick_chat.QuickChat,
--- a/cagou/plugins/plugin_wid_chat.kv	Wed Feb 12 20:02:58 2020 +0100
+++ b/cagou/plugins/plugin_wid_chat.kv	Wed Feb 12 20:02:58 2020 +0100
@@ -221,8 +221,6 @@
     jid_selector: jid_selector
     JidSelector:
         id: jid_selector
-        # we call update() explicitly in on_pre_enter
-        implicit_update: False
         on_select: root.on_select(args[1])
         to_show: ["opened_chats", "roster", "bookmarks"]
 
--- a/cagou/plugins/plugin_wid_chat.py	Wed Feb 12 20:02:58 2020 +0100
+++ b/cagou/plugins/plugin_wid_chat.py	Wed Feb 12 20:02:58 2020 +0100
@@ -434,7 +434,6 @@
         sel_screen = Screen(name='chat_selector')
         chat_selector = ChatSelector(profile=self.profile)
         sel_screen.add_widget(chat_selector)
-        sel_screen.bind(on_pre_enter=chat_selector.on_pre_enter)
         screen_manager.add_widget(sel_screen)
         if self.show_chat_selector:
             transition = screen_manager.transition
@@ -916,9 +915,6 @@
     plugin_info_class = Chat
     use_header_input = True
 
-    def on_pre_enter(self, screen):
-        self.jid_selector.update()
-
     def on_select(self, contact_button):
         contact_jid = jid.JID(contact_button.jid)
         plugin_info = G.host.getPluginInfo(main=Chat)