]> git.ktnx.net Git - mobile-ledger.git/commitdiff
template deletion, with undo
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 5 Feb 2021 06:36:11 +0000 (08:36 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 5 Feb 2021 13:16:49 +0000 (13:16 +0000)
app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplatesActivity.java
app/src/main/res/values-bg/strings.xml
app/src/main/res/values/strings.xml

index b72272ebcfad42b39252afda1c734cb0af0c44ed..d8d1bb907195d71fb3016b9716e198ab5c22846c 100644 (file)
@@ -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
index 7bcece63d7e17f3e0aa67937e4cc6a23f41b85b5..e8668b0b05b86c50016f50548af119f25468767c 100644 (file)
@@ -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
index 75ffb64d67e0d7bd226f4566da350e123795a0e2..2654275a611d8a7e53f9644a41e179cae4c17205 100644 (file)
     <string name="template_details_source_literal">ръчно въвеждане</string>
     <string name="account_comment_source_label">Източник на бележка към сметката</string>
     <string name="account_amount_source_label">Източник на името на сметката</string>
+    <string name="template_xxx_deleted">Макетът „%s“ е изтрит</string>
+    <string name="action_undo">Връщане</string>
 </resources>
index d9d1ab3b223e02632e322359a709f3ba2d83b5d1..8e126f281cdaeb506865d30ea30f27ccc121feaf 100644 (file)
     <string name="implementation_pending">Not implemented yet</string>
     <string name="title_edit_template">Edit template</string>
     <string name="title_new_template">New template</string>
+    <string name="template_xxx_deleted">Template \'%s\' deleted</string>
+    <string name="action_undo">Undo</string>
 </resources>