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.DividerItemDecoration;
37 import androidx.recyclerview.widget.LinearLayoutManager;
38 import androidx.recyclerview.widget.RecyclerView;
40 import net.ktnx.mobileledger.R;
41 import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
42 import net.ktnx.mobileledger.databinding.FragmentTemplateListBinding;
43 import net.ktnx.mobileledger.db.DB;
44 import net.ktnx.mobileledger.db.TemplateHeader;
45 import net.ktnx.mobileledger.ui.FabManager;
46 import net.ktnx.mobileledger.ui.HelpDialog;
47 import net.ktnx.mobileledger.utils.Logger;
49 import org.jetbrains.annotations.NotNull;
51 import java.util.List;
54 * A simple {@link Fragment} subclass.
55 * Use the {@link TemplateListFragment#newInstance} factory method to
56 * create an instance of this fragment.
58 public class TemplateListFragment extends Fragment {
59 private FragmentTemplateListBinding b;
60 private OnTemplateListFragmentInteractionListener mListener;
61 public TemplateListFragment() {
62 // Required empty public constructor
65 * Use this factory method to create a new instance of
66 * this fragment using the provided parameters.
68 * @return A new instance of fragment TemplateListFragment.
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);
97 public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container,
98 Bundle savedInstanceState) {
99 Logger.debug("flow", "PatternListFragment.onCreateView()");
100 b = FragmentTemplateListBinding.inflate(inflater);
101 FragmentActivity activity = requireActivity();
103 if (activity instanceof FabManager.FabHandler)
104 FabManager.handle((FabManager.FabHandler) activity, b.templateList);
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);
116 DividerItemDecoration did =
117 new TemplateListDivider(activity, DividerItemDecoration.VERTICAL);
118 b.templateList.addItemDecoration(did);
123 public void onAttach(@NonNull Context context) {
124 super.onAttach(context);
125 if (context instanceof OnTemplateListFragmentInteractionListener) {
126 mListener = (OnTemplateListFragmentInteractionListener) context;
129 throw new RuntimeException(
130 context.toString() + " must implement OnFragmentInteractionListener");
133 final LifecycleEventObserver observer = new LifecycleEventObserver() {
135 public void onStateChanged(@NonNull LifecycleOwner source,
136 @NonNull Lifecycle.Event event) {
137 if (event.getTargetState() == Lifecycle.State.CREATED) {
138 // getActivity().setActionBar(b.toolbar);
139 getLifecycle().removeObserver(this);
143 getLifecycle().addObserver(observer);
146 * This interface must be implemented by activities that contain this
147 * fragment to allow an interaction in this fragment to be communicated
148 * to the activity and potentially other fragments contained in that
151 * See the Android Training lesson <a href=
152 * "http://developer.android.com/training/basics/fragments/communicating.html"
153 * >Communicating with Other Fragments</a> for more information.
155 public interface OnTemplateListFragmentInteractionListener {
156 void onSaveTemplate();
158 void onEditTemplate(Long id);
160 void onDuplicateTemplate(long id);