Context menu: edit favorites

This commit is contained in:
Alexey Kulish 2015-10-09 20:28:09 +03:00
parent f6d1d70f09
commit a304a9c62d
17 changed files with 394 additions and 67 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:textSize="@dimen/default_list_text_size_large"
android:ellipsize="marquee"
android:singleLine="true"/>

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="42dp"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center_vertical"
android:textAlignment="inherit"
android:textSize="@dimen/default_list_text_size_large"/>

View file

@ -4,24 +4,14 @@
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="44dp"
android:layout_height="50dp">
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:scaleType="center"
android:src="@drawable/ic_action_folder"/>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_transparent"
android:drawableLeft="@drawable/ic_action_folder"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="?android:selectableItemBackground"
android:text="Restaurant"
android:gravity="left|center_vertical"
android:textSize="@dimen/default_list_text_size"

View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/dialog_content_bottom_margin"
android:paddingLeft="@dimen/dialog_content_margin"
android:paddingRight="@dimen/dialog_content_margin"
android:paddingTop="@dimen/dialog_content_bottom_margin">
<EditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:hint="@string/favorite_category_name"/>
<Spinner
android:id="@+id/edit_color"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Spinner>
</LinearLayout>

View file

@ -9,6 +9,9 @@
3. All your modified/created strings are in the top of the file (to make easier find what\'s translated).
PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy
-->
<string name="favorite_category_dublicate_message">Specified category name already exists. Please define other name.</string>
<string name="favorite_category_name">Category name</string>
<string name="favorite_category_add_new_title">Add new category</string>
<string name="regions">Regions</string>
<string name="additional_maps">Additional maps</string>
<string name="region_maps">Region maps</string>

View file

@ -411,13 +411,21 @@ public class FavouritesDbHelper {
}
private void addEmptyCategory(String name) {
public void addEmptyCategory(String name) {
FavoriteGroup group = new FavoriteGroup();
group.name = name;
favoriteGroups.add(group);
flatGroups.put(name, group);
}
public void addEmptyCategory(String name, int color) {
FavoriteGroup group = new FavoriteGroup();
group.name = name;
group.color = color;
favoriteGroups.add(group);
flatGroups.put(name, group);
}
public List<FavouritePoint> getFavouritePoints() {
return cachedFavoritePoints;
}
@ -427,6 +435,16 @@ public class FavouritesDbHelper {
return favoriteGroups;
}
public boolean groupExists(String name) {
String nameLowercase = name.toLowerCase();
for (String groupName : flatGroups.keySet()) {
if (groupName.toLowerCase().equals(nameLowercase)) {
return true;
}
}
return false;
}
public FavoriteGroup getGroup(FavouritePoint p) {
if (flatGroups.containsKey(p.getCategory())) {
return flatGroups.get(p.getCategory());

View file

@ -67,6 +67,7 @@ import net.osmand.plus.helpers.GpxImportHelper;
import net.osmand.plus.helpers.WakeLockHelper;
import net.osmand.plus.mapcontextmenu.MapContextMenu;
import net.osmand.plus.mapcontextmenu.editors.FavoritePointEditor;
import net.osmand.plus.mapcontextmenu.editors.PointEditor;
import net.osmand.plus.render.RendererRegistry;
import net.osmand.plus.resources.ResourceManager;
import net.osmand.plus.routing.RoutingHelper;
@ -992,6 +993,13 @@ public class MapActivity extends AccessibleActivity {
return favoritePointEditor;
}
public PointEditor getPointEditor(String tag) {
if (favoritePointEditor != null && favoritePointEditor.getFragmentTag().equals(tag)) {
return favoritePointEditor;
}
return null;
}
public void openDrawer() {
drawerLayout.openDrawer(Gravity.LEFT);
}

View file

@ -6,17 +6,26 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.Spinner;
import android.widget.TextView;
import static android.util.TypedValue.COMPLEX_UNIT_DIP;
public class ColorDialogs {
public static int[] paletteColors = new int[] {
R.string.color_red,
@ -89,7 +98,53 @@ public class ColorDialogs {
colorSpinner.setOnItemSelectedListener(listener);
colorSpinner.setSelection(selection);
}
public static void setupColorSpinnerEx(final Activity ctx, int selectedColor, final Spinner colorSpinner,
final TIntArrayList colors, OnItemSelectedListener listener) {
colors.add(pallette);
List<String> colorNames = new ArrayList<String>();
int selection = -1;
for (int i = 0; i < pallette.length; i++) {
colorNames.add(ctx.getString(paletteColors[i]));
colors.add(pallette[i]);
if (selectedColor == pallette[i]) {
selection = i;
}
}
if (selection == -1) {
colors.insert(0, selectedColor);
colorNames.add(0, colorToString(selectedColor));
selection = 0;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ctx, R.layout.color_spinner_item, colorNames) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
if (v instanceof TextView) {
TextView textView = (TextView) v;
textView.setCompoundDrawablesWithIntrinsicBounds(getIcon(ctx, R.drawable.ic_action_folder, colors.get(position)), null, null, null);
textView.setCompoundDrawablePadding(dpToPx(ctx, 15f));
}
return v;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
if (v instanceof TextView) {
TextView textView = (TextView) v;
textView.setCompoundDrawablesWithIntrinsicBounds(getIcon(ctx, R.drawable.ic_action_circle, colors.get(position)), null, null, null);
textView.setCompoundDrawablePadding(dpToPx(ctx, 15f));
}
return v;
}
};
adapter.setDropDownViewResource(R.layout.color_spinner_dropdown_item);
colorSpinner.setAdapter(adapter);
colorSpinner.setOnItemSelectedListener(listener);
colorSpinner.setSelection(selection);
}
public static int getRandomColor() {
return pallette[new Random().nextInt(pallette.length)];
}
@ -109,4 +164,21 @@ public class ColorDialogs {
}
return "#" + c; //$NON-NLS-1$
}
private static Drawable getIcon(final Activity activity, int resId, int color) {
OsmandApplication app = (OsmandApplication)activity.getApplication();
Drawable d = app.getResources().getDrawable(resId).mutate();
d.clearColorFilter();
d.setColorFilter(color, PorterDuff.Mode.SRC_IN);
return d;
}
private static int dpToPx(final Activity activity, float dp) {
Resources r = activity.getResources();
return (int) TypedValue.applyDimension(
COMPLEX_UNIT_DIP,
dp,
r.getDisplayMetrics()
);
}
}

View file

@ -7,6 +7,7 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.widgets.AutoCompleteTextViewEx;
public abstract class PointEditor {
@ -59,4 +60,12 @@ public abstract class PointEditor {
if (fragment != null)
((PointEditorFragment)fragment).dismiss();
}
public void setCategory(String name) {
Fragment fragment = mapActivity.getSupportFragmentManager().findFragmentByTag(getFragmentTag());
if (fragment != null) {
PointEditorFragment editorFragment = (PointEditorFragment) fragment;
editorFragment.setCategory(name);
}
}
}

View file

@ -108,8 +108,8 @@ public abstract class PointEditorFragment extends Fragment {
public boolean onTouch(final View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
DialogFragment dialogFragment =
SelectCategoryDialogFragment.createInstance();
dialogFragment.show(getChildFragmentManager(), "SelectCategoryDialogFragment");
SelectCategoryDialogFragment.createInstance(getEditor().getFragmentTag());
dialogFragment.show(getChildFragmentManager(), SelectCategoryDialogFragment.TAG);
return true;
}
return false;
@ -231,7 +231,6 @@ public abstract class PointEditorFragment extends Fragment {
AutoCompleteTextViewEx categoryEdit = (AutoCompleteTextViewEx) getView().findViewById(R.id.category_edit);
String name = categoryEdit.getText().toString().trim();
return name.equals(getString(R.string.shared_string_favorites)) ? "" : name;
}
public String getDescriptionTextValue() {

View file

@ -0,0 +1,173 @@
package net.osmand.plus.mapcontextmenu.editors.dialogs;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.FavouritesDbHelper.FavoriteGroup;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.ColorDialogs;
import net.osmand.plus.mapcontextmenu.MapContextMenuFragment;
import net.osmand.plus.mapcontextmenu.editors.PointEditor;
import net.osmand.util.Algorithms;
import gnu.trove.list.array.TIntArrayList;
public class EditCategoryDialogFragment extends DialogFragment {
public static final String TAG = "EditCategoryDialogFragment";
private static final String KEY_CTX_EDIT_CAT_EDITOR_TAG = "key_ctx_edit_cat_editor_tag";
private static final String KEY_CTX_EDIT_CAT_NEW = "key_ctx_edit_cat_new";
private static final String KEY_CTX_EDIT_CAT_NAME = "key_ctx_edit_cat_name";
private static final String KEY_CTX_EDIT_CAT_COLOR = "key_ctx_edit_cat_color";
private String editorTag;
private boolean isNew = true;
private String name = "";
private int color;
private EditText nameEdit;
private Spinner colorSpinner;
FavouritesDbHelper helper;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
helper = ((OsmandApplication) getActivity().getApplication()).getFavorites();
color = ColorDialogs.pallette[0];
if (savedInstanceState != null) {
restoreState(savedInstanceState);
} else if (getArguments() != null) {
restoreState(getArguments());
}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.favorite_category_add_new_title);
final View v = getActivity().getLayoutInflater().inflate(R.layout.favorite_category_edit_dialog, null, false);
nameEdit = (EditText)v.findViewById(R.id.edit_name);
nameEdit.setText(name);
colorSpinner = (Spinner)v.findViewById(R.id.edit_color);
final TIntArrayList colors = new TIntArrayList();
final int intColor = color;
ColorDialogs.setupColorSpinnerEx(getActivity(), intColor, colorSpinner, colors, new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
color = colors.get(position);
colorSpinner.invalidate();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
builder.setView(v);
builder.setPositiveButton(R.string.shared_string_save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { }
});
builder.setNegativeButton(R.string.shared_string_cancel, null);
return builder.create();
}
@Override
public void onStart()
{
super.onStart();
final AlertDialog d = (AlertDialog)getDialog();
if(d != null)
{
Button positiveButton = d.getButton(Dialog.BUTTON_POSITIVE);
positiveButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
name = nameEdit.getText().toString().trim();
if (!helper.groupExists(name)) {
helper.addEmptyCategory(name, color);
PointEditor editor = ((MapActivity) getActivity()).getPointEditor(editorTag);
if (editor != null) {
editor.setCategory(name);
}
d.dismiss();
} else {
AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
b.setMessage(getString(R.string.favorite_category_dublicate_message));
b.setNegativeButton(R.string.shared_string_ok, null);
b.show();
}
}
});
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
saveState(outState);
super.onSaveInstanceState(outState);
}
public static EditCategoryDialogFragment createInstance(String editorTag) {
EditCategoryDialogFragment fragment = new EditCategoryDialogFragment();
Bundle bundle = new Bundle();
bundle.putString(KEY_CTX_EDIT_CAT_EDITOR_TAG, editorTag);
fragment.setArguments(bundle);
return fragment;
}
public static EditCategoryDialogFragment createInstance(String editorTag, FavoriteGroup group) {
EditCategoryDialogFragment fragment = new EditCategoryDialogFragment();
Bundle bundle = new Bundle();
bundle.putString(KEY_CTX_EDIT_CAT_EDITOR_TAG, editorTag);
bundle.putString(KEY_CTX_EDIT_CAT_NEW, Boolean.valueOf(false).toString());
bundle.putString(KEY_CTX_EDIT_CAT_NAME, group.name);
bundle.putString(KEY_CTX_EDIT_CAT_COLOR, "" + group.color);
fragment.setArguments(bundle);
return fragment;
}
public void saveState(Bundle bundle) {
bundle.putString(KEY_CTX_EDIT_CAT_EDITOR_TAG, editorTag);
bundle.putString(KEY_CTX_EDIT_CAT_NEW, Boolean.valueOf(isNew).toString());
bundle.putString(KEY_CTX_EDIT_CAT_NAME, nameEdit.getText().toString().trim());
bundle.putString(KEY_CTX_EDIT_CAT_COLOR, "" + color);
}
public void restoreState(Bundle bundle) {
editorTag = bundle.getString(KEY_CTX_EDIT_CAT_EDITOR_TAG);
String isNewStr = bundle.getString(KEY_CTX_EDIT_CAT_NEW);
if (isNewStr != null) {
isNew = Boolean.valueOf(isNewStr);
}
name = bundle.getString(KEY_CTX_EDIT_CAT_NAME);
if (name == null) {
name = "";
}
String colorStr = bundle.getString(KEY_CTX_EDIT_CAT_COLOR);
if (!Algorithms.isEmpty(colorStr)) {
color = Integer.parseInt(colorStr);
}
}
}

