1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-30 13:12:25 -05:00

Retain selected messages when message list is reloaded

This commit is contained in:
cketti 2012-11-03 04:26:41 +01:00
parent 36b572fa5f
commit 7a266dcbdf

View File

@ -36,7 +36,6 @@ import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan; import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan; import android.text.style.ForegroundColorSpan;
import android.util.Log; import android.util.Log;
import android.util.SparseBooleanArray;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo; import android.view.ContextMenu.ContextMenuInfo;
@ -367,7 +366,7 @@ public class MessageListFragment extends SherlockFragment implements OnItemClick
private boolean mSenderAboveSubject = false; private boolean mSenderAboveSubject = false;
private int mSelectedCount = 0; private int mSelectedCount = 0;
private SparseBooleanArray mSelected; private Set<Long> mSelected = new HashSet<Long>();
private FontSizes mFontSizes = K9.getFontSizes(); private FontSizes mFontSizes = K9.getFontSizes();
@ -1630,8 +1629,8 @@ public class MessageListFragment extends SherlockFragment implements OnItemClick
int maybeBoldTypeface = (read) ? Typeface.NORMAL : Typeface.BOLD; int maybeBoldTypeface = (read) ? Typeface.NORMAL : Typeface.BOLD;
int adapterPosition = cursor.getPosition(); long id = cursor.getLong(ID_COLUMN);
boolean selected = mSelected.get(adapterPosition, false); boolean selected = mSelected.contains(id);
if (selected) { if (selected) {
holder.chip.setBackgroundDrawable(account.getCheckmarkChip().drawable()); holder.chip.setBackgroundDrawable(account.getCheckmarkChip().drawable());
@ -1818,10 +1817,11 @@ public class MessageListFragment extends SherlockFragment implements OnItemClick
mSelectedCount = 0; mSelectedCount = 0;
for (int i = 0, end = mAdapter.getCount(); i < end; i++) { for (int i = 0, end = mAdapter.getCount(); i < end; i++) {
mSelected.put(i, true); Cursor cursor = (Cursor) mAdapter.getItem(i);
long id = cursor.getLong(ID_COLUMN);
mSelected.add(id);
if (mThreadedList) { if (mThreadedList) {
Cursor cursor = (Cursor) mAdapter.getItem(i);
int threadCount = cursor.getInt(THREAD_COUNT_COLUMN); int threadCount = cursor.getInt(THREAD_COUNT_COLUMN);
mSelectedCount += (threadCount > 1) ? threadCount : 1; mSelectedCount += (threadCount > 1) ? threadCount : 1;
} else { } else {
@ -1853,16 +1853,18 @@ public class MessageListFragment extends SherlockFragment implements OnItemClick
return; return;
} }
boolean selected = mSelected.get(adapterPosition, false); Cursor cursor = (Cursor) mAdapter.getItem(adapterPosition);
long id = cursor.getLong(ID_COLUMN);
boolean selected = mSelected.contains(id);
if (!selected) { if (!selected) {
mSelected.put(adapterPosition, true); mSelected.add(id);
} else { } else {
mSelected.delete(adapterPosition); mSelected.remove(id);
} }
int selectedCountDelta = 1; int selectedCountDelta = 1;
if (mThreadedList) { if (mThreadedList) {
Cursor cursor = (Cursor) mAdapter.getItem(adapterPosition);
int threadCount = cursor.getInt(THREAD_COUNT_COLUMN); int threadCount = cursor.getInt(THREAD_COUNT_COLUMN);
if (threadCount > 1) { if (threadCount > 1) {
selectedCountDelta = threadCount; selectedCountDelta = threadCount;
@ -1909,8 +1911,10 @@ public class MessageListFragment extends SherlockFragment implements OnItemClick
boolean isBatchRead = false; boolean isBatchRead = false;
for (int i = 0, end = mAdapter.getCount(); i < end; i++) { for (int i = 0, end = mAdapter.getCount(); i < end; i++) {
if (mSelected.get(i, false)) {
Cursor cursor = (Cursor) mAdapter.getItem(i); Cursor cursor = (Cursor) mAdapter.getItem(i);
long id = cursor.getLong(ID_COLUMN);
if (mSelected.contains(id)) {
String flags = cursor.getString(FLAGS_COLUMN); String flags = cursor.getString(FLAGS_COLUMN);
if (!flags.contains(Flag.FLAGGED.name())) { if (!flags.contains(Flag.FLAGGED.name())) {
@ -2245,8 +2249,10 @@ public class MessageListFragment extends SherlockFragment implements OnItemClick
Set<String> accountUuids = new HashSet<String>(maxAccounts); Set<String> accountUuids = new HashSet<String>(maxAccounts);
for (int position = 0, end = mAdapter.getCount(); position < end; position++) { for (int position = 0, end = mAdapter.getCount(); position < end; position++) {
if (mSelected.get(position, false)) {
Cursor cursor = (Cursor) mAdapter.getItem(position); Cursor cursor = (Cursor) mAdapter.getItem(position);
long id = cursor.getLong(ID_COLUMN);
if (mSelected.contains(id)) {
String accountUuid = cursor.getString(ACCOUNT_UUID_COLUMN); String accountUuid = cursor.getString(ACCOUNT_UUID_COLUMN);
accountUuids.add(accountUuid); accountUuids.add(accountUuid);
@ -2570,7 +2576,10 @@ public class MessageListFragment extends SherlockFragment implements OnItemClick
Message[] messages = new Message[mSelected.size()]; Message[] messages = new Message[mSelected.size()];
int out = 0; int out = 0;
for (int position = 0, end = mAdapter.getCount(); position < end; position++) { for (int position = 0, end = mAdapter.getCount(); position < end; position++) {
if (mSelected.get(position, false)) { Cursor cursor = (Cursor) mAdapter.getItem(position);
long id = cursor.getLong(ID_COLUMN);
if (mSelected.contains(id)) {
messages[out++] = getMessageAtPosition(position); messages[out++] = getMessageAtPosition(position);
} }
} }
@ -2775,11 +2784,27 @@ public class MessageListFragment extends SherlockFragment implements OnItemClick
cursor = data; cursor = data;
} }
mSelected = new SparseBooleanArray(cursor.getCount()); cleanupSelected(cursor);
//TODO: use the (stable) IDs as index and reuse the old mSelected
mAdapter.swapCursor(cursor); mAdapter.swapCursor(cursor);
} }
private void cleanupSelected(Cursor cursor) {
if (mSelected.size() == 0) {
return;
}
Set<Long> selected = new HashSet<Long>();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
long id = cursor.getLong(ID_COLUMN);
if (mSelected.contains(id)) {
selected.add(id);
}
}
mSelected = selected;
}
@Override @Override
public void onLoaderReset(Loader<Cursor> loader) { public void onLoaderReset(Loader<Cursor> loader) {
mSelected = null; mSelected = null;