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;
} }
if (account.getXmppConnection() != null final Conversation c = conversation;
&& account.getXmppConnection().getFeatures().mam() mDatabaseExecutor.execute(new Runnable() {
&& !muc) { @Override
if (query == null) { public void run() {
this.mMessageArchiveService.query(conversation); if (loadMessagesFromDb) {
} else { c.addAll(0, databaseBackend.getMessages(c, Config.PAGE_SIZE));
if (query.getConversation() == null) { updateConversationUi();
this.mMessageArchiveService.query(conversation, query.getStart()); 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); 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) {