]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplatesActivity.java
constant TAG
[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.content.Context;
21 import android.os.Bundle;
22 import android.view.MenuItem;
23
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;
32
33 import com.google.android.material.snackbar.BaseTransientBottomBar;
34 import com.google.android.material.snackbar.Snackbar;
35
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;
45
46 import java.util.Objects;
47
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;
57     //    @Override
58 //    public boolean onCreateOptionsMenu(Menu menu) {
59 //        super.onCreateOptionsMenu(menu);
60 //        getMenuInflater().inflate(R.menu.template_list_menu, menu);
61 //
62 //        return true;
63 //    }
64     @Override
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);
74         }
75
76         NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull(
77                 getSupportFragmentManager().findFragmentById(R.id.fragment_container));
78         navController = navHostFragment.getNavController();
79
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);
84             }
85             else {
86                 b.fab.setImageResource(R.drawable.ic_save_white_24dp);
87             }
88         });
89
90         b.toolbar.setTitle(getString(R.string.title_activity_templates));
91
92         b.fab.setOnClickListener(v -> {
93             if (navController.getCurrentDestination()
94                              .getId() == R.id.templateListFragment)
95                 onEditTemplate(null);
96             else
97                 onSaveTemplate();
98         });
99
100         qrScanLauncher = QR.registerLauncher(this, this);
101
102         fabManager = new FabManager(b.fab);
103     }
104     @Override
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();
111             else
112                 finish();
113
114             return true;
115         }
116         return super.onOptionsItemSelected(item);
117     }
118     @Override
119     public void onDuplicateTemplate(long id) {
120         DB.get()
121           .getTemplateDAO()
122           .duplicateTemplateWitAccounts(id, null);
123     }
124     @Override
125     public void onEditTemplate(Long id) {
126         if (id == null) {
127             navController.navigate(R.id.action_templateListFragment_to_templateDetailsFragment);
128             b.toolbar.setTitle(getString(R.string.title_new_template));
129         }
130         else {
131             Bundle bundle = new Bundle();
132             bundle.putLong(TemplateDetailsFragment.ARG_TEMPLATE_ID, id);
133             navController.navigate(R.id.action_templateListFragment_to_templateDetailsFragment,
134                     bundle);
135             b.toolbar.setTitle(getString(R.string.title_edit_template));
136         }
137     }
138     @Override
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();
147     }
148     public NavController getNavController() {
149         return navController;
150     }
151     @Override
152     public void onDeleteTemplate(@NonNull Long templateId) {
153         Objects.requireNonNull(templateId);
154         TemplateHeaderDAO dao = DB.get()
155                                   .getTemplateDAO();
156
157         dao.getTemplateWithAccountsAsync(templateId, template -> {
158             TemplateWithAccounts copy = TemplateWithAccounts.from(template);
159             dao.deleteAsync(template.header, () -> {
160                 navController.popBackStack(R.id.templateListFragment, false);
161
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))
166                         .show();
167             });
168         });
169     }
170     @Override
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);
177     }
178     @Override
179     public void triggerQRScan() {
180         qrScanLauncher.launch(null);
181     }
182     @Override
183     public Context getContext() {
184         return this;
185     }
186     @Override
187     public void showManagedFab() {
188         fabManager.showFab();
189     }
190     @Override
191     public void hideManagedFab() {
192         fabManager.hideFab();
193     }
194 }