Bug 1116693 - Support stopping/starting dispatch of onItemChanged updates r=rnewman

This commit is contained in:
Mark Finkle 2014-12-31 14:43:59 -05:00
parent 2cfccff4b5
commit 8dc5f3a0bf

View File

@ -52,6 +52,9 @@ public class GeckoMenuItem implements MenuItem {
final GeckoMenu mMenu;
OnShowAsActionChangedListener mShowAsActionChangedListener;
private volatile boolean mShouldDispatchChanges = true;
private volatile boolean mDidChange;
public GeckoMenuItem(GeckoMenu menu, int id, int order, int titleRes) {
mMenu = menu;
mId = id;
@ -66,6 +69,37 @@ public class GeckoMenuItem implements MenuItem {
setTitle(title);
}
/**
* Stop dispatching item changed events to presenters until
* [start|resume]DispatchingItemsChanged() is called. Useful when
* many menu operations are going to be performed as a batch.
*/
public void stopDispatchingChanges() {
mDidChange = false;
mShouldDispatchChanges = false;
}
/**
* Resume dispatching item changed events to presenters. This method
* will NOT call onItemChanged if any menu operations were queued.
* Only future menu operations will call onItemChanged. Useful for
* sequelching presenter updates.
*/
public void resumeDispatchingChanges() {
mShouldDispatchChanges = true;
}
/**
* Start dispatching item changed events to presenters. This method
* will call onItemChanged if any menu operations were queued.
*/
public void startDispatchingChanges() {
if (mDidChange) {
mMenu.onItemChanged(this);
}
mShouldDispatchChanges = true;
}
@Override
public boolean collapseActionView() {
return false;
@ -247,7 +281,11 @@ public class GeckoMenuItem implements MenuItem {
public MenuItem setCheckable(boolean checkable) {
if (mCheckable != checkable) {
mCheckable = checkable;
if (mShouldDispatchChanges) {
mMenu.onItemChanged(this);
} else {
mDidChange = true;
}
}
return this;
}
@ -256,7 +294,11 @@ public class GeckoMenuItem implements MenuItem {
public MenuItem setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
if (mShouldDispatchChanges) {
mMenu.onItemChanged(this);
} else {
mDidChange = true;
}
}
return this;
}
@ -265,7 +307,11 @@ public class GeckoMenuItem implements MenuItem {
public MenuItem setEnabled(boolean enabled) {
if (mEnabled != enabled) {
mEnabled = enabled;
if (mShouldDispatchChanges) {
mMenu.onItemChanged(this);
} else {
mDidChange = true;
}
}
return this;
}
@ -274,7 +320,11 @@ public class GeckoMenuItem implements MenuItem {
public MenuItem setIcon(Drawable icon) {
if (mIcon != icon) {
mIcon = icon;
if (mShouldDispatchChanges) {
mMenu.onItemChanged(this);
} else {
mDidChange = true;
}
}
return this;
}
@ -283,7 +333,11 @@ public class GeckoMenuItem implements MenuItem {
public MenuItem setIcon(int iconRes) {
if (mIconRes != iconRes) {
mIconRes = iconRes;
if (mShouldDispatchChanges) {
mMenu.onItemChanged(this);
} else {
mDidChange = true;
}
}
return this;
}
@ -367,7 +421,11 @@ public class GeckoMenuItem implements MenuItem {
public MenuItem setTitle(CharSequence title) {
if (!TextUtils.equals(mTitle, title)) {
mTitle = title;
if (mShouldDispatchChanges) {
mMenu.onItemChanged(this);
} else {
mDidChange = true;
}
}
return this;
}
@ -388,7 +446,11 @@ public class GeckoMenuItem implements MenuItem {
public MenuItem setVisible(boolean visible) {
if (mVisible != visible) {
mVisible = visible;
if (mShouldDispatchChanges) {
mMenu.onItemChanged(this);
} else {
mDidChange = true;
}
}
return this;
}