Skip to content

Commit

Permalink
Add notify model changed method (#1063)
Browse files Browse the repository at this point in the history
Co-authored-by: berendjan <kDt2ygc7WsBBZtK>
  • Loading branch information
berendjan authored Sep 30, 2020
1 parent 302ff90 commit 3905f50
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
16 changes: 16 additions & 0 deletions epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,22 @@ public void moveModel(int fromPosition, int toPosition) {
requestDelayedModelBuild(500);
}


/**
* An way to notify the adapter that a model has changed. This is intended to be used with
* {@link androidx.recyclerview.widget.ItemTouchHelper} to allow revert swiping a model.
* <p>
* This will immediately notify the change to the RecyclerView.
*
* @param position Position of the item.
*/
public void notifyModelChanged(int position) {
assertNotBuildingModels();

adapter.notifyModelChanged(position);
}


/**
* Get the underlying adapter built by this controller. Use this to get the adapter to set on a
* RecyclerView, or to get information about models currently in use.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,23 @@ void moveModel(int fromPosition, int toPosition) {
}
}

@UiThread
void notifyModelChanged(int position) {
ArrayList<EpoxyModel<?>> updatedList = new ArrayList<>(getCurrentModels());

notifyBlocker.allowChanges();
notifyItemChanged(position);
notifyBlocker.blockChanges();

boolean interruptedDiff = 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();
}
}

private static final ItemCallback<EpoxyModel<?>> ITEM_CALLBACK =
new ItemCallback<EpoxyModel<?>>() {
@Override
Expand Down

0 comments on commit 3905f50

Please sign in to comment.