blob: a5c5e2aa2f5ae3391924ddc34385a96b9a6c126b (
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
package eu.siacs.conversations.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 eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.ui.ConversationActivity;
import eu.siacs.conversations.ui.ConversationFragment;
import eu.siacs.conversations.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<Message> 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<Message> 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<Message> messages) {
if (uuid == null) {
return 0;
}
for (int i = 0; i < messages.size(); ++i) {
if (uuid.equals(messages.get(i).getUuid())) {
return i;
} else {
Message next = messages.get(i);
while(next != null && next.wasMergedIntoPrevious()) {
if (uuid.equals(next.getUuid())) {
return i;
}
next = next.next();
}
}
}
return 0;
}
}
|