From 07dbf7966259e5d21751557fb90e74a54f1dc002 Mon Sep 17 00:00:00 2001 From: GaidamakUA Date: Tue, 27 Oct 2015 11:18:31 +0200 Subject: [PATCH] Moved autocomplete initialization to background --- .../plus/osmedit/AdvancedEditPoiFragment.java | 87 ++++++++++++------- 1 file changed, 57 insertions(+), 30 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/osmedit/AdvancedEditPoiFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/AdvancedEditPoiFragment.java index d77d2d4149..5090957ac6 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/AdvancedEditPoiFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/AdvancedEditPoiFragment.java @@ -4,6 +4,7 @@ import android.app.Dialog; import android.content.DialogInterface; import android.graphics.Point; import android.graphics.drawable.Drawable; +import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; @@ -82,8 +83,9 @@ public class AdvancedEditPoiFragment extends Fragment final MapPoiTypes mapPoiTypes = ((OsmandApplication) getActivity().getApplication()).getPoiTypes(); allTranslatedSubTypes = mapPoiTypes.getAllTranslatedNames(); - mAdapter = new TagAdapterLinearLayoutHack(editTagsLineaLayout, getData(), - mapPoiTypes); + mAdapter = new TagAdapterLinearLayoutHack(editTagsLineaLayout, getData()); + // TODO do not restart initialization every time, and probably move initialization to appInit + new InitTagsAndValuesAutocompleteTask(mapPoiTypes).execute(); // setListViewHeightBasedOnChildren(editTagsLineaLayout); Button addTagButton = (Button) view.findViewById(R.id.addTagButton); addTagButton.setOnClickListener(new View.OnClickListener() { @@ -150,29 +152,16 @@ public class AdvancedEditPoiFragment extends Fragment public class TagAdapterLinearLayoutHack { private final LinearLayout linearLayout; private final EditPoiData editPoiData; - private final Map allTypes; - private final HashSet tagKeys; - private final HashSet valueKeys; + private final ArrayAdapter tagAdapter; + private final ArrayAdapter valueAdapter; public TagAdapterLinearLayoutHack(LinearLayout linearLayout, - EditPoiData editPoiData, - MapPoiTypes mapPoiTypes) { + EditPoiData editPoiData) { this.linearLayout = linearLayout; this.editPoiData = editPoiData; - this.allTypes = mapPoiTypes.getAllTypesTranslatedNames(new StringMatcher() { - @Override - public boolean matches(String name) { - return true; - } - }); - - tagKeys = new HashSet<>(); - valueKeys = new HashSet<>(); - for (AbstractPoiType abstractPoiType : allTypes.values()) { - addPoiToStringSet(abstractPoiType, tagKeys, valueKeys); - } - addPoiToStringSet(mapPoiTypes.getOtherMapCategory(), tagKeys, valueKeys); + tagAdapter = new ArrayAdapter<>(linearLayout.getContext(), R.layout.list_textview); + valueAdapter = new ArrayAdapter<>(linearLayout.getContext(), R.layout.list_textview); } public void updateViews() { @@ -234,19 +223,16 @@ public class AdvancedEditPoiFragment extends Fragment } }); - ArrayAdapter tagAdapter = new ArrayAdapter<>(linearLayout.getContext(), - R.layout.list_textview, tagKeys.toArray()); tagEditText.setAdapter(tagAdapter); tagEditText.setThreshold(1); tagEditText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - final String[] tags = tagKeys.toArray(new String[tagKeys.size()]); - builder.setItems(tags, new Dialog.OnClickListener() { + builder.setAdapter(tagAdapter, new Dialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - tagEditText.setText(tags[which]); + tagAdapter.getItem(which); } }); @@ -273,19 +259,17 @@ public class AdvancedEditPoiFragment extends Fragment } } }); - ArrayAdapter valueAdapter = new ArrayAdapter<>(linearLayout.getContext(), - R.layout.list_textview, valueKeys.toArray()); + valueEditText.setAdapter(valueAdapter); valueEditText.setThreshold(1); valueEditText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - final String[] values = valueKeys.toArray(new String[tagKeys.size()]); - builder.setItems(values, new Dialog.OnClickListener() { + builder.setAdapter(valueAdapter, new Dialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - valueEditText.setText(values[which]); + valueAdapter.getItem(which); } }); @@ -296,6 +280,18 @@ public class AdvancedEditPoiFragment extends Fragment return convertView; } + + public void setTagData(String[] tags) { + tagAdapter.clear(); + tagAdapter.addAll(tags); + tagAdapter.notifyDataSetChanged(); + } + + public void setValueData(String[] values) { + valueAdapter.clear(); + valueAdapter.addAll(values); + valueAdapter.notifyDataSetChanged(); + } } private static void addPoiToStringSet(AbstractPoiType abstractPoiType, Set stringSet, @@ -337,4 +333,35 @@ public class AdvancedEditPoiFragment extends Fragment + abstractPoiType.getClass()); } } + + class InitTagsAndValuesAutocompleteTask extends AsyncTask> { + private final MapPoiTypes mapPoiTypes; + + public InitTagsAndValuesAutocompleteTask(MapPoiTypes mapPoiTypes) { + this.mapPoiTypes = mapPoiTypes; + } + + @Override + protected Map doInBackground(Void... params) { + return mapPoiTypes.getAllTypesTranslatedNames(new StringMatcher() { + @Override + public boolean matches(String name) { + return true; + } + }); + } + + @Override + protected void onPostExecute(Map result) { + HashSet tagKeys = new HashSet<>(); + HashSet valueKeys = new HashSet<>(); + for (AbstractPoiType abstractPoiType : result.values()) { + addPoiToStringSet(abstractPoiType, tagKeys, valueKeys); + } + addPoiToStringSet(mapPoiTypes.getOtherMapCategory(), tagKeys, valueKeys); + mAdapter.setTagData(tagKeys.toArray(new String[tagKeys.size()])); + mAdapter.setValueData(valueKeys.toArray(new String[valueKeys.size()])); + } + + } }