From a1d0db823989bbc210758351ff1d1bf0ea0ddffa Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Tue, 12 Jan 2021 22:38:44 +0200 Subject: [PATCH] pave the way to fragment navigation for pattern list activity --- .../ui/patterns/PatternListFragment.java | 135 ++++++++++++++++++ .../ui/patterns/PatternsActivity.java | 49 ++++--- app/src/main/res/layout/activity_patterns.xml | 68 +-------- .../main/res/layout/fragment_pattern_list.xml | 82 +++++++++++ .../navigation/pattern_list_navigation.xml | 29 ++++ 5 files changed, 280 insertions(+), 83 deletions(-) create mode 100644 app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternListFragment.java create mode 100644 app/src/main/res/layout/fragment_pattern_list.xml create mode 100644 app/src/main/res/navigation/pattern_list_navigation.xml diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternListFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternListFragment.java new file mode 100644 index 00000000..ad356456 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternListFragment.java @@ -0,0 +1,135 @@ +/* + * Copyright © 2021 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with MoLe. If not, see . + */ + +package net.ktnx.mobileledger.ui.patterns; + +import android.content.Context; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; +import androidx.lifecycle.Lifecycle; +import androidx.lifecycle.LifecycleEventObserver; +import androidx.lifecycle.LifecycleOwner; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import net.ktnx.mobileledger.R; +import net.ktnx.mobileledger.databinding.FragmentPatternListBinding; + +import org.jetbrains.annotations.NotNull; + +/** + * A simple {@link Fragment} subclass. + * Use the {@link PatternListFragment#newInstance} factory method to + * create an instance of this fragment. + */ +public class PatternListFragment extends Fragment { + private FragmentPatternListBinding b; + private OnPatternListFragmentInteractionListener mListener; + + public PatternListFragment() { + // Required empty public constructor + } + /** + * Use this factory method to create a new instance of + * this fragment using the provided parameters. + * + * @return A new instance of fragment PatternListFragment. + */ + // TODO: Rename and change types and number of parameters + public static PatternListFragment newInstance() { + PatternListFragment fragment = new PatternListFragment(); + Bundle args = new Bundle(); + fragment.setArguments(args); + return fragment; + } + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); +// if (getArguments() != null) { +// mParam1 = getArguments().getString(ARG_PARAM1); +// mParam2 = getArguments().getString(ARG_PARAM2); +// } + } + + @Override + public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + b = FragmentPatternListBinding.inflate(inflater); + + b.toolbarLayout.setTitle(getString(R.string.title_activity_patterns)); + + b.fab.setOnClickListener(this::fabClicked); + + PatternsRecyclerViewAdapter modelAdapter = new PatternsRecyclerViewAdapter(); + + b.patternList.setAdapter(modelAdapter); + PatternsModel.retrievePatterns(modelAdapter); + LinearLayoutManager llm = new LinearLayoutManager(getContext()); + llm.setOrientation(RecyclerView.VERTICAL); + b.patternList.setLayoutManager(llm); + return b.getRoot(); + } + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + if (context instanceof OnPatternListFragmentInteractionListener) { + mListener = (OnPatternListFragmentInteractionListener) context; + } + else { + throw new RuntimeException( + context.toString() + " must implement OnFragmentInteractionListener"); + } + + final LifecycleEventObserver observer = new LifecycleEventObserver() { + @Override + public void onStateChanged(@NonNull LifecycleOwner source, + @NonNull Lifecycle.Event event) { + if (event.getTargetState() == Lifecycle.State.CREATED) { +// getActivity().setActionBar(b.toolbar); + getLifecycle().removeObserver(this); + } + } + }; + getLifecycle().addObserver(observer); + } + private void fabClicked(View view) { + if (mListener == null) + return; + + mListener.onNewPattern(); + } + /** + * This interface must be implemented by activities that contain this + * fragment to allow an interaction in this fragment to be communicated + * to the activity and potentially other fragments contained in that + * activity. + *

+ * See the Android Training lesson Communicating with Other Fragments for more information. + */ + public interface OnPatternListFragmentInteractionListener { + void onNewPattern(); + + void onEditPattern(int id); + } +} \ No newline at end of file diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternsActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternsActivity.java index 8e57a2e7..43a6227d 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternsActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternsActivity.java @@ -19,10 +19,9 @@ package net.ktnx.mobileledger.ui.patterns; import android.os.Bundle; import android.view.Menu; -import android.view.View; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; +import androidx.navigation.NavController; +import androidx.navigation.fragment.NavHostFragment; import com.google.android.material.snackbar.Snackbar; @@ -30,8 +29,12 @@ import net.ktnx.mobileledger.R; import net.ktnx.mobileledger.databinding.ActivityPatternsBinding; import net.ktnx.mobileledger.ui.activity.CrashReportingActivity; -public class PatternsActivity extends CrashReportingActivity { +import java.util.Objects; +public class PatternsActivity extends CrashReportingActivity + implements PatternListFragment.OnPatternListFragmentInteractionListener { + private ActivityPatternsBinding b; + private NavController navController; @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); @@ -42,24 +45,28 @@ public class PatternsActivity extends CrashReportingActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - ActivityPatternsBinding b = ActivityPatternsBinding.inflate(getLayoutInflater()); - setContentView(b.getRoot()); - setSupportActionBar(b.toolbar); - b.toolbarLayout.setTitle(getTitle()); + b = ActivityPatternsBinding.inflate(getLayoutInflater()); + setContentView(b.fragmentContainer); - b.fab.setOnClickListener(this::fabClicked); - - PatternsRecyclerViewAdapter modelAdapter = new PatternsRecyclerViewAdapter(); - - b.patternList.setAdapter(modelAdapter); - PatternsModel.retrievePatterns(modelAdapter); - LinearLayoutManager llm = new LinearLayoutManager(this); - llm.setOrientation(RecyclerView.VERTICAL); - b.patternList.setLayoutManager(llm); + NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull( + getSupportFragmentManager().findFragmentById(R.id.fragment_container)); + navController = navHostFragment.getNavController(); } - private void fabClicked(View view) { - Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_INDEFINITE) - .setAction("Action", null) - .show(); + @Override + public void onNewPattern() { +// navController.navigate + final Snackbar snackbar = + Snackbar.make(b.fragmentContainer, "New pattern action coming up soon", + Snackbar.LENGTH_INDEFINITE); +// snackbar.setAction("Action", v -> snackbar.dismiss()); + snackbar.show(); + } + @Override + public void onEditPattern(int id) { + final Snackbar snackbar = + Snackbar.make(b.fragmentContainer, "One Edit pattern action coming up soon", + Snackbar.LENGTH_INDEFINITE); +// snackbar.setAction("Action", v -> snackbar.dismiss()); + snackbar.show(); } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_patterns.xml b/app/src/main/res/layout/activity_patterns.xml index 78cac2f6..69f5aac1 100644 --- a/app/src/main/res/layout/activity_patterns.xml +++ b/app/src/main/res/layout/activity_patterns.xml @@ -15,68 +15,12 @@ ~ along with MoLe. If not, see . --> - - - - - - - - - - - - - - - - - - - \ No newline at end of file + app:defaultNavHost="true" + app:navGraph="@navigation/pattern_list_navigation" + /> \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_pattern_list.xml b/app/src/main/res/layout/fragment_pattern_list.xml new file mode 100644 index 00000000..e476b0cc --- /dev/null +++ b/app/src/main/res/layout/fragment_pattern_list.xml @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/navigation/pattern_list_navigation.xml b/app/src/main/res/navigation/pattern_list_navigation.xml new file mode 100644 index 00000000..ba60d02b --- /dev/null +++ b/app/src/main/res/navigation/pattern_list_navigation.xml @@ -0,0 +1,29 @@ + + + + + + \ No newline at end of file -- 2.39.2