]> git.ktnx.net Git - mobile-ledger.git/commitdiff
pave the way to fragment navigation for pattern list activity
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Tue, 12 Jan 2021 20:38:44 +0000 (22:38 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Tue, 12 Jan 2021 20:38:44 +0000 (22:38 +0200)
app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternListFragment.java [new file with mode: 0644]
app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternsActivity.java
app/src/main/res/layout/activity_patterns.xml
app/src/main/res/layout/fragment_pattern_list.xml [new file with mode: 0644]
app/src/main/res/navigation/pattern_list_navigation.xml [new file with mode: 0644]

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 (file)
index 0000000..ad35645
--- /dev/null
@@ -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 <https://www.gnu.org/licenses/>.
+ */
+
+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.
+     * <p>
+     * See the Android Training lesson <a href=
+     * "http://developer.android.com/training/basics/fragments/communicating.html"
+     * >Communicating with Other Fragments</a> for more information.
+     */
+    public interface OnPatternListFragmentInteractionListener {
+        void onNewPattern();
+
+        void onEditPattern(int id);
+    }
+}
\ No newline at end of file
index 8e57a2e751730f506f3aebf41c07a17670832760..43a6227df1c8e20e951f59c964a1a4057f40aae1 100644 (file)
@@ -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
index 78cac2f68bcd3862cf205e9bc2341d14b07deb5b..69f5aac1324322e18e5327f7a260d9a2adb4a00e 100644 (file)
   ~ along with MoLe. If not, see <https://www.gnu.org/licenses/>.
   -->
 
-<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
-    xmlns:tools="http://schemas.android.com/tools"
+    android:id="@+id/fragment_container"
+    android:name="androidx.navigation.fragment.NavHostFragment"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
-    android:fitsSystemWindows="true"
-
-    tools:context=".ui.patterns.PatternsActivity"
-    >
-
-    <com.google.android.material.appbar.AppBarLayout
-        android:id="@+id/app_bar"
-        android:layout_width="match_parent"
-        android:layout_height="@dimen/app_bar_height"
-        android:fitsSystemWindows="true"
-        >
-
-        <com.google.android.material.appbar.CollapsingToolbarLayout
-            android:id="@+id/toolbar_layout"
-            android:layout_width="match_parent"
-            android:layout_height="match_parent"
-            android:fitsSystemWindows="true"
-            app:contentScrim="?attr/colorPrimary"
-            app:layout_scrollFlags="scroll|exitUntilCollapsed"
-            app:toolbarId="@+id/toolbar"
-            >
-
-            <androidx.appcompat.widget.Toolbar
-                android:id="@+id/toolbar"
-                android:layout_width="match_parent"
-                android:layout_height="?attr/actionBarSize"
-                app:layout_collapseMode="pin"
-                app:popupTheme="@style/AppTheme.PopupOverlay"
-                />
-
-        </com.google.android.material.appbar.CollapsingToolbarLayout>
-    </com.google.android.material.appbar.AppBarLayout>
-
-    <androidx.core.widget.NestedScrollView
-        android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        app:layout_behavior="@string/appbar_scrolling_view_behavior"
-        tools:context=".ui.patterns.PatternsActivity"
-        tools:showIn="@layout/activity_patterns"
-        >
-
-        <androidx.recyclerview.widget.RecyclerView
-            android:id="@+id/pattern_list"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            />
-
-    </androidx.core.widget.NestedScrollView>
-    <com.google.android.material.floatingactionbutton.FloatingActionButton
-        android:id="@+id/fab"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_margin="@dimen/fab_margin"
-        app:layout_anchor="@id/pattern_list"
-        app:layout_anchorGravity="top|end"
-        app:layout_behavior="com.google.android.material.floatingactionbutton.FloatingActionButton$Behavior"
-        app:srcCompat="@drawable/ic_add_white_24dp"
-        />
-
-</androidx.coordinatorlayout.widget.CoordinatorLayout>
\ 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 (file)
index 0000000..e476b0c
--- /dev/null
@@ -0,0 +1,82 @@
+<?xml version="1.0" encoding="utf-8"?><!--
+  ~ 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 <https://www.gnu.org/licenses/>.
+  -->
+
+<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:fitsSystemWindows="true"
+    tools:context=".ui.patterns.PatternsActivity"
+    >
+
+    <com.google.android.material.appbar.AppBarLayout
+        android:id="@+id/app_bar"
+        android:layout_width="match_parent"
+        android:layout_height="@dimen/app_bar_height"
+        android:fitsSystemWindows="true"
+        >
+
+        <com.google.android.material.appbar.CollapsingToolbarLayout
+            android:id="@+id/toolbar_layout"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:fitsSystemWindows="true"
+            app:contentScrim="?attr/colorPrimary"
+            app:layout_scrollFlags="scroll|exitUntilCollapsed"
+            app:toolbarId="@+id/toolbar"
+            >
+
+            <androidx.appcompat.widget.Toolbar
+                android:id="@+id/toolbar"
+                android:layout_width="match_parent"
+                android:layout_height="?attr/actionBarSize"
+                app:layout_collapseMode="pin"
+                app:popupTheme="@style/AppTheme.PopupOverlay"
+                />
+
+        </com.google.android.material.appbar.CollapsingToolbarLayout>
+    </com.google.android.material.appbar.AppBarLayout>
+
+    <androidx.core.widget.NestedScrollView
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        app:layout_behavior="@string/appbar_scrolling_view_behavior"
+        tools:context=".ui.patterns.PatternsActivity"
+        tools:showIn="@layout/activity_patterns"
+        >
+
+        <androidx.recyclerview.widget.RecyclerView
+            android:id="@+id/pattern_list"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            />
+
+    </androidx.core.widget.NestedScrollView>
+
+    <com.google.android.material.floatingactionbutton.FloatingActionButton
+        android:id="@+id/fab"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_margin="@dimen/fab_margin"
+        app:layout_anchor="@id/pattern_list"
+        app:layout_anchorGravity="top|end"
+        app:layout_behavior="com.google.android.material.floatingactionbutton.FloatingActionButton$Behavior"
+        app:srcCompat="@drawable/ic_add_white_24dp"
+        />
+
+</androidx.coordinatorlayout.widget.CoordinatorLayout>
\ 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 (file)
index 0000000..ba60d02
--- /dev/null
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?><!--
+  ~ 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 <https://www.gnu.org/licenses/>.
+  -->
+
+<navigation xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:id="@+id/pattern_list_navigation.xml"
+    app:startDestination="@id/patternListFragment"
+    >
+
+    <fragment
+        android:id="@+id/patternListFragment"
+        android:name="net.ktnx.mobileledger.ui.patterns.PatternListFragment"
+        android:label="PatternListFragment"
+        />
+</navigation>
\ No newline at end of file