From 8ab166e8176076129591aa80ef7a53a51ad81141 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 7 Feb 2017 14:03:23 +0100 Subject: [PATCH] fixed scrolling and async loading of message of new conversation start --- .../services/XmppConnectionService.java | 37 +++++++++++++------ .../ui/ConversationFragment.java | 6 ++- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index f53d62d3d..6b91e1cc6 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -1687,6 +1687,7 @@ public class XmppConnectionService extends Service { return conversation; } conversation = databaseBackend.findConversation(account, jid); + final boolean loadMessagesFromDb; if (conversation != null) { conversation.setStatus(Conversation.STATUS_AVAILABLE); conversation.setAccount(account); @@ -1697,8 +1698,8 @@ public class XmppConnectionService extends Service { conversation.setMode(Conversation.MODE_SINGLE); conversation.setContactJid(jid.toBareJid()); } - conversation.addAll(0, databaseBackend.getMessages(conversation, Config.PAGE_SIZE)); - this.databaseBackend.updateConversation(conversation); + databaseBackend.updateConversation(conversation); + loadMessagesFromDb = conversation.messagesLoaded.compareAndSet(true,false); } else { String conversationName; Contact contact = account.getRoster().getContact(jid); @@ -1715,19 +1716,31 @@ public class XmppConnectionService extends Service { Conversation.MODE_SINGLE); } this.databaseBackend.createConversation(conversation); + loadMessagesFromDb = false; } - if (account.getXmppConnection() != null - && account.getXmppConnection().getFeatures().mam() - && !muc) { - if (query == null) { - this.mMessageArchiveService.query(conversation); - } else { - if (query.getConversation() == null) { - this.mMessageArchiveService.query(conversation, query.getStart()); + final Conversation c = conversation; + mDatabaseExecutor.execute(new Runnable() { + @Override + public void run() { + if (loadMessagesFromDb) { + c.addAll(0, databaseBackend.getMessages(c, Config.PAGE_SIZE)); + updateConversationUi(); + c.messagesLoaded.set(true); } + if (account.getXmppConnection() != null + && account.getXmppConnection().getFeatures().mam() + && !muc) { + if (query == null) { + mMessageArchiveService.query(c); + } else { + if (query.getConversation() == null) { + mMessageArchiveService.query(c, query.getStart()); + } + } + } + checkDeletedFiles(c); } - } - checkDeletedFiles(conversation); + }); this.conversations.add(conversation); updateConversationUi(); return conversation; diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index dfe2c2704..970fbee12 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -143,8 +143,9 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa } activity.xmppConnectionService.loadMoreMessages(conversation, timestamp, new XmppConnectionService.OnMoreMessagesLoaded() { @Override - public void onMoreMessagesLoaded(final int c, Conversation conversation) { + public void onMoreMessagesLoaded(final int c, final Conversation conversation) { if (ConversationFragment.this.conversation != conversation) { + conversation.messagesLoaded.set(true); return; } activity.runOnUiThread(new Runnable() { @@ -172,6 +173,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa if (messageLoaderToast != null) { messageLoaderToast.cancel(); } + conversation.messagesLoaded.set(true); } }); } @@ -1263,7 +1265,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa private boolean showLoadMoreMessages(final Conversation c) { final boolean mam = hasMamSupport(c); final MessageArchiveService service = activity.xmppConnectionService.getMessageArchiveService(); - return mam && (c.getLastClearHistory() != 0 || (c.countMessages() == 0 && c.hasMessagesLeftOnServer() && !service.queryInProgress(c))); + return mam && (c.getLastClearHistory() != 0 || (c.countMessages() == 0 && c.messagesLoaded.get() && c.hasMessagesLeftOnServer() && !service.queryInProgress(c))); } private boolean hasMamSupport(final Conversation c) {