diff cagou/core/common.py @ 397:54f6a47cc60a

core (common): added a notifications counter on ContactButton and use it in JidSelector: A notifications counter is drawned above avatar if `nofifs` key is present and not empty in `data`. This is used in JidSelector to show counter when necessary with opened chats.
author Goffi <goffi@goffi.org>
date Sun, 09 Feb 2020 23:47:29 +0100
parents 442756495a96
children f7476818f9fb
line wrap: on
line diff
--- a/cagou/core/common.py	Sun Feb 09 23:47:29 2020 +0100
+++ b/cagou/core/common.py	Sun Feb 09 23:47:29 2020 +0100
@@ -49,16 +49,32 @@
     pass
 
 
+class NotifLabel(Label):
+    pass
+
+
 class ContactItem(BoxLayout):
     """An item from ContactList
 
-    The item will drawn as an icon (JID avatar) with its jid below
+    The item will drawn as an icon (JID avatar) with its jid below.
+    If "notifs" are present in data, a notification counter will be drawn above the
+    avatar.
     """
     base_width = dp(150)
+    avatar_layout = properties.ObjectProperty()
+    avatar = properties.ObjectProperty()
     profile = properties.StringProperty()
     data = properties.DictProperty()
     jid = properties.StringProperty('')
 
+    def on_kv_post(self, __):
+        if self.data and self.data.get('notifs'):
+            notif = NotifLabel(
+                pos_hint={"right": 0.8, "y": 0},
+                text=str(len(self.data['notifs']))
+            )
+            self.avatar_layout.add_widget(notif)
+
 
 class ContactButton(ButtonBehavior, ContactItem):
     pass
@@ -225,10 +241,18 @@
 
         for wid in opened_chats:
             contact_list = G.host.contact_lists[wid.profile]
+            data=contact_list.getItem(wid.target)
+            notifs = list(G.host.getNotifs(wid.target, profile=wid.profile))
+            if notifs:
+                # we shallow copy the dict to have the notification displayed only with
+                # opened chats (otherwise, the counter would appear on each other
+                # instance of ContactButton for this entity, i.e. in roster too).
+                data = data.copy()
+                data['notifs'] = notifs
             try:
                 item = ContactButton(
                     jid=wid.target,
-                    data=contact_list.getItem(wid.target),
+                    data=data,
                     profile=wid.profile,
                 )
             except Exception as e: