From: Damyan Ivanov Date: Fri, 5 Feb 2021 06:36:11 +0000 (+0200) Subject: template deletion, with undo X-Git-Tag: v0.17.0~155 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=32ac06215a6010a0591645338513a035d074b4da;p=mobile-ledger.git template deletion, with undo --- diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsFragment.java index b72272eb..d8d1bb90 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsFragment.java @@ -20,6 +20,9 @@ package net.ktnx.mobileledger.ui.templates; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; @@ -31,8 +34,6 @@ import androidx.navigation.NavController; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager; -import com.google.android.material.snackbar.Snackbar; - import net.ktnx.mobileledger.R; import net.ktnx.mobileledger.databinding.TemplateDetailsFragmentBinding; import net.ktnx.mobileledger.ui.QRScanCapableFragment; @@ -41,12 +42,18 @@ import net.ktnx.mobileledger.utils.Logger; public class TemplateDetailsFragment extends QRScanCapableFragment { static final String ARG_TEMPLATE_ID = "pattern-id"; private static final String ARG_COLUMN_COUNT = "column-count"; - TemplateDetailsFragmentBinding b; + private TemplateDetailsFragmentBinding b; private TemplateDetailsViewModel mViewModel; private int mColumnCount = 1; private Long mPatternId; + private InteractionListener interactionListener; public TemplateDetailsFragment() { } + @Override + public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { + super.onCreateOptionsMenu(menu, inflater); + inflater.inflate(R.menu.template_details_menu, menu); + } public static TemplateDetailsFragment newInstance(int columnCount, int patternId) { final TemplateDetailsFragment fragment = new TemplateDetailsFragment(); Bundle args = new Bundle(); @@ -57,6 +64,19 @@ public class TemplateDetailsFragment extends QRScanCapableFragment { return fragment; } @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + if (item.getItemId() == R.id.delete_template) { + signalDeleteTemplateInteraction(); + return true; + } + + return super.onOptionsItemSelected(item); + } + private void signalDeleteTemplateInteraction() { + if (interactionListener != null) + interactionListener.onDeleteTemplate(mPatternId); + } + @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -67,10 +87,18 @@ public class TemplateDetailsFragment extends QRScanCapableFragment { if (mPatternId == -1) mPatternId = null; } + + setHasOptionsMenu(mPatternId != null); } @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + if (!(getActivity() instanceof InteractionListener)) + throw new IllegalStateException( + "Containing activity must implement TemplateDetailsFragment" + + ".InteractionListener"); + interactionListener = (InteractionListener) getActivity(); + NavController controller = ((TemplatesActivity) requireActivity()).getNavController(); final ViewModelStoreOwner viewModelStoreOwner = controller.getViewModelStoreOwner(R.id.template_list_navigation); @@ -102,4 +130,7 @@ public class TemplateDetailsFragment extends QRScanCapableFragment { if (text != null) mViewModel.setTestText(text); } + interface InteractionListener { + void onDeleteTemplate(@NonNull Long templateId); + } } \ No newline at end of file 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 7bcece63..e8668b0b 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 @@ -18,9 +18,9 @@ package net.ktnx.mobileledger.ui.templates; import android.os.Bundle; -import android.view.Menu; import android.view.MenuItem; +import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelStoreOwner; @@ -28,25 +28,32 @@ import androidx.navigation.NavController; import androidx.navigation.NavDestination; import androidx.navigation.fragment.NavHostFragment; +import com.google.android.material.snackbar.BaseTransientBottomBar; +import com.google.android.material.snackbar.Snackbar; + import net.ktnx.mobileledger.R; +import net.ktnx.mobileledger.dao.TemplateHeaderDAO; import net.ktnx.mobileledger.databinding.ActivityTemplatesBinding; +import net.ktnx.mobileledger.db.DB; +import net.ktnx.mobileledger.db.TemplateWithAccounts; import net.ktnx.mobileledger.ui.activity.CrashReportingActivity; import net.ktnx.mobileledger.utils.Logger; import java.util.Objects; public class TemplatesActivity extends CrashReportingActivity - implements TemplateListFragment.OnTemplateListFragmentInteractionListener { + implements TemplateListFragment.OnTemplateListFragmentInteractionListener, + TemplateDetailsFragment.InteractionListener { public static final String ARG_ADD_TEMPLATE = "add-template"; private ActivityTemplatesBinding b; private NavController navController; - @Override - public boolean onCreateOptionsMenu(Menu menu) { - super.onCreateOptionsMenu(menu); - getMenuInflater().inflate(R.menu.template_list_menu, menu); - - return true; - } + // @Override +// public boolean onCreateOptionsMenu(Menu menu) { +// super.onCreateOptionsMenu(menu); +// getMenuInflater().inflate(R.menu.template_list_menu, menu); +// +// return true; +// } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -122,4 +129,23 @@ public class TemplatesActivity extends CrashReportingActivity public NavController getNavController() { return navController; } + @Override + public void onDeleteTemplate(@NonNull Long templateId) { + Objects.requireNonNull(templateId); + TemplateHeaderDAO dao = DB.get() + .getTemplateDAO(); + + dao.getTemplateWitAccountsAsync(templateId, template -> { + TemplateWithAccounts copy = TemplateWithAccounts.from(template); + dao.deleteAsync(template.header, () -> { + navController.popBackStack(R.id.templateListFragment, false); + + Snackbar.make(b.getRoot(), String.format( + TemplatesActivity.this.getString(R.string.template_xxx_deleted), + template.header.getName()), BaseTransientBottomBar.LENGTH_LONG) + .setAction(R.string.action_undo, v -> dao.insertAsync(copy, null)) + .show(); + }); + }); + } } \ No newline at end of file diff --git a/app/src/main/res/values-bg/strings.xml b/app/src/main/res/values-bg/strings.xml index 75ffb64d..2654275a 100644 --- a/app/src/main/res/values-bg/strings.xml +++ b/app/src/main/res/values-bg/strings.xml @@ -215,4 +215,6 @@ ръчно въвеждане Източник на бележка към сметката Източник на името на сметката + Макетът „%s“ е изтрит + Връщане diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d9d1ab3b..8e126f28 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -225,4 +225,6 @@ Not implemented yet Edit template New template + Template \'%s\' deleted + Undo