View file

@ -3,29 +3,47 @@ package net.osmand.plus.mapcontextmenu.editors.dialogs;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.res.Resources;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.IconsCache;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.editors.PointEditor;
import net.osmand.plus.mapcontextmenu.editors.PointEditorFragment;
import java.util.List;
import static android.util.TypedValue.COMPLEX_UNIT_DIP;
public class SelectCategoryDialogFragment extends DialogFragment {
public static final String TAG = "SelectCategoryDialogFragment";
private static final String KEY_CTX_SEL_CAT_EDITOR_TAG = "key_ctx_sel_cat_editor_tag";
private String editorTag;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
if (savedInstanceState != null) {
restoreState(savedInstanceState);
} else if (getArguments() != null) {
restoreState(getArguments());
}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.favorite_category_select);
final View v = getActivity().getLayoutInflater().inflate(R.layout.favorite_categories_dialog, null, false);
@ -36,34 +54,38 @@ public class SelectCategoryDialogFragment extends DialogFragment {
List<FavouritesDbHelper.FavoriteGroup> gs = helper.getFavoriteGroups();
for (final FavouritesDbHelper.FavoriteGroup category : gs) {
View itemView = getActivity().getLayoutInflater().inflate(R.layout.favorite_category_dialog_item, null);
ImageView icon = (ImageView)itemView.findViewById(R.id.image_view);
if (category.color != 0) {
icon.setImageDrawable(getIcon(getActivity(), R.drawable.ic_action_folder, category.color));
} else {
icon.setImageDrawable(getIcon(getActivity(), R.drawable.ic_action_folder));
}
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), null, null, null);
}
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) {
((PointEditorFragment) getParentFragment()).setCategory(category.name);
PointEditor editor = ((MapActivity) getActivity()).getPointEditor(editorTag);
if (editor != null) {
editor.setCategory(category.name);
}
dismiss();
}
});
ll.addView(itemView);
}
View itemView = getActivity().getLayoutInflater().inflate(R.layout.favorite_category_dialog_item, null);
ImageView icon = (ImageView)itemView.findViewById(R.id.image_view);
icon.setImageDrawable(getIcon(getActivity(), R.drawable.map_zoom_in));
Button button = (Button)itemView.findViewById(R.id.button);
button.setCompoundDrawablesWithIntrinsicBounds(getIcon(getActivity(), R.drawable.map_zoom_in), null, null, null);
button.setCompoundDrawablePadding(dpToPx(15f));
button.setText(getActivity().getResources().getText(R.string.favorite_category_add_new));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//((PointEditorFragment) getParentFragment()).setCategory(null);
//dismiss();
dismiss();
DialogFragment dialogFragment = EditCategoryDialogFragment.createInstance(editorTag);
dialogFragment.show(getActivity().getSupportFragmentManager(), EditCategoryDialogFragment.TAG);
}
});
ll.addView(itemView);
@ -74,8 +96,20 @@ public class SelectCategoryDialogFragment extends DialogFragment {
return builder.create();
}
public static SelectCategoryDialogFragment createInstance() {
return new SelectCategoryDialogFragment();
public static SelectCategoryDialogFragment createInstance(String editorTag) {
SelectCategoryDialogFragment fragment = new SelectCategoryDialogFragment();
Bundle bundle = new Bundle();
bundle.putString(KEY_CTX_SEL_CAT_EDITOR_TAG, editorTag);
fragment.setArguments(bundle);
return fragment;
}
public void saveState(Bundle bundle) {
bundle.putString(KEY_CTX_SEL_CAT_EDITOR_TAG, editorTag);
}
public void restoreState(Bundle bundle) {
editorTag = bundle.getString(KEY_CTX_SEL_CAT_EDITOR_TAG);
}
private static Drawable getIcon(final Activity activity, int iconId) {
@ -93,4 +127,13 @@ public class SelectCategoryDialogFragment extends DialogFragment {
d.setColorFilter(color, PorterDuff.Mode.SRC_IN);
return d;
}
private int dpToPx(float dp) {
Resources r = getActivity().getResources();
return (int) TypedValue.applyDimension(
COMPLEX_UNIT_DIP,
dp,
r.getDisplayMetrics()
);
}
}

View file

@ -189,40 +189,6 @@ public class FavoritesLayer extends OsmandMapLayer implements ContextMenuLayer.
}
return null;
}
@Override
public void populateObjectContextMenu(Object o, ContextMenuAdapter adapter) {
if(o instanceof FavouritePoint) {
final FavouritePoint a = (FavouritePoint) o;
OnContextMenuClick listener = new ContextMenuAdapter.OnContextMenuClick() {
@Override
public boolean onContextMenuClick(ArrayAdapter<?> adapter, int itemId, int pos, boolean isChecked) {
if (itemId == R.string.favourites_context_menu_edit) {
FavoritesTreeFragment.editPoint(view.getContext(), a, null);
} else if (itemId == R.string.favourites_context_menu_delete) {
final Resources resources = view.getContext().getResources();
Builder builder = new AlertDialog.Builder(view.getContext());
builder.setMessage(resources.getString(R.string.favourites_remove_dialog_msg, a.getName()));
builder.setNegativeButton(R.string.shared_string_no, null);
builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
favorites.deleteFavourite(a);
view.refreshMap();
}
});
builder.create().show();
}
return true;
}
};
adapter.item(R.string.favourites_context_menu_edit).iconColor(R.drawable.ic_action_edit_dark)
.listen(listener).reg();
adapter.item(R.string.favourites_context_menu_delete)
.iconColor(R.drawable.ic_action_delete_dark).listen(listener)
.reg();
}
}
@Override
public LatLon getTextLocation(LocationPoint o) {