Skip to content

Commit

Permalink
Migrate ConversationList to paging library and apply abstractions to …
Browse files Browse the repository at this point in the history
…conversation.
  • Loading branch information
alex-signal authored and greyson-signal committed Jun 16, 2020
1 parent ce94023 commit 49f75d7
Show file tree
Hide file tree
Showing 25 changed files with 1,210 additions and 620 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ void bind(@NonNull ThreadRecord thread,
@NonNull GlideRequests glideRequests, @NonNull Locale locale,
@NonNull Set<Long> typingThreads,
@NonNull Set<Long> selectedThreads, boolean batchMode);

void setBatchMode(boolean batchMode);
void updateTypingIndicator(@NonNull Set<Long> typingThreads);
}
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ private boolean hasHeader() {
return headerView != null;
}

private boolean hasFooter() {
public boolean hasFooter() {
return footerView != null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final class ConversationData {
private final boolean isMessageRequestAccepted;
private final boolean hasPreMessageRequestMessages;
private final int jumpToPosition;
private final int threadSize;

ConversationData(long threadId,
long lastSeen,
Expand All @@ -20,7 +21,8 @@ final class ConversationData {
boolean hasSent,
boolean isMessageRequestAccepted,
boolean hasPreMessageRequestMessages,
int jumpToPosition)
int jumpToPosition,
int threadSize)
{
this.threadId = threadId;
this.lastSeen = lastSeen;
Expand All @@ -30,6 +32,7 @@ final class ConversationData {
this.isMessageRequestAccepted = isMessageRequestAccepted;
this.hasPreMessageRequestMessages = hasPreMessageRequestMessages;
this.jumpToPosition = jumpToPosition;
this.threadSize = threadSize;
}

public long getThreadId() {
Expand Down Expand Up @@ -71,4 +74,8 @@ boolean shouldScrollToLastSeen() {
int getJumpToPosition() {
return jumpToPosition;
}

int getThreadSize() {
return threadSize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.util.concurrent.SignalExecutors;
import org.thoughtcrime.securesms.util.paging.Invalidator;
import org.thoughtcrime.securesms.util.paging.SizeFixResult;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -76,9 +78,9 @@ public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialC
}

if (!isInvalid()) {
SizeFixResult result = ensureMultipleOfPageSize(records, params.requestedStartPosition, params.pageSize, totalCount);
SizeFixResult<MessageRecord> result = SizeFixResult.ensureMultipleOfPageSize(records, params.requestedStartPosition, params.pageSize, totalCount);

callback.onResult(result.messages, params.requestedStartPosition, result.total);
callback.onResult(result.getItems(), params.requestedStartPosition, result.getTotal());
}

Log.d(TAG, "[Initial Load] " + (System.currentTimeMillis() - start) + " ms" + (isInvalid() ? " -- invalidated" : ""));
Expand All @@ -103,54 +105,6 @@ public void loadRange(@NonNull LoadRangeParams params, @NonNull LoadRangeCallbac
Log.d(TAG, "[Update] " + (System.currentTimeMillis() - start) + " ms" + (isInvalid() ? " -- invalidated" : ""));
}

private static @NonNull SizeFixResult ensureMultipleOfPageSize(@NonNull List<MessageRecord> records,
int startPosition,
int pageSize,
int total)
{
if (records.size() + startPosition == total || (records.size() != 0 && records.size() % pageSize == 0)) {
return new SizeFixResult(records, total);
}

if (records.size() < pageSize) {
Log.w(TAG, "Hit a miscalculation where we don't have the full dataset, but it's smaller than a page size. records: " + records.size() + ", startPosition: " + startPosition + ", pageSize: " + pageSize + ", total: " + total);
return new SizeFixResult(records, records.size() + startPosition);
}

Log.w(TAG, "Hit a miscalculation where our data size isn't a multiple of the page size. records: " + records.size() + ", startPosition: " + startPosition + ", pageSize: " + pageSize + ", total: " + total);
int overflow = records.size() % pageSize;

return new SizeFixResult(records.subList(0, records.size() - overflow), total);
}

private static class SizeFixResult {
final List<MessageRecord> messages;
final int total;

private SizeFixResult(@NonNull List<MessageRecord> messages, int total) {
this.messages = messages;
this.total = total;
}
}

interface DataUpdatedCallback {
void onDataUpdated();
}

static class Invalidator {
private Runnable callback;

synchronized void invalidate() {
if (callback != null) {
callback.run();
}
}

private synchronized void observe(@NonNull Runnable callback) {
this.callback = callback;
}
}

static class Factory extends DataSource.Factory<Integer, MessageRecord> {

private final Context context;
Expand Down

0 comments on commit 49f75d7

Please sign in to comment.