2 * Copyright © 2021 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.templates;
20 import android.os.Bundle;
21 import android.view.MenuItem;
23 import androidx.annotation.NonNull;
24 import androidx.appcompat.app.ActionBar;
25 import androidx.lifecycle.ViewModelProvider;
26 import androidx.lifecycle.ViewModelStoreOwner;
27 import androidx.navigation.NavController;
28 import androidx.navigation.NavDestination;
29 import androidx.navigation.fragment.NavHostFragment;
31 import com.google.android.material.snackbar.BaseTransientBottomBar;
32 import com.google.android.material.snackbar.Snackbar;
34 import net.ktnx.mobileledger.R;
35 import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
36 import net.ktnx.mobileledger.databinding.ActivityTemplatesBinding;
37 import net.ktnx.mobileledger.db.DB;
38 import net.ktnx.mobileledger.db.TemplateWithAccounts;
39 import net.ktnx.mobileledger.ui.activity.CrashReportingActivity;
40 import net.ktnx.mobileledger.utils.Logger;
42 import java.util.Objects;
44 public class TemplatesActivity extends CrashReportingActivity
45 implements TemplateListFragment.OnTemplateListFragmentInteractionListener,
46 TemplateDetailsFragment.InteractionListener {
47 public static final String ARG_ADD_TEMPLATE = "add-template";
48 private ActivityTemplatesBinding b;
49 private NavController navController;
51 // public boolean onCreateOptionsMenu(Menu menu) {
52 // super.onCreateOptionsMenu(menu);
53 // getMenuInflater().inflate(R.menu.template_list_menu, menu);
58 protected void onCreate(Bundle savedInstanceState) {
59 super.onCreate(savedInstanceState);
60 b = ActivityTemplatesBinding.inflate(getLayoutInflater());
61 setContentView(b.getRoot());
62 setSupportActionBar(b.toolbar);
63 // Show the Up button in the action bar.
64 ActionBar actionBar = getSupportActionBar();
65 if (actionBar != null) {
66 actionBar.setDisplayHomeAsUpEnabled(true);
69 NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull(
70 getSupportFragmentManager().findFragmentById(R.id.fragment_container));
71 navController = navHostFragment.getNavController();
73 navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
74 if (destination.getId() == R.id.templateListFragment) {
77 b.toolbarLayout.setTitle(getString(R.string.title_activity_templates));
79 if (destination.getId() == R.id.templateDetailsFragment) {
85 b.toolbarLayout.setTitle(getString(R.string.title_activity_templates));
87 b.fabAdd.setOnClickListener(v -> onEditTemplate(null));
88 b.fabSave.setOnClickListener(v -> onSaveTemplate());
91 public boolean onOptionsItemSelected(MenuItem item) {
92 if (item.getItemId() == android.R.id.home) {
93 final NavDestination currentDestination = navController.getCurrentDestination();
94 if (currentDestination != null &&
95 currentDestination.getId() == R.id.templateDetailsFragment)
96 navController.popBackStack();
102 return super.onOptionsItemSelected(item);
105 public void onDuplicateTemplate(long id) {
108 .duplicateTemplateWitAccounts(id, null);
111 public void onEditTemplate(Long id) {
113 navController.navigate(R.id.action_templateListFragment_to_templateDetailsFragment);
114 b.toolbarLayout.setTitle(getString(R.string.title_new_template));
117 Bundle bundle = new Bundle();
118 bundle.putLong(TemplateDetailsFragment.ARG_TEMPLATE_ID, id);
119 navController.navigate(R.id.action_templateListFragment_to_templateDetailsFragment,
121 b.toolbarLayout.setTitle(getString(R.string.title_edit_template));
125 public void onSaveTemplate() {
126 final ViewModelStoreOwner viewModelStoreOwner =
127 navController.getViewModelStoreOwner(R.id.template_list_navigation);
128 TemplateDetailsViewModel model =
129 new ViewModelProvider(viewModelStoreOwner).get(TemplateDetailsViewModel.class);
130 Logger.debug("flow", "TemplatesActivity.onSavePattern(): model=" + model);
131 model.onSaveTemplate();
132 navController.navigateUp();
134 public NavController getNavController() {
135 return navController;
138 public void onDeleteTemplate(@NonNull Long templateId) {
139 Objects.requireNonNull(templateId);
140 TemplateHeaderDAO dao = DB.get()
143 dao.getTemplateWitAccountsAsync(templateId, template -> {
144 TemplateWithAccounts copy = TemplateWithAccounts.from(template);
145 dao.deleteAsync(template.header, () -> {
146 navController.popBackStack(R.id.templateListFragment, false);
148 Snackbar.make(b.getRoot(), String.format(
149 TemplatesActivity.this.getString(R.string.template_xxx_deleted),
150 template.header.getName()), BaseTransientBottomBar.LENGTH_LONG)
151 .setAction(R.string.action_undo, v -> dao.insertAsync(copy, null))