fixed scrolling and async loading of message of new conversation start

This commit is contained in:
Daniel Gultsch 2017-02-07 14:03:23 +01:00
parent d7198bd68c
commit 8ab166e817
2 changed files with 29 additions and 14 deletions

View file

@ -1687,6 +1687,7 @@ public class XmppConnectionService extends Service {
return conversation; return conversation;
} }
conversation = databaseBackend.findConversation(account, jid); conversation = databaseBackend.findConversation(account, jid);
final boolean loadMessagesFromDb;
if (conversation != null) { if (conversation != null) {
conversation.setStatus(Conversation.STATUS_AVAILABLE); conversation.setStatus(Conversation.STATUS_AVAILABLE);
conversation.setAccount(account); conversation.setAccount(account);
@ -1697,8 +1698,8 @@ public class XmppConnectionService extends Service {
conversation.setMode(Conversation.MODE_SINGLE); conversation.setMode(Conversation.MODE_SINGLE);
conversation.setContactJid(jid.toBareJid()); conversation.setContactJid(jid.toBareJid());
} }
conversation.addAll(0, databaseBackend.getMessages(conversation, Config.PAGE_SIZE)); databaseBackend.updateConversation(conversation);
this.databaseBackend.updateConversation(conversation); loadMessagesFromDb = conversation.messagesLoaded.compareAndSet(true,false);
} else { } else {
String conversationName; String conversationName;
Contact contact = account.getRoster().getContact(jid); Contact contact = account.getRoster().getContact(jid);
@ -1715,19 +1716,31 @@ public class XmppConnectionService extends Service {
Conversation.MODE_SINGLE); Conversation.MODE_SINGLE);
} }
this.databaseBackend.createConversation(conversation); this.databaseBackend.createConversation(conversation);
loadMessagesFromDb = false;
}
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 if (account.getXmppConnection() != null
&& account.getXmppConnection().getFeatures().mam() && account.getXmppConnection().getFeatures().mam()
&& !muc) { && !muc) {
if (query == null) { if (query == null) {
this.mMessageArchiveService.query(conversation); mMessageArchiveService.query(c);
} else { } else {
if (query.getConversation() == null) { if (query.getConversation() == null) {
this.mMessageArchiveService.query(conversation, query.getStart()); mMessageArchiveService.query(c, query.getStart());
} }
} }
} }
checkDeletedFiles(conversation); checkDeletedFiles(c);
}
});
this.conversations.add(conversation); this.conversations.add(conversation);
updateConversationUi(); updateConversationUi();
return conversation; return conversation;

View file

@ -143,8 +143,9 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
} }
activity.xmppConnectionService.loadMoreMessages(conversation, timestamp, new XmppConnectionService.OnMoreMessagesLoaded() { activity.xmppConnectionService.loadMoreMessages(conversation, timestamp, new XmppConnectionService.OnMoreMessagesLoaded() {
@Override @Override
public void onMoreMessagesLoaded(final int c, Conversation conversation) { public void onMoreMessagesLoaded(final int c, final Conversation conversation) {
if (ConversationFragment.this.conversation != conversation) { if (ConversationFragment.this.conversation != conversation) {
conversation.messagesLoaded.set(true);
return; return;
} }
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@ -172,6 +173,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
if (messageLoaderToast != null) { if (messageLoaderToast != null) {
messageLoaderToast.cancel(); messageLoaderToast.cancel();
} }
conversation.messagesLoaded.set(true);
} }
}); });
} }
@ -1263,7 +1265,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
private boolean showLoadMoreMessages(final Conversation c) { private boolean showLoadMoreMessages(final Conversation c) {
final boolean mam = hasMamSupport(c); final boolean mam = hasMamSupport(c);
final MessageArchiveService service = activity.xmppConnectionService.getMessageArchiveService(); 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) { private boolean hasMamSupport(final Conversation c) {