Display categories from gpx waypoints

This commit is contained in:
PavelRatushny 2017-08-11 15:27:28 +03:00
parent 3ab0dee4b0
commit 468d1eb4e2
5 changed files with 90 additions and 30 deletions

View file

@ -6,6 +6,7 @@ import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.ColorInt;
import android.text.TextUtils;
import net.osmand.Location;
import net.osmand.PlatformUtil;
@ -1034,6 +1035,19 @@ public class GPXUtilities {
}
return count;
}
public List<String> getWaypointCategories() {
List<String> categories = new ArrayList<>();
for (WptPt pt : points) {
String category = pt.category;
if (!TextUtils.isEmpty(category)) {
if (!categories.contains(category)) {
categories.add(category);
}
}
}
return categories;
}
}
public static String asString(GPXFile file, OsmandApplication ctx) {

View file

@ -118,7 +118,7 @@ public abstract class PointEditorFragment extends Fragment {
public boolean onTouch(final View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
DialogFragment dialogFragment =
SelectCategoryDialogFragment.createInstance(getEditor().getFragmentTag());
createSelectCategoryDialog();
dialogFragment.show(getChildFragmentManager(), SelectCategoryDialogFragment.TAG);
return true;
}
@ -176,6 +176,10 @@ public abstract class PointEditorFragment extends Fragment {
return view;
}
protected DialogFragment createSelectCategoryDialog() {
return SelectCategoryDialogFragment.createInstance(getEditor().getFragmentTag());
}
public Drawable getRowIcon(int iconId) {
IconsCache iconsCache = getMyApplication().getIconsCache();
return iconsCache.getIcon(iconId,

View file

@ -1,5 +1,7 @@
package net.osmand.plus.mapcontextmenu.editors;
import android.support.v4.app.DialogFragment;
import net.osmand.plus.GPXUtilities;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
@ -16,6 +18,11 @@ public class RtePtEditorFragment extends WptPtEditorFragment {
return getMapActivity().getResources().getString(R.string.save_route_point);
}
@Override
protected DialogFragment createSelectCategoryDialog() {
return SelectCategoryDialogFragment.createInstance(getEditor().getFragmentTag());
}
public static void showInstance(final MapActivity mapActivity) {
RtePtEditor editor = mapActivity.getContextMenu().getRtePtPointEditor();
//int slideInAnim = editor.getSlideInAnimation();

View file

@ -15,6 +15,7 @@ import android.widget.Button;
import android.widget.LinearLayout;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.IconsCache;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
@ -40,6 +41,15 @@ public class SelectCategoryDialogFragment extends DialogFragment {
private String editorTag;
private CategorySelectionListener selectionListener;
private GPXFile gpxFile;
public void setGpxFile(GPXFile gpxFile) {
this.gpxFile = gpxFile;
}
public GPXFile getGpxFile() {
return gpxFile;
}
@NonNull
@Override
@ -58,36 +68,16 @@ public class SelectCategoryDialogFragment extends DialogFragment {
LinearLayout ll = (LinearLayout) v.findViewById(R.id.list_container);
final FavouritesDbHelper helper = ((OsmandApplication) getActivity().getApplication()).getFavorites();
List<FavouritesDbHelper.FavoriteGroup> gs = helper.getFavoriteGroups();
for (final FavouritesDbHelper.FavoriteGroup category : gs) {
View itemView = getActivity().getLayoutInflater().inflate(R.layout.favorite_category_dialog_item, null);
Button button = (Button)itemView.findViewById(R.id.button);
if (category.color != 0) {
button.setCompoundDrawablesWithIntrinsicBounds(getIcon(getActivity(), R.drawable.ic_action_folder, category.color), null, null, null);
} else {
button.setCompoundDrawablesWithIntrinsicBounds(getIcon(getActivity(), R.drawable.ic_action_folder, getResources().getColor(R.color.color_favorite)), null, null, null);
if (gpxFile != null) {
List<String> categories = gpxFile.getWaypointCategories();
for (final String category : categories) {
addCategory(ll, category, 0);
}
} else {
List<FavouritesDbHelper.FavoriteGroup> gs = helper.getFavoriteGroups();
for (final FavouritesDbHelper.FavoriteGroup category : gs) {
addCategory(ll, category.name, category.color);
}
button.setCompoundDrawablePadding(dpToPx(15f));
String name = category.name.length() == 0 ? getString(R.string.shared_string_favorites) : category.name;
button.setText(name);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PointEditor editor = ((MapActivity) getActivity()).getContextMenu().getPointEditor(editorTag);
if (editor != null) {
editor.setCategory(category.name);
}
if (selectionListener != null) {
selectionListener.onCategorySelected(category.name, category.color);
}
dismiss();
}
});
ll.addView(itemView);
}
View itemView = getActivity().getLayoutInflater().inflate(R.layout.favorite_category_dialog_item, null);
Button button = (Button)itemView.findViewById(R.id.button);
@ -111,6 +101,40 @@ public class SelectCategoryDialogFragment extends DialogFragment {
return builder.create();
}
private void addCategory(LinearLayout ll, final String categoryName, final int categoryColor) {
View itemView = getActivity().getLayoutInflater().inflate(R.layout.favorite_category_dialog_item, null);
Button button = (Button)itemView.findViewById(R.id.button);
if (categoryColor != 0) {
button.setCompoundDrawablesWithIntrinsicBounds(getIcon(getActivity(), R.drawable.ic_action_folder, categoryColor), null, null, null);
} else {
button.setCompoundDrawablesWithIntrinsicBounds(getIcon(getActivity(), R.drawable.ic_action_folder, getResources().getColor(R.color.color_favorite)), null, null, null);
}
button.setCompoundDrawablePadding(dpToPx(15f));
String name = categoryName.length() == 0 ? getString(R.string.shared_string_favorites) : categoryName;
button.setText(name);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PointEditor editor = ((MapActivity) getActivity()).getContextMenu().getPointEditor(editorTag);
if (editor != null) {
editor.setCategory(categoryName);
if (gpxFile != null && editor instanceof WptPtEditor) {
((WptPtEditor) editor).getWptPt().category = categoryName;
}
}
if (selectionListener != null) {
selectionListener.onCategorySelected(categoryName, categoryColor);
}
dismiss();
}
});
ll.addView(itemView);
}
public static SelectCategoryDialogFragment createInstance(String editorTag) {
SelectCategoryDialogFragment fragment = new SelectCategoryDialogFragment();
Bundle bundle = new Bundle();

View file

@ -7,6 +7,7 @@ import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.View;
import net.osmand.data.LatLon;
@ -47,6 +48,16 @@ public class WptPtEditorFragment extends PointEditorFragment {
defaultColor = getResources().getColor(R.color.gpx_color_point);
}
@Override
protected DialogFragment createSelectCategoryDialog() {
SelectCategoryDialogFragment selectCategoryDialogFragment = SelectCategoryDialogFragment.createInstance(getEditor().getFragmentTag());
GPXFile gpx = editor.getGpxFile();
if (gpx != null) {
selectCategoryDialogFragment.setGpxFile(gpx);
}
return selectCategoryDialogFragment;
}
protected void assignEditor() {
editor = getMapActivity().getContextMenu().getWptPtPointEditor();
}