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.R;
35 import net.ktnx.mobileledger.databinding.FragmentPatternListBinding;
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 b = FragmentPatternListBinding.inflate(inflater);
78 b.toolbarLayout.setTitle(getString(R.string.title_activity_patterns));
80 b.fab.setOnClickListener(this::fabClicked);
82 PatternsRecyclerViewAdapter modelAdapter = new PatternsRecyclerViewAdapter();
84 b.patternList.setAdapter(modelAdapter);
85 PatternsModel.retrievePatterns(modelAdapter);
86 LinearLayoutManager llm = new LinearLayoutManager(getContext());
87 llm.setOrientation(RecyclerView.VERTICAL);
88 b.patternList.setLayoutManager(llm);
92 public void onAttach(@NonNull Context context) {
93 super.onAttach(context);
94 if (context instanceof OnPatternListFragmentInteractionListener) {
95 mListener = (OnPatternListFragmentInteractionListener) context;
98 throw new RuntimeException(
99 context.toString() + " must implement OnFragmentInteractionListener");
102 final LifecycleEventObserver observer = new LifecycleEventObserver() {
104 public void onStateChanged(@NonNull LifecycleOwner source,
105 @NonNull Lifecycle.Event event) {
106 if (event.getTargetState() == Lifecycle.State.CREATED) {
107 // getActivity().setActionBar(b.toolbar);
108 getLifecycle().removeObserver(this);
112 getLifecycle().addObserver(observer);
114 private void fabClicked(View view) {
115 if (mListener == null)
118 mListener.onNewPattern();
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
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.
130 public interface OnPatternListFragmentInteractionListener {
133 void onEditPattern(int id);