]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplatesActivity.java
include account position in template account head
[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.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;
30
31 import com.google.android.material.snackbar.BaseTransientBottomBar;
32 import com.google.android.material.snackbar.Snackbar;
33
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;
41
42 import java.util.Objects;
43
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;
50     //    @Override
51 //    public boolean onCreateOptionsMenu(Menu menu) {
52 //        super.onCreateOptionsMenu(menu);
53 //        getMenuInflater().inflate(R.menu.template_list_menu, menu);
54 //
55 //        return true;
56 //    }
57     @Override
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);
67         }
68
69         NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull(
70                 getSupportFragmentManager().findFragmentById(R.id.fragment_container));
71         navController = navHostFragment.getNavController();
72
73         navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
74             if (destination.getId() == R.id.templateListFragment) {
75                 b.fabAdd.show();
76                 b.fabSave.hide();
77                 b.toolbarLayout.setTitle(getString(R.string.title_activity_templates));
78             }
79             if (destination.getId() == R.id.templateDetailsFragment) {
80                 b.fabAdd.hide();
81                 b.fabSave.show();
82             }
83         });
84
85         b.toolbarLayout.setTitle(getString(R.string.title_activity_templates));
86
87         b.fabAdd.setOnClickListener(v -> onEditTemplate(null));
88         b.fabSave.setOnClickListener(v -> onSaveTemplate());
89     }
90     @Override
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();
97             else
98                 finish();
99
100             return true;
101         }
102         return super.onOptionsItemSelected(item);
103     }
104     @Override
105     public void onDuplicateTemplate(long id) {
106         DB.get()
107           .getTemplateDAO()
108           .duplicateTemplateWitAccounts(id, null);
109     }
110     @Override
111     public void onEditTemplate(Long id) {
112         if (id == null) {
113             navController.navigate(R.id.action_templateListFragment_to_templateDetailsFragment);
114             b.toolbarLayout.setTitle(getString(R.string.title_new_template));
115         }
116         else {
117             Bundle bundle = new Bundle();
118             bundle.putLong(TemplateDetailsFragment.ARG_TEMPLATE_ID, id);
119             navController.navigate(R.id.action_templateListFragment_to_templateDetailsFragment,
120                     bundle);
121             b.toolbarLayout.setTitle(getString(R.string.title_edit_template));
122         }
123     }
124     @Override
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();
133     }
134     public NavController getNavController() {
135         return navController;
136     }
137     @Override
138     public void onDeleteTemplate(@NonNull Long templateId) {
139         Objects.requireNonNull(templateId);
140         TemplateHeaderDAO dao = DB.get()
141                                   .getTemplateDAO();
142
143         dao.getTemplateWitAccountsAsync(templateId, template -> {
144             TemplateWithAccounts copy = TemplateWithAccounts.from(template);
145             dao.deleteAsync(template.header, () -> {
146                 navController.popBackStack(R.id.templateListFragment, false);
147
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))
152                         .show();
153             });
154         });
155     }
156 }