Introduce FolderDisplayData class to make code less hacky

This commit is contained in:
cketti 2018-03-01 17:08:56 +01:00
parent aa5dc0b414
commit db53af22cd

View file

@ -18,7 +18,6 @@ import android.widget.ArrayAdapter;
import android.widget.Filter; import android.widget.Filter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.SearchView; import android.widget.SearchView;
import android.widget.TextView;
import com.fsck.k9.Account; import com.fsck.k9.Account;
import com.fsck.k9.Account.FolderMode; import com.fsck.k9.Account.FolderMode;
@ -47,9 +46,8 @@ public class ChooseFolder extends K9ListActivity {
String mSelectFolder; String mSelectFolder;
Account mAccount; Account mAccount;
MessageReference mMessageReference; MessageReference mMessageReference;
ArrayAdapter<String> mAdapter; ArrayAdapter<FolderDisplayData> mAdapter;
private ChooseFolderHandler mHandler = new ChooseFolderHandler(); private ChooseFolderHandler mHandler = new ChooseFolderHandler();
String mHeldInbox = null;
boolean mHideCurrentFolder = true; boolean mHideCurrentFolder = true;
boolean mShowOptionNone = false; boolean mShowOptionNone = false;
boolean mShowDisplayableOnly = false; boolean mShowDisplayableOnly = false;
@ -101,13 +99,13 @@ public class ChooseFolder extends K9ListActivity {
if (mFolder == null) if (mFolder == null)
mFolder = ""; mFolder = "";
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1) { mAdapter = new ArrayAdapter<FolderDisplayData>(this, android.R.layout.simple_list_item_1) {
private Filter myFilter = null; private Filter myFilter = null;
@Override @Override
public Filter getFilter() { public Filter getFilter() {
if (myFilter == null) { if (myFilter == null) {
myFilter = new FolderListFilter<String>(this); myFilter = new FolderListFilter<FolderDisplayData>(this);
} }
return myFilter; return myFilter;
} }
@ -121,13 +119,15 @@ public class ChooseFolder extends K9ListActivity {
this.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { this.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FolderDisplayData folder = mAdapter.getItem(position);
if (folder == null) {
throw new AssertionError("Couldn't get item at adapter position " + position);
}
Intent result = new Intent(); Intent result = new Intent();
result.putExtra(EXTRA_ACCOUNT, mAccount.getUuid()); result.putExtra(EXTRA_ACCOUNT, mAccount.getUuid());
result.putExtra(EXTRA_CUR_FOLDER, mFolder); result.putExtra(EXTRA_CUR_FOLDER, mFolder);
String destFolderName = ((TextView)view).getText().toString(); String destFolderName = folder.name;
if (mHeldInbox != null && getString(R.string.special_mailbox_name_inbox).equals(destFolderName)) {
destFolderName = mHeldInbox;
}
result.putExtra(EXTRA_NEW_FOLDER, destFolderName); result.putExtra(EXTRA_NEW_FOLDER, destFolderName);
if (mMessageReference != null) { if (mMessageReference != null) {
result.putExtra(EXTRA_MESSAGE, mMessageReference.toIdentityString()); result.putExtra(EXTRA_MESSAGE, mMessageReference.toIdentityString());
@ -273,10 +273,10 @@ public class ChooseFolder extends K9ListActivity {
} }
Account.FolderMode aMode = mMode; Account.FolderMode aMode = mMode;
List<String> newFolders = new ArrayList<String>(); List<FolderDisplayData> newFolders = new ArrayList<>();
List<String> topFolders = new ArrayList<String>(); List<FolderDisplayData> topFolders = new ArrayList<>();
for (Folder folder : folders) { for (LocalFolder folder : folders) {
String name = folder.getName(); String name = folder.getName();
if (mHideCurrentFolder && name.equals(mFolder)) { if (mHideCurrentFolder && name.equals(mFolder)) {
@ -298,34 +298,33 @@ public class ChooseFolder extends K9ListActivity {
continue; continue;
} }
if (account.getInboxFolderName().equals(name)) { long id = folder.getDatabaseId();
mHeldInbox = name; String displayName = buildDisplayName(account, name);
name = getString(R.string.special_mailbox_name_inbox); FolderDisplayData folderDisplayData = new FolderDisplayData(id, name, displayName);
}
if (folder.isInTopGroup()) { if (folder.isInTopGroup()) {
topFolders.add(name); topFolders.add(folderDisplayData);
} else { } else {
newFolders.add(name); newFolders.add(folderDisplayData);
} }
} }
final Comparator<String> comparator = new Comparator<String>() { final Comparator<FolderDisplayData> comparator = new Comparator<FolderDisplayData>() {
@Override @Override
public int compare(String s1, String s2) { public int compare(FolderDisplayData lhs, FolderDisplayData rhs) {
int ret = s1.compareToIgnoreCase(s2); int result = lhs.displayName.compareToIgnoreCase(rhs.displayName);
return (ret != 0) ? ret : s1.compareTo(s2); return (result != 0) ? result : lhs.displayName.compareTo(rhs.displayName);
} }
}; };
Collections.sort(topFolders, comparator); Collections.sort(topFolders, comparator);
Collections.sort(newFolders, comparator); Collections.sort(newFolders, comparator);
final List<String> folderList = new ArrayList<String>(newFolders.size() + final List<FolderDisplayData> folderList = new ArrayList<>(newFolders.size() +
topFolders.size() + ((mShowOptionNone) ? 1 : 0)); topFolders.size() + ((mShowOptionNone) ? 1 : 0));
if (mShowOptionNone) { if (mShowOptionNone) {
folderList.add(K9.FOLDER_NONE); folderList.add(new FolderDisplayData(0, K9.FOLDER_NONE, K9.FOLDER_NONE));
} }
folderList.addAll(topFolders); folderList.addAll(topFolders);
@ -339,18 +338,18 @@ public class ChooseFolder extends K9ListActivity {
*/ */
try { try {
int position = 0; int position = 0;
for (String name : folderList) { for (FolderDisplayData folder : folderList) {
if (mSelectFolder != null) { if (mSelectFolder != null) {
/* /*
* Never select EXTRA_CUR_FOLDER (mFolder) if EXTRA_SEL_FOLDER * Never select EXTRA_CUR_FOLDER (mFolder) if EXTRA_SEL_FOLDER
* (mSelectedFolder) was provided. * (mSelectedFolder) was provided.
*/ */
if (name.equals(mSelectFolder)) { if (folder.name.equals(mSelectFolder)) {
selectedFolder = position; selectedFolder = position;
break; break;
} }
} else if (name.equals(mFolder)) { } else if (folder.name.equals(mFolder)) {
selectedFolder = position; selectedFolder = position;
break; break;
} }
@ -362,10 +361,7 @@ public class ChooseFolder extends K9ListActivity {
public void run() { public void run() {
// Now we're in the UI-thread, we can safely change the contents of the adapter. // Now we're in the UI-thread, we can safely change the contents of the adapter.
mAdapter.clear(); mAdapter.clear();
for (String folderName: folderList) { mAdapter.addAll(folderList);
mAdapter.add(folderName);
}
mAdapter.notifyDataSetChanged(); mAdapter.notifyDataSetChanged();
/* /*
@ -383,4 +379,30 @@ public class ChooseFolder extends K9ListActivity {
} }
} }
}; };
private String buildDisplayName(Account account, String name) {
if (!account.getInboxFolderName().equals(name)) {
return name;
}
return getString(R.string.special_mailbox_name_inbox);
}
static class FolderDisplayData {
final long id;
final String name;
final String displayName;
FolderDisplayData(long id, String name, String displayName) {
this.id = id;
this.name = name;
this.displayName = displayName;
}
@Override
public String toString() {
return displayName;
}
}
} }