Commit e4350116 authored by Matthew Jones's avatar Matthew Jones Committed by Commit Bot

Update public API of ModelListAdapter to be more generic

This patch is largely removing the use of 'suggestion' from a
generic MVC class.

Change-Id: I20e5932d0638da0489ed002ffa8555874bd10eba
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1506206Reviewed-by: default avatarTheresa <twellington@chromium.org>
Commit-Queue: Matthew Jones <mdjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#638625}
parent 9dbd723e
...@@ -338,7 +338,7 @@ public class SuggestionsSection extends InnerNode<NewTabPageViewHolder, PartialB ...@@ -338,7 +338,7 @@ public class SuggestionsSection extends InnerNode<NewTabPageViewHolder, PartialB
int numberOfSuggestionsExposed = getNumberOfSuggestionsExposed(); int numberOfSuggestionsExposed = getNumberOfSuggestionsExposed();
if (!canUpdateSuggestions(numberOfSuggestionsExposed)) { if (!canUpdateSuggestions(numberOfSuggestionsExposed)) {
mIsDataStale = true; mIsDataStale = true;
Log.d(TAG, "updateSuggestions: Category %d is stale, it can't replace suggestions.", Log.d(TAG, "updateModels: Category %d is stale, it can't replace suggestions.",
getCategory()); getCategory());
return; return;
} }
...@@ -354,8 +354,7 @@ public class SuggestionsSection extends InnerNode<NewTabPageViewHolder, PartialB ...@@ -354,8 +354,7 @@ public class SuggestionsSection extends InnerNode<NewTabPageViewHolder, PartialB
if (numberOfSuggestionsExposed > 0) { if (numberOfSuggestionsExposed > 0) {
mIsDataStale = true; mIsDataStale = true;
Log.d(TAG, Log.d(TAG, "updateModels: Category %d is stale, will keep already seen suggestions.",
"updateSuggestions: Category %d is stale, will keep already seen suggestions.",
getCategory()); getCategory());
} }
appendSuggestions(suggestions, /* keepSectionSize = */ true, appendSuggestions(suggestions, /* keepSectionSize = */ true,
...@@ -377,8 +376,7 @@ public class SuggestionsSection extends InnerNode<NewTabPageViewHolder, PartialB ...@@ -377,8 +376,7 @@ public class SuggestionsSection extends InnerNode<NewTabPageViewHolder, PartialB
int numberOfSuggestionsExposed = getNumberOfSuggestionsExposed(); int numberOfSuggestionsExposed = getNumberOfSuggestionsExposed();
if (keepSectionSize) { if (keepSectionSize) {
Log.d(TAG, "updateSuggestions: keeping the first %d suggestion", Log.d(TAG, "updateModels: keeping the first %d suggestion", numberOfSuggestionsExposed);
numberOfSuggestionsExposed);
int numSuggestionsToAppend = int numSuggestionsToAppend =
Math.max(0, suggestions.size() - numberOfSuggestionsExposed); Math.max(0, suggestions.size() - numberOfSuggestionsExposed);
int itemCount = mSuggestions.size(); int itemCount = mSuggestions.size();
...@@ -432,7 +430,7 @@ public class SuggestionsSection extends InnerNode<NewTabPageViewHolder, PartialB ...@@ -432,7 +430,7 @@ public class SuggestionsSection extends InnerNode<NewTabPageViewHolder, PartialB
if (!hasSuggestions()) return true; // If we don't have any, we always accept updates. if (!hasSuggestions()) return true; // If we don't have any, we always accept updates.
if (CardsVariationParameters.ignoreUpdatesForExistingSuggestions()) { if (CardsVariationParameters.ignoreUpdatesForExistingSuggestions()) {
Log.d(TAG, "updateSuggestions: replacing existing suggestion disabled"); Log.d(TAG, "updateModels: replacing existing suggestion disabled");
NewTabPageUma.recordUIUpdateResult( NewTabPageUma.recordUIUpdateResult(
NewTabPageUma.ContentSuggestionsUIUpdateResult.FAIL_DISABLED); NewTabPageUma.ContentSuggestionsUIUpdateResult.FAIL_DISABLED);
return false; return false;
...@@ -441,7 +439,7 @@ public class SuggestionsSection extends InnerNode<NewTabPageViewHolder, PartialB ...@@ -441,7 +439,7 @@ public class SuggestionsSection extends InnerNode<NewTabPageViewHolder, PartialB
if (numberOfSuggestionsExposed >= getSuggestionsCount() || mHasAppended) { if (numberOfSuggestionsExposed >= getSuggestionsCount() || mHasAppended) {
// In case that suggestions got removed, we assume they already were seen. This might // In case that suggestions got removed, we assume they already were seen. This might
// be over-simplifying things, but given the rare occurences it should be good enough. // be over-simplifying things, but given the rare occurences it should be good enough.
Log.d(TAG, "updateSuggestions: replacing existing suggestion not possible, all seen"); Log.d(TAG, "updateModels: replacing existing suggestion not possible, all seen");
NewTabPageUma.recordUIUpdateResult( NewTabPageUma.recordUIUpdateResult(
NewTabPageUma.ContentSuggestionsUIUpdateResult.FAIL_ALL_SEEN); NewTabPageUma.ContentSuggestionsUIUpdateResult.FAIL_ALL_SEEN);
return false; return false;
......
...@@ -53,7 +53,7 @@ class SuggestionListViewBinder { ...@@ -53,7 +53,7 @@ class SuggestionListViewBinder {
} else if (SuggestionListProperties.EMBEDDER.equals(propertyKey)) { } else if (SuggestionListProperties.EMBEDDER.equals(propertyKey)) {
view.listView.setEmbedder(model.get(SuggestionListProperties.EMBEDDER)); view.listView.setEmbedder(model.get(SuggestionListProperties.EMBEDDER));
} else if (SuggestionListProperties.SUGGESTION_MODELS.equals(propertyKey)) { } else if (SuggestionListProperties.SUGGESTION_MODELS.equals(propertyKey)) {
view.adapter.updateSuggestions(model.get(SuggestionListProperties.SUGGESTION_MODELS)); view.adapter.updateModels(model.get(SuggestionListProperties.SUGGESTION_MODELS));
view.listView.setSelection(0); view.listView.setSelection(0);
} else if (SuggestionListProperties.USE_DARK_BACKGROUND.equals(propertyKey)) { } else if (SuggestionListProperties.USE_DARK_BACKGROUND.equals(propertyKey)) {
view.listView.refreshPopupBackground( view.listView.refreshPopupBackground(
......
...@@ -603,7 +603,7 @@ public class SuggestionsSectionTest { ...@@ -603,7 +603,7 @@ public class SuggestionsSectionTest {
/** /**
* Tests that the More button appends new suggestions after dismissing all items. The tricky * Tests that the More button appends new suggestions after dismissing all items. The tricky
* condition is that if a section is empty, we issue a fetch instead of a fetch-more. This means * condition is that if a section is empty, we issue a fetch instead of a fetch-more. This means
* we are using the 'updateSuggestions()' flow to append to the list the user is looking at. * we are using the 'updateModels()' flow to append to the list the user is looking at.
*/ */
@Test @Test
@Feature({"Ntp"}) @Feature({"Ntp"})
......
...@@ -36,7 +36,7 @@ public class ModelListAdapter extends BaseAdapter { ...@@ -36,7 +36,7 @@ public class ModelListAdapter extends BaseAdapter {
} }
private final Context mContext; private final Context mContext;
private final List<Pair<Integer, PropertyModel>> mSuggestionItems = new ArrayList<>(); private final List<Pair<Integer, PropertyModel>> mModelList = new ArrayList<>();
private final SparseArray<Pair<ViewBuilder, PropertyModelChangeProcessor.ViewBinder>> private final SparseArray<Pair<ViewBuilder, PropertyModelChangeProcessor.ViewBinder>>
mViewBuilderMap = new SparseArray<>(); mViewBuilderMap = new SparseArray<>();
...@@ -45,22 +45,22 @@ public class ModelListAdapter extends BaseAdapter { ...@@ -45,22 +45,22 @@ public class ModelListAdapter extends BaseAdapter {
} }
/** /**
* Update the visible omnibox suggestions. * Update the visible models (list items).
*/ */
public void updateSuggestions(List<Pair<Integer, PropertyModel>> suggestionModels) { public void updateModels(List<Pair<Integer, PropertyModel>> models) {
mSuggestionItems.clear(); mModelList.clear();
mSuggestionItems.addAll(suggestionModels); mModelList.addAll(models);
notifyDataSetChanged(); notifyDataSetChanged();
} }
@Override @Override
public int getCount() { public int getCount() {
return mSuggestionItems.size(); return mModelList.size();
} }
@Override @Override
public Object getItem(int position) { public Object getItem(int position) {
return mSuggestionItems.get(position); return mModelList.get(position);
} }
@Override @Override
...@@ -83,7 +83,7 @@ public class ModelListAdapter extends BaseAdapter { ...@@ -83,7 +83,7 @@ public class ModelListAdapter extends BaseAdapter {
@Override @Override
public int getItemViewType(int position) { public int getItemViewType(int position) {
return mSuggestionItems.get(position).first; return mModelList.get(position).first;
} }
@Override @Override
...@@ -96,33 +96,33 @@ public class ModelListAdapter extends BaseAdapter { ...@@ -96,33 +96,33 @@ public class ModelListAdapter extends BaseAdapter {
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null || convertView.getTag(R.id.view_type) == null if (convertView == null || convertView.getTag(R.id.view_type) == null
|| (int) convertView.getTag(R.id.view_type) != getItemViewType(position)) { || (int) convertView.getTag(R.id.view_type) != getItemViewType(position)) {
int suggestionTypeId = mSuggestionItems.get(position).first; int modelTypeId = mModelList.get(position).first;
convertView = mViewBuilderMap.get(suggestionTypeId).first.buildView(); convertView = mViewBuilderMap.get(modelTypeId).first.buildView();
// Since the view type returned by getView is not guaranteed to return a view of that // Since the view type returned by getView is not guaranteed to return a view of that
// type, we need a means of checking it. The "view_type" tag is attached to the views // type, we need a means of checking it. The "view_type" tag is attached to the views
// and identify what type the view is. This should allow lists that aren't necessarily // and identify what type the view is. This should allow lists that aren't necessarily
// recycler views to work correctly with heterogeneous lists. // recycler views to work correctly with heterogeneous lists.
convertView.setTag(R.id.view_type, suggestionTypeId); convertView.setTag(R.id.view_type, modelTypeId);
} }
PropertyModel suggestionModel = mSuggestionItems.get(position).second; PropertyModel model = mModelList.get(position).second;
PropertyModel viewModel = PropertyModel viewModel =
getOrCreateModelFromExisting(convertView, mSuggestionItems.get(position)); getOrCreateModelFromExisting(convertView, mModelList.get(position));
for (PropertyKey key : suggestionModel.getAllSetProperties()) { for (PropertyKey key : model.getAllSetProperties()) {
if (key instanceof WritableIntPropertyKey) { if (key instanceof WritableIntPropertyKey) {
WritableIntPropertyKey intKey = (WritableIntPropertyKey) key; WritableIntPropertyKey intKey = (WritableIntPropertyKey) key;
viewModel.set(intKey, suggestionModel.get(intKey)); viewModel.set(intKey, model.get(intKey));
} else if (key instanceof WritableBooleanPropertyKey) { } else if (key instanceof WritableBooleanPropertyKey) {
WritableBooleanPropertyKey booleanKey = (WritableBooleanPropertyKey) key; WritableBooleanPropertyKey booleanKey = (WritableBooleanPropertyKey) key;
viewModel.set(booleanKey, suggestionModel.get(booleanKey)); viewModel.set(booleanKey, model.get(booleanKey));
} else if (key instanceof WritableFloatPropertyKey) { } else if (key instanceof WritableFloatPropertyKey) {
WritableFloatPropertyKey floatKey = (WritableFloatPropertyKey) key; WritableFloatPropertyKey floatKey = (WritableFloatPropertyKey) key;
viewModel.set(floatKey, suggestionModel.get(floatKey)); viewModel.set(floatKey, model.get(floatKey));
} else if (key instanceof WritableObjectPropertyKey<?>) { } else if (key instanceof WritableObjectPropertyKey<?>) {
@SuppressWarnings({"unchecked", "rawtypes"}) @SuppressWarnings({"unchecked", "rawtypes"})
WritableObjectPropertyKey objectKey = (WritableObjectPropertyKey) key; WritableObjectPropertyKey objectKey = (WritableObjectPropertyKey) key;
viewModel.set(objectKey, suggestionModel.get(objectKey)); viewModel.set(objectKey, model.get(objectKey));
} else { } else {
assert false : "Unexpected key received"; assert false : "Unexpected key received";
} }
......
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