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 private ActivityPatternsBinding b;
42 private NavController navController;
44 public boolean onCreateOptionsMenu(Menu menu) {
45 super.onCreateOptionsMenu(menu);
46 getMenuInflater().inflate(R.menu.pattern_list_menu, menu);
51 protected void onCreate(Bundle savedInstanceState) {
52 super.onCreate(savedInstanceState);
53 b = ActivityPatternsBinding.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(
67 new NavController.OnDestinationChangedListener() {
69 public void onDestinationChanged(@NonNull NavController controller,
70 @NonNull NavDestination destination,
71 @Nullable Bundle arguments) {
72 if (destination.getId() == R.id.patternListFragment) {
76 if (destination.getId() == R.id.patternDetailsFragment) {
83 b.toolbarLayout.setTitle(getString(R.string.title_activity_patterns));
85 b.fabAdd.setOnClickListener(v -> onNewPattern());
86 b.fabSave.setOnClickListener(v -> onSavePattern());
89 public void onNewPattern() {
90 navController.navigate(R.id.action_patternListFragment_to_patternDetailsFragment);
91 // final Snackbar snackbar =
92 // Snackbar.make(b.fragmentContainer, "New pattern action coming up soon",
93 // Snackbar.LENGTH_INDEFINITE);
94 // snackbar.setAction("Action", v -> snackbar.dismiss());
98 public void onEditPattern(int id) {
99 Bundle bundle = new Bundle();
100 bundle.putInt(PatternDetailsFragment.ARG_PATTERN_ID, id);
101 navController.navigate(R.id.action_patternListFragment_to_patternDetailsFragment, bundle);
104 public void onSavePattern() {
105 final ViewModelStoreOwner viewModelStoreOwner =
106 navController.getViewModelStoreOwner(R.id.pattern_list_navigation);
107 PatternDetailsViewModel model =
108 new ViewModelProvider(viewModelStoreOwner).get(PatternDetailsViewModel.class);
109 Logger.debug("flow", "PatternsActivity.onSavePattern(): model=" + model);
110 model.onSavePattern();
111 navController.navigate(R.id.patternListFragment);
113 public NavController getNavController() {
114 return navController;