Commit 475a0b47 authored by Bernhard Bauer's avatar Bernhard Bauer Committed by Commit Bot

Push notification convenience methods from ChildNode into ListObservable

Bug: 847420
Change-Id: I0e0441c339b53e12eb87735de2eb5ab978eecea4
Reviewed-on: https://chromium-review.googlesource.com/1095174Reviewed-by: default avatarMichael van Ouwerkerk <mvanouwerkerk@chromium.org>
Commit-Queue: Bernhard Bauer <bauerb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#566395}
parent 5a7fa9df
......@@ -63,7 +63,6 @@ public abstract class BatchListObservable extends ListObservable<Void> {
@Override
protected void notifyItemRangeChanged(int index, int count, @Nullable Void payload) {
assert payload == null;
mBatchingCallback.onChanged(index, count, null);
}
......
......@@ -35,11 +35,11 @@ class DecoratedListItemModel extends ListObservable<Void> implements ListObserve
mHeaderItem = item;
if (oldHeaderItem != null && item == null) {
notifyItemRangeRemoved(0, 1);
notifyItemRemoved(0);
} else if (oldHeaderItem == null && item != null) {
notifyItemRangeInserted(0, 1);
notifyItemInserted(0);
} else {
notifyItemRangeChanged(0, 1, null);
notifyItemRangeChanged(0, 1);
}
}
......
......@@ -29,19 +29,19 @@ class ListItemModel extends BatchListObservable {
/** Adds {@code item} to this list at {@code index}. */
public void addItem(int index, ListItem item) {
mItems.add(index, item);
notifyItemRangeInserted(index, 1);
notifyItemInserted(index);
}
/** Removes the {@link ListItem} at {@code index}. */
public void removeItem(int index) {
mItems.remove(index);
notifyItemRangeRemoved(index, 1);
notifyItemRemoved(index);
}
/** Sets the {@link ListItem} at {@code index} to {@code item}. */
public void setItem(int index, ListItem item) {
mItems.set(index, item);
notifyItemRangeChanged(index, 1, null);
notifyItemChanged(index);
}
/** @return The {@link ListItem} at {@code index}. */
......
......@@ -76,6 +76,26 @@ public abstract class ListObservable<P> {
assert success;
}
protected final void notifyItemChanged(int index) {
notifyItemRangeChanged(index, 1, null);
}
protected final void notifyItemRangeChanged(int index, int count) {
notifyItemRangeChanged(index, count, null);
}
protected final void notifyItemChanged(int index, @Nullable P payload) {
notifyItemRangeChanged(index, 1, payload);
}
protected final void notifyItemInserted(int index) {
notifyItemRangeInserted(index, 1);
}
protected final void notifyItemRemoved(int index) {
notifyItemRangeRemoved(index, 1);
}
/**
* Notifies observers that {@code count} items starting at position {@code index} have been
* added.
......
......@@ -37,7 +37,7 @@ public class SimpleListObservable<T> extends ListObservable<Void> {
*/
public void add(T item) {
mItems.add(item);
notifyItemRangeInserted(mItems.size() - 1, 1);
notifyItemInserted(mItems.size() - 1);
}
/**
......@@ -47,7 +47,7 @@ public class SimpleListObservable<T> extends ListObservable<Void> {
public void remove(T item) {
int position = mItems.indexOf(item);
mItems.remove(position);
notifyItemRangeRemoved(position, 1);
notifyItemRemoved(position);
}
/**
......@@ -72,7 +72,7 @@ public class SimpleListObservable<T> extends ListObservable<Void> {
mItems.addAll(newItems);
int min = Math.min(oldSize, newSize);
if (min > 0) notifyItemRangeChanged(0, min, null);
if (min > 0) notifyItemRangeChanged(0, min);
if (newSize > oldSize) {
notifyItemRangeInserted(min, newSize - oldSize);
......@@ -88,6 +88,6 @@ public class SimpleListObservable<T> extends ListObservable<Void> {
*/
public void update(int index, T item) {
mItems.set(index, item);
notifyItemRangeChanged(index, 1, null);
notifyItemRangeChanged(index, 1);
}
}
......@@ -124,6 +124,12 @@ public class ActionItem extends OptionalLeaf {
return mState;
}
public void maybeResetForDismiss() {
if (isVisible()) {
notifyItemChanged(0, NewTabPageRecyclerView::resetForDismissCallback);
}
}
/**
* Perform the Action associated with this ActionItem.
* @param uiDelegate A {@link SuggestionsUiDelegate} to provide context.
......
......@@ -33,12 +33,6 @@ public abstract class ChildNode extends ListObservable<PartialBindCallback> impl
super.notifyItemRangeChanged(index, count, callback);
}
// TODO(bauerb): Push these convenience methods to the base class once they're only called
// from subclasses.
protected void notifyItemRangeChanged(int index, int count) {
notifyItemRangeChanged(index, count, null);
}
@Override
protected void notifyItemRangeInserted(int index, int count) {
mNumItems += count;
......@@ -55,28 +49,6 @@ public abstract class ChildNode extends ListObservable<PartialBindCallback> impl
super.notifyItemRangeRemoved(index, count);
}
protected void notifyItemChanged(int index, @Nullable PartialBindCallback callback) {
notifyItemRangeChanged(index, 1, callback);
}
/**
* @deprecated Change notifications without payload recreate the view holder. Is that on
* purpose? Use {@link #notifyItemChanged(int, PartialBindCallback)} if the item to be notified
* should not be entirely replaced. (see https://crbug.com/704130)
*/
@Deprecated // Can be valid in specific cases, but marked as deprecated to provide the warning.
protected void notifyItemChanged(int index) {
notifyItemRangeChanged(index, 1);
}
protected void notifyItemInserted(int index) {
notifyItemRangeInserted(index, 1);
}
protected void notifyItemRemoved(int index) {
notifyItemRangeRemoved(index, 1);
}
protected void checkIndex(int position) {
if (!isRangeValid(position, 1)) {
throw new IndexOutOfBoundsException(position + "/" + getItemCount());
......
......@@ -255,9 +255,7 @@ public class SuggestionsSection extends InnerNode {
// When the ActionItem stops being dismissable, it is possible that it was being
// interacted with. We need to reset the view's related property changes.
if (mMoreButton.isVisible()) {
mMoreButton.notifyItemChanged(0, NewTabPageRecyclerView::resetForDismissCallback);
}
mMoreButton.maybeResetForDismiss();
}
@Override
......
......@@ -322,7 +322,7 @@ public class SectionListTest {
assertTrue(sectionList.isEmpty());
// Verify that the section has been detached by notifying its parent about changes. If not
// detached, it should crash.
section.notifyItemRangeChanged(0, 1);
section.notifyItemRangeChanged(0, 1, null);
}
@Test
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment