package de.thedevstack.conversationsplus.ui.listeners; import android.view.View; import android.widget.ListView; import android.widget.Toast; import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout; import java.util.List; import de.thedevstack.conversationsplus.entities.Conversation; import de.thedevstack.conversationsplus.entities.Message; import de.thedevstack.conversationsplus.services.XmppConnectionService; import de.thedevstack.conversationsplus.ui.ConversationActivity; import de.thedevstack.conversationsplus.ui.ConversationFragment; import de.thedevstack.conversationsplus.ui.adapter.MessageAdapter; /** * This listener updates the UI when messages are loaded from the server. */ public class ConversationMoreMessagesLoadedListener implements XmppConnectionService.OnMoreMessagesLoaded { private SwipyRefreshLayout swipeLayout; private List messageList; private ConversationFragment fragment; private ListView messagesView; private MessageAdapter messageListAdapter; private Toast messageLoaderToast; /* The current loading status */ private boolean loadingMessages = false; /** * Whether the user is loading only history messages or not. * History messages are messages which are older than the oldest in the database. */ private boolean loadHistory = true; public ConversationMoreMessagesLoadedListener(SwipyRefreshLayout swipeLayout, List messageList, ConversationFragment fragment, ListView messagesView, MessageAdapter messageListAdapter) { this.swipeLayout = swipeLayout; this.messageList = messageList; this.fragment = fragment; this.messagesView = messagesView; this.messageListAdapter = messageListAdapter; } public void setLoadHistory(boolean value) { this.loadHistory = value; } public void setLoadingInProgress() { this.loadingMessages = true; } public boolean isLoadingInProgress() { return this.loadingMessages; } @Override public void onMoreMessagesLoaded(final int c, final Conversation conversation) { ConversationActivity activity = (ConversationActivity) fragment.getActivity(); // Current selected conversation is not the same the messages are loaded - skip updating message view and hide loading graphic if (activity.getSelectedConversation() != conversation) { activity.runOnUiThread(new Runnable() { @Override public void run() { swipeLayout.setRefreshing(false); } }); return; } // No new messages are loaded if (0 == c) { if (this.loadHistory) { conversation.setHasMessagesLeftOnServer(false); } activity.runOnUiThread(new Runnable() { @Override public void run() { swipeLayout.setRefreshing(false); } }); } activity.runOnUiThread(new Runnable() { @Override public void run() { final int oldPosition = messagesView.getFirstVisiblePosition(); // Always 0 - because loading starts always when hitting the top String uuid = null; boolean oldMessageListWasEmpty = messageList.isEmpty(); if (-1 < oldPosition && messageList.size() > oldPosition) { Message message = messageList.get(oldPosition); uuid = message != null ? message.getUuid() : null; } View v = messagesView.getChildAt(0); final int pxOffset = (v == null) ? 0 : v.getTop(); conversation.populateWithMessages(messageList); // This overrides the old message list fragment.updateStatusMessages(); // This adds "messages" to the list for the status messageListAdapter.notifyDataSetChanged(); loadingMessages = false; // Loading of messages is finished - next query can be loaded int pos = getIndexOf(uuid, messageList); if (!oldMessageListWasEmpty) { messagesView.setSelectionFromTop(pos, pxOffset); } if (messageLoaderToast != null) { messageLoaderToast.cancel(); } swipeLayout.setRefreshing(false); } }); } @Override public void informUser(final int resId) { final ConversationActivity activity = (ConversationActivity) fragment.getActivity(); activity.runOnUiThread(new Runnable() { @Override public void run() { if (messageLoaderToast != null) { messageLoaderToast.cancel(); } messageLoaderToast = Toast.makeText(activity, resId, Toast.LENGTH_LONG); messageLoaderToast.show(); } }); } private int getIndexOf(String uuid, List messages) { if (uuid == null) { return 0; } for (int i = 0; i < messages.size(); ++i) { if (uuid.equals(messages.get(i).getUuid())) { return i; } } return 0; } }