Merge pull request #9004 from osmandapp/delete_quick_action

Add ability to delete quick action in editing screen
This commit is contained in:
vshcherb 2020-05-19 14:06:07 +02:00 committed by GitHub
commit d0a8696f36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 14 deletions

View file

@ -1,6 +1,7 @@
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.content.DialogInterface;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
@ -9,6 +10,8 @@ import android.os.Bundle;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.Window; import android.view.Window;
@ -31,6 +34,8 @@ import net.osmand.plus.activities.MapActivity;
import java.util.List; import java.util.List;
import static net.osmand.plus.quickaction.QuickActionListFragment.showConfirmActionDeleteDialog;
/** /**
* Created by rosty on 12/27/16. * Created by rosty on 12/27/16.
*/ */
@ -168,6 +173,32 @@ public class CreateEditActionDialog extends DialogFragment implements CallbackWi
dismiss(); dismiss();
} }
}); });
if (!isNew) {
Menu menu = toolbar.getMenu();
menu.clear();
MenuItem item = menu.add(R.string.shared_string_delete).setIcon(R.drawable.ic_action_delete_dark);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Context ctx = getContext();
if (ctx != null) {
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
quickActionRegistry.deleteQuickAction(action);
quickActionRegistry.notifyUpdates();
dialog.dismiss();
dismiss();
}
};
showConfirmActionDeleteDialog(ctx, action, !isLightContent, listener);
}
return true;
}
});
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
} }
private void setupHeader(View root, Bundle savedInstanceState){ private void setupHeader(View root, Bundle savedInstanceState){

View file

@ -184,26 +184,24 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements Quick
quickActionRegistry.updateQuickActions(adapter.getQuickActions()); quickActionRegistry.updateQuickActions(adapter.getQuickActions());
} }
void createAndShowDeleteDialog(final int itemPosition, final String itemName) { static void showConfirmActionDeleteDialog(Context ctx, QuickAction action, boolean nightMode, DialogInterface.OnClickListener listener) {
AlertDialog.Builder builder = new AlertDialog.Builder(UiUtilities.getThemedContext(getContext(), !isLightContent)); OsmandApplication app = (OsmandApplication) ctx.getApplicationContext();
AlertDialog.Builder builder = new AlertDialog.Builder(UiUtilities.getThemedContext(ctx, nightMode));
builder.setTitle(R.string.quick_actions_delete); builder.setTitle(R.string.quick_actions_delete);
builder.setMessage(getResources().getString(R.string.quick_actions_delete_text, itemName)); builder.setMessage(app.getString(R.string.quick_actions_delete_text, ctx.getString(action.getNameRes())));
builder.setIcon(getMyApplication().getUIUtilities().getThemedIcon(R.drawable.ic_action_delete_dark)); builder.setIcon(app.getUIUtilities().getThemedIcon(R.drawable.ic_action_delete_dark));
builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { builder.setPositiveButton(R.string.shared_string_yes, listener);
public void onClick(DialogInterface dialog, int id) {
adapter.deleteItem(itemPosition);
dialog.dismiss();
}
});
builder.setNegativeButton(R.string.shared_string_no, new DialogInterface.OnClickListener() { builder.setNegativeButton(R.string.shared_string_no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
dialog.dismiss(); dialog.dismiss();
} }
}); });
AlertDialog dialog = builder.show(); AlertDialog dialog = builder.show();
int activeColorPrimaryResId = isLightContent ? R.color.active_color_primary_light : R.color.active_color_primary_dark; int activeColorId = nightMode ? R.color.active_color_primary_dark : R.color.active_color_primary_light;
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(getContext(), activeColorPrimaryResId)); int activeColor = ContextCompat.getColor(ctx, activeColorId);
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(getContext(), activeColorPrimaryResId)); dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(activeColor);
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(activeColor);
} }
@Override @Override
@ -257,7 +255,16 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements Quick
itemVH.closeBtn.setOnClickListener(new View.OnClickListener() { itemVH.closeBtn.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
createAndShowDeleteDialog(holder.getAdapterPosition(), getResources().getString(item.getNameRes())); Context ctx = getContext();
if (ctx != null) {
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
adapter.deleteItem(holder.getAdapterPosition());
dialog.dismiss();
}
};
showConfirmActionDeleteDialog(ctx, item, !isLightContent, listener);
}
} }
}); });