Simplify FolderListFilter

This commit is contained in:
cketti 2019-10-03 23:11:40 +02:00
parent 9df97445c2
commit b7f4ab9a70
2 changed files with 34 additions and 59 deletions

View file

@ -29,6 +29,7 @@ import com.fsck.k9.Account;
import com.fsck.k9.Account.FolderMode;
import com.fsck.k9.K9;
import com.fsck.k9.Preferences;
import com.fsck.k9.activity.FolderListFilter.FolderAdapter;
import com.fsck.k9.controller.MessageReference;
import com.fsck.k9.ui.R;
import com.fsck.k9.controller.MessagingController;
@ -352,11 +353,10 @@ public class ChooseFolder extends K9ListActivity {
}
};
class FolderListAdapter extends BaseAdapter implements Filterable,
FolderListFilter.FilterableAdapter<FolderInfoHolder> {
class FolderListAdapter extends BaseAdapter implements Filterable, FolderAdapter {
private List<FolderInfoHolder> mFolders = new ArrayList<>();
private List<FolderInfoHolder> mFilteredFolders = Collections.unmodifiableList(mFolders);
private Filter mFilter = new FolderListFilter<>(this);
private Filter mFilter = new FolderListFilter(this);
private FolderIconProvider folderIconProvider = new FolderIconProvider(getTheme());
public FolderInfoHolder getItem(long position) {
@ -373,16 +373,6 @@ public class ChooseFolder extends K9ListActivity {
return mFilteredFolders.get(position).folder.getDatabaseId();
}
@Override
public void clear() {
mFolders.clear();
}
@Override
public void add(FolderInfoHolder object) {
mFolders.add(object);
}
@Override
public int getCount() {
return mFilteredFolders.size();
@ -455,6 +445,17 @@ public class ChooseFolder extends K9ListActivity {
public Filter getFilter() {
return mFilter;
}
@Override
public List<FolderInfoHolder> getFolders() {
return mFolders;
}
@Override
public void setFilteredFolders(List<FolderInfoHolder> folders) {
mFilteredFolders = new ArrayList<>(folders);
notifyDataSetChanged();
}
}
static class FolderViewHolder {

View file

@ -4,17 +4,16 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import timber.log.Timber;
import android.widget.Filter;
/**
* Filter to search for occurrences of the search-expression in any place of the folder name.
*/
public class FolderListFilter<T> extends Filter {
private FilterableAdapter<T> adapter;
private List<T> unfilteredFolderList = null;
public class FolderListFilter extends Filter {
private final FolderAdapter adapter;
public FolderListFilter(final FilterableAdapter<T> folders) {
public FolderListFilter(FolderAdapter folders) {
this.adapter = folders;
}
@ -22,34 +21,22 @@ public class FolderListFilter<T> extends Filter {
protected FilterResults performFiltering(CharSequence searchTerm) {
FilterResults results = new FilterResults();
if (unfilteredFolderList == null) {
int count = adapter.getCount();
unfilteredFolderList = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
unfilteredFolderList.add(adapter.getItem(i));
}
}
Locale locale = Locale.getDefault();
if ((searchTerm == null) || (searchTerm.length() == 0)) {
List<T> list = new ArrayList<>(unfilteredFolderList);
if (searchTerm == null || searchTerm.length() == 0) {
List<FolderInfoHolder> list = new ArrayList<>(adapter.getFolders());
results.values = list;
results.count = list.size();
} else {
final String searchTermString = searchTerm.toString().toLowerCase(locale);
final String[] words = searchTermString.split(" ");
final int wordCount = words.length;
String searchTermString = searchTerm.toString().toLowerCase(locale);
String[] words = searchTermString.split(" ");
final List<T> values = unfilteredFolderList;
List<FolderInfoHolder> newValues = new ArrayList<>();
for (FolderInfoHolder folderInfoHolder : adapter.getFolders()) {
String valueText = folderInfoHolder.displayName.toLowerCase(locale);
final List<T> newValues = new ArrayList<>();
for (final T value : values) {
final String valueText = value.toString().toLowerCase(locale);
for (int k = 0; k < wordCount; k++) {
if (valueText.contains(words[k])) {
newValues.add(value);
for (String word : words) {
if (valueText.contains(word)) {
newValues.add(folderInfoHolder);
break;
}
}
@ -65,26 +52,13 @@ public class FolderListFilter<T> extends Filter {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
final List<T> folders = (List<T>) results.values;
adapter.clear();
if (folders != null) {
for (T folder : folders) {
if (folder != null) {
adapter.add(folder);
}
}
} else {
Timber.w("FolderListFilter.publishResults - null search-result ");
}
adapter.notifyDataSetChanged();
List<FolderInfoHolder> folders = (List<FolderInfoHolder>) results.values;
adapter.setFilteredFolders(folders);
}
public interface FilterableAdapter<T> {
void notifyDataSetChanged();
void clear();
void add(T object);
int getCount();
T getItem(int i);
public interface FolderAdapter {
List<FolderInfoHolder> getFolders();
void setFilteredFolders(List<FolderInfoHolder> folders);
}
}