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.fragment.app.Fragment;
31 import androidx.fragment.app.FragmentActivity;
32 import androidx.lifecycle.Lifecycle;
33 import androidx.lifecycle.LifecycleEventObserver;
34 import androidx.lifecycle.LifecycleOwner;
35 import androidx.lifecycle.LiveData;
36 import androidx.recyclerview.widget.LinearLayoutManager;
37 import androidx.recyclerview.widget.RecyclerView;
39 import net.ktnx.mobileledger.R;
40 import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
41 import net.ktnx.mobileledger.databinding.FragmentTemplateListBinding;
42 import net.ktnx.mobileledger.db.DB;
43 import net.ktnx.mobileledger.db.TemplateHeader;
44 import net.ktnx.mobileledger.ui.FabManager;
45 import net.ktnx.mobileledger.ui.HelpDialog;
46 import net.ktnx.mobileledger.utils.Logger;
48 import org.jetbrains.annotations.NotNull;
50 import java.util.List;
53 * A simple {@link Fragment} subclass.
54 * Use the {@link TemplateListFragment#newInstance} factory method to
55 * create an instance of this fragment.
57 public class TemplateListFragment extends Fragment {
58 private FragmentTemplateListBinding b;
59 private OnTemplateListFragmentInteractionListener mListener;
60 public TemplateListFragment() {
61 // Required empty public constructor
64 * Use this factory method to create a new instance of
65 * this fragment using the provided parameters.
67 * @return A new instance of fragment TemplateListFragment.
69 public static TemplateListFragment newInstance() {
70 TemplateListFragment fragment = new TemplateListFragment();
71 Bundle args = new Bundle();
72 fragment.setArguments(args);
76 public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
77 super.onCreateOptionsMenu(menu, inflater);
78 inflater.inflate(R.menu.template_list_menu, menu);
81 public boolean onOptionsItemSelected(@NonNull MenuItem item) {
82 if (item.getItemId() == R.id.menu_item_template_list_help) {
83 HelpDialog.show(requireContext(), R.string.template_list_help_title,
84 R.array.template_list_help_text);
87 return super.onOptionsItemSelected(item);
90 public void onCreate(Bundle savedInstanceState) {
91 super.onCreate(savedInstanceState);
92 setHasOptionsMenu(true);
96 public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container,
97 Bundle savedInstanceState) {
98 Logger.debug("flow", "PatternListFragment.onCreateView()");
99 b = FragmentTemplateListBinding.inflate(inflater);
101 TemplatesRecyclerViewAdapter modelAdapter = new TemplatesRecyclerViewAdapter();
103 b.templateList.setAdapter(modelAdapter);
104 TemplateHeaderDAO pDao = DB.get()
106 LiveData<List<TemplateHeader>> templates = pDao.getTemplates();
107 templates.observe(getViewLifecycleOwner(), modelAdapter::setTemplates);
108 LinearLayoutManager llm = new LinearLayoutManager(getContext());
109 llm.setOrientation(RecyclerView.VERTICAL);
110 b.templateList.setLayoutManager(llm);
112 FragmentActivity activity = requireActivity();
113 if (activity instanceof FabManager.FabHandler)
114 FabManager.handle((FabManager.FabHandler) activity, b.templateList);
119 public void onAttach(@NonNull Context context) {
120 super.onAttach(context);
121 if (context instanceof OnTemplateListFragmentInteractionListener) {
122 mListener = (OnTemplateListFragmentInteractionListener) context;
125 throw new RuntimeException(
126 context.toString() + " must implement OnFragmentInteractionListener");
129 final LifecycleEventObserver observer = new LifecycleEventObserver() {
131 public void onStateChanged(@NonNull LifecycleOwner source,
132 @NonNull Lifecycle.Event event) {
133 if (event.getTargetState() == Lifecycle.State.CREATED) {
134 // getActivity().setActionBar(b.toolbar);
135 getLifecycle().removeObserver(this);
139 getLifecycle().addObserver(observer);
142 * This interface must be implemented by activities that contain this
143 * fragment to allow an interaction in this fragment to be communicated
144 * to the activity and potentially other fragments contained in that
147 * See the Android Training lesson <a href=
148 * "http://developer.android.com/training/basics/fragments/communicating.html"
149 * >Communicating with Other Fragments</a> for more information.
151 public interface OnTemplateListFragmentInteractionListener {
152 void onSaveTemplate();
154 void onEditTemplate(Long id);
156 void onDuplicateTemplate(long id);