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.
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.
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/>.
18 package net.ktnx.mobileledger.ui.patterns;
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;
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;
34 import net.ktnx.mobileledger.databinding.FragmentPatternListBinding;
35 import net.ktnx.mobileledger.utils.Logger;
37 import org.jetbrains.annotations.NotNull;
40 * A simple {@link Fragment} subclass.
41 * Use the {@link PatternListFragment#newInstance} factory method to
42 * create an instance of this fragment.
44 public class PatternListFragment extends Fragment {
45 private FragmentPatternListBinding b;
46 private OnPatternListFragmentInteractionListener mListener;
48 public PatternListFragment() {
49 // Required empty public constructor
52 * Use this factory method to create a new instance of
53 * this fragment using the provided parameters.
55 * @return A new instance of fragment PatternListFragment.
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);
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);
74 public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container,
75 Bundle savedInstanceState) {
76 Logger.debug("flow", "PatternListFragment.onCreateView()");
77 b = FragmentPatternListBinding.inflate(inflater);
79 PatternsRecyclerViewAdapter modelAdapter = new PatternsRecyclerViewAdapter();
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);
89 public void onAttach(@NonNull Context context) {
90 super.onAttach(context);
91 if (context instanceof OnPatternListFragmentInteractionListener) {
92 mListener = (OnPatternListFragmentInteractionListener) context;
95 throw new RuntimeException(
96 context.toString() + " must implement OnFragmentInteractionListener");
99 final LifecycleEventObserver observer = new LifecycleEventObserver() {
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);
109 getLifecycle().addObserver(observer);
111 private void fabClicked(View view) {
112 if (mListener == null)
115 mListener.onNewPattern();
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
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.
127 public interface OnPatternListFragmentInteractionListener {
129 void onSavePattern();
130 void onEditPattern(int id);