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.os.Bundle;
21 import android.view.Menu;
22 import android.view.MenuItem;
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;
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;
36 import java.util.Objects;
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;
44 public boolean onCreateOptionsMenu(Menu menu) {
45 super.onCreateOptionsMenu(menu);
46 getMenuInflater().inflate(R.menu.template_list_menu, menu);
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);
62 NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull(
63 getSupportFragmentManager().findFragmentById(R.id.fragment_container));
64 navController = navHostFragment.getNavController();
66 navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
67 if (destination.getId() == R.id.templateListFragment) {
70 b.toolbarLayout.setTitle(getString(R.string.title_activity_templates));
72 if (destination.getId() == R.id.templateDetailsFragment) {
78 b.toolbarLayout.setTitle(getString(R.string.title_activity_templates));
80 b.fabAdd.setOnClickListener(v -> onEditTemplate(null));
81 b.fabSave.setOnClickListener(v -> onSaveTemplate());
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();
95 return super.onOptionsItemSelected(item);
99 public void onEditTemplate(Long id) {
101 navController.navigate(R.id.action_templateListFragment_to_templateDetailsFragment);
102 b.toolbarLayout.setTitle(getString(R.string.title_new_template));
105 Bundle bundle = new Bundle();
106 bundle.putLong(TemplateDetailsFragment.ARG_TEMPLATE_ID, id);
107 navController.navigate(R.id.action_templateListFragment_to_templateDetailsFragment,
109 b.toolbarLayout.setTitle(getString(R.string.title_edit_template));
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();
122 public NavController getNavController() {
123 return navController;