]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateListFragment.java
separate FAB management in a helper class
[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.View;
24 import android.view.ViewGroup;
25
26 import androidx.annotation.NonNull;
27 import androidx.fragment.app.Fragment;
28 import androidx.fragment.app.FragmentActivity;
29 import androidx.lifecycle.Lifecycle;
30 import androidx.lifecycle.LifecycleEventObserver;
31 import androidx.lifecycle.LifecycleOwner;
32 import androidx.lifecycle.LiveData;
33 import androidx.recyclerview.widget.LinearLayoutManager;
34 import androidx.recyclerview.widget.RecyclerView;
35
36 import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
37 import net.ktnx.mobileledger.databinding.FragmentTemplateListBinding;
38 import net.ktnx.mobileledger.db.DB;
39 import net.ktnx.mobileledger.db.TemplateHeader;
40 import net.ktnx.mobileledger.ui.FabManager;
41 import net.ktnx.mobileledger.utils.Logger;
42
43 import org.jetbrains.annotations.NotNull;
44
45 import java.util.List;
46
47 /**
48  * A simple {@link Fragment} subclass.
49  * Use the {@link TemplateListFragment#newInstance} factory method to
50  * create an instance of this fragment.
51  */
52 public class TemplateListFragment extends Fragment {
53     private FragmentTemplateListBinding b;
54     private OnTemplateListFragmentInteractionListener mListener;
55
56     public TemplateListFragment() {
57         // Required empty public constructor
58     }
59     /**
60      * Use this factory method to create a new instance of
61      * this fragment using the provided parameters.
62      *
63      * @return A new instance of fragment TemplateListFragment.
64      */
65     // TODO: Rename and change types and number of parameters
66     public static TemplateListFragment newInstance() {
67         TemplateListFragment fragment = new TemplateListFragment();
68         Bundle args = new Bundle();
69         fragment.setArguments(args);
70         return fragment;
71     }
72     @Override
73     public void onCreate(Bundle savedInstanceState) {
74         super.onCreate(savedInstanceState);
75 //        if (getArguments() != null) {
76 //            mParam1 = getArguments().getString(ARG_PARAM1);
77 //            mParam2 = getArguments().getString(ARG_PARAM2);
78 //        }
79     }
80
81     @Override
82     public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container,
83                              Bundle savedInstanceState) {
84         Logger.debug("flow", "PatternListFragment.onCreateView()");
85         b = FragmentTemplateListBinding.inflate(inflater);
86
87         TemplatesRecyclerViewAdapter modelAdapter = new TemplatesRecyclerViewAdapter();
88
89         b.templateList.setAdapter(modelAdapter);
90         TemplateHeaderDAO pDao = DB.get()
91                                    .getTemplateDAO();
92         LiveData<List<TemplateHeader>> templates = pDao.getTemplates();
93         templates.observe(getViewLifecycleOwner(), modelAdapter::setTemplates);
94         LinearLayoutManager llm = new LinearLayoutManager(getContext());
95         llm.setOrientation(RecyclerView.VERTICAL);
96         b.templateList.setLayoutManager(llm);
97
98         FragmentActivity activity = requireActivity();
99         if (activity instanceof FabManager.FabHandler)
100             FabManager.handle((FabManager.FabHandler) activity, b.templateList);
101
102         return b.getRoot();
103     }
104     @Override
105     public void onAttach(@NonNull Context context) {
106         super.onAttach(context);
107         if (context instanceof OnTemplateListFragmentInteractionListener) {
108             mListener = (OnTemplateListFragmentInteractionListener) context;
109         }
110         else {
111             throw new RuntimeException(
112                     context.toString() + " must implement OnFragmentInteractionListener");
113         }
114
115         final LifecycleEventObserver observer = new LifecycleEventObserver() {
116             @Override
117             public void onStateChanged(@NonNull LifecycleOwner source,
118                                        @NonNull Lifecycle.Event event) {
119                 if (event.getTargetState() == Lifecycle.State.CREATED) {
120 //                    getActivity().setActionBar(b.toolbar);
121                     getLifecycle().removeObserver(this);
122                 }
123             }
124         };
125         getLifecycle().addObserver(observer);
126     }
127     /**
128      * This interface must be implemented by activities that contain this
129      * fragment to allow an interaction in this fragment to be communicated
130      * to the activity and potentially other fragments contained in that
131      * activity.
132      * <p>
133      * See the Android Training lesson <a href=
134      * "http://developer.android.com/training/basics/fragments/communicating.html"
135      * >Communicating with Other Fragments</a> for more information.
136      */
137     public interface OnTemplateListFragmentInteractionListener {
138         void onSaveTemplate();
139
140         void onEditTemplate(Long id);
141
142         void onDuplicateTemplate(long id);
143     }
144 }