aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/ui/listeners/ConversationSwipeRefreshListener.java
blob: c6e00bed562477fe4feb9361eb6a61092da13c5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package de.thedevstack.conversationsplus.ui.listeners;

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;
import de.thedevstack.conversationsplus.entities.Message;
import de.thedevstack.conversationsplus.ui.ConversationActivity;
import de.thedevstack.conversationsplus.ui.ConversationFragment;
import de.thedevstack.conversationsplus.ui.adapter.MessageAdapter;

/**
 * This listener starts loading messages from the server.
 */
public class ConversationSwipeRefreshListener implements SwipyRefreshLayout.OnRefreshListener {
    private List<Message> messageList;
    private ConversationFragment fragment;
    private ConversationMoreMessagesLoadedListener listener;

    public ConversationSwipeRefreshListener(List<Message> 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(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) {
                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);
            }
        }
        Logging.d(Config.LOGTAG, "End Refresh swipe container");
    }
}