]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateListFragment.java
drop a dummy TODO
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / templates / TemplateListFragment.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.templates;
19
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;
28
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;
38
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;
47
48 import org.jetbrains.annotations.NotNull;
49
50 import java.util.List;
51
52 /**
53  * A simple {@link Fragment} subclass.
54  * Use the {@link TemplateListFragment#newInstance} factory method to
55  * create an instance of this fragment.
56  */
57 public class TemplateListFragment extends Fragment {
58     private FragmentTemplateListBinding b;
59     private OnTemplateListFragmentInteractionListener mListener;
60     public TemplateListFragment() {
61         // Required empty public constructor
62     }
63     /**
64      * Use this factory method to create a new instance of
65      * this fragment using the provided parameters.
66      *
67      * @return A new instance of fragment TemplateListFragment.
68      */
69     public static TemplateListFragment newInstance() {
70         TemplateListFragment fragment = new TemplateListFragment();
71         Bundle args = new Bundle();
72         fragment.setArguments(args);
73         return fragment;
74     }
75     @Override
76     public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
77         super.onCreateOptionsMenu(menu, inflater);
78         inflater.inflate(R.menu.template_list_menu, menu);
79     }
80     @Override
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);
85             return true;
86         }
87         return super.onOptionsItemSelected(item);
88     }
89     @Override
90     public void onCreate(Bundle savedInstanceState) {
91         super.onCreate(savedInstanceState);
92         setHasOptionsMenu(true);
93     }
94
95     @Override
96     public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container,
97                              Bundle savedInstanceState) {
98         Logger.debug("flow", "PatternListFragment.onCreateView()");
99         b = FragmentTemplateListBinding.inflate(inflater);
100
101         TemplatesRecyclerViewAdapter modelAdapter = new TemplatesRecyclerViewAdapter();
102
103         b.templateList.setAdapter(modelAdapter);
104         TemplateHeaderDAO pDao = DB.get()
105                                    .getTemplateDAO();
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);
111
112         FragmentActivity activity = requireActivity();
113         if (activity instanceof FabManager.FabHandler)
114             FabManager.handle((FabManager.FabHandler) activity, b.templateList);
115
116         return b.getRoot();
117     }
118     @Override
119     public void onAttach(@NonNull Context context) {
120         super.onAttach(context);
121         if (context instanceof OnTemplateListFragmentInteractionListener) {
122             mListener = (OnTemplateListFragmentInteractionListener) context;
123         }
124         else {
125             throw new RuntimeException(
126                     context.toString() + " must implement OnFragmentInteractionListener");
127         }
128
129         final LifecycleEventObserver observer = new LifecycleEventObserver() {
130             @Override
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);
136                 }
137             }
138         };
139         getLifecycle().addObserver(observer);
140     }
141     /**
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
145      * activity.
146      * <p>
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.
150      */
151     public interface OnTemplateListFragmentInteractionListener {
152         void onSaveTemplate();
153
154         void onEditTemplate(Long id);
155
156         void onDuplicateTemplate(long id);
157     }
158 }