]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternListFragment.java
226f846f253929870a47278b177334599528eb67
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / patterns / PatternListFragment.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.patterns;
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.PatternHeaderDAO;
36 import net.ktnx.mobileledger.databinding.FragmentPatternListBinding;
37 import net.ktnx.mobileledger.db.DB;
38 import net.ktnx.mobileledger.db.PatternHeader;
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 PatternListFragment#newInstance} factory method to
48  * create an instance of this fragment.
49  */
50 public class PatternListFragment extends Fragment {
51     private FragmentPatternListBinding b;
52     private OnPatternListFragmentInteractionListener mListener;
53
54     public PatternListFragment() {
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 PatternListFragment.
62      */
63     // TODO: Rename and change types and number of parameters
64     public static PatternListFragment newInstance() {
65         PatternListFragment fragment = new PatternListFragment();
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 = FragmentPatternListBinding.inflate(inflater);
84
85         PatternsRecyclerViewAdapter modelAdapter = new PatternsRecyclerViewAdapter();
86
87         b.patternList.setAdapter(modelAdapter);
88         PatternHeaderDAO pDao = DB.get()
89                                   .getPatternDAO();
90         LiveData<List<PatternHeader>> patterns = pDao.getPatterns();
91         patterns.observe(getViewLifecycleOwner(), list -> {modelAdapter.setPatterns(list);});
92         LinearLayoutManager llm = new LinearLayoutManager(getContext());
93         llm.setOrientation(RecyclerView.VERTICAL);
94         b.patternList.setLayoutManager(llm);
95         return b.getRoot();
96     }
97     @Override
98     public void onAttach(@NonNull Context context) {
99         super.onAttach(context);
100         if (context instanceof OnPatternListFragmentInteractionListener) {
101             mListener = (OnPatternListFragmentInteractionListener) 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     private void fabClicked(View view) {
121         if (mListener == null)
122             return;
123
124         mListener.onEditPattern(null);
125     }
126     /**
127      * This interface must be implemented by activities that contain this
128      * fragment to allow an interaction in this fragment to be communicated
129      * to the activity and potentially other fragments contained in that
130      * activity.
131      * <p>
132      * See the Android Training lesson <a href=
133      * "http://developer.android.com/training/basics/fragments/communicating.html"
134      * >Communicating with Other Fragments</a> for more information.
135      */
136     public interface OnPatternListFragmentInteractionListener {
137         void onSavePattern();
138
139         void onEditPattern(Long id);
140     }
141 }