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.patterns;
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.fragment.NavHostFragment;
30 import net.ktnx.mobileledger.R;
31 import net.ktnx.mobileledger.databinding.ActivityPatternsBinding;
32 import net.ktnx.mobileledger.ui.activity.CrashReportingActivity;
33 import net.ktnx.mobileledger.utils.Logger;
35 import java.util.Objects;
37 public class PatternsActivity extends CrashReportingActivity
38 implements PatternListFragment.OnPatternListFragmentInteractionListener {
39 public static final String ARG_ADD_PATTERN = "add-pattern";
40 private ActivityPatternsBinding b;
41 private NavController navController;
43 public boolean onCreateOptionsMenu(Menu menu) {
44 super.onCreateOptionsMenu(menu);
45 getMenuInflater().inflate(R.menu.pattern_list_menu, menu);
50 protected void onCreate(Bundle savedInstanceState) {
51 super.onCreate(savedInstanceState);
52 b = ActivityPatternsBinding.inflate(getLayoutInflater());
53 setContentView(b.getRoot());
54 setSupportActionBar(b.toolbar);
55 // Show the Up button in the action bar.
56 ActionBar actionBar = getSupportActionBar();
57 if (actionBar != null) {
58 actionBar.setDisplayHomeAsUpEnabled(true);
61 NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull(
62 getSupportFragmentManager().findFragmentById(R.id.fragment_container));
63 navController = navHostFragment.getNavController();
65 navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
66 if (destination.getId() == R.id.patternListFragment) {
69 b.toolbarLayout.setTitle(getString(R.string.title_activity_patterns));
71 if (destination.getId() == R.id.patternDetailsFragment) {
77 b.toolbarLayout.setTitle(getString(R.string.title_activity_patterns));
79 b.fabAdd.setOnClickListener(v -> onEditPattern(null));
80 b.fabSave.setOnClickListener(v -> onSavePattern());
83 public boolean onOptionsItemSelected(MenuItem item) {
84 if (item.getItemId() == android.R.id.home) {
85 if (navController.getCurrentDestination()
86 .getId() == R.id.patternDetailsFragment)
87 navController.popBackStack();
93 return super.onOptionsItemSelected(item);
97 public void onEditPattern(Long id) {
99 navController.navigate(R.id.action_patternListFragment_to_patternDetailsFragment);
100 b.toolbarLayout.setTitle(getString(R.string.title_new_pattern));
103 Bundle bundle = new Bundle();
104 bundle.putLong(PatternDetailsFragment.ARG_PATTERN_ID, id);
105 navController.navigate(R.id.action_patternListFragment_to_patternDetailsFragment,
107 b.toolbarLayout.setTitle(getString(R.string.title_edit_pattern));
111 public void onSavePattern() {
112 final ViewModelStoreOwner viewModelStoreOwner =
113 navController.getViewModelStoreOwner(R.id.pattern_list_navigation);
114 PatternDetailsViewModel model =
115 new ViewModelProvider(viewModelStoreOwner).get(PatternDetailsViewModel.class);
116 Logger.debug("flow", "PatternsActivity.onSavePattern(): model=" + model);
117 model.onSavePattern();
118 navController.navigateUp();
120 public NavController getNavController() {
121 return navController;