From ea902ce680f336667c15f7fa76d87c57d90b84bb Mon Sep 17 00:00:00 2001 From: cketti Date: Wed, 16 May 2012 20:33:36 +0200 Subject: [PATCH] Merge onToggleSortAscending() with changeSort() to avoid code duplication --- src/com/fsck/k9/activity/MessageList.java | 76 +++++++++++++---------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/src/com/fsck/k9/activity/MessageList.java b/src/com/fsck/k9/activity/MessageList.java index 3bb8b53e1..fdea8b478 100644 --- a/src/com/fsck/k9/activity/MessageList.java +++ b/src/com/fsck/k9/activity/MessageList.java @@ -1049,7 +1049,7 @@ public class MessageList return true; } case KeyEvent.KEYCODE_I: { - onToggleSortAscending(); + changeSort(mSortType); return true; } case KeyEvent.KEYCODE_H: { @@ -1202,30 +1202,55 @@ public class MessageList } private void changeSort(SortType sortType) { - if (mSortType == sortType) { - onToggleSortAscending(); - } else { - mSortType = sortType; + Boolean sortAscending = (mSortType == sortType) ? !mSortAscending : null; + changeSort(sortType, sortAscending); + } - Preferences prefs = Preferences.getPreferences(getApplicationContext()); - Account account = getCurrentAccount(prefs); + /** + * Change the sort type and sort order used for the message list. + * + * @param sortType + * Specifies which field to use for sorting the message list. + * @param sortAscending + * Specifies the sort order. If this argument is {@code null} the default search order + * for the sort type is used. + */ + // FIXME: Don't save the changes in the UI thread + private void changeSort(SortType sortType, Boolean sortAscending) { + mSortType = sortType; - if (account != null) { - account.setSortType(mSortType); + Preferences prefs = Preferences.getPreferences(getApplicationContext()); + Account account = getCurrentAccount(prefs); + + if (account != null) { + account.setSortType(mSortType); + + if (sortAscending == null) { mSortAscending = account.isSortAscending(mSortType); - mSortDateAscending = account.isSortAscending(SortType.SORT_DATE); - account.save(prefs); } else { - K9.setSortType(mSortType); - mSortAscending = K9.isSortAscending(mSortType); - mSortDateAscending = K9.isSortAscending(SortType.SORT_DATE); - - Editor editor = prefs.getPreferences().edit(); - K9.save(editor); - editor.commit(); + mSortAscending = sortAscending; } - reSort(); + account.setSortAscending(mSortType, mSortAscending); + mSortDateAscending = account.isSortAscending(SortType.SORT_DATE); + + account.save(prefs); + } else { + K9.setSortType(mSortType); + + if (sortAscending == null) { + mSortAscending = K9.isSortAscending(mSortType); + } else { + mSortAscending = sortAscending; + } + K9.setSortAscending(mSortType, mSortAscending); + mSortDateAscending = K9.isSortAscending(SortType.SORT_DATE); + + Editor editor = prefs.getPreferences().edit(); + K9.save(editor); + editor.commit(); } + + reSort(); } private void reSort() { @@ -1257,19 +1282,6 @@ public class MessageList changeSort(sorts[curIndex]); } - private void onToggleSortAscending() { - mSortAscending = !mSortAscending; - if (mAccount != null) { - mAccount.setSortAscending(mSortType, mSortAscending); - mSortDateAscending = mAccount.isSortAscending(SortType.SORT_DATE); - mAccount.save(Preferences.getPreferences(this)); - } else { - K9.setSortAscending(mSortType, mSortAscending); - mSortDateAscending = K9.isSortAscending(SortType.SORT_DATE); - } - reSort(); - } - /** * @param holders * Never {@code null}.