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
int numberOfSuggestionsExposed = getNumberOfSuggestionsExposed();
if (!canUpdateSuggestions(numberOfSuggestionsExposed)) {
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());
return;
}
......@@ -354,8 +354,7 @@ public class SuggestionsSection extends InnerNode<NewTabPageViewHolder, PartialB
if (numberOfSuggestionsExposed > 0) {
mIsDataStale = true;
Log.d(TAG,
"updateSuggestions: Category %d is stale, will keep already seen suggestions.",
Log.d(TAG, "updateModels: Category %d is stale, will keep already seen suggestions.",
getCategory());
}
appendSuggestions(suggestions, /* keepSectionSize = */ true,
......@@ -377,8 +376,7 @@ public class SuggestionsSection extends InnerNode<NewTabPageViewHolder, PartialB
int numberOfSuggestionsExposed = getNumberOfSuggestionsExposed();
if (keepSectionSize) {
Log.d(TAG, "updateSuggestions: keeping the first %d suggestion",
numberOfSuggestionsExposed);
Log.d(TAG, "updateModels: keeping the first %d suggestion", numberOfSuggestionsExposed);
int numSuggestionsToAppend =
Math.max(0, suggestions.size() - numberOfSuggestionsExposed);
int itemCount = mSuggestions.size();
......@@ -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 (CardsVariationParameters.ignoreUpdatesForExistingSuggestions()) {
Log.d(TAG, "updateSuggestions: replacing existing suggestion disabled");
Log.d(TAG, "updateModels: replacing existing suggestion disabled");
NewTabPageUma.recordUIUpdateResult(
NewTabPageUma.ContentSuggestionsUIUpdateResult.FAIL_DISABLED);
return false;
......@@ -441,7 +439,7 @@ public class SuggestionsSection extends InnerNode<NewTabPageViewHolder, PartialB
if (numberOfSuggestionsExposed >= getSuggestionsCount() || mHasAppended) {
// 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.
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.ContentSuggestionsUIUpdateResult.FAIL_ALL_SEEN);
return false;
......
......@@ -53,7 +53,7 @@ class SuggestionListViewBinder {
} else if (SuggestionListProperties.EMBEDDER.equals(propertyKey)) {
view.listView.setEmbedder(model.get(SuggestionListProperties.EMBEDDER));
} 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);
} else if (SuggestionListProperties.USE_DARK_BACKGROUND.equals(propertyKey)) {
view.listView.refreshPopupBackground(
......
......@@ -603,7 +603,7 @@ public class SuggestionsSectionTest {
/**
* 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
* 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
@Feature({"Ntp"})
......
......@@ -36,7 +36,7 @@ public class ModelListAdapter extends BaseAdapter {
}
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>>
mViewBuilderMap = new SparseArray<>();
......@@ -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) {
mSuggestionItems.clear();
mSuggestionItems.addAll(suggestionModels);
public void updateModels(List<Pair<Integer, PropertyModel>> models) {
mModelList.clear();
mModelList.addAll(models);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mSuggestionItems.size();
return mModelList.size();
}
@Override
public Object getItem(int position) {
return mSuggestionItems.get(position);
return mModelList.get(position);
}
@Override
......@@ -83,7 +83,7 @@ public class ModelListAdapter extends BaseAdapter {
@Override
public int getItemViewType(int position) {
return mSuggestionItems.get(position).first;
return mModelList.get(position).first;
}
@Override
......@@ -96,33 +96,33 @@ public class ModelListAdapter extends BaseAdapter {
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null || convertView.getTag(R.id.view_type) == null
|| (int) convertView.getTag(R.id.view_type) != getItemViewType(position)) {
int suggestionTypeId = mSuggestionItems.get(position).first;
convertView = mViewBuilderMap.get(suggestionTypeId).first.buildView();
int modelTypeId = mModelList.get(position).first;
convertView = mViewBuilderMap.get(modelTypeId).first.buildView();
// 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
// and identify what type the view is. This should allow lists that aren't necessarily
// 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 =
getOrCreateModelFromExisting(convertView, mSuggestionItems.get(position));
for (PropertyKey key : suggestionModel.getAllSetProperties()) {
getOrCreateModelFromExisting(convertView, mModelList.get(position));
for (PropertyKey key : model.getAllSetProperties()) {
if (key instanceof WritableIntPropertyKey) {
WritableIntPropertyKey intKey = (WritableIntPropertyKey) key;
viewModel.set(intKey, suggestionModel.get(intKey));
viewModel.set(intKey, model.get(intKey));
} else if (key instanceof WritableBooleanPropertyKey) {
WritableBooleanPropertyKey booleanKey = (WritableBooleanPropertyKey) key;
viewModel.set(booleanKey, suggestionModel.get(booleanKey));
viewModel.set(booleanKey, model.get(booleanKey));
} else if (key instanceof WritableFloatPropertyKey) {
WritableFloatPropertyKey floatKey = (WritableFloatPropertyKey) key;
viewModel.set(floatKey, suggestionModel.get(floatKey));
viewModel.set(floatKey, model.get(floatKey));
} else if (key instanceof WritableObjectPropertyKey<?>) {
@SuppressWarnings({"unchecked", "rawtypes"})
WritableObjectPropertyKey objectKey = (WritableObjectPropertyKey) key;
viewModel.set(objectKey, suggestionModel.get(objectKey));
viewModel.set(objectKey, model.get(objectKey));
} else {
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