From 80435eca5cda499e0319cc4b248bb807693c952d Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Wed, 17 Dec 2014 09:32:51 +0100 Subject: smoothed out scrolling a bit --- .../services/XmppConnectionService.java | 39 ++++++++++++---------- .../conversations/ui/ConversationFragment.java | 28 +++++++++++++--- 2 files changed, 44 insertions(+), 23 deletions(-) (limited to 'src/main/java/eu/siacs/conversations') diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 4b09ac9db..e66f71107 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -961,29 +961,32 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa }); } - public void loadMoreMessages(Conversation conversation, long timestamp, final OnMoreMessagesLoaded callback) { - if (this.getMessageArchiveService().queryInProgress(conversation)) { - Log.d(Config.LOGTAG,"query in progress"); - return; - } - List messages = databaseBackend.getMessages(conversation, 50,timestamp); - if (messages.size() == 0 && (conversation.getAccount().getXmppConnection() != null && conversation.getAccount().getXmppConnection().getFeatures().mam())) { - Log.d(Config.LOGTAG,"load more messages with mam"); - MessageArchiveService.Query query = getMessageArchiveService().query(conversation,0,timestamp - 1); - if (query != null) { - query.setCallback(callback); + public void loadMoreMessages(final Conversation conversation, final long timestamp, final OnMoreMessagesLoaded callback) { + new Thread(new Runnable() { + @Override + public void run() { + if (XmppConnectionService.this.getMessageArchiveService().queryInProgress(conversation)) { + return; + } + final Account account = conversation.getAccount(); + List messages = databaseBackend.getMessages(conversation, 50,timestamp); + if (messages.size() > 0) { + conversation.addAll(0, messages); + callback.onMoreMessagesLoaded(messages.size(), conversation); + } else if (account.getStatus() == Account.State.ONLINE && account.getXmppConnection() != null && account.getXmppConnection().getFeatures().mam()) { + MessageArchiveService.Query query = getMessageArchiveService().query(conversation,0,timestamp - 1); + if (query != null) { + query.setCallback(callback); + } + callback.informUser(R.string.fetching_history_from_server); + } } - return; - } - for (Message message : messages) { - message.setConversation(conversation); - } - conversation.addAll(0, messages); - callback.onMoreMessagesLoaded(messages.size(),conversation); + }).start(); } public interface OnMoreMessagesLoaded { public void onMoreMessagesLoaded(int count,Conversation conversation); + public void informUser(int r); } public List getAccounts() { diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index c333bab79..d0b3e0220 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -106,6 +106,7 @@ public class ConversationFragment extends Fragment { private TextView snackbarMessage; private TextView snackbarAction; private boolean messagesLoaded = false; + private Toast messageLoaderToast; private OnScrollListener mOnScrollListener = new OnScrollListener() { @@ -119,10 +120,9 @@ public class ConversationFragment extends Fragment { public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { synchronized (ConversationFragment.this.messageList) { - if (firstVisibleItem == 0 && messagesLoaded) { + if (firstVisibleItem < 5 && messagesLoaded) { long timestamp = ConversationFragment.this.messageList.get(0).getTimeSent(); messagesLoaded = false; - Log.d(Config.LOGTAG,"load more messages"); activity.xmppConnectionService.loadMoreMessages(conversation, timestamp, new XmppConnectionService.OnMoreMessagesLoaded() { @Override public void onMoreMessagesLoaded(final int count, Conversation conversation) { @@ -132,18 +132,36 @@ public class ConversationFragment extends Fragment { activity.runOnUiThread(new Runnable() { @Override public void run() { - int firstItem = messagesView.getFirstVisiblePosition(); - Log.d(Config.LOGTAG, "done loading more messages. first item: " + firstItem); + int oldPosition = messagesView.getFirstVisiblePosition(); ConversationFragment.this.conversation.populateWithMessages(ConversationFragment.this.messageList); updateStatusMessages(); messageListAdapter.notifyDataSetChanged(); if (count != 0) { + final int newPosition = oldPosition + count; + Message tmpMessage = messageList.get(newPosition); + int offset = 0; + while(tmpMessage.wasMergedIntoPrevious()) { + offset++; + tmpMessage = tmpMessage.prev(); + } + messagesView.setSelectionFromTop(newPosition - offset, 0); messagesLoaded = true; + if (messageLoaderToast != null) { + messageLoaderToast.cancel(); + } } - messagesView.setSelectionFromTop(firstItem + count, 0); } }); } + + @Override + public void informUser(int resId) { + if (messageLoaderToast != null) { + messageLoaderToast.cancel(); + } + messageLoaderToast = Toast.makeText(activity,resId,Toast.LENGTH_LONG); + messageLoaderToast.show(); + } }); } -- cgit v1.2.3