]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateListFragment.java
more pronounced day/month delimiters in the transaction list
[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.DividerItemDecoration;
37 import androidx.recyclerview.widget.LinearLayoutManager;
38 import androidx.recyclerview.widget.RecyclerView;
39
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;
48
49 import org.jetbrains.annotations.NotNull;
50
51 import java.util.List;
52
53 /**
54  * A simple {@link Fragment} subclass.
55  * Use the {@link TemplateListFragment#newInstance} factory method to
56  * create an instance of this fragment.
57  */
58 public class TemplateListFragment extends Fragment {
59     private FragmentTemplateListBinding b;
60     private OnTemplateListFragmentInteractionListener mListener;
61     public TemplateListFragment() {
62         // Required empty public constructor
63     }
64     /**
65      * Use this factory method to create a new instance of
66      * this fragment using the provided parameters.
67      *
68      * @return A new instance of fragment TemplateListFragment.
69      */
70     public static TemplateListFragment newInstance() {
71         TemplateListFragment fragment = new TemplateListFragment();
72         Bundle args = new Bundle();
73         fragment.setArguments(args);
74         return fragment;
75     }
76     @Override
77     public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
78         super.onCreateOptionsMenu(menu, inflater);
79         inflater.inflate(R.menu.template_list_menu, menu);
80     }
81     @Override
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);
86             return true;
87         }
88         return super.onOptionsItemSelected(item);
89     }
90     @Override
91     public void onCreate(Bundle savedInstanceState) {
92         super.onCreate(savedInstanceState);
93         setHasOptionsMenu(true);
94     }
95
96     @Override
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();
102
103         if (activity instanceof FabManager.FabHandler)
104             FabManager.handle((FabManager.FabHandler) activity, b.templateList);
105
106         TemplatesRecyclerViewAdapter modelAdapter = new TemplatesRecyclerViewAdapter();
107
108         b.templateList.setAdapter(modelAdapter);
109         TemplateHeaderDAO pDao = DB.get()
110                                    .getTemplateDAO();
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);
119
120         return b.getRoot();
121     }
122     @Override
123     public void onAttach(@NonNull Context context) {
124         super.onAttach(context);
125         if (context instanceof OnTemplateListFragmentInteractionListener) {
126             mListener = (OnTemplateListFragmentInteractionListener) context;
127         }
128         else {
129             throw new RuntimeException(
130                     context.toString() + " must implement OnFragmentInteractionListener");
131         }
132
133         final LifecycleEventObserver observer = new LifecycleEventObserver() {
134             @Override
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);
140                 }
141             }
142         };
143         getLifecycle().addObserver(observer);
144     }
145     /**
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
149      * activity.
150      * <p>
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.
154      */
155     public interface OnTemplateListFragmentInteractionListener {
156         void onSaveTemplate();
157
158         void onEditTemplate(Long id);
159
160         void onDuplicateTemplate(long id);
161     }
162 }