Iterator to go through all messages of a conversation

This commit is contained in:
fiaxh 2015-09-20 22:17:32 +01:00
parent a954e32b16
commit 024e697cee

View file

@ -21,6 +21,7 @@ import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
@ -407,6 +408,43 @@ public class DatabaseBackend extends SQLiteOpenHelper {
return list;
}
public Iterable<Message> getMessagesIterable(final Conversation conversation){
return new Iterable<Message>() {
@Override
public Iterator<Message> iterator() {
class MessageIterator implements Iterator<Message>{
SQLiteDatabase db = getReadableDatabase();
String[] selectionArgs = { conversation.getUuid() };
Cursor cursor = db.query(Message.TABLENAME, null, Message.CONVERSATION
+ "=?", selectionArgs, null, null, Message.TIME_SENT
+ " ASC", null);
public MessageIterator() {
cursor.moveToFirst();
}
@Override
public boolean hasNext() {
return !cursor.isAfterLast();
}
@Override
public Message next() {
Message message = Message.fromCursor(cursor);
cursor.moveToNext();
return message;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
return new MessageIterator();
}
};
}
public Conversation findConversation(final Account account, final Jid contactJid) {
SQLiteDatabase db = this.getReadableDatabase();
String[] selectionArgs = { account.getUuid(),