mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-23 18:02:15 -05:00
refactored sorting
This commit is contained in:
parent
d6a1f2e95c
commit
539ab93bcb
@ -7,7 +7,6 @@ import android.net.ConnectivityManager;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
|
||||
import com.fsck.k9.controller.MessagingController.SortType;
|
||||
import com.fsck.k9.crypto.Apg;
|
||||
import com.fsck.k9.crypto.CryptoProvider;
|
||||
import com.fsck.k9.helper.Utility;
|
||||
@ -25,6 +24,7 @@ import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -78,6 +78,37 @@ public class Account implements BaseAccount {
|
||||
public static final String IDENTITY_EMAIL_KEY = "email";
|
||||
public static final String IDENTITY_DESCRIPTION_KEY = "description";
|
||||
|
||||
public enum SortType {
|
||||
SORT_DATE(R.string.sort_earliest_first, R.string.sort_latest_first, false),
|
||||
SORT_ARRIVAL(R.string.sort_earliest_first, R.string.sort_latest_first, false),
|
||||
SORT_SUBJECT(R.string.sort_subject_alpha, R.string.sort_subject_re_alpha, true),
|
||||
SORT_SENDER(R.string.sort_sender_alpha, R.string.sort_sender_re_alpha, true),
|
||||
SORT_UNREAD(R.string.sort_unread_first, R.string.sort_unread_last, true),
|
||||
SORT_FLAGGED(R.string.sort_flagged_first, R.string.sort_flagged_last, true),
|
||||
SORT_ATTACHMENT(R.string.sort_attach_first, R.string.sort_unattached_first, true);
|
||||
|
||||
private int ascendingToast;
|
||||
private int descendingToast;
|
||||
private boolean defaultAscending;
|
||||
|
||||
SortType(int ascending, int descending, boolean ndefaultAscending) {
|
||||
ascendingToast = ascending;
|
||||
descendingToast = descending;
|
||||
defaultAscending = ndefaultAscending;
|
||||
}
|
||||
|
||||
public int getToast(boolean ascending) {
|
||||
if (ascending) {
|
||||
return ascendingToast;
|
||||
} else {
|
||||
return descendingToast;
|
||||
}
|
||||
}
|
||||
public boolean isDefaultAscending() {
|
||||
return defaultAscending;
|
||||
}
|
||||
}
|
||||
|
||||
public static final SortType DEFAULT_SORT_TYPE = SortType.SORT_DATE;
|
||||
public static final boolean DEFAULT_SORT_ASCENDING = false;
|
||||
|
||||
@ -126,7 +157,7 @@ public class Account implements BaseAccount {
|
||||
private boolean mPushPollOnConnect;
|
||||
private boolean mNotifySync;
|
||||
private SortType mSortType;
|
||||
private boolean mSortAscending;
|
||||
private HashMap<SortType, Boolean> mSortAscending = new HashMap<SortType, Boolean>();
|
||||
private ShowPictures mShowPictures;
|
||||
private boolean mEnableMoveButtons;
|
||||
private boolean mIsSignatureBeforeQuotedText;
|
||||
@ -218,7 +249,7 @@ public class Account implements BaseAccount {
|
||||
mFolderPushMode = FolderMode.FIRST_CLASS;
|
||||
mFolderTargetMode = FolderMode.NOT_SECOND_CLASS;
|
||||
mSortType = DEFAULT_SORT_TYPE;
|
||||
mSortAscending = DEFAULT_SORT_ASCENDING;
|
||||
mSortAscending.put(DEFAULT_SORT_TYPE, DEFAULT_SORT_ASCENDING);
|
||||
mShowPictures = ShowPictures.NEVER;
|
||||
mEnableMoveButtons = false;
|
||||
mIsSignatureBeforeQuotedText = false;
|
||||
@ -348,7 +379,7 @@ public class Account implements BaseAccount {
|
||||
mSortType = SortType.SORT_DATE;
|
||||
}
|
||||
|
||||
mSortAscending = prefs.getBoolean(mUuid + ".sortAscending", false);
|
||||
mSortAscending.put(mSortType, prefs.getBoolean(mUuid + ".sortAscending", false));
|
||||
|
||||
try {
|
||||
mShowPictures = ShowPictures.valueOf(prefs.getString(mUuid + ".showPicturesEnum",
|
||||
@ -619,7 +650,7 @@ public class Account implements BaseAccount {
|
||||
editor.putString(mUuid + ".autoExpandFolderName", mAutoExpandFolderName);
|
||||
editor.putInt(mUuid + ".accountNumber", mAccountNumber);
|
||||
editor.putString(mUuid + ".sortTypeEnum", mSortType.name());
|
||||
editor.putBoolean(mUuid + ".sortAscending", mSortAscending);
|
||||
editor.putBoolean(mUuid + ".sortAscending", mSortAscending.get(mSortType));
|
||||
editor.putString(mUuid + ".showPicturesEnum", mShowPictures.name());
|
||||
editor.putBoolean(mUuid + ".enableMoveButtons", mEnableMoveButtons);
|
||||
editor.putString(mUuid + ".folderDisplayMode", mFolderDisplayMode.name());
|
||||
@ -1042,12 +1073,15 @@ public class Account implements BaseAccount {
|
||||
mSortType = sortType;
|
||||
}
|
||||
|
||||
public synchronized boolean isSortAscending() {
|
||||
return mSortAscending;
|
||||
public synchronized boolean isSortAscending(SortType sortType) {
|
||||
if (mSortAscending.get(sortType) == null) {
|
||||
mSortAscending.put(sortType, sortType.isDefaultAscending());
|
||||
}
|
||||
return mSortAscending.get(sortType);
|
||||
}
|
||||
|
||||
public synchronized void setSortAscending(boolean sortAscending) {
|
||||
mSortAscending = sortAscending;
|
||||
public synchronized void setSortAscending(SortType sortType, boolean sortAscending) {
|
||||
mSortAscending.put(sortType, sortAscending);
|
||||
}
|
||||
|
||||
public synchronized ShowPictures getShowPictures() {
|
||||
|
@ -51,6 +51,7 @@ import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.fsck.k9.Account;
|
||||
import com.fsck.k9.Account.SortType;
|
||||
import com.fsck.k9.AccountStats;
|
||||
import com.fsck.k9.BaseAccount;
|
||||
import com.fsck.k9.FontSizes;
|
||||
@ -63,7 +64,6 @@ import com.fsck.k9.activity.setup.AccountSettings;
|
||||
import com.fsck.k9.activity.setup.FolderSettings;
|
||||
import com.fsck.k9.activity.setup.Prefs;
|
||||
import com.fsck.k9.controller.MessagingController;
|
||||
import com.fsck.k9.controller.MessagingController.SortType;
|
||||
import com.fsck.k9.controller.MessagingListener;
|
||||
import com.fsck.k9.helper.MessageHelper;
|
||||
import com.fsck.k9.helper.Utility;
|
||||
@ -292,10 +292,9 @@ public class MessageList
|
||||
|
||||
private MessageListHandler mHandler = new MessageListHandler();
|
||||
|
||||
private SortType sortType = SortType.SORT_DATE;
|
||||
|
||||
private boolean sortAscending = true;
|
||||
private boolean sortDateAscending = false;
|
||||
private SortType mSortType = SortType.SORT_DATE;
|
||||
private boolean mSortAscending = true;
|
||||
private boolean mSortDateAscending = false;
|
||||
|
||||
private boolean mStars = true;
|
||||
private boolean mCheckboxes = true;
|
||||
@ -462,8 +461,8 @@ public class MessageList
|
||||
|
||||
{
|
||||
// add the specified comparator
|
||||
final Comparator<MessageInfoHolder> comparator = SORT_COMPARATORS.get(sortType);
|
||||
if (sortAscending) {
|
||||
final Comparator<MessageInfoHolder> comparator = SORT_COMPARATORS.get(mSortType);
|
||||
if (mSortAscending) {
|
||||
chain.add(comparator);
|
||||
} else {
|
||||
chain.add(new ReverseComparator<MessageInfoHolder>(comparator));
|
||||
@ -472,9 +471,9 @@ public class MessageList
|
||||
|
||||
{
|
||||
// add the date comparator if not already specified
|
||||
if (sortType != SortType.SORT_DATE && sortType != SortType.SORT_ARRIVAL) {
|
||||
if (mSortType != SortType.SORT_DATE && mSortType != SortType.SORT_ARRIVAL) {
|
||||
final Comparator<MessageInfoHolder> comparator = SORT_COMPARATORS.get(SortType.SORT_DATE);
|
||||
if (sortDateAscending) {
|
||||
if (mSortDateAscending) {
|
||||
chain.add(comparator);
|
||||
} else {
|
||||
chain.add(new ReverseComparator<MessageInfoHolder>(comparator));
|
||||
@ -821,11 +820,9 @@ public class MessageList
|
||||
mStars = K9.messageListStars();
|
||||
mCheckboxes = K9.messageListCheckboxes();
|
||||
|
||||
sortType = mAccount.getSortType();
|
||||
mController.setSortType(sortType);
|
||||
sortAscending = mAccount.isSortAscending();
|
||||
mController.setSortAscending(sortType, sortAscending);
|
||||
sortDateAscending = mController.isSortAscending(SortType.SORT_DATE);
|
||||
mSortType = mAccount.getSortType();
|
||||
mSortAscending = mAccount.isSortAscending(mSortType);
|
||||
mSortDateAscending = mAccount.isSortAscending(SortType.SORT_DATE);
|
||||
|
||||
mController.addListener(mAdapter.mListener);
|
||||
|
||||
@ -1209,24 +1206,22 @@ public class MessageList
|
||||
AccountSettings.actionSettings(this, mAccount);
|
||||
}
|
||||
|
||||
private void changeSort(SortType newSortType) {
|
||||
if (sortType == newSortType) {
|
||||
private void changeSort(SortType sortType) {
|
||||
if (mSortType == sortType) {
|
||||
onToggleSortAscending();
|
||||
} else {
|
||||
sortType = newSortType;
|
||||
mController.setSortType(sortType);
|
||||
sortAscending = mController.isSortAscending(sortType);
|
||||
sortDateAscending = mController.isSortAscending(SortType.SORT_DATE);
|
||||
mSortType = sortType;
|
||||
mAccount.setSortType(mSortType);
|
||||
mSortAscending = mAccount.isSortAscending(mSortType);
|
||||
mSortDateAscending = mAccount.isSortAscending(SortType.SORT_DATE);
|
||||
|
||||
mAccount.setSortType(sortType);
|
||||
mAccount.setSortAscending(sortAscending);
|
||||
mAccount.save(Preferences.getPreferences(this));
|
||||
reSort();
|
||||
}
|
||||
}
|
||||
|
||||
private void reSort() {
|
||||
int toastString = sortType.getToast(sortAscending);
|
||||
int toastString = mSortType.getToast(mSortAscending);
|
||||
|
||||
Toast toast = Toast.makeText(this, toastString, Toast.LENGTH_SHORT);
|
||||
toast.show();
|
||||
@ -1239,7 +1234,7 @@ public class MessageList
|
||||
int curIndex = 0;
|
||||
|
||||
for (int i = 0; i < sorts.length; i++) {
|
||||
if (sorts[i] == sortType) {
|
||||
if (sorts[i] == mSortType) {
|
||||
curIndex = i;
|
||||
break;
|
||||
}
|
||||
@ -1255,14 +1250,10 @@ public class MessageList
|
||||
}
|
||||
|
||||
private void onToggleSortAscending() {
|
||||
mController.setSortAscending(sortType, !sortAscending);
|
||||
|
||||
sortAscending = mController.isSortAscending(sortType);
|
||||
sortDateAscending = mController.isSortAscending(SortType.SORT_DATE);
|
||||
|
||||
mAccount.setSortAscending( sortAscending);
|
||||
mSortAscending = !mSortAscending;
|
||||
mAccount.setSortAscending(mSortType, mSortAscending);
|
||||
mSortDateAscending = mAccount.isSortAscending(SortType.SORT_DATE);
|
||||
mAccount.save(Preferences.getPreferences(this));
|
||||
|
||||
reSort();
|
||||
}
|
||||
|
||||
|
@ -136,47 +136,12 @@ public class MessagingController implements Runnable {
|
||||
private Thread mThread;
|
||||
private Set<MessagingListener> mListeners = new CopyOnWriteArraySet<MessagingListener>();
|
||||
|
||||
private HashMap<SortType, Boolean> sortAscending = new HashMap<SortType, Boolean>();
|
||||
|
||||
private final ConcurrentHashMap<String, AtomicInteger> sendCount = new ConcurrentHashMap<String, AtomicInteger>();
|
||||
|
||||
ConcurrentHashMap<Account, Pusher> pushers = new ConcurrentHashMap<Account, Pusher>();
|
||||
|
||||
private final ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||
|
||||
public enum SortType {
|
||||
SORT_DATE(R.string.sort_earliest_first, R.string.sort_latest_first, false),
|
||||
SORT_ARRIVAL(R.string.sort_earliest_first, R.string.sort_latest_first, false),
|
||||
SORT_SUBJECT(R.string.sort_subject_alpha, R.string.sort_subject_re_alpha, true),
|
||||
SORT_SENDER(R.string.sort_sender_alpha, R.string.sort_sender_re_alpha, true),
|
||||
SORT_UNREAD(R.string.sort_unread_first, R.string.sort_unread_last, true),
|
||||
SORT_FLAGGED(R.string.sort_flagged_first, R.string.sort_flagged_last, true),
|
||||
SORT_ATTACHMENT(R.string.sort_attach_first, R.string.sort_unattached_first, true);
|
||||
|
||||
private int ascendingToast;
|
||||
private int descendingToast;
|
||||
private boolean defaultAscending;
|
||||
|
||||
SortType(int ascending, int descending, boolean ndefaultAscending) {
|
||||
ascendingToast = ascending;
|
||||
descendingToast = descending;
|
||||
defaultAscending = ndefaultAscending;
|
||||
}
|
||||
|
||||
public int getToast(boolean ascending) {
|
||||
if (ascending) {
|
||||
return ascendingToast;
|
||||
} else {
|
||||
return descendingToast;
|
||||
}
|
||||
}
|
||||
public boolean isDefaultAscending() {
|
||||
return defaultAscending;
|
||||
}
|
||||
}
|
||||
|
||||
private SortType sortType = Account.DEFAULT_SORT_TYPE;
|
||||
|
||||
private MessagingListener checkMailListener = null;
|
||||
|
||||
private MemorizingListener memorizingListener = new MemorizingListener();
|
||||
@ -4362,25 +4327,6 @@ public class MessagingController implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
public SortType getSortType() {
|
||||
return sortType;
|
||||
}
|
||||
|
||||
public void setSortType(SortType sortType) {
|
||||
this.sortType = sortType;
|
||||
}
|
||||
|
||||
public boolean isSortAscending(SortType sortType) {
|
||||
Boolean sortAsc = sortAscending.get(sortType);
|
||||
if (sortAsc == null) {
|
||||
return sortType.isDefaultAscending();
|
||||
} else return sortAsc;
|
||||
}
|
||||
|
||||
public void setSortAscending(SortType sortType, boolean nsortAscending) {
|
||||
sortAscending.put(sortType, nsortAscending);
|
||||
}
|
||||
|
||||
public Collection<Pusher> getPushers() {
|
||||
return pushers.values();
|
||||
}
|
||||
|
@ -9,10 +9,10 @@ import java.util.TreeMap;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import com.fsck.k9.Account;
|
||||
import com.fsck.k9.Account.SortType;
|
||||
import com.fsck.k9.K9;
|
||||
import com.fsck.k9.R;
|
||||
import com.fsck.k9.Account.FolderMode;
|
||||
import com.fsck.k9.controller.MessagingController.SortType;
|
||||
import com.fsck.k9.crypto.Apg;
|
||||
import com.fsck.k9.mail.store.StorageManager;
|
||||
import com.fsck.k9.preferences.Settings.*;
|
||||
|
Loading…
Reference in New Issue
Block a user