]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternsActivity.java
change toolbar title when editing/adding a pattern
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / patterns / PatternsActivity.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.patterns;
19
20 import android.os.Bundle;
21 import android.view.Menu;
22
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;
28
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;
33
34 import java.util.Objects;
35
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;
41     @Override
42     public boolean onCreateOptionsMenu(Menu menu) {
43         super.onCreateOptionsMenu(menu);
44         getMenuInflater().inflate(R.menu.pattern_list_menu, menu);
45
46         return true;
47     }
48     @Override
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);
58         }
59
60         NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull(
61                 getSupportFragmentManager().findFragmentById(R.id.fragment_container));
62         navController = navHostFragment.getNavController();
63
64         navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
65             if (destination.getId() == R.id.patternListFragment) {
66                 b.fabAdd.show();
67                 b.fabSave.hide();
68                 b.toolbarLayout.setTitle(getString(R.string.title_activity_patterns));
69             }
70             if (destination.getId() == R.id.patternDetailsFragment) {
71                 b.fabAdd.hide();
72                 b.fabSave.show();
73             }
74         });
75
76         b.toolbarLayout.setTitle(getString(R.string.title_activity_patterns));
77
78         b.fabAdd.setOnClickListener(v -> onEditPattern(null));
79         b.fabSave.setOnClickListener(v -> onSavePattern());
80     }
81     @Override
82     public void onEditPattern(Long id) {
83         if (id == null) {
84             navController.navigate(R.id.action_patternListFragment_to_patternDetailsFragment);
85             b.toolbarLayout.setTitle(getString(R.string.title_new_pattern));
86         }
87         else {
88             Bundle bundle = new Bundle();
89             bundle.putLong(PatternDetailsFragment.ARG_PATTERN_ID, id);
90             navController.navigate(R.id.action_patternListFragment_to_patternDetailsFragment,
91                     bundle);
92             b.toolbarLayout.setTitle(getString(R.string.title_edit_pattern));
93         }
94     }
95     @Override
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();
104     }
105     public NavController getNavController() {
106         return navController;
107     }
108 }