From 38c4f24111ac864c86772d9b8040ebbef345d707 Mon Sep 17 00:00:00 2001 From: Andrew Chen Date: Sun, 9 Sep 2012 14:09:35 -0700 Subject: [PATCH] Move message selection functionality into the MessageAdapter. --- src/com/fsck/k9/activity/MessageList.java | 50 ++++++++++++++--------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/src/com/fsck/k9/activity/MessageList.java b/src/com/fsck/k9/activity/MessageList.java index d60dd2a8f..cb39a6552 100644 --- a/src/com/fsck/k9/activity/MessageList.java +++ b/src/com/fsck/k9/activity/MessageList.java @@ -1849,6 +1849,18 @@ public class MessageList extends K9ListActivity implements OnItemClickListener, notifyDataSetChanged(); } + /** + * Set the selection state for all messages at once. + * @param selected Selection state to set. + */ + public void setSelectionForAllMesages(final boolean selected) { + for (MessageInfoHolder message : mMessages) { + message.selected = selected; + } + + notifyDataSetChanged(); + } + public void addMessages(final List messages) { if (messages.isEmpty()) { return; @@ -2400,30 +2412,28 @@ public class MessageList extends K9ListActivity implements OnItemClickListener, mAdapter.notifyDataSetChanged(); } + /** + * Toggle all selected message states. Sort of. If anything selected, unselect everything. If nothing is + * selected, select everything. + */ private void toggleAllSelected() { boolean newState = true; - synchronized (mAdapter.messages) { - if (mSelectedCount > 0) { - newState = false; - } - - mSelectedCount = 0; - - for (MessageInfoHolder holder : mAdapter.messages) { - holder.selected = newState; - mSelectedCount += (newState ? 1 : 0); - } - - if (newState) { - mActionMode = MessageList.this.startActionMode(mActionModeCallback); - mActionMode.setTitle(String.format(getString(R.string.actionbar_selected), mSelectedCount)); - } else { - mActionMode.finish(); - mSelectedCount = 0; - } + // If there was anything selected, unselect everything. + if (mSelectedCount > 0) { + newState = false; + } + + mAdapter.setSelectionForAllMesages(newState); + + if (newState) { + mSelectedCount = mAdapter.getCount(); + mActionMode = MessageList.this.startActionMode(mActionModeCallback); + mActionMode.setTitle(String.format(getString(R.string.actionbar_selected), mSelectedCount)); + } else { + mSelectedCount = 0; + mActionMode.finish(); } - mAdapter.notifyDataSetChanged(); } private void setSelected(final List holders, final boolean newState) {