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