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 // TODO: Rename and change types and number of parameters
70 public static TemplateListFragment newInstance() {
71 TemplateListFragment fragment = new TemplateListFragment();
72 Bundle args = new Bundle();
73 fragment.setArguments(args);
77 public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
78 super.onCreateOptionsMenu(menu, inflater);
79 inflater.inflate(R.menu.template_list_menu, menu);
82 public boolean onOptionsItemSelected(@NonNull MenuItem item) {
83 if (item.getItemId() == R.id.menu_item_template_list_help) {
84 HelpDialog.show(requireContext(), R.string.template_list_help_title,
85 R.array.template_list_help_text);
88 return super.onOptionsItemSelected(item);
91 public void onCreate(Bundle savedInstanceState) {
92 super.onCreate(savedInstanceState);
93 setHasOptionsMenu(true);
94 // if (getArguments() != null) {
95 // mParam1 = getArguments().getString(ARG_PARAM1);
96 // mParam2 = getArguments().getString(ARG_PARAM2);
101 public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container,
102 Bundle savedInstanceState) {
103 Logger.debug("flow", "PatternListFragment.onCreateView()");
104 b = FragmentTemplateListBinding.inflate(inflater);
106 TemplatesRecyclerViewAdapter modelAdapter = new TemplatesRecyclerViewAdapter();
108 b.templateList.setAdapter(modelAdapter);
109 TemplateHeaderDAO pDao = DB.get()
111 LiveData<List<TemplateHeader>> templates = pDao.getTemplates();
112 templates.observe(getViewLifecycleOwner(), modelAdapter::setTemplates);
113 LinearLayoutManager llm = new LinearLayoutManager(getContext());
114 llm.setOrientation(RecyclerView.VERTICAL);
115 b.templateList.setLayoutManager(llm);
117 FragmentActivity activity = requireActivity();
118 if (activity instanceof FabManager.FabHandler)
119 FabManager.handle((FabManager.FabHandler) activity, b.templateList);
124 public void onAttach(@NonNull Context context) {
125 super.onAttach(context);
126 if (context instanceof OnTemplateListFragmentInteractionListener) {
127 mListener = (OnTemplateListFragmentInteractionListener) context;
130 throw new RuntimeException(
131 context.toString() + " must implement OnFragmentInteractionListener");
134 final LifecycleEventObserver observer = new LifecycleEventObserver() {
136 public void onStateChanged(@NonNull LifecycleOwner source,
137 @NonNull Lifecycle.Event event) {
138 if (event.getTargetState() == Lifecycle.State.CREATED) {
139 // getActivity().setActionBar(b.toolbar);
140 getLifecycle().removeObserver(this);
144 getLifecycle().addObserver(observer);
147 * This interface must be implemented by activities that contain this
148 * fragment to allow an interaction in this fragment to be communicated
149 * to the activity and potentially other fragments contained in that
152 * See the Android Training lesson <a href=
153 * "http://developer.android.com/training/basics/fragments/communicating.html"
154 * >Communicating with Other Fragments</a> for more information.
156 public interface OnTemplateListFragmentInteractionListener {
157 void onSaveTemplate();
159 void onEditTemplate(Long id);
161 void onDuplicateTemplate(long id);