Skip to content

Commit

Permalink
Fix message jump-to-position.
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-signal committed Jun 10, 2020
1 parent bf40a07 commit e13f325
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 50 deletions.
Expand Up @@ -30,16 +30,13 @@ class ConversationDataSource extends PositionalDataSource<MessageRecord> {

private final Context context;
private final long threadId;
private final DataUpdatedCallback dataUpdateCallback;

private ConversationDataSource(@NonNull Context context,
long threadId,
@NonNull Invalidator invalidator,
@NonNull DataUpdatedCallback dataUpdateCallback)
@NonNull Invalidator invalidator)
{
this.context = context;
this.threadId = threadId;
this.dataUpdateCallback = dataUpdateCallback;

ContentObserver contentObserver = new ContentObserver(null) {
@Override
Expand Down Expand Up @@ -82,7 +79,6 @@ public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialC
SizeFixResult result = ensureMultipleOfPageSize(records, params.requestedStartPosition, params.pageSize, totalCount);

callback.onResult(result.messages, params.requestedStartPosition, result.total);
Util.runOnMain(dataUpdateCallback::onDataUpdated);
}

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

callback.onResult(records);

if (!isInvalid()) {
Util.runOnMain(dataUpdateCallback::onDataUpdated);
}

Log.d(TAG, "[Update] " + (System.currentTimeMillis() - start) + " ms" + (isInvalid() ? " -- invalidated" : ""));
}

Expand Down Expand Up @@ -164,18 +156,16 @@ static class Factory extends DataSource.Factory<Integer, MessageRecord> {
private final Context context;
private final long threadId;
private final Invalidator invalidator;
private final DataUpdatedCallback callback;

Factory(Context context, long threadId, @NonNull Invalidator invalidator, @NonNull DataUpdatedCallback callback) {
Factory(Context context, long threadId, @NonNull Invalidator invalidator) {
this.context = context;
this.threadId = threadId;
this.invalidator = invalidator;
this.callback = callback;
}

@Override
public @NonNull DataSource<Integer, MessageRecord> create() {
return new ConversationDataSource(context, threadId, invalidator, callback);
return new ConversationDataSource(context, threadId, invalidator);
}
}
}
Expand Up @@ -941,17 +941,15 @@ public void jumpToMessage(@NonNull RecipientId author, long timestamp, @Nullable
}

private void moveToMessagePosition(int position, @Nullable Runnable onMessageNotFound) {
if (position >= 0) {
list.scrollToPosition(position);

if (getListAdapter() == null || getListAdapter().getItem(position) == null) {
Log.i(TAG, "[moveToMessagePosition] Position " + position + " not currently populated. Scheduling a jump.");
conversationViewModel.scheduleForNextMessageUpdate(() -> {
list.scrollToPosition(position);
getListAdapter().pulseHighlightItem(position);
});
int itemCount = getListAdapter() != null ? getListAdapter().getItemCount() : 0;

if (position >= 0 && position < itemCount) {
if (getListAdapter().getItem(position) == null) {
conversationViewModel.onConversationDataAvailable(threadId, position);
deferred.setDeferred(true);
deferred.defer(() -> moveToMessagePosition(position, onMessageNotFound));
} else {
getListAdapter().pulseHighlightItem(position);
scrollToStartingPosition(position);
}
} else {
Log.w(TAG, "[moveToMessagePosition] Tried to navigate to message, but it wasn't found.");
Expand Down
Expand Up @@ -40,7 +40,6 @@ class ConversationViewModel extends ViewModel {
private final MutableLiveData<Long> threadId;
private final LiveData<PagedList<MessageRecord>> messages;
private final LiveData<ConversationData> conversationMetadata;
private final List<Runnable> onNextMessageLoad;
private final Invalidator invalidator;

private int jumpToPosition;
Expand All @@ -51,28 +50,31 @@ private ConversationViewModel() {
this.conversationRepository = new ConversationRepository();
this.recentMedia = new MutableLiveData<>();
this.threadId = new MutableLiveData<>();
this.onNextMessageLoad = new CopyOnWriteArrayList<>();
this.invalidator = new Invalidator();

LiveData<ConversationData> conversationDataForRequestedThreadId = Transformations.switchMap(threadId, thread -> {
return conversationRepository.getConversationData(thread, jumpToPosition);
LiveData<ConversationData> metadata = Transformations.switchMap(threadId, thread -> {
LiveData<ConversationData> conversationData = conversationRepository.getConversationData(thread, jumpToPosition);

jumpToPosition = -1;

return conversationData;
});

LiveData<Pair<Long, PagedList<MessageRecord>>> messagesForThreadId = Transformations.switchMap(conversationDataForRequestedThreadId, data -> {
DataSource.Factory<Integer, MessageRecord> factory = new ConversationDataSource.Factory(context, data.getThreadId(), invalidator, this::onMessagesUpdated);
LiveData<Pair<Long, PagedList<MessageRecord>>> messagesForThreadId = Transformations.switchMap(metadata, data -> {
DataSource.Factory<Integer, MessageRecord> factory = new ConversationDataSource.Factory(context, data.getThreadId(), invalidator);
PagedList.Config config = new PagedList.Config.Builder()
.setPageSize(25)
.setInitialLoadSizeHint(25)
.build();

final int startPosition;
if (jumpToPosition > 0) {
startPosition = jumpToPosition;
if (data.shouldJumpToMessage()) {
startPosition = data.getJumpToPosition();
} else {
startPosition = data.getLastSeenPosition();
}

Log.d(TAG, "Starting at position " + startPosition + " :: " + jumpToPosition + " :: " + data.getLastSeenPosition());
Log.d(TAG, "Starting at position " + startPosition + " :: " + data.getJumpToPosition() + " :: " + data.getLastSeenPosition());

return Transformations.map(new LivePagedListBuilder<>(factory, config).setFetchExecutor(ConversationDataSource.EXECUTOR)
.setInitialLoadKey(Math.max(startPosition, 0))
Expand All @@ -82,13 +84,9 @@ private ConversationViewModel() {

this.messages = Transformations.map(messagesForThreadId, Pair::second);

LiveData<Long> threadIdForLoadedMessages = Transformations.distinctUntilChanged(Transformations.map(messagesForThreadId, Pair::first));
LiveData<Long> distinctThread = Transformations.distinctUntilChanged(threadId);

conversationMetadata = Transformations.switchMap(threadIdForLoadedMessages, m -> {
LiveData<ConversationData> data = conversationRepository.getConversationData(m, jumpToPosition);
jumpToPosition = -1;
return data;
});
conversationMetadata = Transformations.switchMap(distinctThread, thread -> metadata);
}

void onAttachmentKeyboardOpen() {
Expand Down Expand Up @@ -122,24 +120,12 @@ int getLastSeenPosition() {
return conversationMetadata.getValue() != null ? conversationMetadata.getValue().getLastSeenPosition() : 0;
}

void scheduleForNextMessageUpdate(@NonNull Runnable runnable) {
onNextMessageLoad.add(runnable);
}

@Override
protected void onCleared() {
super.onCleared();
invalidator.invalidate();
}

private void onMessagesUpdated() {
for (Runnable runnable : onNextMessageLoad) {
runnable.run();
}

onNextMessageLoad.clear();
}

static class Factory extends ViewModelProvider.NewInstanceFactory {
@Override
public @NonNull<T extends ViewModel> T create(@NonNull Class<T> modelClass) {
Expand Down

0 comments on commit e13f325

Please sign in to comment.