Correct notifications to user if swiping with disabled or not connected account (FS#159)

This commit is contained in:
lookshe 2016-03-22 00:46:18 +01:00
parent c0267bb8f6
commit 02f958ce76

View file

@ -10,6 +10,7 @@ import java.util.List;
import de.thedevstack.android.logcat.Logging;
import de.thedevstack.conversationsplus.Config;
import de.thedevstack.conversationsplus.R;
import de.thedevstack.conversationsplus.entities.Account;
import de.thedevstack.conversationsplus.entities.Conversation;
import de.thedevstack.conversationsplus.entities.Message;
import de.thedevstack.conversationsplus.services.MessageArchiveService;
@ -24,10 +25,12 @@ public class ConversationSwipeRefreshListener implements SwipyRefreshLayout.OnRe
private List<Message> messageList;
private ConversationFragment fragment;
private ConversationMoreMessagesLoadedListener listener;
private SwipyRefreshLayout swipeLayout;
public ConversationSwipeRefreshListener(List<Message> messageList, SwipyRefreshLayout swipeLayout, ConversationFragment fragment, ListView messagesView, MessageAdapter messageListAdapter) {
this.messageList = messageList;
this.fragment = fragment;
this.swipeLayout = swipeLayout;
this.listener = new ConversationMoreMessagesLoadedListener(swipeLayout, messageList, fragment, messagesView, messageListAdapter);
}
@ -35,33 +38,42 @@ public class ConversationSwipeRefreshListener implements SwipyRefreshLayout.OnRe
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 (SwipyRefreshLayoutDirection.TOP == direction) { // Load history -> messages sent/received before first message in database
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)
final ConversationActivity activity = (ConversationActivity) fragment.getActivity();
if (activity.getSelectedConversation().getAccount().getStatus() != Account.State.DISABLED) {
synchronized (this.messageList) {
long timestamp;
if (SwipyRefreshLayoutDirection.TOP == direction) { // Load history -> messages sent/received before first message in database
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)
}
this.listener.setLoadHistory(true);
activity.xmppConnectionService.loadMoreMessages(activity.getSelectedConversation(), timestamp, this.listener);
} else if (SwipyRefreshLayoutDirection.BOTTOM == direction) { // load messages sent/received between last received or last session establishment and now
if (activity.getSelectedConversation().getAccount().isOnlineAndConnected()) {
Logging.d("mam", "loading missing messages from mam (last session establishing or last received message)");
long lastSessionEstablished = this.getTimestampOfLastSessionEstablished(activity.getSelectedConversation());
long lastReceivedMessage = this.getTimestampOfLastReceivedOrTransmittedMessage();
long startTimestamp = Math.min(lastSessionEstablished, lastReceivedMessage);
MessageArchiveService.Query query = activity.xmppConnectionService.getMessageArchiveService().query(activity.getSelectedConversation(), startTimestamp, System.currentTimeMillis(), this.listener);
if (query != null) {
this.listener.setLoadHistory(false);
} else {
Logging.d("mam", "no query built - no messages loaded");
this.listener.onMoreMessagesLoaded(0, activity.getSelectedConversation());
this.listener.informUser(R.string.no_more_history_on_server);
}
this.listener.informUser(R.string.fetching_history_from_server);
} else {
this.listener.informUser(R.string.not_connected_try_again);
swipeLayout.setRefreshing(false);
}
}
ConversationActivity activity = (ConversationActivity) fragment.getActivity();
this.listener.setLoadHistory(true);
activity.xmppConnectionService.loadMoreMessages(activity.getSelectedConversation(), timestamp, this.listener);
} else if (SwipyRefreshLayoutDirection.BOTTOM == direction) { // load messages sent/received between last received or last session establishment and now
Logging.d("mam", "loading missing messages from mam (last session establishing or last received message)");
final ConversationActivity activity = (ConversationActivity) fragment.getActivity();
long lastSessionEstablished = this.getTimestampOfLastSessionEstablished(activity.getSelectedConversation());
long lastReceivedMessage = this.getTimestampOfLastReceivedOrTransmittedMessage();
long startTimestamp = Math.min(lastSessionEstablished, lastReceivedMessage);
MessageArchiveService.Query query = activity.xmppConnectionService.getMessageArchiveService().query(activity.getSelectedConversation(), startTimestamp, System.currentTimeMillis(), this.listener);
if (query != null) {
this.listener.setLoadHistory(false);
} else {
Logging.d("mam", "no query built - no messages loaded");
this.listener.onMoreMessagesLoaded(0, activity.getSelectedConversation());
this.listener.informUser(R.string.no_more_history_on_server);
}
this.listener.informUser(R.string.fetching_history_from_server);
}
} else {
this.listener.informUser(R.string.this_account_is_disabled);
swipeLayout.setRefreshing(false);
}
Logging.d(Config.LOGTAG, "End Refresh swipe container");
}