From 024e697cee47955019d842faa440381e814487b0 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Sun, 20 Sep 2015 22:17:32 +0100 Subject: [PATCH] Iterator to go through all messages of a conversation --- .../persistance/DatabaseBackend.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java index d420c1cd8..be827929c 100644 --- a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -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 getMessagesIterable(final Conversation conversation){ + return new Iterable() { + @Override + public Iterator iterator() { + class MessageIterator implements Iterator{ + 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(),