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.activity.result.ActivityResultLauncher;
24 import androidx.annotation.NonNull;
25 import androidx.appcompat.app.ActionBar;
26 import androidx.lifecycle.ViewModelProvider;
27 import androidx.lifecycle.ViewModelStoreOwner;
28 import androidx.navigation.NavController;
29 import androidx.navigation.NavDestination;
30 import androidx.navigation.fragment.NavHostFragment;
32 import com.google.android.material.snackbar.BaseTransientBottomBar;
33 import com.google.android.material.snackbar.Snackbar;
35 import net.ktnx.mobileledger.R;
36 import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
37 import net.ktnx.mobileledger.databinding.ActivityTemplatesBinding;
38 import net.ktnx.mobileledger.db.DB;
39 import net.ktnx.mobileledger.db.TemplateWithAccounts;
40 import net.ktnx.mobileledger.ui.QR;
41 import net.ktnx.mobileledger.ui.activity.CrashReportingActivity;
42 import net.ktnx.mobileledger.utils.Logger;
44 import java.util.Objects;
46 public class TemplatesActivity extends CrashReportingActivity
47 implements TemplateListFragment.OnTemplateListFragmentInteractionListener,
48 TemplateDetailsFragment.InteractionListener, QR.QRScanResultReceiver, QR.QRScanTrigger {
49 public static final String ARG_ADD_TEMPLATE = "add-template";
50 private ActivityTemplatesBinding b;
51 private NavController navController;
52 private ActivityResultLauncher<Void> qrScanLauncher;
54 // public boolean onCreateOptionsMenu(Menu menu) {
55 // super.onCreateOptionsMenu(menu);
56 // getMenuInflater().inflate(R.menu.template_list_menu, menu);
61 protected void onCreate(Bundle savedInstanceState) {
62 super.onCreate(savedInstanceState);
63 b = ActivityTemplatesBinding.inflate(getLayoutInflater());
64 setContentView(b.getRoot());
65 setSupportActionBar(b.toolbar);
66 // Show the Up button in the action bar.
67 ActionBar actionBar = getSupportActionBar();
68 if (actionBar != null) {
69 actionBar.setDisplayHomeAsUpEnabled(true);
72 NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull(
73 getSupportFragmentManager().findFragmentById(R.id.fragment_container));
74 navController = navHostFragment.getNavController();
76 navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
77 if (destination.getId() == R.id.templateListFragment) {
80 b.toolbarLayout.setTitle(getString(R.string.title_activity_templates));
82 if (destination.getId() == R.id.templateDetailsFragment) {
88 b.toolbarLayout.setTitle(getString(R.string.title_activity_templates));
90 b.fabAdd.setOnClickListener(v -> onEditTemplate(null));
91 b.fabSave.setOnClickListener(v -> onSaveTemplate());
93 qrScanLauncher = QR.registerLauncher(this, this);
96 public boolean onOptionsItemSelected(MenuItem item) {
97 if (item.getItemId() == android.R.id.home) {
98 final NavDestination currentDestination = navController.getCurrentDestination();
99 if (currentDestination != null &&
100 currentDestination.getId() == R.id.templateDetailsFragment)
101 navController.popBackStack();
107 return super.onOptionsItemSelected(item);
110 public void onDuplicateTemplate(long id) {
113 .duplicateTemplateWitAccounts(id, null);
116 public void onEditTemplate(Long id) {
118 navController.navigate(R.id.action_templateListFragment_to_templateDetailsFragment);
119 b.toolbarLayout.setTitle(getString(R.string.title_new_template));
122 Bundle bundle = new Bundle();
123 bundle.putLong(TemplateDetailsFragment.ARG_TEMPLATE_ID, id);
124 navController.navigate(R.id.action_templateListFragment_to_templateDetailsFragment,
126 b.toolbarLayout.setTitle(getString(R.string.title_edit_template));
130 public void onSaveTemplate() {
131 final ViewModelStoreOwner viewModelStoreOwner =
132 navController.getViewModelStoreOwner(R.id.template_list_navigation);
133 TemplateDetailsViewModel model =
134 new ViewModelProvider(viewModelStoreOwner).get(TemplateDetailsViewModel.class);
135 Logger.debug("flow", "TemplatesActivity.onSavePattern(): model=" + model);
136 model.onSaveTemplate();
137 navController.navigateUp();
139 public NavController getNavController() {
140 return navController;
143 public void onDeleteTemplate(@NonNull Long templateId) {
144 Objects.requireNonNull(templateId);
145 TemplateHeaderDAO dao = DB.get()
148 dao.getTemplateWithAccountsAsync(templateId, template -> {
149 TemplateWithAccounts copy = TemplateWithAccounts.from(template);
150 dao.deleteAsync(template.header, () -> {
151 navController.popBackStack(R.id.templateListFragment, false);
153 Snackbar.make(b.getRoot(), String.format(
154 TemplatesActivity.this.getString(R.string.template_xxx_deleted),
155 template.header.getName()), BaseTransientBottomBar.LENGTH_LONG)
156 .setAction(R.string.action_undo, v -> dao.insertAsync(copy, null))
162 public void onQRScanResult(String scanned) {
163 Logger.debug("PatDet_fr", String.format("Got scanned text '%s'", scanned));
164 TemplateDetailsViewModel model = new ViewModelProvider(
165 navController.getViewModelStoreOwner(R.id.template_list_navigation)).get(
166 TemplateDetailsViewModel.class);
167 model.setTestText(scanned);
170 public void triggerQRScan() {
171 qrScanLauncher.launch(null);