From 37a2c95e546b2f79438bf1b8b628f29b1e4cf8d9 Mon Sep 17 00:00:00 2001 From: Rosty Date: Thu, 29 Dec 2016 14:12:47 +0200 Subject: [PATCH] quick action name duplicates fix --- OsmAnd/res/values/strings.xml | 2 + .../quickaction/CreateEditActionDialog.java | 40 +++++++++++++++++-- .../plus/quickaction/QuickActionRegistry.java | 32 +++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 9f844ce21e..9df977609f 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -2485,4 +2485,6 @@ If you need help with OsmAnd application, please contact our support team: suppo Show favorite dialog " is saved to " Place + Specified quick action name already in use, was changed to %1$s to avoid duplication. + Quick action name duplicate diff --git a/OsmAnd/src/net/osmand/plus/quickaction/CreateEditActionDialog.java b/OsmAnd/src/net/osmand/plus/quickaction/CreateEditActionDialog.java index 16cec18464..7168ae69ac 100644 --- a/OsmAnd/src/net/osmand/plus/quickaction/CreateEditActionDialog.java +++ b/OsmAnd/src/net/osmand/plus/quickaction/CreateEditActionDialog.java @@ -1,11 +1,14 @@ package net.osmand.plus.quickaction; import android.app.Dialog; +import android.content.Context; +import android.content.DialogInterface; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; +import android.support.v7.app.AlertDialog; import android.support.v7.view.ContextThemeWrapper; import android.support.v7.widget.Toolbar; import android.text.Editable; @@ -17,6 +20,8 @@ import android.view.Window; import android.widget.EditText; import android.widget.ImageView; +import net.osmand.data.FavouritePoint; +import net.osmand.plus.FavouritesDbHelper; import net.osmand.plus.IconsCache; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; @@ -188,12 +193,39 @@ public class CreateEditActionDialog extends DialogFragment { action.fillParams(((ViewGroup) root.findViewById(R.id.container)).getChildAt(0), (MapActivity) getActivity()); - if (isNew) quickActionRegistry.addQuickAction(action); - else quickActionRegistry.updateQuickAction(action); + if (quickActionRegistry.isNameUnique(action, getContext())) { - quickActionRegistry.notifyUpdates(); + if (isNew) quickActionRegistry.addQuickAction(action); + else quickActionRegistry.updateQuickAction(action); - dismiss(); + quickActionRegistry.notifyUpdates(); + + dismiss(); + + } else { + + action = quickActionRegistry.generateUniqueName(action, getContext()); + + AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); + builder.setTitle(R.string.quick_action_duplicate); + builder.setMessage(getString(R.string.quick_action_duplicates, action.getName(getContext()))); + builder.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + + if (isNew) quickActionRegistry.addQuickAction(action); + else quickActionRegistry.updateQuickAction(action); + + quickActionRegistry.notifyUpdates(); + + CreateEditActionDialog.this.dismiss(); + dismiss(); + } + }); + builder.create().show(); + + ((EditText) root.findViewById(R.id.name)).setText(action.getName(getContext())); + } } }); } diff --git a/OsmAnd/src/net/osmand/plus/quickaction/QuickActionRegistry.java b/OsmAnd/src/net/osmand/plus/quickaction/QuickActionRegistry.java index 26dccbffe5..05960e48d3 100644 --- a/OsmAnd/src/net/osmand/plus/quickaction/QuickActionRegistry.java +++ b/OsmAnd/src/net/osmand/plus/quickaction/QuickActionRegistry.java @@ -1,5 +1,7 @@ package net.osmand.plus.quickaction; +import android.content.Context; + import net.osmand.plus.OsmandSettings; import java.util.ArrayList; @@ -104,4 +106,34 @@ public class QuickActionRegistry { return null; } + + public boolean isNameUnique(QuickAction action, Context context){ + + + for (QuickAction a: quickActions){ + + if (action.id != a.id) { + + if (action.getName(context).equals(a.getName(context))) + return false; + } + } + + return true; + } + + public QuickAction generateUniqueName(QuickAction action, Context context) { + + int number = 0; + String name = action.getName(context); + + while (true) { + + number++; + + action.setName(name + "(" + number + ")"); + + if (isNameUnique(action, context)) return action; + } + } }