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.app.AlertDialog;
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.text.TextUtils;
24 import android.view.LayoutInflater;
25 import android.view.Menu;
26 import android.view.MenuInflater;
27 import android.view.MenuItem;
28 import android.view.View;
29 import android.view.ViewGroup;
31 import androidx.annotation.NonNull;
32 import androidx.fragment.app.Fragment;
33 import androidx.fragment.app.FragmentActivity;
34 import androidx.lifecycle.Lifecycle;
35 import androidx.lifecycle.LifecycleEventObserver;
36 import androidx.lifecycle.LifecycleOwner;
37 import androidx.lifecycle.LiveData;
38 import androidx.recyclerview.widget.LinearLayoutManager;
39 import androidx.recyclerview.widget.RecyclerView;
41 import net.ktnx.mobileledger.R;
42 import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
43 import net.ktnx.mobileledger.databinding.FragmentTemplateListBinding;
44 import net.ktnx.mobileledger.db.DB;
45 import net.ktnx.mobileledger.db.TemplateHeader;
46 import net.ktnx.mobileledger.ui.FabManager;
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 // TODO: Rename and change types and number of parameters
71 public static TemplateListFragment newInstance() {
72 TemplateListFragment fragment = new TemplateListFragment();
73 Bundle args = new Bundle();
74 fragment.setArguments(args);
78 public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
79 super.onCreateOptionsMenu(menu, inflater);
80 inflater.inflate(R.menu.template_list_menu, menu);
83 public boolean onOptionsItemSelected(@NonNull MenuItem item) {
84 if (item.getItemId() == R.id.menu_item_template_list_help) {
85 AlertDialog.Builder adb = new AlertDialog.Builder(requireContext());
86 adb.setTitle(R.string.template_list_help_title);
87 adb.setMessage(TextUtils.join("\n\n", requireContext().getResources()
89 R.array.template_list_help_text)));
90 adb.setPositiveButton(R.string.close_button, (dialog, buttonId) -> dialog.dismiss());
95 return super.onOptionsItemSelected(item);
98 public void onCreate(Bundle savedInstanceState) {
99 super.onCreate(savedInstanceState);
100 setHasOptionsMenu(true);
101 // if (getArguments() != null) {
102 // mParam1 = getArguments().getString(ARG_PARAM1);
103 // mParam2 = getArguments().getString(ARG_PARAM2);
108 public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container,
109 Bundle savedInstanceState) {
110 Logger.debug("flow", "PatternListFragment.onCreateView()");
111 b = FragmentTemplateListBinding.inflate(inflater);
113 TemplatesRecyclerViewAdapter modelAdapter = new TemplatesRecyclerViewAdapter();
115 b.templateList.setAdapter(modelAdapter);
116 TemplateHeaderDAO pDao = DB.get()
118 LiveData<List<TemplateHeader>> templates = pDao.getTemplates();
119 templates.observe(getViewLifecycleOwner(), modelAdapter::setTemplates);
120 LinearLayoutManager llm = new LinearLayoutManager(getContext());
121 llm.setOrientation(RecyclerView.VERTICAL);
122 b.templateList.setLayoutManager(llm);
124 FragmentActivity activity = requireActivity();
125 if (activity instanceof FabManager.FabHandler)
126 FabManager.handle((FabManager.FabHandler) activity, b.templateList);
131 public void onAttach(@NonNull Context context) {
132 super.onAttach(context);
133 if (context instanceof OnTemplateListFragmentInteractionListener) {
134 mListener = (OnTemplateListFragmentInteractionListener) context;
137 throw new RuntimeException(
138 context.toString() + " must implement OnFragmentInteractionListener");
141 final LifecycleEventObserver observer = new LifecycleEventObserver() {
143 public void onStateChanged(@NonNull LifecycleOwner source,
144 @NonNull Lifecycle.Event event) {
145 if (event.getTargetState() == Lifecycle.State.CREATED) {
146 // getActivity().setActionBar(b.toolbar);
147 getLifecycle().removeObserver(this);
151 getLifecycle().addObserver(observer);
154 * This interface must be implemented by activities that contain this
155 * fragment to allow an interaction in this fragment to be communicated
156 * to the activity and potentially other fragments contained in that
159 * See the Android Training lesson <a href=
160 * "http://developer.android.com/training/basics/fragments/communicating.html"
161 * >Communicating with Other Fragments</a> for more information.
163 public interface OnTemplateListFragmentInteractionListener {
164 void onSaveTemplate();
166 void onEditTemplate(Long id);
168 void onDuplicateTemplate(long id);