]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternsActivity.java
global function for formatting numbers (locale-aware)
[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     private ActivityPatternsBinding b;
42     private NavController navController;
43     @Override
44     public boolean onCreateOptionsMenu(Menu menu) {
45         super.onCreateOptionsMenu(menu);
46         getMenuInflater().inflate(R.menu.pattern_list_menu, menu);
47
48         return true;
49     }
50     @Override
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);
60         }
61
62         NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull(
63                 getSupportFragmentManager().findFragmentById(R.id.fragment_container));
64         navController = navHostFragment.getNavController();
65
66         navController.addOnDestinationChangedListener(
67                 new NavController.OnDestinationChangedListener() {
68                     @Override
69                     public void onDestinationChanged(@NonNull NavController controller,
70                                                      @NonNull NavDestination destination,
71                                                      @Nullable Bundle arguments) {
72                         if (destination.getId() == R.id.patternListFragment) {
73                             b.fabAdd.show();
74                             b.fabSave.hide();
75                         }
76                         if (destination.getId() == R.id.patternDetailsFragment) {
77                             b.fabAdd.hide();
78                             b.fabSave.show();
79                         }
80                     }
81                 });
82
83         b.toolbarLayout.setTitle(getString(R.string.title_activity_patterns));
84
85         b.fabAdd.setOnClickListener(v -> onNewPattern());
86         b.fabSave.setOnClickListener(v -> onSavePattern());
87     }
88     @Override
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());
95 //        snackbar.show();
96     }
97     @Override
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);
102     }
103     @Override
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);
112     }
113     public NavController getNavController() {
114         return navController;
115     }
116 }