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.lifecycle.ViewModelProvider;
32 import androidx.lifecycle.ViewModelStoreOwner;
33 import androidx.navigation.NavController;
34 import androidx.recyclerview.widget.GridLayoutManager;
35 import androidx.recyclerview.widget.LinearLayoutManager;
37 import net.ktnx.mobileledger.R;
38 import net.ktnx.mobileledger.databinding.TemplateDetailsFragmentBinding;
39 import net.ktnx.mobileledger.ui.QRScanCapableFragment;
40 import net.ktnx.mobileledger.utils.Logger;
42 public class TemplateDetailsFragment extends QRScanCapableFragment {
43 static final String ARG_TEMPLATE_ID = "pattern-id";
44 private static final String ARG_COLUMN_COUNT = "column-count";
45 private TemplateDetailsFragmentBinding b;
46 private TemplateDetailsViewModel mViewModel;
47 private int mColumnCount = 1;
48 private Long mPatternId;
49 private InteractionListener interactionListener;
50 public TemplateDetailsFragment() {
52 public static TemplateDetailsFragment newInstance(int columnCount, int patternId) {
53 final TemplateDetailsFragment fragment = new TemplateDetailsFragment();
54 Bundle args = new Bundle();
55 args.putInt(ARG_COLUMN_COUNT, columnCount);
57 args.putInt(ARG_TEMPLATE_ID, patternId);
58 fragment.setArguments(args);
62 public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
63 super.onCreateOptionsMenu(menu, inflater);
64 inflater.inflate(R.menu.template_details_menu, menu);
67 public boolean onOptionsItemSelected(@NonNull MenuItem item) {
68 if (item.getItemId() == R.id.delete_template) {
69 signalDeleteTemplateInteraction();
73 return super.onOptionsItemSelected(item);
75 private void signalDeleteTemplateInteraction() {
76 if (interactionListener != null)
77 interactionListener.onDeleteTemplate(mPatternId);
80 public void onCreate(@Nullable Bundle savedInstanceState) {
81 super.onCreate(savedInstanceState);
83 final Bundle args = getArguments();
85 mColumnCount = args.getInt(ARG_COLUMN_COUNT, 1);
86 mPatternId = args.getLong(ARG_TEMPLATE_ID, -1);
91 setHasOptionsMenu(mPatternId != null);
94 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
95 @Nullable Bundle savedInstanceState) {
96 if (!(getActivity() instanceof InteractionListener))
97 throw new IllegalStateException(
98 "Containing activity must implement TemplateDetailsFragment" +
99 ".InteractionListener");
100 interactionListener = (InteractionListener) getActivity();
102 NavController controller = ((TemplatesActivity) requireActivity()).getNavController();
103 final ViewModelStoreOwner viewModelStoreOwner =
104 controller.getViewModelStoreOwner(R.id.template_list_navigation);
105 mViewModel = new ViewModelProvider(viewModelStoreOwner).get(TemplateDetailsViewModel.class);
106 mViewModel.setDefaultPatternName(getString(R.string.unnamed_template));
107 Logger.debug("flow", "PatternDetailsFragment.onCreateView(): model=" + mViewModel);
109 b = TemplateDetailsFragmentBinding.inflate(inflater);
110 Context context = b.patternDetailsRecyclerView.getContext();
111 if (mColumnCount <= 1) {
112 b.patternDetailsRecyclerView.setLayoutManager(new LinearLayoutManager(context));
115 b.patternDetailsRecyclerView.setLayoutManager(
116 new GridLayoutManager(context, mColumnCount));
120 TemplateDetailsAdapter adapter = new TemplateDetailsAdapter(mViewModel);
121 b.patternDetailsRecyclerView.setAdapter(adapter);
122 mViewModel.getItems(mPatternId)
123 .observe(getViewLifecycleOwner(), adapter::setItems);
128 protected void onQrScanned(String text) {
129 Logger.debug("PatDet_fr", String.format("Got scanned text '%s'", text));
131 mViewModel.setTestText(text);
133 interface InteractionListener {
134 void onDeleteTemplate(@NonNull Long templateId);