From 0086d5dc80dd95b1e8e4dcfe2d3d3090d260bb88 Mon Sep 17 00:00:00 2001 From: steckbrief Date: Fri, 4 Dec 2015 23:18:36 +0100 Subject: Implements FS#83: Reload from last received message --- .../conversationsplus/ui/ConversationFragment.java | 7 ++-- .../ConversationMoreMessagesLoadedListener.java | 7 ++-- .../ConversationSwipeRefreshListener.java | 46 +++++++++++++++++----- 3 files changed, 44 insertions(+), 16 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java b/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java index 94cb896b..3ee79412 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java @@ -10,7 +10,6 @@ import android.content.Intent; import android.content.IntentSender; import android.content.IntentSender.SendIntentException; import android.os.Bundle; -import android.support.v4.widget.SwipeRefreshLayout; import android.text.InputType; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; @@ -34,6 +33,8 @@ import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; import android.widget.Toast; +import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout; + import net.java.otr4j.session.SessionStatus; import java.util.ArrayList; @@ -108,7 +109,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa } }; protected ListView messagesView; - protected SwipeRefreshLayout swipeLayout; + protected SwipyRefreshLayout swipeLayout; final protected List messageList = new ArrayList<>(); protected MessageAdapter messageListAdapter; private EditMessage mEditMessage; @@ -439,7 +440,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa // Start of swipe refresh // New Swipe refresh - swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_container); + swipeLayout = (SwipyRefreshLayout) view.findViewById(R.id.swipe_refresh_container); swipeLayout.setOnRefreshListener(new ConversationSwipeRefreshListener(messageList, swipeLayout, this, messagesView, messageListAdapter)); // End of swipe refresh diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/listeners/ConversationMoreMessagesLoadedListener.java b/src/main/java/de/thedevstack/conversationsplus/ui/listeners/ConversationMoreMessagesLoadedListener.java index cdc60187..774e72ba 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/listeners/ConversationMoreMessagesLoadedListener.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/listeners/ConversationMoreMessagesLoadedListener.java @@ -1,10 +1,11 @@ package de.thedevstack.conversationsplus.ui.listeners; -import android.support.v4.widget.SwipeRefreshLayout; 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; @@ -18,7 +19,7 @@ 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 SwipeRefreshLayout swipeLayout; + private SwipyRefreshLayout swipeLayout; private List messageList; private ConversationFragment fragment; private ListView messagesView; @@ -29,7 +30,7 @@ public class ConversationMoreMessagesLoadedListener implements XmppConnectionSer */ private boolean loadingMessages = false; - public ConversationMoreMessagesLoadedListener(SwipeRefreshLayout swipeLayout, List messageList, ConversationFragment fragment, ListView messagesView, MessageAdapter messageListAdapter) { + public ConversationMoreMessagesLoadedListener(SwipyRefreshLayout swipeLayout, List messageList, ConversationFragment fragment, ListView messagesView, MessageAdapter messageListAdapter) { this.swipeLayout = swipeLayout; this.messageList = messageList; diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/listeners/ConversationSwipeRefreshListener.java b/src/main/java/de/thedevstack/conversationsplus/ui/listeners/ConversationSwipeRefreshListener.java index 84203bf4..c6e00bed 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/listeners/ConversationSwipeRefreshListener.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/listeners/ConversationSwipeRefreshListener.java @@ -1,9 +1,13 @@ package de.thedevstack.conversationsplus.ui.listeners; -import android.support.v4.widget.SwipeRefreshLayout; +import de.thedevstack.conversationsplus.R; +import de.thedevstack.conversationsplus.services.MessageArchiveService; import de.thedevstack.conversationsplus.utils.Logging; import android.widget.ListView; +import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout; +import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection; + import java.util.List; import de.thedevstack.conversationsplus.Config; @@ -15,29 +19,51 @@ import de.thedevstack.conversationsplus.ui.adapter.MessageAdapter; /** * This listener starts loading messages from the server. */ -public class ConversationSwipeRefreshListener implements SwipeRefreshLayout.OnRefreshListener { +public class ConversationSwipeRefreshListener implements SwipyRefreshLayout.OnRefreshListener { private List messageList; private ConversationFragment fragment; private ConversationMoreMessagesLoadedListener listener; - public ConversationSwipeRefreshListener(List messageList, SwipeRefreshLayout swipeLayout, ConversationFragment fragment, ListView messagesView, MessageAdapter messageListAdapter) { + public ConversationSwipeRefreshListener(List messageList, SwipyRefreshLayout swipeLayout, ConversationFragment fragment, ListView messagesView, MessageAdapter messageListAdapter) { this.messageList = messageList; this.fragment = fragment; this.listener = new ConversationMoreMessagesLoadedListener(swipeLayout, messageList, fragment, messagesView, messageListAdapter); } @Override - public void onRefresh() { + public void onRefresh(SwipyRefreshLayoutDirection direction) { Logging.d(Config.LOGTAG, "Refresh swipe container"); + Logging.d(Config.LOGTAG, "Refresh direction " + direction); synchronized (this.messageList) { long timestamp; - if (messageList.isEmpty()) { - timestamp = System.currentTimeMillis(); - } else { - timestamp = this.messageList.get(0).getTimeSent(); // works only because of the ordering (last msg = first msg in list) + if (SwipyRefreshLayoutDirection.TOP == direction) { + if (messageList.isEmpty()) { + timestamp = System.currentTimeMillis(); + } else { + timestamp = this.messageList.get(0).getTimeSent(); // works only because of the ordering (last msg = first msg in list) + } + ConversationActivity activity = (ConversationActivity) fragment.getActivity(); + activity.xmppConnectionService.loadMoreMessages(activity.getSelectedConversation(), timestamp, this.listener); + } else if (SwipyRefreshLayoutDirection.BOTTOM == direction) { + Logging.d("mam", "loading missing messages from mam (last session establishing or last received message)"); + final ConversationActivity activity = (ConversationActivity) fragment.getActivity(); + long lastSessionEstablished = activity.getSelectedConversation().getAccount().getXmppConnection().getLastSessionEstablished(); + int lastMessageIndex = this.messageList.size() - 1; + long lastReceivedMessage = (0 <= lastMessageIndex) ? this.messageList.get(lastMessageIndex).getTimeSent() : Long.MAX_VALUE; + long startTimestamp = Math.min(lastSessionEstablished, lastReceivedMessage); + MessageArchiveService.Query query = activity.xmppConnectionService.getMessageArchiveService().query(activity.getSelectedConversation(), startTimestamp, System.currentTimeMillis()); + if (query != null) { + query.setCallback(this.listener); + } else { + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + listener.onMoreMessagesLoaded(0, activity.getSelectedConversation()); + } + }); + } + this.listener.informUser(R.string.fetching_history_from_server); } - ConversationActivity activity = (ConversationActivity) fragment.getActivity(); - activity.xmppConnectionService.loadMoreMessages(activity.getSelectedConversation(), timestamp, this.listener); } Logging.d(Config.LOGTAG, "End Refresh swipe container"); } -- cgit v1.2.3