Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/suggestions/TileGroup.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/suggestions/TileGroup.java b/chrome/android/java/src/org/chromium/chrome/browser/suggestions/TileGroup.java |
| index 97358343430531bad0ffadafc8a6a99cfc4c22f2..d5a628c99d1ce1802cdcd1d961e6b45754496123 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/suggestions/TileGroup.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/suggestions/TileGroup.java |
| @@ -22,6 +22,7 @@ |
| import android.view.ViewGroup; |
| import org.chromium.base.ApiCompatibilityUtils; |
| +import org.chromium.base.Callback; |
| import org.chromium.base.Log; |
| import org.chromium.base.VisibleForTesting; |
| import org.chromium.chrome.R; |
| @@ -48,7 +49,11 @@ |
| * Performs work in other parts of the system that the {@link TileGroup} should not know about. |
| */ |
| public interface Delegate { |
| - void removeMostVisitedItem(Tile tile); |
| + /** |
| + * @param tile The tile corresponding to the most visited item to remove. |
| + * @param removalUndoneCallback The callback to invoke if the removal is reverted. |
| + */ |
| + void removeMostVisitedItem(Tile tile, @Nullable Callback<String> removalUndoneCallback); |
|
Michael van Ouwerkerk
2017/03/17 15:53:07
Why is the callback nullable? Please document what
dgn
2017/03/17 16:13:45
I intended the callback to be optional but there i
|
| void openMostVisitedItem(int windowDisposition, Tile tile); |
| @@ -153,6 +158,14 @@ |
| @Nullable |
| private String mPendingRemovalUrl; |
| + /** |
| + * URL of the most recently added tile. Used to identify when a given tile's insertion is |
| + * confirmed by the tile backend. This is relevant when a previously existing tile is removed, |
| + * then the user undoes the action and wants that tile back. |
| + */ |
| + @Nullable |
| + private String mPendingInsertionUrl; |
| + |
| private boolean mHasReceivedData; |
| /** |
| @@ -196,6 +209,8 @@ public TileGroup(Context context, SuggestionsUiDelegate uiDelegate, |
| public void onMostVisitedURLsAvailable(final String[] titles, final String[] urls, |
| final String[] whitelistIconPaths, final int[] sources) { |
| boolean removalCompleted = mPendingRemovalUrl != null; |
| + boolean insertionCompleted = mPendingInsertionUrl == null; |
| + |
| Set<String> addedUrls = new HashSet<>(); |
| mPendingTileData = new Tile[titles.length]; |
| for (int i = 0; i < titles.length; i++) { |
| @@ -213,10 +228,20 @@ public void onMostVisitedURLsAvailable(final String[] titles, final String[] url |
| addedUrls.add(urls[i]); |
| if (urls[i].equals(mPendingRemovalUrl)) removalCompleted = false; |
| + if (urls[i].equals(mPendingInsertionUrl)) insertionCompleted = true; |
| } |
| - if (removalCompleted) mPendingRemovalUrl = null; |
| - if (!mHasReceivedData || !mUiDelegate.isVisible() || removalCompleted) loadTiles(); |
| + boolean expectedChangeCompleted = false; |
| + if (mPendingRemovalUrl != null && removalCompleted) { |
| + mPendingRemovalUrl = null; |
| + expectedChangeCompleted = true; |
| + } |
| + if (mPendingInsertionUrl != null && insertionCompleted) { |
| + mPendingInsertionUrl = null; |
| + expectedChangeCompleted = true; |
| + } |
| + |
| + if (!mHasReceivedData || !mUiDelegate.isVisible() || expectedChangeCompleted) loadTiles(); |
| } |
| @Override |
| @@ -436,7 +461,7 @@ public void removeItem() { |
| // Note: This does not track all the removals, but will track the most recent one. If |
| // that removal is committed, it's good enough for change detection. |
| mPendingRemovalUrl = mUrl; |
| - mTileGroupDelegate.removeMostVisitedItem(tile); |
| + mTileGroupDelegate.removeMostVisitedItem(tile, new RemovalUndoneCallback()); |
| } |
| @Override |
| @@ -459,6 +484,13 @@ public void onCreateContextMenu( |
| } |
| } |
| + private class RemovalUndoneCallback extends Callback<String> { |
| + @Override |
| + public void onResult(String result) { |
| + mPendingInsertionUrl = result; |
| + } |
| + } |
| + |
| private class OfflineModelObserver extends SuggestionsOfflineModelObserver<Tile> { |
| public OfflineModelObserver(OfflinePageBridge bridge) { |
| super(bridge); |