]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsFragment.java
Room-based profile management
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / templates / TemplateDetailsFragment.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.templates;
19
20 import android.content.Context;
21 import android.os.Bundle;
22 import android.view.LayoutInflater;
23 import android.view.Menu;
24 import android.view.MenuInflater;
25 import android.view.MenuItem;
26 import android.view.View;
27 import android.view.ViewGroup;
28
29 import androidx.annotation.NonNull;
30 import androidx.annotation.Nullable;
31 import androidx.fragment.app.Fragment;
32 import androidx.fragment.app.FragmentActivity;
33 import androidx.lifecycle.ViewModelProvider;
34 import androidx.lifecycle.ViewModelStoreOwner;
35 import androidx.navigation.NavController;
36 import androidx.recyclerview.widget.GridLayoutManager;
37 import androidx.recyclerview.widget.LinearLayoutManager;
38
39 import net.ktnx.mobileledger.R;
40 import net.ktnx.mobileledger.databinding.TemplateDetailsFragmentBinding;
41 import net.ktnx.mobileledger.ui.FabManager;
42 import net.ktnx.mobileledger.utils.Logger;
43
44 public class TemplateDetailsFragment extends Fragment {
45     static final String ARG_TEMPLATE_ID = "pattern-id";
46     private static final String ARG_COLUMN_COUNT = "column-count";
47     private TemplateDetailsFragmentBinding b;
48     private TemplateDetailsViewModel mViewModel;
49     private int mColumnCount = 1;
50     private Long mPatternId;
51     private InteractionListener interactionListener;
52     public TemplateDetailsFragment() {
53     }
54     public static TemplateDetailsFragment newInstance(int columnCount, int patternId) {
55         final TemplateDetailsFragment fragment = new TemplateDetailsFragment();
56         Bundle args = new Bundle();
57         args.putInt(ARG_COLUMN_COUNT, columnCount);
58         if (patternId > 0)
59             args.putInt(ARG_TEMPLATE_ID, patternId);
60         fragment.setArguments(args);
61         return fragment;
62     }
63     @Override
64     public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
65         super.onCreateOptionsMenu(menu, inflater);
66         inflater.inflate(R.menu.template_details_menu, menu);
67     }
68     @Override
69     public boolean onOptionsItemSelected(@NonNull MenuItem item) {
70         if (item.getItemId() == R.id.delete_template) {
71             signalDeleteTemplateInteraction();
72             return true;
73         }
74
75         return super.onOptionsItemSelected(item);
76     }
77     private void signalDeleteTemplateInteraction() {
78         if (interactionListener != null)
79             interactionListener.onDeleteTemplate(mPatternId);
80     }
81     @Override
82     public void onCreate(@Nullable Bundle savedInstanceState) {
83         super.onCreate(savedInstanceState);
84
85         final Bundle args = getArguments();
86         if (args != null) {
87             mColumnCount = args.getInt(ARG_COLUMN_COUNT, 1);
88             mPatternId = args.getLong(ARG_TEMPLATE_ID, -1);
89             if (mPatternId == -1)
90                 mPatternId = null;
91         }
92
93         setHasOptionsMenu(mPatternId != null);
94     }
95     @Override
96     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
97                              @Nullable Bundle savedInstanceState) {
98         if (!(getActivity() instanceof InteractionListener))
99             throw new IllegalStateException(
100                     "Containing activity must implement TemplateDetailsFragment" +
101                     ".InteractionListener");
102         interactionListener = (InteractionListener) getActivity();
103
104         NavController controller = ((TemplatesActivity) requireActivity()).getNavController();
105         final ViewModelStoreOwner viewModelStoreOwner =
106                 controller.getViewModelStoreOwner(R.id.template_list_navigation);
107         mViewModel = new ViewModelProvider(viewModelStoreOwner).get(TemplateDetailsViewModel.class);
108         mViewModel.setDefaultTemplateName(getString(R.string.unnamed_template));
109         Logger.debug("flow", "PatternDetailsFragment.onCreateView(): model=" + mViewModel);
110
111         b = TemplateDetailsFragmentBinding.inflate(inflater);
112         Context context = b.patternDetailsRecyclerView.getContext();
113         if (mColumnCount <= 1) {
114             b.patternDetailsRecyclerView.setLayoutManager(new LinearLayoutManager(context));
115         }
116         else {
117             b.patternDetailsRecyclerView.setLayoutManager(
118                     new GridLayoutManager(context, mColumnCount));
119         }
120
121
122         TemplateDetailsAdapter adapter = new TemplateDetailsAdapter(mViewModel);
123         b.patternDetailsRecyclerView.setAdapter(adapter);
124         mViewModel.getItems(mPatternId)
125                   .observe(getViewLifecycleOwner(), adapter::setItems);
126
127         FragmentActivity activity = requireActivity();
128         if (activity instanceof FabManager.FabHandler)
129             FabManager.handle((FabManager.FabHandler) activity, b.patternDetailsRecyclerView);
130
131         return b.getRoot();
132     }
133     interface InteractionListener {
134         void onDeleteTemplate(@NonNull Long templateId);
135     }
136 }