quick action name duplicates fix

This commit is contained in:
Rosty 2016-12-29 14:12:47 +02:00
parent 4b64b70d75
commit 37a2c95e54
3 changed files with 70 additions and 4 deletions

View file

@ -2485,4 +2485,6 @@ If you need help with OsmAnd application, please contact our support team: suppo
<string name="quick_action_favorite_dialog">Show favorite dialog</string> <string name="quick_action_favorite_dialog">Show favorite dialog</string>
<string name="favorite_autofill_toast_text">" is saved to "</string> <string name="favorite_autofill_toast_text">" is saved to "</string>
<string name="favorite_empty_place_name">Place</string> <string name="favorite_empty_place_name">Place</string>
<string name="quick_action_duplicates">Specified quick action name already in use, was changed to %1$s to avoid duplication.</string>
<string name="quick_action_duplicate">Quick action name duplicate</string>
</resources> </resources>

View file

@ -1,11 +1,14 @@
package net.osmand.plus.quickaction; package net.osmand.plus.quickaction;
import android.app.Dialog; import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.view.ContextThemeWrapper; import android.support.v7.view.ContextThemeWrapper;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.text.Editable; import android.text.Editable;
@ -17,6 +20,8 @@ import android.view.Window;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageView; import android.widget.ImageView;
import net.osmand.data.FavouritePoint;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.IconsCache; import net.osmand.plus.IconsCache;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; 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()); action.fillParams(((ViewGroup) root.findViewById(R.id.container)).getChildAt(0), (MapActivity) getActivity());
if (isNew) quickActionRegistry.addQuickAction(action); if (quickActionRegistry.isNameUnique(action, getContext())) {
else quickActionRegistry.updateQuickAction(action);
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()));
}
} }
}); });
} }

View file

@ -1,5 +1,7 @@
package net.osmand.plus.quickaction; package net.osmand.plus.quickaction;
import android.content.Context;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import java.util.ArrayList; import java.util.ArrayList;
@ -104,4 +106,34 @@ public class QuickActionRegistry {
return null; 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;
}
}
} }