From b2643f5967c2b300f946da4d42f84ba3275cbda6 Mon Sep 17 00:00:00 2001
From: Nicolas Vengeon <nicolas.vengeon@savoirfairelinux.com>
Date: Fri, 3 Feb 2023 15:54:28 -0500
Subject: [PATCH] messagelistmodel: correctly search for index in model

Iterate through CPP elements and use positionView(ListView.Center)
as other approach seems bugguy or slower.

Change-Id: I43879969ccb457166879a156efb482e77ff07d6b
---
 src/app/commoncomponents/SBSMessageBase.qml     | 15 +++++++++++++++
 src/app/mainview/components/MessageListView.qml | 11 ++---------
 src/app/messagesadapter.cpp                     | 17 +++++++++++++++++
 src/app/messagesadapter.h                       |  5 ++---
 src/libclient/api/interaction.h                 |  7 +++++++
 src/libclient/messagelistmodel.cpp              |  6 ++++++
 src/libclient/messagelistmodel.h                |  2 ++
 7 files changed, 51 insertions(+), 12 deletions(-)

diff --git a/src/app/commoncomponents/SBSMessageBase.qml b/src/app/commoncomponents/SBSMessageBase.qml
index 686b710cb..321df2ece 100644
--- a/src/app/commoncomponents/SBSMessageBase.qml
+++ b/src/app/commoncomponents/SBSMessageBase.qml
@@ -54,6 +54,7 @@ Control {
     readonly property real msgRadius: 20
     readonly property real hPadding: JamiTheme.sbsMessageBasePreferredPadding
     property bool textHovered: false
+    property alias replyAnimation: selectAnimation
     width: ListView.view ? ListView.view.width : 0
     height: mainColumnLayout.implicitHeight
 
@@ -353,6 +354,20 @@ Control {
                         to: 0
                         duration: JamiTheme.longFadeDuration
                     }
+                    PropertyAnimation {
+                        properties: "opacity"
+                        target: opacityMask
+                        from: 0
+                        to: 1
+                        duration: JamiTheme.longFadeDuration
+                    }
+                    PropertyAnimation {
+                        properties: "opacity"
+                        target: opacityMask
+                        from: 1
+                        to: 0
+                        duration: JamiTheme.longFadeDuration
+                    }
                 }
 
                 OpacityMask {
diff --git a/src/app/mainview/components/MessageListView.qml b/src/app/mainview/components/MessageListView.qml
index 2eb6a94d5..81d33ace1 100644
--- a/src/app/mainview/components/MessageListView.qml
+++ b/src/app/mainview/components/MessageListView.qml
@@ -152,16 +152,9 @@ JamiListView {
 
     Connections {
         target: CurrentConversation
-        function onIdChanged() { fadeAnimation.start() }
         function onScrollTo(id) {
-            var idx = -1
-            for (var i = 1; i < root.count; i++) {
-                var delegate = root.itemAtIndex(i)
-                if (delegate && delegate.id === id) {
-                    idx = i
-                }
-            }
-            positionViewAtIndex(idx, ListView.Center)
+            var idx = MessagesAdapter.getMessageIndexFromId(id)
+            positionViewAtIndex(idx, ListView.Visible)
         }
     }
 
diff --git a/src/app/messagesadapter.cpp b/src/app/messagesadapter.cpp
index 2bc816d37..a13aaa446 100644
--- a/src/app/messagesadapter.cpp
+++ b/src/app/messagesadapter.cpp
@@ -761,6 +761,23 @@ MessagesAdapter::getConvMedias()
     }
 }
 
+int
+MessagesAdapter::getMessageIndexFromId(QString& id)
+{
+    const QString& convId = lrcInstance_->get_selectedConvUid();
+    const auto& conversation = lrcInstance_->getConversationFromConvUid(convId);
+    auto allInteractions = conversation.interactions.get();
+    int index = 0;
+    for (auto it = allInteractions->rbegin(); it != allInteractions->rend(); it++) {
+        if (interaction::isDisplayedInChatview(it->second.type)) {
+            if (it->first == id)
+                return index;
+            index++;
+        }
+    }
+    return -1;
+}
+
 MessageListModel*
 MessagesAdapter::getMsgListSourceModel() const
 {
diff --git a/src/app/messagesadapter.h b/src/app/messagesadapter.h
index 8913d89a2..b01b4496d 100644
--- a/src/app/messagesadapter.h
+++ b/src/app/messagesadapter.h
@@ -42,9 +42,7 @@ public:
         auto index = sourceModel()->index(sourceRow, 0, sourceParent);
         auto type = static_cast<interaction::Type>(
             sourceModel()->data(index, MessageList::Role::Type).toInt());
-        return type != interaction::Type::MERGE && type != interaction::Type::EDITED
-               && type != interaction::Type::REACTION && type != interaction::Type::VOTE
-               && type != interaction::Type::UPDATE_PROFILE && type != interaction::Type::INVALID;
+        return interaction::isDisplayedInChatview(type);
     };
     bool lessThan(const QModelIndex& left, const QModelIndex& right) const override
     {
@@ -136,6 +134,7 @@ protected:
     Q_INVOKABLE QVariant dataForInteraction(const QString& interactionId,
                                             int role = Qt::DisplayRole) const;
     Q_INVOKABLE void getConvMedias();
+    Q_INVOKABLE int getMessageIndexFromId(QString& id);
 
     // Run corrsponding js functions, c++ to qml.
     void setMessagesImageContent(const QString& path, bool isBased64 = false);
diff --git a/src/libclient/api/interaction.h b/src/libclient/api/interaction.h
index 3abfb2dfb..9e928ceaa 100644
--- a/src/libclient/api/interaction.h
+++ b/src/libclient/api/interaction.h
@@ -47,6 +47,13 @@ enum class Type {
     COUNT__
 };
 Q_ENUM_NS(Type)
+static inline bool
+isDisplayedInChatview(const Type& type)
+{
+    return type != interaction::Type::MERGE && type != interaction::Type::EDITED
+           && type != interaction::Type::REACTION && type != interaction::Type::VOTE
+           && type != interaction::Type::UPDATE_PROFILE && type != interaction::Type::INVALID;
+}
 
 static inline const QString
 to_string(const Type& type)
diff --git a/src/libclient/messagelistmodel.cpp b/src/libclient/messagelistmodel.cpp
index b9c98b188..6c2171937 100644
--- a/src/libclient/messagelistmodel.cpp
+++ b/src/libclient/messagelistmodel.cpp
@@ -152,6 +152,12 @@ MessageListModel::end() const
     return interactions_.end();
 }
 
+reverseIterator
+MessageListModel::rend()
+{
+    return interactions_.rend();
+}
+
 constIterator
 MessageListModel::cend() const
 {
diff --git a/src/libclient/messagelistmodel.h b/src/libclient/messagelistmodel.h
index 452be1b8a..302f94fe6 100644
--- a/src/libclient/messagelistmodel.h
+++ b/src/libclient/messagelistmodel.h
@@ -96,6 +96,8 @@ public:
     interaction::Info& operator[](const QString& messageId);
     iterator end();
     constIterator end() const;
+    reverseIterator rend();
+
     constIterator cend() const;
     iterator begin();
     constIterator begin() const;
-- 
GitLab