From 6f674b9dee607ea70c2d56ca68cff90ebce544be Mon Sep 17 00:00:00 2001
From: Arne <arne@monocles.de>
Date: Mon, 26 Aug 2024 14:15:56 +0200
Subject: [PATCH] Speed up Conversations Overview with UI thread

---
 .../ui/ConversationsOverviewFragment.java     | 33 ++++++++++---------
 1 file changed, 18 insertions(+), 15 deletions(-)

diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationsOverviewFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationsOverviewFragment.java
index 852b902d3..7ba0bff18 100644
--- a/src/main/java/eu/siacs/conversations/ui/ConversationsOverviewFragment.java
+++ b/src/main/java/eu/siacs/conversations/ui/ConversationsOverviewFragment.java
@@ -309,21 +309,22 @@ public class ConversationsOverviewFragment extends XmppFragment {
         this.mSwipeEscapeVelocity = getResources().getDimension(R.dimen.swipe_escape_velocity);
         this.binding = DataBindingUtil.inflate(inflater, R.layout.fragment_conversations_overview, container, false);
         this.binding.fab.setOnClickListener((view) -> StartConversationActivity.launch(getActivity()));
-
-        this.conversationsAdapter = new ConversationAdapter(this.activity, this.conversations);
-        if (this.conversations.size() > 0) {
-            this.activity.xmppConnectionService.updateNotificationChannels();
-        }
-        this.conversationsAdapter.setConversationClickListener((view, conversation) -> {
-            if (activity instanceof OnConversationSelected) {
-                ((OnConversationSelected) activity).onConversationSelected(conversation);
-            } else {
-                Log.w(ConversationsOverviewFragment.class.getCanonicalName(), "Activity does not implement OnConversationSelected");
+        activity.runOnUiThread(() -> {
+            this.conversationsAdapter = new ConversationAdapter(this.activity, this.conversations);
+            if (!this.conversations.isEmpty()) {
+                this.activity.xmppConnectionService.updateNotificationChannels();
             }
+            this.conversationsAdapter.setConversationClickListener((view, conversation) -> {
+                if (activity instanceof OnConversationSelected) {
+                    ((OnConversationSelected) activity).onConversationSelected(conversation);
+                } else {
+                    Log.w(ConversationsOverviewFragment.class.getCanonicalName(), "Activity does not implement OnConversationSelected");
+                }
+            });
+            this.binding.list.setAdapter(this.conversationsAdapter);
+            this.binding.list.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
+            registerForContextMenu(this.binding.list);
         });
-        this.binding.list.setAdapter(this.conversationsAdapter);
-        this.binding.list.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
-        registerForContextMenu(this.binding.list);
         if (activity.xmppConnectionService != null && !activity.xmppConnectionService.getBooleanPreference("hide_donation_snackbar", R.bool.hide_donation_snackbar)) {
             askForDonationSnackbar();
         }
@@ -521,7 +522,7 @@ public class ConversationsOverviewFragment extends XmppFragment {
             return;
         }
         this.activity.xmppConnectionService.populateWithOrderedConversations(this.conversations);
-        if (this.conversations.size() > 0) {
+        if (!this.conversations.isEmpty()) {
             this.activity.xmppConnectionService.updateNotificationChannels();
         }
         Conversation removed = this.swipedConversation.peek();
@@ -532,7 +533,9 @@ public class ConversationsOverviewFragment extends XmppFragment {
                 pendingActionHelper.execute();
             }
         }
-        this.conversationsAdapter.notifyDataSetChanged();
+        activity.runOnUiThread(() -> {
+                    this.conversationsAdapter.notifyDataSetChanged();
+                });
         ScrollState scrollState = pendingScrollState.pop();
         if (scrollState != null) {
             setScrollPosition(scrollState);