]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternsActivity.java
c787769481cd6078759e899484f4d83236986890
[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.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;
31
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;
36
37 import java.util.Objects;
38
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;
44     @Override
45     public boolean onCreateOptionsMenu(Menu menu) {
46         super.onCreateOptionsMenu(menu);
47         getMenuInflater().inflate(R.menu.pattern_list_menu, menu);
48
49         return true;
50     }
51     @Override
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);
61         }
62
63         NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull(
64                 getSupportFragmentManager().findFragmentById(R.id.fragment_container));
65         navController = navHostFragment.getNavController();
66
67         navController.addOnDestinationChangedListener(
68                 new NavController.OnDestinationChangedListener() {
69                     @Override
70                     public void onDestinationChanged(@NonNull NavController controller,
71                                                      @NonNull NavDestination destination,
72                                                      @Nullable Bundle arguments) {
73                         if (destination.getId() == R.id.patternListFragment) {
74                             b.fabAdd.show();
75                             b.fabSave.hide();
76                         }
77                         if (destination.getId() == R.id.patternDetailsFragment) {
78                             b.fabAdd.hide();
79                             b.fabSave.show();
80                         }
81                     }
82                 });
83
84         b.toolbarLayout.setTitle(getString(R.string.title_activity_patterns));
85
86         b.fabAdd.setOnClickListener(v -> onEditPattern(null));
87         b.fabSave.setOnClickListener(v -> onSavePattern());
88     }
89     @Override
90     public void onEditPattern(Long id) {
91         if (id == null){
92             navController.navigate(R.id.action_patternListFragment_to_patternDetailsFragment);
93         }
94         else{
95             Bundle bundle = new Bundle();
96             bundle.putLong(PatternDetailsFragment.ARG_PATTERN_ID, id);
97             navController.navigate(R.id.action_patternListFragment_to_patternDetailsFragment, bundle);
98         }
99     }
100     @Override
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();
109     }
110     public NavController getNavController() {
111         return navController;
112     }
113 }