Migrate ConversationList to paging library and apply abstractions to conversation.

This commit is contained in:
Alex Hart
2020-06-12 15:11:36 -03:00
committed by Greyson Parrelli
parent ce940235b0
commit 49f75d7036
25 changed files with 1212 additions and 622 deletions

View File

@@ -552,9 +552,21 @@ public class ThreadDatabase extends Database {
return positions;
}
public Cursor getConversationList(long offset, long limit) {
return getConversationList("0", offset, limit);
}
public Cursor getArchivedConversationList(long offset, long limit) {
return getConversationList("1", offset, limit);
}
private Cursor getConversationList(String archived) {
return getConversationList(archived, 0, 0);
}
private Cursor getConversationList(@NonNull String archived, long offset, long limit) {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
String query = createQuery(ARCHIVED + " = ? AND " + MESSAGE_COUNT + " != 0", 0);
String query = createQuery(ARCHIVED + " = ? AND " + MESSAGE_COUNT + " != 0", offset, limit);
Cursor cursor = db.rawQuery(query, new String[]{archived});
setNotifyConversationListListeners(cursor);
@@ -562,20 +574,24 @@ public class ThreadDatabase extends Database {
return cursor;
}
public int getUnarchivedConversationListCount() {
return getConversationListCount(false);
}
public int getArchivedConversationListCount() {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor cursor = null;
return getConversationListCount(true);
}
try {
cursor = db.query(TABLE_NAME, new String[] {"COUNT(*)"}, ARCHIVED + " = ?",
new String[] {"1"}, null, null, null);
private int getConversationListCount(boolean archived) {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
String[] columns = new String[] { "COUNT(*)" };
String query = ARCHIVED + " = ? AND " + MESSAGE_COUNT + " != 0";
String[] args = new String[] { archived ? "1" : "0" };
try (Cursor cursor = db.query(TABLE_NAME, columns, query, args, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
return cursor.getInt(0);
}
} finally {
if (cursor != null) cursor.close();
}
return 0;
@@ -854,7 +870,11 @@ public class ThreadDatabase extends Database {
return null;
}
private @NonNull String createQuery(@NonNull String where, int limit) {
private @NonNull String createQuery(@NonNull String where, long limit) {
return createQuery(where, 0, limit);
}
private @NonNull String createQuery(@NonNull String where, long offset, long limit) {
String projection = Util.join(COMBINED_THREAD_RECIPIENT_GROUP_PROJECTION, ",");
String query =
"SELECT " + projection + " FROM " + TABLE_NAME +
@@ -869,6 +889,10 @@ public class ThreadDatabase extends Database {
query += " LIMIT " + limit;
}
if (offset > 0) {
query += " OFFSET " + offset;
}
return query;
}

View File

@@ -1,83 +0,0 @@
package org.thoughtcrime.securesms.database.loaders;
import android.content.Context;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.MergeCursor;
import org.thoughtcrime.securesms.contacts.ContactAccessor;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.ThreadDatabase;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientId;
import org.thoughtcrime.securesms.util.AbstractCursorLoader;
import java.util.LinkedList;
import java.util.List;
public class ConversationListLoader extends AbstractCursorLoader {
private final String filter;
private final boolean archived;
public ConversationListLoader(Context context, String filter, boolean archived) {
super(context);
this.filter = filter;
this.archived = archived;
}
@Override
public Cursor getCursor() {
if (filter != null && filter.trim().length() != 0) return getFilteredConversationList(filter);
else if (!archived) return getUnarchivedConversationList();
else return getArchivedConversationList();
}
private Cursor getUnarchivedConversationList() {
List<Cursor> cursorList = new LinkedList<>();
cursorList.add(DatabaseFactory.getThreadDatabase(context).getConversationList());
int archivedCount = DatabaseFactory.getThreadDatabase(context)
.getArchivedConversationListCount();
if (archivedCount > 0) {
MatrixCursor switchToArchiveCursor = new MatrixCursor(new String[] {
ThreadDatabase.ID, ThreadDatabase.DATE, ThreadDatabase.MESSAGE_COUNT,
ThreadDatabase.RECIPIENT_ID, ThreadDatabase.SNIPPET, ThreadDatabase.READ, ThreadDatabase.UNREAD_COUNT,
ThreadDatabase.TYPE, ThreadDatabase.SNIPPET_TYPE, ThreadDatabase.SNIPPET_URI,
ThreadDatabase.SNIPPET_CONTENT_TYPE, ThreadDatabase.SNIPPET_EXTRAS,
ThreadDatabase.ARCHIVED, ThreadDatabase.STATUS, ThreadDatabase.DELIVERY_RECEIPT_COUNT,
ThreadDatabase.EXPIRES_IN, ThreadDatabase.LAST_SEEN, ThreadDatabase.READ_RECEIPT_COUNT}, 1);
if (cursorList.get(0).getCount() <= 0) {
switchToArchiveCursor.addRow(new Object[] {-1L, System.currentTimeMillis(), archivedCount,
"-1", null, 1, 0, ThreadDatabase.DistributionTypes.INBOX_ZERO,
0, null, null, null, 0, -1, 0, 0, 0, -1});
}
switchToArchiveCursor.addRow(new Object[] {-1L, System.currentTimeMillis(), archivedCount,
"-1", null, 1, 0, ThreadDatabase.DistributionTypes.ARCHIVE,
0, null, null, null, 0, -1, 0, 0, 0, -1});
cursorList.add(switchToArchiveCursor);
}
return new MergeCursor(cursorList.toArray(new Cursor[0]));
}
private Cursor getArchivedConversationList() {
return DatabaseFactory.getThreadDatabase(context).getArchivedConversationList();
}
private Cursor getFilteredConversationList(String filter) {
List<String> numbers = ContactAccessor.getInstance().getNumbersForThreadSearchFilter(context, filter);
List<RecipientId> recipientIds = new LinkedList<>();
for (String number : numbers) {
recipientIds.add(Recipient.external(context, number).getId());
}
return DatabaseFactory.getThreadDatabase(context).getFilteredConversationList(recipientIds);
}
}

View File

@@ -187,6 +187,53 @@ public final class ThreadRecord {
else return true;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ThreadRecord that = (ThreadRecord) o;
return threadId == that.threadId &&
type == that.type &&
date == that.date &&
deliveryStatus == that.deliveryStatus &&
deliveryReceiptCount == that.deliveryReceiptCount &&
readReceiptCount == that.readReceiptCount &&
count == that.count &&
unreadCount == that.unreadCount &&
forcedUnread == that.forcedUnread &&
distributionType == that.distributionType &&
archived == that.archived &&
expiresIn == that.expiresIn &&
lastSeen == that.lastSeen &&
body.equals(that.body) &&
recipient.equals(that.recipient) &&
Objects.equals(snippetUri, that.snippetUri) &&
Objects.equals(contentType, that.contentType) &&
Objects.equals(extra, that.extra);
}
@Override
public int hashCode() {
return Objects.hash(threadId,
body,
recipient,
type,
date,
deliveryStatus,
deliveryReceiptCount,
readReceiptCount,
snippetUri,
contentType,
extra,
count,
unreadCount,
forcedUnread,
distributionType,
archived,
expiresIn,
lastSeen);
}
public static class Builder {
private long threadId;
private String body;