From 20bf9e983f65ae6e7e963130d4352104f0dc87e3 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sat, 13 Feb 2021 23:08:16 +0200 Subject: [PATCH] manipulation of templates via pop-up menu, add template duplication --- .../ui/templates/TemplateListFragment.java | 2 ++ .../ui/templates/TemplateViewHolder.java | 28 +++++++++++++++++-- .../ui/templates/TemplatesActivity.java | 7 ++++- .../layout/template_list_template_item.xml | 15 ++-------- app/src/main/res/values-bg/arrays.xml | 8 +++++- app/src/main/res/values/arrays.xml | 7 ++++- 6 files changed, 50 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateListFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateListFragment.java index c7020e95..52e41f2f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateListFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateListFragment.java @@ -131,5 +131,7 @@ public class TemplateListFragment extends Fragment { void onSaveTemplate(); void onEditTemplate(Long id); + + void onDuplicateTemplate(long id); } } \ No newline at end of file diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateViewHolder.java b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateViewHolder.java index e9b9032d..c8c197f4 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateViewHolder.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateViewHolder.java @@ -18,8 +18,10 @@ package net.ktnx.mobileledger.ui.templates; import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; import androidx.recyclerview.widget.RecyclerView; +import net.ktnx.mobileledger.R; import net.ktnx.mobileledger.databinding.TemplateListTemplateItemBinding; import net.ktnx.mobileledger.db.TemplateHeader; @@ -31,8 +33,30 @@ class TemplateViewHolder extends RecyclerView.ViewHolder { } public void bindToItem(TemplateHeader item) { b.templateName.setText(item.getName()); - b.editButton.setOnClickListener(v -> { - ((TemplatesActivity) v.getContext()).onEditTemplate(item.getId()); + b.templateName.setOnClickListener( + v -> ((TemplatesActivity) v.getContext()).onEditTemplate(item.getId())); + b.templateName.setOnLongClickListener((v) -> { + TemplatesActivity activity = (TemplatesActivity) v.getContext(); + AlertDialog.Builder builder = new AlertDialog.Builder(activity); + final String templateName = item.getName(); + builder.setTitle(templateName); + builder.setItems(R.array.templates_ctx_menu, (dialog, which) -> { + if (which == 0) { // edit + activity.onEditTemplate(item.getId()); + } + else if (which == 1) { // duplicate + activity.onDuplicateTemplate(item.getId()); + } + else if (which == 2) { // delete + activity.onDeleteTemplate(item.getId()); + } + else { + throw new RuntimeException(String.format("Unknown menu item id (%d)", which)); + } + dialog.dismiss(); + }); + builder.show(); + return true; }); } } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplatesActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplatesActivity.java index e8668b0b..62a005ff 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplatesActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplatesActivity.java @@ -101,7 +101,12 @@ public class TemplatesActivity extends CrashReportingActivity } return super.onOptionsItemSelected(item); } - + @Override + public void onDuplicateTemplate(long id) { + DB.get() + .getTemplateDAO() + .duplicateTemplateWitAccounts(id, null); + } @Override public void onEditTemplate(Long id) { if (id == null) { diff --git a/app/src/main/res/layout/template_list_template_item.xml b/app/src/main/res/layout/template_list_template_item.xml index 46d6410e..fb5e2431 100644 --- a/app/src/main/res/layout/template_list_template_item.xml +++ b/app/src/main/res/layout/template_list_template_item.xml @@ -25,21 +25,12 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="@dimen/text_margin" - android:layout_marginEnd="@dimen/text_margin" + android:gravity="center_vertical" app:layout_constraintBottom_toBottomOf="parent" - app:layout_constraintEnd_toStartOf="@id/edit_button" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" - /> - \ No newline at end of file diff --git a/app/src/main/res/values-bg/arrays.xml b/app/src/main/res/values-bg/arrays.xml index b475943c..95305670 100644 --- a/app/src/main/res/values-bg/arrays.xml +++ b/app/src/main/res/values-bg/arrays.xml @@ -1,5 +1,5 @@ + + Промяна + Дублиране + Изтриване + + diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 17b63296..0e855653 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -1,5 +1,5 @@ + + Edit + Duplicate + Delete + \ No newline at end of file -- 2.39.5