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.content.Context;
21 import android.os.Bundle;
22 import android.view.MenuItem;
24 import androidx.activity.result.ActivityResultLauncher;
25 import androidx.annotation.NonNull;
26 import androidx.appcompat.app.ActionBar;
27 import androidx.lifecycle.ViewModelProvider;
28 import androidx.lifecycle.ViewModelStoreOwner;
29 import androidx.navigation.NavController;
30 import androidx.navigation.NavDestination;
31 import androidx.navigation.fragment.NavHostFragment;
33 import com.google.android.material.snackbar.BaseTransientBottomBar;
34 import com.google.android.material.snackbar.Snackbar;
36 import net.ktnx.mobileledger.R;
37 import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
38 import net.ktnx.mobileledger.databinding.ActivityTemplatesBinding;
39 import net.ktnx.mobileledger.db.DB;
40 import net.ktnx.mobileledger.db.TemplateWithAccounts;
41 import net.ktnx.mobileledger.ui.FabManager;
42 import net.ktnx.mobileledger.ui.QR;
43 import net.ktnx.mobileledger.ui.activity.CrashReportingActivity;
44 import net.ktnx.mobileledger.utils.Logger;
46 import java.util.Objects;
48 public class TemplatesActivity extends CrashReportingActivity
49 implements TemplateListFragment.OnTemplateListFragmentInteractionListener,
50 TemplateDetailsFragment.InteractionListener, QR.QRScanResultReceiver, QR.QRScanTrigger,
51 FabManager.FabHandler {
52 public static final String ARG_ADD_TEMPLATE = "add-template";
53 private ActivityTemplatesBinding b;
54 private NavController navController;
55 private ActivityResultLauncher<Void> qrScanLauncher;
56 private FabManager fabManager;
58 // public boolean onCreateOptionsMenu(Menu menu) {
59 // super.onCreateOptionsMenu(menu);
60 // getMenuInflater().inflate(R.menu.template_list_menu, menu);
65 protected void onCreate(Bundle savedInstanceState) {
66 super.onCreate(savedInstanceState);
67 b = ActivityTemplatesBinding.inflate(getLayoutInflater());
68 setContentView(b.getRoot());
69 setSupportActionBar(b.toolbar);
70 // Show the Up button in the action bar.
71 ActionBar actionBar = getSupportActionBar();
72 if (actionBar != null) {
73 actionBar.setDisplayHomeAsUpEnabled(true);
76 NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull(
77 getSupportFragmentManager().findFragmentById(R.id.fragment_container));
78 navController = navHostFragment.getNavController();
80 navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
81 if (destination.getId() == R.id.templateListFragment) {
82 b.toolbar.setTitle(getString(R.string.title_activity_templates));
83 b.fab.setImageResource(R.drawable.ic_add_white_24dp);
86 b.fab.setImageResource(R.drawable.ic_save_white_24dp);
90 b.toolbar.setTitle(getString(R.string.title_activity_templates));
92 b.fab.setOnClickListener(v -> {
93 if (navController.getCurrentDestination()
94 .getId() == R.id.templateListFragment)
100 qrScanLauncher = QR.registerLauncher(this, this);
102 fabManager = new FabManager(b.fab);
105 public boolean onOptionsItemSelected(MenuItem item) {
106 if (item.getItemId() == android.R.id.home) {
107 final NavDestination currentDestination = navController.getCurrentDestination();
108 if (currentDestination != null &&
109 currentDestination.getId() == R.id.templateDetailsFragment)
110 navController.popBackStack();
116 return super.onOptionsItemSelected(item);
119 public void onDuplicateTemplate(long id) {
122 .duplicateTemplateWitAccounts(id, null);
125 public void onEditTemplate(Long id) {
127 navController.navigate(R.id.action_templateListFragment_to_templateDetailsFragment);
128 b.toolbar.setTitle(getString(R.string.title_new_template));
131 Bundle bundle = new Bundle();
132 bundle.putLong(TemplateDetailsFragment.ARG_TEMPLATE_ID, id);
133 navController.navigate(R.id.action_templateListFragment_to_templateDetailsFragment,
135 b.toolbar.setTitle(getString(R.string.title_edit_template));
139 public void onSaveTemplate() {
140 final ViewModelStoreOwner viewModelStoreOwner =
141 navController.getViewModelStoreOwner(R.id.template_list_navigation);
142 TemplateDetailsViewModel model =
143 new ViewModelProvider(viewModelStoreOwner).get(TemplateDetailsViewModel.class);
144 Logger.debug("flow", "TemplatesActivity.onSavePattern(): model=" + model);
145 model.onSaveTemplate();
146 navController.navigateUp();
148 public NavController getNavController() {
149 return navController;
152 public void onDeleteTemplate(@NonNull Long templateId) {
153 Objects.requireNonNull(templateId);
154 TemplateHeaderDAO dao = DB.get()
157 dao.getTemplateWithAccountsAsync(templateId, template -> {
158 TemplateWithAccounts copy = TemplateWithAccounts.from(template);
159 dao.deleteAsync(template.header, () -> {
160 navController.popBackStack(R.id.templateListFragment, false);
162 Snackbar.make(b.getRoot(), String.format(
163 TemplatesActivity.this.getString(R.string.template_xxx_deleted),
164 template.header.getName()), BaseTransientBottomBar.LENGTH_LONG)
165 .setAction(R.string.action_undo, v -> dao.insertAsync(copy, null))
171 public void onQRScanResult(String scanned) {
172 Logger.debug("PatDet_fr", String.format("Got scanned text '%s'", scanned));
173 TemplateDetailsViewModel model = new ViewModelProvider(
174 navController.getViewModelStoreOwner(R.id.template_list_navigation)).get(
175 TemplateDetailsViewModel.class);
176 model.setTestText(scanned);
179 public void triggerQRScan() {
180 qrScanLauncher.launch(null);
183 public Context getContext() {
187 public void showManagedFab() {
188 fabManager.showFab();
191 public void hideManagedFab() {
192 fabManager.hideFab();