]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternDetailsFragment.java
rework pattern management machinery
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / patterns / PatternDetailsFragment.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.content.Context;
21 import android.os.Bundle;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 import androidx.lifecycle.ViewModelProvider;
29 import androidx.lifecycle.ViewModelStoreOwner;
30 import androidx.navigation.NavController;
31 import androidx.recyclerview.widget.GridLayoutManager;
32 import androidx.recyclerview.widget.LinearLayoutManager;
33
34 import com.google.android.material.snackbar.Snackbar;
35
36 import net.ktnx.mobileledger.R;
37 import net.ktnx.mobileledger.databinding.PatternDetailsFragmentBinding;
38 import net.ktnx.mobileledger.ui.QRScanAbleFragment;
39 import net.ktnx.mobileledger.utils.Logger;
40
41 public class PatternDetailsFragment extends QRScanAbleFragment {
42     static final String ARG_PATTERN_ID = "pattern-id";
43     private static final String ARG_COLUMN_COUNT = "column-count";
44     PatternDetailsFragmentBinding b;
45     private PatternDetailsViewModel mViewModel;
46     private int mColumnCount = 1;
47     private Long mPatternId;
48     public PatternDetailsFragment() {
49     }
50     public static PatternDetailsFragment newInstance(int columnCount, int patternId) {
51         final PatternDetailsFragment fragment = new PatternDetailsFragment();
52         Bundle args = new Bundle();
53         args.putInt(ARG_COLUMN_COUNT, columnCount);
54         if (patternId > 0)
55             args.putInt(ARG_PATTERN_ID, patternId);
56         fragment.setArguments(args);
57         return fragment;
58     }
59     @Override
60     public void onCreate(@Nullable Bundle savedInstanceState) {
61         super.onCreate(savedInstanceState);
62
63         final Bundle args = getArguments();
64         if (args != null) {
65             mColumnCount = args.getInt(ARG_COLUMN_COUNT, 1);
66             mPatternId = args.getLong(ARG_PATTERN_ID, -1);
67             if (mPatternId == -1)
68                 mPatternId = null;
69         }
70     }
71     @Override
72     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
73                              @Nullable Bundle savedInstanceState) {
74         b = PatternDetailsFragmentBinding.inflate(inflater);
75         Context context = b.patternDetailsRecyclerView.getContext();
76         if (mColumnCount <= 1) {
77             b.patternDetailsRecyclerView.setLayoutManager(new LinearLayoutManager(context));
78         }
79         else {
80             b.patternDetailsRecyclerView.setLayoutManager(
81                     new GridLayoutManager(context, mColumnCount));
82         }
83
84
85         PatternDetailsAdapter adapter = new PatternDetailsAdapter();
86         b.patternDetailsRecyclerView.setAdapter(adapter);
87         mViewModel.getItems(mPatternId)
88                   .observe(getViewLifecycleOwner(), adapter::setItems);
89         return b.getRoot();
90     }
91     @Override
92     public void onAttach(@NonNull Context context) {
93         super.onAttach(context);
94         NavController controller = ((PatternsActivity) context).getNavController();
95         final ViewModelStoreOwner viewModelStoreOwner =
96                 controller.getViewModelStoreOwner(R.id.pattern_list_navigation);
97         mViewModel = new ViewModelProvider(viewModelStoreOwner).get(PatternDetailsViewModel.class);
98         mViewModel.setDefaultPatternName(getString(R.string.unnamed_pattern));
99         Logger.debug("flow", "PatternDetailsFragment.onAttach(): model=" + mViewModel);
100
101     }
102     @Override
103     protected void onQrScanned(String text) {
104         Logger.debug("PatDet_fr", String.format("Got scanned text '%s'", text));
105         if (text != null)
106             mViewModel.setTestText(text);
107     }
108     public void onSavePattern() {
109         mViewModel.onSavePattern();
110         final Snackbar snackbar = Snackbar.make(b.getRoot(),
111                 "One Save pattern action coming up soon in a fragment near you",
112                 Snackbar.LENGTH_INDEFINITE);
113 //        snackbar.setAction("Action", v -> snackbar.dismiss());
114         snackbar.show();
115     }
116 }