Skip to content

Commit

Permalink
fix ups
Browse files Browse the repository at this point in the history
  • Loading branch information
elihart committed Apr 29, 2018
1 parent a4339bf commit ad2493c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 29 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {

ext.KOTLIN_VERSION = "1.2.40"
ext.KOTLIN_VERSION = "1.2.41"
ext.ANDROID_PLUGIN_VERSION = "3.1.1"

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
/**
* An adaptation of Google's {@link android.support.v7.recyclerview.extensions.AsyncListDiffer}
* that adds support for payloads in changes.
*
* <p>
* Also adds support for canceling an in progress diff.
*/
class AsyncListDifferWithPayload<T> {
private final static Executor DIFF_EXECUTOR = Executors.newFixedThreadPool(2);
private final static Executor MAIN_THREAD_EXECUTOR = new MainThreadExecutor();
private static final Executor DIFF_EXECUTOR = Executors.newFixedThreadPool(2);
private static final Executor MAIN_THREAD_EXECUTOR = new MainThreadExecutor();
private final ListUpdateCallback updateCallback;
private final ItemCallback<T> diffCallback;

Expand Down Expand Up @@ -67,11 +67,25 @@ public List<T> getCurrentList() {
* diff to cancel, false otherwise.
*/
public boolean cancelDiff() {
boolean diffInProgress = maxScheduledGeneration > maxFinishedGeneration;
boolean diffInProgress = isDiffInProgress();
maxFinishedGeneration = maxScheduledGeneration;
return diffInProgress;
}

public boolean isDiffInProgress() {
return maxScheduledGeneration > maxFinishedGeneration;
}

/**
* Set the current list without performing any diffing. Cancels any diff in progress.
* <p>
* This can be used if you notified a change to the adapter manually and need this list to be
* synced.
*/
public void forceListOverride(@Nullable List<T> newList) {
onRunCompleted(newList, ++maxScheduledGeneration);
}

/**
* Pass a new List to the AdapterHelper. Adapter updates will be computed on a background
* thread.
Expand All @@ -83,7 +97,7 @@ public boolean cancelDiff() {
* @param newList The new List.
*/
@SuppressWarnings("WeakerAccess")
public void submitList(final List<T> newList) {
public void submitList(@Nullable final List<T> newList) {
if (newList == list) {
// nothing to do
return;
Expand All @@ -92,19 +106,18 @@ public void submitList(final List<T> newList) {
// incrementing generation means any currently-running diffs are discarded when they finish
final int runGeneration = ++maxScheduledGeneration;

if (newList == null) {
//noinspection ConstantConditions
updateCallback.onRemoved(0, list.size());
list = null;
readOnlyList = Collections.emptyList();
if (newList == null || newList.isEmpty()) {
if (list != null && !list.isEmpty()) {
updateCallback.onRemoved(0, list.size());
}
onRunCompleted(null, runGeneration);
return;
}

if (list == null) {
if (list == null || list.isEmpty()) {
// fast simple first insert
updateCallback.onInserted(0, newList.size());
list = newList;
readOnlyList = Collections.unmodifiableList(newList);
onRunCompleted(newList, runGeneration);
return;
}

Expand All @@ -119,19 +132,24 @@ public void run() {
@Override
public void run() {
if (maxScheduledGeneration == runGeneration && runGeneration > maxFinishedGeneration) {
latchList(wrappedCallback.newList, result);
maxFinishedGeneration = runGeneration;
result.dispatchUpdatesTo(updateCallback);
onRunCompleted(newList, runGeneration);
}
}
});
}
});
}

private void latchList(@NonNull List<T> newList, @NonNull DiffUtil.DiffResult diffResult) {
diffResult.dispatchUpdatesTo(updateCallback);
private void onRunCompleted(@Nullable List<T> newList, int runGeneration) {
maxFinishedGeneration = runGeneration;
list = newList;
readOnlyList = Collections.unmodifiableList(newList);

if (newList == null) {
readOnlyList = Collections.emptyList();
} else {
readOnlyList = Collections.unmodifiableList(newList);
}
}

private static class MainThreadExecutor implements Executor {
Expand Down
5 changes: 5 additions & 0 deletions epoxy-adapter/src/main/java/com/airbnb/epoxy/DiffPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.support.annotation.VisibleForTesting;
import android.support.v4.util.LongSparseArray;

import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -36,6 +37,10 @@ public class DiffPayload {
}
}

public DiffPayload(EpoxyModel<?> changedItem) {
this(Collections.singletonList(changedItem));
}

/**
* Looks through the payloads list and returns the first model found with the given model id. This
* assumes that the payloads list will only contain objects of type {@link DiffPayload}, and will
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.airbnb.epoxy;

import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.support.v7.util.DiffUtil.ItemCallback;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class EpoxyControllerAdapter extends BaseEpoxyAdapter {
Expand Down Expand Up @@ -147,7 +145,10 @@ void moveModel(int fromPosition, int toPosition) {
notifyItemMoved(fromPosition, toPosition);
notifyBlocker.blockChanges();

if (differ.cancelDiff()) {
boolean interruptedDiff = differ.isDiffInProgress();
differ.forceListOverride(updatedList);

if (interruptedDiff) {
// The move interrupted a model rebuild/diff that was in progress,
// so models may be out of date and we should force them to rebuilt
epoxyController.requestModelBuild();
Expand All @@ -168,7 +169,7 @@ public boolean areContentsTheSame(EpoxyModel<?> oldItem, EpoxyModel<?> newItem)

@Override
public Object getChangePayload(EpoxyModel<?> oldItem, EpoxyModel<?> newItem) {
return new DiffPayload(Collections.singletonList(oldItem));
return new DiffPayload(oldItem);
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,4 @@ public void onChanged(int position, int count, Object payload) {
adapter.notifyItemRangeChanged(position, count, payload);
blockChanges();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ public class SimpleEpoxyController extends EpoxyController {
private List<? extends EpoxyModel<?>> currentModels;
private boolean insideSetModels;

public SimpleEpoxyController() {
setDebugLoggingEnabled(true);
}

/**
* Set the models to add to this controller. Clears any previous models and adds this new list
* .
Expand Down

0 comments on commit ad2493c

Please sign in to comment.