infite scrolling

This commit is contained in:
iNPUTmice 2014-06-14 16:59:07 +02:00
parent 7bdee8c19b
commit 7f1a4a6fb8
3 changed files with 69 additions and 15 deletions

View file

@ -129,13 +129,25 @@ public class DatabaseBackend extends SQLiteOpenHelper {
return list; return list;
} }
public List<Message> getMessages(Conversation conversation, int limit) { public List<Message> getMessages(Conversation conversations, int limit) {
return getMessages(conversations, limit,-1);
}
public List<Message> getMessages(Conversation conversation, int limit, long timestamp) {
List<Message> list = new CopyOnWriteArrayList<Message>(); List<Message> list = new CopyOnWriteArrayList<Message>();
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
String[] selectionArgs = { conversation.getUuid() }; Cursor cursor;
Cursor cursor = db.query(Message.TABLENAME, null, Message.CONVERSATION if (timestamp==-1) {
+ "=?", selectionArgs, null, null, Message.TIME_SENT + " DESC", String[] selectionArgs = { conversation.getUuid() };
String.valueOf(limit)); cursor = db.query(Message.TABLENAME, null, Message.CONVERSATION
+ "=?", selectionArgs, null, null, Message.TIME_SENT + " DESC",
String.valueOf(limit));
} else {
String[] selectionArgs = { conversation.getUuid() , ""+timestamp};
cursor = db.query(Message.TABLENAME, null, Message.CONVERSATION
+ "=? and "+Message.TIME_SENT+"<?", selectionArgs, null, null, Message.TIME_SENT + " DESC",
String.valueOf(limit));
}
if (cursor.getCount() > 0) { if (cursor.getCount() > 0) {
cursor.moveToLast(); cursor.moveToLast();
do { do {

View file

@ -932,6 +932,14 @@ public class XmppConnectionService extends Service {
return this.conversations; return this.conversations;
} }
public List<Message> getMoreMessages(Conversation conversation,long timestamp) {
List<Message> messages = databaseBackend.getMessages(conversation, 50, timestamp);
for(Message message : messages) {
message.setConversation(conversation);
}
return messages;
}
public List<Account> getAccounts() { public List<Account> getAccounts() {
return this.accounts; return this.accounts;
} }

View file

@ -40,6 +40,8 @@ import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener; import android.view.View.OnLongClickListener;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AbsListView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
@ -68,6 +70,7 @@ public class ConversationFragment extends Fragment {
protected Bitmap selfBitmap; protected Bitmap selfBitmap;
private boolean useSubject = true; private boolean useSubject = true;
private boolean messagesLoaded = false;
private IntentSender askForPassphraseIntent = null; private IntentSender askForPassphraseIntent = null;
@ -118,6 +121,33 @@ public class ConversationFragment extends Fragment {
startActivity(intent); startActivity(intent);
} }
}; };
private OnScrollListener mOnScrollListener = new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (firstVisibleItem == 0 && messagesLoaded) {
long timestamp = messageList.get(0).getTimeSent();
messagesLoaded = false;
List<Message> messages = activity.xmppConnectionService
.getMoreMessages(conversation, timestamp);
messageList.addAll(0, messages);
messageListAdapter.notifyDataSetChanged();
if (messages.size() != 0) {
messagesLoaded = true;
}
messagesView.setSelectionFromTop(messages.size() + 1, 0);
}
}
};
private ConversationActivity activity; private ConversationActivity activity;
public void hidePgpPassphraseBox() { public void hidePgpPassphraseBox() {
@ -163,6 +193,7 @@ public class ConversationFragment extends Fragment {
mucErrorText = (TextView) view.findViewById(R.id.muc_error_msg); mucErrorText = (TextView) view.findViewById(R.id.muc_error_msg);
messagesView = (ListView) view.findViewById(R.id.messages_view); messagesView = (ListView) view.findViewById(R.id.messages_view);
messagesView.setOnScrollListener(mOnScrollListener);
messagesView.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL); messagesView.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
messageListAdapter = new ArrayAdapter<Message>(this.getActivity() messageListAdapter = new ArrayAdapter<Message>(this.getActivity()
@ -259,7 +290,7 @@ public class ConversationFragment extends Fragment {
} else if ((filesize == null) && (info != null)) { } else if ((filesize == null) && (info != null)) {
if (error) { if (error) {
viewHolder.time.setText(info + " \u00B7 " viewHolder.time.setText(info + " \u00B7 "
+ formatedTime); + formatedTime);
} else { } else {
viewHolder.time.setText(info); viewHolder.time.setText(info);
} }
@ -510,13 +541,14 @@ public class ConversationFragment extends Fragment {
} else { } else {
displayInfoMessage(viewHolder, displayInfoMessage(viewHolder,
R.string.install_openkeychain); R.string.install_openkeychain);
viewHolder.message_box.setOnClickListener(new OnClickListener() { viewHolder.message_box
.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
activity.showInstallPgpDialog(); activity.showInstallPgpDialog();
} }
}); });
} }
} else if (item.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) { } else if (item.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
displayDecryptionFailed(viewHolder); displayDecryptionFailed(viewHolder);
@ -685,12 +717,14 @@ public class ConversationFragment extends Fragment {
} }
if (this.conversation.getMessages().size() == 0) { if (this.conversation.getMessages().size() == 0) {
this.messageList.clear(); this.messageList.clear();
messagesLoaded = false;
} else { } else {
for(Message message : this.conversation.getMessages()) { for (Message message : this.conversation.getMessages()) {
if (!this.messageList.contains(message)) { if (!this.messageList.contains(message)) {
this.messageList.add(message); this.messageList.add(message);
} }
} }
messagesLoaded = true;
updateStatusMessages(); updateStatusMessages();
} }
this.messageListAdapter.notifyDataSetChanged(); this.messageListAdapter.notifyDataSetChanged();