From 538481a9f899f995f24416931fd5a191121cb6db Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Mon, 20 Dec 2021 09:50:48 -0500
Subject: [PATCH] smartlist: improve cache cleaning and avoid glitches

Interaction's cache is cleaned to avoid to store too much informations
and to refresh preferences for the chatview when necessary.
However, this was called too much times. We only need to clear cache
when the settings are changing or if we need to remove a lot of
conversations. This patch uses the SmartListItem to determine
what conversation needs to be cleaned.

Change-Id: I731bb9e7d41b140bbc2607800c1a8e0a1eff3244
---
 src/lrcinstance.cpp                           |  6 +-----
 .../components/SmartListItemDelegate.qml      | 19 +++++++++++++++++--
 src/utilsadapter.cpp                          | 12 ++++++++++++
 src/utilsadapter.h                            |  1 +
 4 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/src/lrcinstance.cpp b/src/lrcinstance.cpp
index 8f01703d7..0e3bd09a2 100644
--- a/src/lrcinstance.cpp
+++ b/src/lrcinstance.cpp
@@ -433,11 +433,7 @@ void
 LRCInstance::set_selectedConvUid(QString selectedConvUid)
 {
     if (selectedConvUid_ != selectedConvUid) {
-        auto& accInfo = accountModel().getAccountInfo(get_currentAccountId());
-        auto& convModel = accInfo.conversationModel;
-        convModel->clearInteractionsCache(selectedConvUid_);
-
         selectedConvUid_ = selectedConvUid;
         Q_EMIT selectedConvUidChanged();
     }
-}
+}
\ No newline at end of file
diff --git a/src/mainview/components/SmartListItemDelegate.qml b/src/mainview/components/SmartListItemDelegate.qml
index 6797e4cc7..612e95061 100644
--- a/src/mainview/components/SmartListItemDelegate.qml
+++ b/src/mainview/components/SmartListItemDelegate.qml
@@ -33,8 +33,23 @@ ItemDelegate {
     width: ListView.view.width
     height: JamiTheme.smartListItemHeight
 
-    function convUid() {
-        return UID
+    property string accountId: ""
+    property string convId: ""
+
+    onVisibleChanged: {
+        if (visible)
+            return
+        UtilsAdapter.clearInteractionsCache(root.accountId, root.convId)
+    }
+
+    Component.onCompleted: {
+        // Store to avoid undefined at the end
+        root.accountId = CurrentAccount.id
+        root.convId = UID
+    }
+
+    Component.onDestruction: {
+        UtilsAdapter.clearInteractionsCache(root.accountId, root.convId)
     }
 
     RowLayout {
diff --git a/src/utilsadapter.cpp b/src/utilsadapter.cpp
index 0352cd76b..2d470aa4b 100644
--- a/src/utilsadapter.cpp
+++ b/src/utilsadapter.cpp
@@ -399,4 +399,16 @@ UtilsAdapter::monitor(const bool& continuous)
                                    Q_EMIT debugMessageReceived(data);
                                });
     lrcInstance_->monitor(continuous);
+}
+
+void
+UtilsAdapter::clearInteractionsCache(const QString& accountId, const QString& convId)
+{
+    if (lrcInstance_->get_selectedConvUid() != convId) {
+        try {
+            auto& accInfo = lrcInstance_->accountModel().getAccountInfo(accountId);
+            auto& convModel = accInfo.conversationModel;
+            convModel->clearInteractionsCache(convId);
+        } catch (...) {}
+    }
 }
\ No newline at end of file
diff --git a/src/utilsadapter.h b/src/utilsadapter.h
index 2f595bab4..7a92b68db 100644
--- a/src/utilsadapter.h
+++ b/src/utilsadapter.h
@@ -89,6 +89,7 @@ public:
     Q_INVOKABLE void setRunOnStartUp(bool state);
     Q_INVOKABLE void setDownloadPath(QString dir);
     Q_INVOKABLE void monitor(const bool& continuous);
+    Q_INVOKABLE void clearInteractionsCache(const QString& accountId, const QString& convUid);
 
 Q_SIGNALS:
     void debugMessageReceived(const QString& message);
-- 
GitLab