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;
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;
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();
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);
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);
if (text != null)
mViewModel.setTestText(text);
}
+ interface InteractionListener {
+ void onDeleteTemplate(@NonNull Long templateId);
+ }
}
\ No newline at end of file
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;
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);
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
<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>
<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>