smoothed out scrolling a bit

This commit is contained in:
iNPUTmice 2014-12-17 09:32:51 +01:00
parent 1a7ed4ed7c
commit 80435eca5c
3 changed files with 45 additions and 23 deletions

View File

@ -961,29 +961,32 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}); });
} }
public void loadMoreMessages(Conversation conversation, long timestamp, final OnMoreMessagesLoaded callback) { public void loadMoreMessages(final Conversation conversation, final long timestamp, final OnMoreMessagesLoaded callback) {
if (this.getMessageArchiveService().queryInProgress(conversation)) { new Thread(new Runnable() {
Log.d(Config.LOGTAG,"query in progress"); @Override
public void run() {
if (XmppConnectionService.this.getMessageArchiveService().queryInProgress(conversation)) {
return; return;
} }
final Account account = conversation.getAccount();
List<Message> messages = databaseBackend.getMessages(conversation, 50,timestamp); List<Message> messages = databaseBackend.getMessages(conversation, 50,timestamp);
if (messages.size() == 0 && (conversation.getAccount().getXmppConnection() != null && conversation.getAccount().getXmppConnection().getFeatures().mam())) { if (messages.size() > 0) {
Log.d(Config.LOGTAG,"load more messages with mam"); 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); MessageArchiveService.Query query = getMessageArchiveService().query(conversation,0,timestamp - 1);
if (query != null) { if (query != null) {
query.setCallback(callback); query.setCallback(callback);
} }
return; callback.informUser(R.string.fetching_history_from_server);
} }
for (Message message : messages) {
message.setConversation(conversation);
} }
conversation.addAll(0, messages); }).start();
callback.onMoreMessagesLoaded(messages.size(),conversation);
} }
public interface OnMoreMessagesLoaded { public interface OnMoreMessagesLoaded {
public void onMoreMessagesLoaded(int count,Conversation conversation); public void onMoreMessagesLoaded(int count,Conversation conversation);
public void informUser(int r);
} }
public List<Account> getAccounts() { public List<Account> getAccounts() {

View File

@ -106,6 +106,7 @@ public class ConversationFragment extends Fragment {
private TextView snackbarMessage; private TextView snackbarMessage;
private TextView snackbarAction; private TextView snackbarAction;
private boolean messagesLoaded = false; private boolean messagesLoaded = false;
private Toast messageLoaderToast;
private OnScrollListener mOnScrollListener = new OnScrollListener() { private OnScrollListener mOnScrollListener = new OnScrollListener() {
@ -119,10 +120,9 @@ public class ConversationFragment extends Fragment {
public void onScroll(AbsListView view, int firstVisibleItem, public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) { int visibleItemCount, int totalItemCount) {
synchronized (ConversationFragment.this.messageList) { synchronized (ConversationFragment.this.messageList) {
if (firstVisibleItem == 0 && messagesLoaded) { if (firstVisibleItem < 5 && messagesLoaded) {
long timestamp = ConversationFragment.this.messageList.get(0).getTimeSent(); long timestamp = ConversationFragment.this.messageList.get(0).getTimeSent();
messagesLoaded = false; messagesLoaded = false;
Log.d(Config.LOGTAG,"load more messages");
activity.xmppConnectionService.loadMoreMessages(conversation, timestamp, new XmppConnectionService.OnMoreMessagesLoaded() { activity.xmppConnectionService.loadMoreMessages(conversation, timestamp, new XmppConnectionService.OnMoreMessagesLoaded() {
@Override @Override
public void onMoreMessagesLoaded(final int count, Conversation conversation) { public void onMoreMessagesLoaded(final int count, Conversation conversation) {
@ -132,18 +132,36 @@ public class ConversationFragment extends Fragment {
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
int firstItem = messagesView.getFirstVisiblePosition(); int oldPosition = messagesView.getFirstVisiblePosition();
Log.d(Config.LOGTAG, "done loading more messages. first item: " + firstItem);
ConversationFragment.this.conversation.populateWithMessages(ConversationFragment.this.messageList); ConversationFragment.this.conversation.populateWithMessages(ConversationFragment.this.messageList);
updateStatusMessages(); updateStatusMessages();
messageListAdapter.notifyDataSetChanged(); messageListAdapter.notifyDataSetChanged();
if (count != 0) { if (count != 0) {
messagesLoaded = true; 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();
}
}); });
} }

View File

@ -366,4 +366,5 @@
<string name="reset">Reset</string> <string name="reset">Reset</string>
<string name="account_image_description">Account avatar</string> <string name="account_image_description">Account avatar</string>
<string name="copy_otr_clipboard_description">Copy OTR fingerprint to clipboard</string> <string name="copy_otr_clipboard_description">Copy OTR fingerprint to clipboard</string>
<string name="fetching_history_from_server">Fetching history from server</string>
</resources> </resources>