]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplatesActivity.java
410b7d62878ac4027ce418f55280a5175674ea23
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / templates / TemplatesActivity.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.templates;
19
20 import android.os.Bundle;
21 import android.view.MenuItem;
22
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;
31
32 import com.google.android.material.snackbar.BaseTransientBottomBar;
33 import com.google.android.material.snackbar.Snackbar;
34
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;
43
44 import java.util.Objects;
45
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;
53     //    @Override
54 //    public boolean onCreateOptionsMenu(Menu menu) {
55 //        super.onCreateOptionsMenu(menu);
56 //        getMenuInflater().inflate(R.menu.template_list_menu, menu);
57 //
58 //        return true;
59 //    }
60     @Override
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);
70         }
71
72         NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull(
73                 getSupportFragmentManager().findFragmentById(R.id.fragment_container));
74         navController = navHostFragment.getNavController();
75
76         navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
77             if (destination.getId() == R.id.templateListFragment) {
78                 b.fabAdd.show();
79                 b.fabSave.hide();
80                 b.toolbarLayout.setTitle(getString(R.string.title_activity_templates));
81             }
82             if (destination.getId() == R.id.templateDetailsFragment) {
83                 b.fabAdd.hide();
84                 b.fabSave.show();
85             }
86         });
87
88         b.toolbarLayout.setTitle(getString(R.string.title_activity_templates));
89
90         b.fabAdd.setOnClickListener(v -> onEditTemplate(null));
91         b.fabSave.setOnClickListener(v -> onSaveTemplate());
92
93         qrScanLauncher = QR.registerLauncher(this, this);
94     }
95     @Override
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();
102             else
103                 finish();
104
105             return true;
106         }
107         return super.onOptionsItemSelected(item);
108     }
109     @Override
110     public void onDuplicateTemplate(long id) {
111         DB.get()
112           .getTemplateDAO()
113           .duplicateTemplateWitAccounts(id, null);
114     }
115     @Override
116     public void onEditTemplate(Long id) {
117         if (id == null) {
118             navController.navigate(R.id.action_templateListFragment_to_templateDetailsFragment);
119             b.toolbarLayout.setTitle(getString(R.string.title_new_template));
120         }
121         else {
122             Bundle bundle = new Bundle();
123             bundle.putLong(TemplateDetailsFragment.ARG_TEMPLATE_ID, id);
124             navController.navigate(R.id.action_templateListFragment_to_templateDetailsFragment,
125                     bundle);
126             b.toolbarLayout.setTitle(getString(R.string.title_edit_template));
127         }
128     }
129     @Override
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();
138     }
139     public NavController getNavController() {
140         return navController;
141     }
142     @Override
143     public void onDeleteTemplate(@NonNull Long templateId) {
144         Objects.requireNonNull(templateId);
145         TemplateHeaderDAO dao = DB.get()
146                                   .getTemplateDAO();
147
148         dao.getTemplateWithAccountsAsync(templateId, template -> {
149             TemplateWithAccounts copy = TemplateWithAccounts.from(template);
150             dao.deleteAsync(template.header, () -> {
151                 navController.popBackStack(R.id.templateListFragment, false);
152
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))
157                         .show();
158             });
159         });
160     }
161     @Override
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);
168     }
169     @Override
170     public void triggerQRScan() {
171         qrScanLauncher.launch(null);
172     }
173 }