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.
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.
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/>.
18 package net.ktnx.mobileledger.ui.templates;
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;
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;
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;
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() {
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);
59 args.putInt(ARG_TEMPLATE_ID, patternId);
60 fragment.setArguments(args);
64 public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
65 super.onCreateOptionsMenu(menu, inflater);
66 inflater.inflate(R.menu.template_details_menu, menu);
69 public boolean onOptionsItemSelected(@NonNull MenuItem item) {
70 if (item.getItemId() == R.id.delete_template) {
71 signalDeleteTemplateInteraction();
75 return super.onOptionsItemSelected(item);
77 private void signalDeleteTemplateInteraction() {
78 if (interactionListener != null)
79 interactionListener.onDeleteTemplate(mPatternId);
82 public void onCreate(@Nullable Bundle savedInstanceState) {
83 super.onCreate(savedInstanceState);
85 final Bundle args = getArguments();
87 mColumnCount = args.getInt(ARG_COLUMN_COUNT, 1);
88 mPatternId = args.getLong(ARG_TEMPLATE_ID, -1);
93 setHasOptionsMenu(mPatternId != null);
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();
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);
111 b = TemplateDetailsFragmentBinding.inflate(inflater);
112 Context context = b.patternDetailsRecyclerView.getContext();
113 if (mColumnCount <= 1) {
114 b.patternDetailsRecyclerView.setLayoutManager(new LinearLayoutManager(context));
117 b.patternDetailsRecyclerView.setLayoutManager(
118 new GridLayoutManager(context, mColumnCount));
122 TemplateDetailsAdapter adapter = new TemplateDetailsAdapter(mViewModel);
123 b.patternDetailsRecyclerView.setAdapter(adapter);
124 mViewModel.getItems(mPatternId)
125 .observe(getViewLifecycleOwner(), adapter::setItems);
127 FragmentActivity activity = requireActivity();
128 if (activity instanceof FabManager.FabHandler)
129 FabManager.handle((FabManager.FabHandler) activity, b.patternDetailsRecyclerView);
133 interface InteractionListener {
134 void onDeleteTemplate(@NonNull Long templateId);