Commit 109eda25 authored by Bernhard Bauer's avatar Bernhard Bauer Committed by Commit Bot

Separate SuggestionsList into a model and a ModelChangeProcessor

This will allow extracting a mediator that interacts only with the model.

Bug: 847420

Change-Id: I2cfc0f816ae04570dcc12f40757339fc4ed5de0c
Reviewed-on: https://chromium-review.googlesource.com/1142153Reviewed-by: default avatarTheresa <twellington@chromium.org>
Commit-Queue: Bernhard Bauer <bauerb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#580489}
parent 3d4d85f7
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.modelutil;
import android.support.annotation.Nullable;
import java.util.Collection;
/**
* Represents a list of (property-)observable items, and notifies about changes to any of its items.
*
* @param <T> The type of item in the list.
* @param <P> The property key type for {@code T} to be used as payload for partial updates.
*/
public class PropertyListObservable<T extends PropertyObservable<P>, P>
extends SimpleListObservableBase<T, P> {
private final PropertyObservable.PropertyObserver<P> mPropertyObserver =
this::onPropertyChanged;
@Override
public void add(T item) {
super.add(item);
item.addObserver(mPropertyObserver);
}
@Override
public void remove(T item) {
item.removeObserver(mPropertyObserver);
super.remove(item);
}
@Override
public void update(int index, T item) {
get(index).removeObserver(mPropertyObserver);
super.update(index, item);
item.addObserver(mPropertyObserver);
}
@Override
public void set(Collection<T> newItems) {
for (T item : this) {
item.removeObserver(mPropertyObserver);
}
super.set(newItems);
for (T item : newItems) {
item.addObserver(mPropertyObserver);
}
}
private void onPropertyChanged(PropertyObservable<P> source, @Nullable P propertyKey) {
notifyItemChanged(indexOf(source), propertyKey);
}
}
...@@ -77,10 +77,12 @@ public class SimpleListObservableBase<T, P> extends ListObservableImpl<P> implem ...@@ -77,10 +77,12 @@ public class SimpleListObservableBase<T, P> extends ListObservableImpl<P> implem
/** /**
* Removes an item by position from the held {@link List}. Notifies observers about the removal. * Removes an item by position from the held {@link List}. Notifies observers about the removal.
* @param position The position of the item to be removed. * @param position The position of the item to be removed.
* @return The item that has been removed.
*/ */
public void removeAt(int position) { public T removeAt(int position) {
mItems.remove(position); T item = mItems.remove(position);
notifyItemRemoved(position); notifyItemRemoved(position);
return item;
} }
/** /**
......
...@@ -9,6 +9,8 @@ import android.support.annotation.ColorInt; ...@@ -9,6 +9,8 @@ import android.support.annotation.ColorInt;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import org.chromium.base.DiscardableReferencePool.DiscardableReference; import org.chromium.base.DiscardableReferencePool.DiscardableReference;
import org.chromium.chrome.browser.modelutil.PropertyObservable;
import org.chromium.chrome.browser.ntp.cards.NewTabPageViewHolder.PartialBindCallback;
import org.chromium.chrome.browser.suggestions.OfflinableSuggestion; import org.chromium.chrome.browser.suggestions.OfflinableSuggestion;
import java.io.File; import java.io.File;
...@@ -16,7 +18,8 @@ import java.io.File; ...@@ -16,7 +18,8 @@ import java.io.File;
/** /**
* Represents the data for an article card on the NTP. * Represents the data for an article card on the NTP.
*/ */
public class SnippetArticle implements OfflinableSuggestion { public class SnippetArticle
extends PropertyObservable<PartialBindCallback> implements OfflinableSuggestion {
/** The category of this article. */ /** The category of this article. */
public final int mCategory; public final int mCategory;
......
...@@ -809,6 +809,7 @@ chrome_java_sources = [ ...@@ -809,6 +809,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/modelutil/ListObservable.java", "java/src/org/chromium/chrome/browser/modelutil/ListObservable.java",
"java/src/org/chromium/chrome/browser/modelutil/ListObservableImpl.java", "java/src/org/chromium/chrome/browser/modelutil/ListObservableImpl.java",
"java/src/org/chromium/chrome/browser/modelutil/PropertyKey.java", "java/src/org/chromium/chrome/browser/modelutil/PropertyKey.java",
"java/src/org/chromium/chrome/browser/modelutil/PropertyListObservable.java",
"java/src/org/chromium/chrome/browser/modelutil/PropertyModel.java", "java/src/org/chromium/chrome/browser/modelutil/PropertyModel.java",
"java/src/org/chromium/chrome/browser/modelutil/PropertyModelChangeProcessor.java", "java/src/org/chromium/chrome/browser/modelutil/PropertyModelChangeProcessor.java",
"java/src/org/chromium/chrome/browser/modelutil/PropertyObservable.java", "java/src/org/chromium/chrome/browser/modelutil/PropertyObservable.java",
......
...@@ -16,6 +16,7 @@ import static org.junit.Assert.assertTrue; ...@@ -16,6 +16,7 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.same; import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doNothing;
...@@ -576,6 +577,7 @@ public class SuggestionsSectionTest { ...@@ -576,6 +577,7 @@ public class SuggestionsSectionTest {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Callback<String> callback = mock(Callback.class); Callback<String> callback = mock(Callback.class);
section.dismissItem(1, callback); section.dismissItem(1, callback);
verify(callback).onResult(anyString());
} }
assertEquals(0, section.getSuggestionsCount()); assertEquals(0, section.getSuggestionsCount());
......
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