]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplatesActivity.java
7bcece63d7e17f3e0aa67937e4cc6a23f41b85b5
[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.Menu;
22 import android.view.MenuItem;
23
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 net.ktnx.mobileledger.R;
32 import net.ktnx.mobileledger.databinding.ActivityTemplatesBinding;
33 import net.ktnx.mobileledger.ui.activity.CrashReportingActivity;
34 import net.ktnx.mobileledger.utils.Logger;
35
36 import java.util.Objects;
37
38 public class TemplatesActivity extends CrashReportingActivity
39         implements TemplateListFragment.OnTemplateListFragmentInteractionListener {
40     public static final String ARG_ADD_TEMPLATE = "add-template";
41     private ActivityTemplatesBinding b;
42     private NavController navController;
43     @Override
44     public boolean onCreateOptionsMenu(Menu menu) {
45         super.onCreateOptionsMenu(menu);
46         getMenuInflater().inflate(R.menu.template_list_menu, menu);
47
48         return true;
49     }
50     @Override
51     protected void onCreate(Bundle savedInstanceState) {
52         super.onCreate(savedInstanceState);
53         b = ActivityTemplatesBinding.inflate(getLayoutInflater());
54         setContentView(b.getRoot());
55         setSupportActionBar(b.toolbar);
56         // Show the Up button in the action bar.
57         ActionBar actionBar = getSupportActionBar();
58         if (actionBar != null) {
59             actionBar.setDisplayHomeAsUpEnabled(true);
60         }
61
62         NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull(
63                 getSupportFragmentManager().findFragmentById(R.id.fragment_container));
64         navController = navHostFragment.getNavController();
65
66         navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
67             if (destination.getId() == R.id.templateListFragment) {
68                 b.fabAdd.show();
69                 b.fabSave.hide();
70                 b.toolbarLayout.setTitle(getString(R.string.title_activity_templates));
71             }
72             if (destination.getId() == R.id.templateDetailsFragment) {
73                 b.fabAdd.hide();
74                 b.fabSave.show();
75             }
76         });
77
78         b.toolbarLayout.setTitle(getString(R.string.title_activity_templates));
79
80         b.fabAdd.setOnClickListener(v -> onEditTemplate(null));
81         b.fabSave.setOnClickListener(v -> onSaveTemplate());
82     }
83     @Override
84     public boolean onOptionsItemSelected(MenuItem item) {
85         if (item.getItemId() == android.R.id.home) {
86             final NavDestination currentDestination = navController.getCurrentDestination();
87             if (currentDestination != null &&
88                 currentDestination.getId() == R.id.templateDetailsFragment)
89                 navController.popBackStack();
90             else
91                 finish();
92
93             return true;
94         }
95         return super.onOptionsItemSelected(item);
96     }
97
98     @Override
99     public void onEditTemplate(Long id) {
100         if (id == null) {
101             navController.navigate(R.id.action_templateListFragment_to_templateDetailsFragment);
102             b.toolbarLayout.setTitle(getString(R.string.title_new_template));
103         }
104         else {
105             Bundle bundle = new Bundle();
106             bundle.putLong(TemplateDetailsFragment.ARG_TEMPLATE_ID, id);
107             navController.navigate(R.id.action_templateListFragment_to_templateDetailsFragment,
108                     bundle);
109             b.toolbarLayout.setTitle(getString(R.string.title_edit_template));
110         }
111     }
112     @Override
113     public void onSaveTemplate() {
114         final ViewModelStoreOwner viewModelStoreOwner =
115                 navController.getViewModelStoreOwner(R.id.template_list_navigation);
116         TemplateDetailsViewModel model =
117                 new ViewModelProvider(viewModelStoreOwner).get(TemplateDetailsViewModel.class);
118         Logger.debug("flow", "TemplatesActivity.onSavePattern(): model=" + model);
119         model.onSaveTemplate();
120         navController.navigateUp();
121     }
122     public NavController getNavController() {
123         return navController;
124     }
125 }