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.appcompat.app.ActionBar;
24 import androidx.lifecycle.ViewModelProvider;
25 import androidx.lifecycle.ViewModelStoreOwner;
26 import androidx.navigation.NavController;
27 import androidx.navigation.fragment.NavHostFragment;
29 import net.ktnx.mobileledger.R;
30 import net.ktnx.mobileledger.databinding.ActivityPatternsBinding;
31 import net.ktnx.mobileledger.ui.activity.CrashReportingActivity;
32 import net.ktnx.mobileledger.utils.Logger;
34 import java.util.Objects;
36 public class PatternsActivity extends CrashReportingActivity
37 implements PatternListFragment.OnPatternListFragmentInteractionListener {
38 public static final String ARG_ADD_PATTERN = "add-pattern";
39 private ActivityPatternsBinding b;
40 private NavController navController;
42 public boolean onCreateOptionsMenu(Menu menu) {
43 super.onCreateOptionsMenu(menu);
44 getMenuInflater().inflate(R.menu.pattern_list_menu, menu);
49 protected void onCreate(Bundle savedInstanceState) {
50 super.onCreate(savedInstanceState);
51 b = ActivityPatternsBinding.inflate(getLayoutInflater());
52 setContentView(b.getRoot());
53 setSupportActionBar(b.toolbar);
54 // Show the Up button in the action bar.
55 ActionBar actionBar = getSupportActionBar();
56 if (actionBar != null) {
57 actionBar.setDisplayHomeAsUpEnabled(true);
60 NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull(
61 getSupportFragmentManager().findFragmentById(R.id.fragment_container));
62 navController = navHostFragment.getNavController();
64 navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
65 if (destination.getId() == R.id.patternListFragment) {
68 b.toolbarLayout.setTitle(getString(R.string.title_activity_patterns));
70 if (destination.getId() == R.id.patternDetailsFragment) {
76 b.toolbarLayout.setTitle(getString(R.string.title_activity_patterns));
78 b.fabAdd.setOnClickListener(v -> onEditPattern(null));
79 b.fabSave.setOnClickListener(v -> onSavePattern());
82 public void onEditPattern(Long id) {
84 navController.navigate(R.id.action_patternListFragment_to_patternDetailsFragment);
85 b.toolbarLayout.setTitle(getString(R.string.title_new_pattern));
88 Bundle bundle = new Bundle();
89 bundle.putLong(PatternDetailsFragment.ARG_PATTERN_ID, id);
90 navController.navigate(R.id.action_patternListFragment_to_patternDetailsFragment,
92 b.toolbarLayout.setTitle(getString(R.string.title_edit_pattern));
96 public void onSavePattern() {
97 final ViewModelStoreOwner viewModelStoreOwner =
98 navController.getViewModelStoreOwner(R.id.pattern_list_navigation);
99 PatternDetailsViewModel model =
100 new ViewModelProvider(viewModelStoreOwner).get(PatternDetailsViewModel.class);
101 Logger.debug("flow", "PatternsActivity.onSavePattern(): model=" + model);
102 model.onSavePattern();
103 navController.navigateUp();
105 public NavController getNavController() {
106 return navController;