Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapter.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapter.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapter.java |
| index 4048be5cddb9a19c98e00fda7e0cfad87b7b5782..d4d45f283602c79f642b63e3035ea99676cbc6dc 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapter.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapter.java |
| @@ -39,6 +39,7 @@ |
| private final AboveTheFoldListItem mAboveTheFoldListItem; |
| private final List<NewTabPageListItem> mNewTabPageListItems; |
| private final ItemTouchCallbacks mItemTouchCallbacks; |
| + private boolean mIsExpectingSnippets; |
|
Bernhard Bauer
2016/05/17 09:12:48
Nit: "is expecting snippets" makes it sound like i
dgn
2016/05/17 10:46:00
Done.
|
| private static final Interpolator FADE_INTERPOLATOR = new FastOutLinearInInterpolator(); |
| @@ -93,6 +94,7 @@ public NewTabPageAdapter(NewTabPageManager manager, NewTabPageLayout newTabPageL |
| mItemTouchCallbacks = new ItemTouchCallbacks(); |
| mNewTabPageListItems = new ArrayList<NewTabPageListItem>(); |
| mNewTabPageListItems.add(mAboveTheFoldListItem); |
| + mIsExpectingSnippets = true; |
| mNewTabPageManager.setSnippetsObserver(this); |
| } |
| @@ -103,31 +105,23 @@ public NewTabPageAdapter(NewTabPageManager manager, NewTabPageLayout newTabPageL |
| @Override |
| public void onSnippetsReceived(List<SnippetArticle> listSnippets) { |
| + if (!mIsExpectingSnippets) return; |
| + |
| int newSnippetCount = listSnippets.size(); |
| Log.d(TAG, "Received %d new snippets.", newSnippetCount); |
| // At first, there might be no snippets available, we wait until they have been fetched. |
| if (newSnippetCount == 0) return; |
| - // Copy thumbnails over |
| - for (SnippetArticle newSnippet : listSnippets) { |
| - int existingSnippetIdx = mNewTabPageListItems.indexOf(newSnippet); |
| - if (existingSnippetIdx == -1) continue; |
| - |
| - newSnippet.setThumbnailBitmap( |
| - ((SnippetArticle) mNewTabPageListItems.get(existingSnippetIdx)) |
| - .getThumbnailBitmap()); |
| - } |
| - |
| - mNewTabPageListItems.clear(); |
| - mNewTabPageListItems.add(mAboveTheFoldListItem); |
| - mNewTabPageListItems.add(new SnippetHeaderListItem()); |
| - mNewTabPageListItems.addAll(listSnippets); |
| - |
| - notifyDataSetChanged(); |
| + loadSnippets(listSnippets); |
| // We don't want to get notified of other changes. |
| - mNewTabPageManager.setSnippetsObserver(null); |
| + mIsExpectingSnippets = false; |
| + } |
| + |
| + @Override |
| + public void onSnippetsCleared() { |
| + loadSnippets(new ArrayList<SnippetArticle>()); |
| } |
| @Override |
| @@ -164,6 +158,25 @@ public int getItemCount() { |
| return mNewTabPageListItems.size(); |
| } |
| + private void loadSnippets(List<SnippetArticle> listSnippets) { |
| + // Copy thumbnails over |
| + for (SnippetArticle newSnippet : listSnippets) { |
| + int existingSnippetIdx = mNewTabPageListItems.indexOf(newSnippet); |
| + if (existingSnippetIdx == -1) continue; |
| + |
| + newSnippet.setThumbnailBitmap( |
| + ((SnippetArticle) mNewTabPageListItems.get(existingSnippetIdx)) |
| + .getThumbnailBitmap()); |
| + } |
| + |
| + mNewTabPageListItems.clear(); |
| + mNewTabPageListItems.add(mAboveTheFoldListItem); |
| + mNewTabPageListItems.add(new SnippetHeaderListItem()); |
| + mNewTabPageListItems.addAll(listSnippets); |
| + |
| + notifyDataSetChanged(); |
| + } |
| + |
| private void dismissItem(int position) { |
| assert getItemViewType(position) == NewTabPageListItem.VIEW_TYPE_SNIPPET; |
| mNewTabPageManager.onSnippetDismissed((SnippetArticle) mNewTabPageListItems.get(position)); |