Get option search suggestions in the background

pull/194/merge
M66B 3 years ago
parent 6e250f601b
commit 50d7612449

@ -21,7 +21,6 @@ package eu.faircode.email;
import android.Manifest; import android.Manifest;
import android.app.Activity; import android.app.Activity;
import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
@ -62,12 +61,16 @@ import com.google.android.material.tabs.TabLayout;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService;
public class FragmentOptions extends FragmentBase { public class FragmentOptions extends FragmentBase {
private ViewPager pager; private ViewPager pager;
private PagerAdapter adapter; private PagerAdapter adapter;
private String searching = null; private String searching = null;
private final ExecutorService executor =
Helper.getBackgroundExecutor(1, "suggest");
private static final int[] TAB_PAGES = { private static final int[] TAB_PAGES = {
R.layout.fragment_setup, R.layout.fragment_setup,
R.layout.fragment_options_synchronize, R.layout.fragment_options_synchronize,
@ -321,8 +324,7 @@ public class FragmentOptions extends FragmentBase {
searchView.setOnSuggestionListener(onSuggestionListener); searchView.setOnSuggestionListener(onSuggestionListener);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
private String[] titles = null; private SuggestData data = null;
private View[] views = null;
@Override @Override
public boolean onQueryTextSubmit(String query) { public boolean onQueryTextSubmit(String query) {
@ -343,22 +345,46 @@ public class FragmentOptions extends FragmentBase {
} }
private void suggest(String query) { private void suggest(String query) {
MatrixCursor cursor = new MatrixCursor(new String[]{"_id", "tab", "resid", "title"}); if (data == null)
new SimpleTask<SuggestData>() {
@Override
protected SuggestData onExecute(Context context, Bundle args) throws Throwable {
SuggestData data = new SuggestData();
data.titles = new String[TAB_PAGES.length];
data.views = new View[TAB_PAGES.length];
LayoutInflater inflater = LayoutInflater.from(context);
for (int tab = 0; tab < TAB_PAGES.length; tab++) {
data.titles[tab] = getString(PAGE_TITLES[tab]);
data.views[tab] = inflater.inflate(TAB_PAGES[tab], null);
}
return data;
}
if (query != null && query.length() > 1) { @Override
if (titles == null || views == null) { protected void onExecuted(Bundle args, SuggestData result) {
titles = new String[TAB_PAGES.length]; data = result;
views = new View[TAB_PAGES.length]; _suggest(query);
LayoutInflater inflater = LayoutInflater.from(searchView.getContext());
for (int tab = 0; tab < TAB_PAGES.length; tab++) {
titles[tab] = getString(PAGE_TITLES[tab]);
views[tab] = inflater.inflate(TAB_PAGES[tab], null);
} }
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.setExecutor(executor)
.execute(FragmentOptions.this, new Bundle(), "option:suggest");
else
_suggest(query);
}
private void _suggest(String query) {
MatrixCursor cursor = new MatrixCursor(new String[]{"_id", "tab", "resid", "title"});
if (query != null && query.length() > 1) {
int id = 0; int id = 0;
for (int tab = 0; tab < TAB_PAGES.length; tab++) for (int tab = 0; tab < TAB_PAGES.length; tab++)
id = getSuggestions(query.toLowerCase(), id, tab, titles[tab], views[tab], cursor); id = getSuggestions(query.toLowerCase(), id, tab, data.titles[tab], data.views[tab], cursor);
} }
searchView.setSuggestionsAdapter(new SimpleCursorAdapter( searchView.setSuggestionsAdapter(new SimpleCursorAdapter(
@ -504,4 +530,9 @@ public class FragmentOptions extends FragmentBase {
return POSITION_NONE; // always recreate fragment return POSITION_NONE; // always recreate fragment
} }
} }
private static class SuggestData {
private String[] titles;
private View[] views;
}
} }

Loading…
Cancel
Save