Merge pull request #9103 from osmandapp/custom_poi_category_fix
Custom poi category fix
This commit is contained in:
commit
a9bdd48bb7
4 changed files with 150 additions and 44 deletions
|
@ -103,6 +103,15 @@
|
|||
android:layout_marginRight="@dimen/content_padding"
|
||||
tools:src="@drawable/ic_action_cancel" />
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/searchProgressBar"
|
||||
android:layout_width="@dimen/card_button_progress_size"
|
||||
android:layout_height="@dimen/card_button_progress_size"
|
||||
android:layout_marginEnd="@dimen/content_padding_half"
|
||||
android:layout_marginRight="@dimen/content_padding_half"
|
||||
android:indeterminate="true"
|
||||
android:visibility="gone" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
|
|
@ -20,6 +20,7 @@ import android.widget.EditText;
|
|||
import android.widget.FrameLayout;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ListView;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
@ -34,21 +35,30 @@ import androidx.fragment.app.Fragment;
|
|||
import androidx.fragment.app.FragmentManager;
|
||||
|
||||
import net.osmand.AndroidUtils;
|
||||
import net.osmand.Collator;
|
||||
import net.osmand.OsmAndCollator;
|
||||
import net.osmand.osm.PoiCategory;
|
||||
import net.osmand.osm.PoiType;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.helpers.AndroidUiHelper;
|
||||
import net.osmand.plus.settings.backend.OsmandSettings;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.UiUtilities;
|
||||
import net.osmand.plus.poi.PoiFiltersHelper;
|
||||
import net.osmand.plus.poi.PoiUIFilter;
|
||||
import net.osmand.plus.render.RenderingIcons;
|
||||
import net.osmand.search.SearchUICore;
|
||||
import net.osmand.search.core.SearchCoreFactory;
|
||||
import net.osmand.search.core.SearchResult;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -80,6 +90,10 @@ public class QuickSearchCustomPoiFragment extends DialogFragment implements OnFi
|
|||
private List<PoiCategory> poiCategoryList;
|
||||
private View headerShadow;
|
||||
private View headerDescription;
|
||||
private ProgressBar searchProgressBar;
|
||||
private ImageView searchCloseIcon;
|
||||
private SearchUICore searchUICore;
|
||||
private Collator collator;
|
||||
|
||||
public QuickSearchCustomPoiFragment() {
|
||||
}
|
||||
|
@ -91,8 +105,10 @@ public class QuickSearchCustomPoiFragment extends DialogFragment implements OnFi
|
|||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
collator = OsmAndCollator.primaryCollator();
|
||||
app = getMyApplication();
|
||||
uiUtilities = app.getUIUtilities();
|
||||
searchUICore = app.getSearchUICore().getCore();
|
||||
this.nightMode = app.getSettings().OSMAND_THEME.get() == OsmandSettings.OSMAND_DARK_THEME;
|
||||
setStyle(STYLE_NO_FRAME, nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme);
|
||||
poiCategoryList = app.getPoiTypes().getCategories(false);
|
||||
|
@ -124,8 +140,8 @@ public class QuickSearchCustomPoiFragment extends DialogFragment implements OnFi
|
|||
editMode = !filterId.equals(helper.getCustomPOIFilter().getFilterId());
|
||||
|
||||
view = layoutInflater.inflate(R.layout.search_custom_poi, container, false);
|
||||
|
||||
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
|
||||
searchProgressBar = view.findViewById(R.id.searchProgressBar);
|
||||
Toolbar toolbar = view.findViewById(R.id.toolbar);
|
||||
Drawable icClose = app.getUIUtilities().getIcon(R.drawable.ic_action_remove_dark,
|
||||
nightMode ? R.color.active_buttons_and_links_text_dark : R.color.active_buttons_and_links_text_light);
|
||||
toolbar.setNavigationIcon(icClose);
|
||||
|
@ -142,13 +158,12 @@ public class QuickSearchCustomPoiFragment extends DialogFragment implements OnFi
|
|||
});
|
||||
toolbar.setBackgroundColor(ContextCompat.getColor(app, nightMode ? R.color.app_bar_color_dark : R.color.app_bar_color_light));
|
||||
toolbar.setTitleTextColor(ContextCompat.getColor(app, nightMode ? R.color.active_buttons_and_links_text_dark : R.color.active_buttons_and_links_text_light));
|
||||
|
||||
TextView title = (TextView) view.findViewById(R.id.title);
|
||||
TextView title = view.findViewById(R.id.title);
|
||||
if (editMode) {
|
||||
title.setText(filter.getName());
|
||||
}
|
||||
|
||||
listView = (ListView) view.findViewById(android.R.id.list);
|
||||
listView = view.findViewById(android.R.id.list);
|
||||
listView.setBackgroundColor(getResources().getColor(
|
||||
app.getSettings().isLightContent() ? R.color.activity_background_color_light
|
||||
: R.color.activity_background_color_dark));
|
||||
|
@ -208,7 +223,7 @@ public class QuickSearchCustomPoiFragment extends DialogFragment implements OnFi
|
|||
AndroidUtils.showSoftKeyboard(searchEditText);
|
||||
}
|
||||
});
|
||||
ImageView searchCloseIcon = view.findViewById(R.id.search_close);
|
||||
searchCloseIcon = view.findViewById(R.id.search_close);
|
||||
searchCloseIcon.setImageDrawable(uiUtilities.getIcon(R.drawable.ic_action_cancel, nightMode));
|
||||
searchCloseIcon.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
|
@ -226,12 +241,12 @@ public class QuickSearchCustomPoiFragment extends DialogFragment implements OnFi
|
|||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
||||
searchSubCategory(charSequence.toString());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable editable) {
|
||||
|
||||
searchSubCategory(editable.toString());
|
||||
}
|
||||
});
|
||||
view.findViewById(R.id.topBarShadow).setVisibility(View.VISIBLE);
|
||||
|
@ -255,12 +270,14 @@ public class QuickSearchCustomPoiFragment extends DialogFragment implements OnFi
|
|||
quickSearchDialogFragment.reloadCategories();
|
||||
}
|
||||
}
|
||||
searchUICore.getSearchSettings().resetSearchTypes();
|
||||
super.onDismiss(dialog);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
saveFilter();
|
||||
getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
|
||||
@Override
|
||||
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
|
||||
|
@ -281,6 +298,89 @@ public class QuickSearchCustomPoiFragment extends DialogFragment implements OnFi
|
|||
});
|
||||
}
|
||||
|
||||
private void updateCloseSearchIcon(boolean searching) {
|
||||
AndroidUiHelper.updateVisibility(searchProgressBar, searching);
|
||||
AndroidUiHelper.updateVisibility(searchCloseIcon, !searching);
|
||||
}
|
||||
|
||||
private void searchSubCategory(String text) {
|
||||
if (text.isEmpty()) {
|
||||
if (subCategoriesAdapter.getSelectedItems().isEmpty()) {
|
||||
listView.setAdapter(categoryListAdapter);
|
||||
categoryListAdapter.notifyDataSetChanged();
|
||||
removeAllHeaders();
|
||||
listView.addHeaderView(headerDescription, null, false);
|
||||
saveFilter();
|
||||
} else {
|
||||
listView.setAdapter(subCategoriesAdapter);
|
||||
subCategoriesAdapter.clear();
|
||||
subCategoriesAdapter.addAll(new ArrayList<>(subCategoriesAdapter.getSelectedItems()));
|
||||
subCategoriesAdapter.notifyDataSetChanged();
|
||||
removeAllHeaders();
|
||||
listView.addHeaderView(headerShadow, null, false);
|
||||
setupAddButton();
|
||||
}
|
||||
} else {
|
||||
updateCloseSearchIcon(true);
|
||||
subCategoriesAdapter.setSelectedItems(getSelectedSubCategories());
|
||||
startSearchSubCategories(text);
|
||||
}
|
||||
}
|
||||
|
||||
private void startSearchSubCategories(String text) {
|
||||
try {
|
||||
SearchUICore.SearchResultCollection res = searchUICore.shallowSearch(SearchCoreFactory.SearchAmenityTypesAPI.class, text, null);
|
||||
List<PoiType> results = new ArrayList<>();
|
||||
for (SearchResult result : res.getCurrentSearchResults()) {
|
||||
Object poiObject = result.object;
|
||||
if (poiObject instanceof PoiType) {
|
||||
PoiType poiType = (PoiType) poiObject;
|
||||
if (!poiType.isAdditional()) {
|
||||
results.add(poiType);
|
||||
}
|
||||
}
|
||||
}
|
||||
showSearchResults(results);
|
||||
} catch (IOException e) {
|
||||
app.showToastMessage(e.getMessage());
|
||||
updateCloseSearchIcon(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void showSearchResults(List<PoiType> poiTypes) {
|
||||
listView.setAdapter(subCategoriesAdapter);
|
||||
subCategoriesAdapter.clear();
|
||||
subCategoriesAdapter.addAll(poiTypes);
|
||||
subCategoriesAdapter.notifyDataSetChanged();
|
||||
removeAllHeaders();
|
||||
listView.addHeaderView(headerShadow, null, false);
|
||||
setupAddButton();
|
||||
updateCloseSearchIcon(false);
|
||||
}
|
||||
|
||||
private List<PoiType> getSelectedSubCategories() {
|
||||
List<PoiType> poiTypes = new ArrayList<>();
|
||||
for (Map.Entry<PoiCategory, LinkedHashSet<String>> entry : filter.getAcceptedTypes().entrySet()) {
|
||||
if (entry.getValue() == null) {
|
||||
poiTypes.addAll(entry.getKey().getPoiTypes());
|
||||
} else {
|
||||
for (String key : entry.getValue()) {
|
||||
PoiType poiType = app.getPoiTypes().getPoiTypeByKey(key);
|
||||
if (poiType != null) {
|
||||
poiTypes.add(poiType);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Collections.sort(poiTypes, new Comparator<PoiType>() {
|
||||
@Override
|
||||
public int compare(PoiType poiType1, PoiType poiType2) {
|
||||
return collator.compare(poiType1.getTranslation(), poiType2.getTranslation());
|
||||
}
|
||||
});
|
||||
return poiTypes;
|
||||
}
|
||||
|
||||
private QuickSearchDialogFragment getQuickSearchDialogFragment() {
|
||||
Fragment parent = getParentFragment();
|
||||
if (parent instanceof QuickSearchDialogFragment) {
|
||||
|
@ -386,6 +486,8 @@ public class QuickSearchCustomPoiFragment extends DialogFragment implements OnFi
|
|||
AppCompatImageView iconView = (AppCompatImageView) row.findViewById(R.id.icon);
|
||||
row.findViewById(R.id.secondary_icon).setVisibility(View.GONE);
|
||||
AppCompatTextView titleView = (AppCompatTextView) row.findViewById(R.id.title);
|
||||
titleView.setMaxLines(Integer.MAX_VALUE);
|
||||
titleView.setEllipsize(null);
|
||||
AppCompatTextView descView = (AppCompatTextView) row.findViewById(R.id.description);
|
||||
SwitchCompat check = (SwitchCompat) row.findViewById(R.id.toggle_item);
|
||||
UiUtilities.setupCompoundButton(check, nightMode, UiUtilities.CompoundButtonType.GLOBAL);
|
||||
|
@ -450,8 +552,34 @@ public class QuickSearchCustomPoiFragment extends DialogFragment implements OnFi
|
|||
}
|
||||
}
|
||||
|
||||
private void updateFilterName(PoiUIFilter filter) {
|
||||
if (!editMode) {
|
||||
Map<PoiCategory, LinkedHashSet<String>> acceptedTypes = filter.getAcceptedTypes();
|
||||
List<PoiCategory> categories = new ArrayList<>(acceptedTypes.keySet());
|
||||
if (categories.size() == 1) {
|
||||
String name = "";
|
||||
PoiCategory category = categories.get(0);
|
||||
LinkedHashSet<String> filters = acceptedTypes.get(category);
|
||||
if (filters == null || filters.size() > 1) {
|
||||
name = category.getTranslation();
|
||||
} else {
|
||||
PoiType poiType = category.getPoiTypeByKeyName(filters.iterator().next());
|
||||
if (poiType != null) {
|
||||
name = poiType.getTranslation();
|
||||
}
|
||||
}
|
||||
if (!Algorithms.isEmpty(name)) {
|
||||
filter.setName(Algorithms.capitalizeFirstLetter(name));
|
||||
}
|
||||
} else {
|
||||
filter.setName(getString(R.string.poi_filter_custom_filter));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
private void saveFilter() {
|
||||
updateFilterName(filter);
|
||||
helper.editPoiFilter(filter);
|
||||
Context ctx = getContext();
|
||||
if (ctx != null) {
|
||||
|
@ -488,40 +616,6 @@ public class QuickSearchCustomPoiFragment extends DialogFragment implements OnFi
|
|||
}
|
||||
}
|
||||
|
||||
private void searchSubCategory(String search) {
|
||||
List<PoiType> result = new ArrayList<>();
|
||||
if (search.isEmpty()) {
|
||||
if (subCategoriesAdapter.getSelectedItems().isEmpty()) {
|
||||
listView.setAdapter(categoryListAdapter);
|
||||
categoryListAdapter.notifyDataSetChanged();
|
||||
removeAllHeaders();
|
||||
listView.addHeaderView(headerDescription, null, false);
|
||||
} else {
|
||||
listView.setAdapter(subCategoriesAdapter);
|
||||
subCategoriesAdapter.clear();
|
||||
subCategoriesAdapter.addAll(new ArrayList<>(subCategoriesAdapter.getSelectedItems()));
|
||||
subCategoriesAdapter.notifyDataSetChanged();
|
||||
removeAllHeaders();
|
||||
listView.addHeaderView(headerShadow, null, false);
|
||||
}
|
||||
} else {
|
||||
for (PoiCategory category : poiCategoryList) {
|
||||
for (PoiType poiType : category.getPoiTypes()) {
|
||||
if (poiType.getTranslation().toLowerCase().contains(search.toLowerCase())) {
|
||||
result.add(poiType);
|
||||
}
|
||||
}
|
||||
}
|
||||
listView.setAdapter(subCategoriesAdapter);
|
||||
subCategoriesAdapter.clear();
|
||||
subCategoriesAdapter.addAll(result);
|
||||
subCategoriesAdapter.notifyDataSetChanged();
|
||||
removeAllHeaders();
|
||||
listView.addHeaderView(headerShadow, null, false);
|
||||
}
|
||||
setupAddButton();
|
||||
}
|
||||
|
||||
private void removeAllHeaders() {
|
||||
listView.removeHeaderView(headerDescription);
|
||||
listView.removeHeaderView(headerShadow);
|
||||
|
@ -572,11 +666,11 @@ public class QuickSearchCustomPoiFragment extends DialogFragment implements OnFi
|
|||
|
||||
for (Map.Entry<PoiCategory, LinkedHashSet<String>> entry : map.entrySet()) {
|
||||
PoiCategory poiCategory = entry.getKey();
|
||||
Set<String> acceptedSubtypes = filter.getAcceptedSubtypes(poiCategory);
|
||||
List<PoiType> poiTypes = poiCategory.getPoiTypes();
|
||||
LinkedHashSet<String> filters = entry.getValue();
|
||||
if (filters.isEmpty()) {
|
||||
filter.setTypeToAccept(poiCategory, false);
|
||||
} else if (acceptedSubtypes != null && acceptedSubtypes.size() == filters.size()) {
|
||||
} else if (poiTypes != null && poiTypes.size() == filters.size()) {
|
||||
filter.selectSubTypesToAccept(poiCategory, null);
|
||||
} else {
|
||||
filter.selectSubTypesToAccept(poiCategory, filters);
|
||||
|
|
|
@ -172,6 +172,7 @@ public class QuickSearchSubCategoriesFragment extends BaseOsmAndDialogFragment {
|
|||
headerShadow = inflater.inflate(R.layout.list_shadow_header, listView, false);
|
||||
footerShadow = inflater.inflate(R.layout.list_shadow_footer, listView, false);
|
||||
headerSelectAll = inflater.inflate(R.layout.select_all_switch_list_item, listView, false);
|
||||
headerSelectAll.setVerticalScrollBarEnabled(false);
|
||||
selectAllSwitch = headerSelectAll.findViewById(R.id.select_all);
|
||||
selectAllSwitch.setChecked(selectAll);
|
||||
selectAllSwitch.setOnClickListener(new View.OnClickListener() {
|
||||
|
|
|
@ -72,6 +72,8 @@ public class SubCategoriesAdapter extends ArrayAdapter<PoiType> {
|
|||
int tintIconColorRes = selected ? activeIconColorRes : secondaryColorRes;
|
||||
if (poiType != null) {
|
||||
TextView title = convertView.findViewById(R.id.title_tv);
|
||||
title.setMaxLines(Integer.MAX_VALUE);
|
||||
title.setEllipsize(null);
|
||||
title.setText(poiType.getTranslation());
|
||||
final CheckBox checkBox = convertView.findViewById(R.id.check_box);
|
||||
checkBox.setChecked(selected);
|
||||
|
|
Loading…
Reference in a new issue