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