Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.snackbar; | |
| 6 | |
| 7 import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarController; | |
| 8 | |
| 9 import java.util.Deque; | |
| 10 import java.util.Iterator; | |
| 11 import java.util.LinkedList; | |
| 12 import java.util.Queue; | |
| 13 | |
| 14 /** | |
| 15 * A collection of {@link Snackbar}s. | |
|
newt (away)
2016/01/28 17:32:00
Explain why this class exists :)
The current comm
Ian Wen
2016/01/29 02:38:43
Done.
| |
| 16 */ | |
| 17 class SnackbarCollection { | |
| 18 private Deque<Snackbar> mStack = new LinkedList<>(); | |
| 19 private Queue<Snackbar> mQueue = new LinkedList<>(); | |
| 20 | |
| 21 /** | |
| 22 * Adds a new snackbar to the collection. If the new snackbar is of | |
| 23 * {@link Snackbar#TYPE_ACTION} and current snackbar is of | |
| 24 * {@link Snackbar#TYPE_NOTIFICATION}, the current snackbar will be removed from the | |
| 25 * collection immediately. | |
| 26 */ | |
| 27 public void add(Snackbar snackbar) { | |
|
newt (away)
2016/01/28 17:32:00
My personal preference is to remove "public" on al
Ian Wen
2016/01/29 02:38:43
Done.
| |
| 28 if (snackbar.isTypeAction()) { | |
| 29 if (getCurrent() != null && !getCurrent().isTypeAction()) { | |
| 30 removeCurrent(false); | |
| 31 } | |
| 32 mStack.push(snackbar); | |
| 33 } else { | |
| 34 mQueue.offer(snackbar); | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 /** | |
| 39 * Removes the current snackbar from the collection. | |
| 40 * @param isAction Whether the removal is triggered by user clicking the act ion button. | |
| 41 */ | |
| 42 public Snackbar removeCurrent(boolean isAction) { | |
|
newt (away)
2016/01/28 17:32:00
Since removeCurrent(false) shouldn't be called by
Ian Wen
2016/01/29 02:38:42
Ah good suggestion!! Done.
| |
| 43 Snackbar current = !mStack.isEmpty() ? mStack.pop() : mQueue.poll(); | |
| 44 if (current != null) { | |
| 45 SnackbarController controller = current.getController(); | |
| 46 if (isAction) controller.onAction(current.getActionData()); | |
| 47 else controller.onDismissNoAction(current.getActionData()); | |
| 48 } | |
| 49 return current; | |
| 50 } | |
| 51 | |
| 52 /** | |
| 53 * @return The snackbar that is currently displayed. | |
| 54 */ | |
| 55 public Snackbar getCurrent() { | |
| 56 return !mStack.isEmpty() ? mStack.peek() : mQueue.peek(); | |
| 57 } | |
| 58 | |
| 59 public boolean isEmpty() { | |
| 60 return mStack.isEmpty() && mQueue.isEmpty(); | |
| 61 } | |
| 62 | |
| 63 public void clear() { | |
| 64 while (!isEmpty()) { | |
| 65 removeCurrent(false); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 public void removeCurrentDueToTimeout() { | |
| 70 removeCurrent(false); | |
| 71 Snackbar current; | |
| 72 while ((current = getCurrent()) != null && current.isTypeAction()) { | |
| 73 removeCurrent(false); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 public boolean removeMatchingSnackbars(SnackbarController controller) { | |
| 78 boolean snackbarRemoved = false; | |
| 79 Iterator<Snackbar> iter = mStack.iterator(); | |
| 80 while (iter.hasNext()) { | |
| 81 Snackbar snackbar = iter.next(); | |
| 82 if (snackbar.getController() == controller) { | |
| 83 iter.remove(); | |
| 84 snackbarRemoved = true; | |
| 85 } | |
| 86 } | |
| 87 iter = mQueue.iterator(); | |
| 88 while (iter.hasNext()) { | |
| 89 Snackbar snackbar = iter.next(); | |
| 90 if (snackbar.getController() == controller) { | |
| 91 iter.remove(); | |
| 92 snackbarRemoved = true; | |
| 93 } | |
| 94 } | |
| 95 return snackbarRemoved; | |
| 96 } | |
| 97 | |
| 98 public boolean removeMatchingSnackbars(SnackbarController controller, Object data) { | |
| 99 boolean snackbarRemoved = false; | |
| 100 Iterator<Snackbar> iter = mStack.iterator(); | |
| 101 while (iter.hasNext()) { | |
| 102 Snackbar snackbar = iter.next(); | |
| 103 if (snackbar.getController() == controller | |
| 104 && objectsAreEqual(snackbar.getActionData(), data)) { | |
| 105 iter.remove(); | |
| 106 snackbarRemoved = true; | |
| 107 } | |
| 108 } | |
| 109 iter = mQueue.iterator(); | |
| 110 while (iter.hasNext()) { | |
| 111 Snackbar snackbar = iter.next(); | |
| 112 if (snackbar.getController() == controller | |
| 113 && objectsAreEqual(snackbar.getActionData(), data)) { | |
| 114 iter.remove(); | |
| 115 snackbarRemoved = true; | |
| 116 } | |
| 117 } | |
| 118 return snackbarRemoved; | |
| 119 } | |
| 120 | |
| 121 private static boolean objectsAreEqual(Object a, Object b) { | |
| 122 if (a == null && b == null) return true; | |
| 123 if (a == null || b == null) return false; | |
| 124 return a.equals(b); | |
| 125 } | |
| 126 } | |
| OLD | NEW |