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;
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 import androidx.appcompat.app.ActionBar;
26 import androidx.lifecycle.ViewModelProvider;
27 import androidx.lifecycle.ViewModelStoreOwner;
28 import androidx.navigation.NavController;
29 import androidx.navigation.NavDestination;
30 import androidx.navigation.fragment.NavHostFragment;
32 import net.ktnx.mobileledger.R;
33 import net.ktnx.mobileledger.databinding.ActivityPatternsBinding;
34 import net.ktnx.mobileledger.ui.activity.CrashReportingActivity;
35 import net.ktnx.mobileledger.utils.Logger;
37 import java.util.Objects;
39 public class PatternsActivity extends CrashReportingActivity
40 implements PatternListFragment.OnPatternListFragmentInteractionListener {
41 public static final String ARG_ADD_PATTERN = "add-pattern";
42 private ActivityPatternsBinding b;
43 private NavController navController;
45 public boolean onCreateOptionsMenu(Menu menu) {
46 super.onCreateOptionsMenu(menu);
47 getMenuInflater().inflate(R.menu.pattern_list_menu, menu);
52 protected void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
54 b = ActivityPatternsBinding.inflate(getLayoutInflater());
55 setContentView(b.getRoot());
56 setSupportActionBar(b.toolbar);
57 // Show the Up button in the action bar.
58 ActionBar actionBar = getSupportActionBar();
59 if (actionBar != null) {
60 actionBar.setDisplayHomeAsUpEnabled(true);
63 NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull(
64 getSupportFragmentManager().findFragmentById(R.id.fragment_container));
65 navController = navHostFragment.getNavController();
67 navController.addOnDestinationChangedListener(
68 new NavController.OnDestinationChangedListener() {
70 public void onDestinationChanged(@NonNull NavController controller,
71 @NonNull NavDestination destination,
72 @Nullable Bundle arguments) {
73 if (destination.getId() == R.id.patternListFragment) {
77 if (destination.getId() == R.id.patternDetailsFragment) {
84 b.toolbarLayout.setTitle(getString(R.string.title_activity_patterns));
86 b.fabAdd.setOnClickListener(v -> onEditPattern(null));
87 b.fabSave.setOnClickListener(v -> onSavePattern());
90 public void onEditPattern(Long id) {
92 navController.navigate(R.id.action_patternListFragment_to_patternDetailsFragment);
95 Bundle bundle = new Bundle();
96 bundle.putLong(PatternDetailsFragment.ARG_PATTERN_ID, id);
97 navController.navigate(R.id.action_patternListFragment_to_patternDetailsFragment, bundle);
101 public void onSavePattern() {
102 final ViewModelStoreOwner viewModelStoreOwner =
103 navController.getViewModelStoreOwner(R.id.pattern_list_navigation);
104 PatternDetailsViewModel model =
105 new ViewModelProvider(viewModelStoreOwner).get(PatternDetailsViewModel.class);
106 Logger.debug("flow", "PatternsActivity.onSavePattern(): model=" + model);
107 model.onSavePattern();
108 navController.navigateUp();
110 public NavController getNavController() {
111 return navController;