manage_feeds: Use navigation for ManageFeedsActivity
authorDa Risk <da_risk@geekorum.com>
Fri, 05 Jul 2019 10:53:54 -0700
changeset 348 6671b50e2c80
parent 347 48bf767716e7
child 349 7cca2914671f
manage_feeds: Use navigation for ManageFeedsActivity
manage_feeds/build.gradle.kts
manage_feeds/src/main/java/com/geekorum/ttrss/manage_feeds/ManageFeedsActivity.kt
manage_feeds/src/main/java/com/geekorum/ttrss/manage_feeds/add_feed/SubscribeToFeedActivity.kt
manage_feeds/src/main/res/layout/activity_manage_feeds.xml
manage_feeds/src/main/res/navigation/manage_feeds.xml
--- a/manage_feeds/build.gradle.kts	Fri Jul 05 10:20:03 2019 -0700
+++ b/manage_feeds/build.gradle.kts	Fri Jul 05 10:53:54 2019 -0700
@@ -89,7 +89,7 @@
     androidTestImplementation("androidx.work:work-testing:2.1.0-rc01")
     dualTestImplementation("androidx.arch.core:core-testing:2.0.1")
 
-    implementation("androidx.navigation:navigation-fragment-ktx:2.1.0-alpha05")
+    implementation("androidx.navigation:navigation-fragment-ktx:2.1.0-alpha06")
     debugImplementation("androidx.fragment:fragment-testing:1.2.0-alpha01")
 
 }
--- a/manage_feeds/src/main/java/com/geekorum/ttrss/manage_feeds/ManageFeedsActivity.kt	Fri Jul 05 10:20:03 2019 -0700
+++ b/manage_feeds/src/main/java/com/geekorum/ttrss/manage_feeds/ManageFeedsActivity.kt	Fri Jul 05 10:53:54 2019 -0700
@@ -21,18 +21,19 @@
 package com.geekorum.ttrss.manage_feeds
 
 import android.app.Dialog
-import android.content.Intent
 import android.os.Bundle
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
-import androidx.core.os.bundleOf
 import androidx.databinding.DataBindingUtil
 import androidx.databinding.ViewDataBinding
 import androidx.fragment.app.FragmentFactory
-import androidx.fragment.app.commit
 import androidx.lifecycle.ViewModelProvider
 import androidx.lifecycle.observe
+import androidx.navigation.NavController
+import androidx.navigation.findNavController
+import androidx.navigation.fragment.findNavController
+import androidx.navigation.fragment.navArgs
 import androidx.paging.PagedListAdapter
 import androidx.recyclerview.widget.DiffUtil
 import androidx.recyclerview.widget.RecyclerView
@@ -44,7 +45,6 @@
 import com.geekorum.ttrss.activityViewModels
 import com.geekorum.ttrss.applicationComponent
 import com.geekorum.ttrss.data.Feed
-import com.geekorum.ttrss.manage_feeds.add_feed.SubscribeToFeedActivity
 import com.geekorum.ttrss.manage_feeds.databinding.ActivityManageFeedsBinding
 import com.geekorum.ttrss.manage_feeds.databinding.DialogUnsubscribeFeedBinding
 import com.geekorum.ttrss.manage_feeds.databinding.FragmentManageFeedsBinding
@@ -55,27 +55,17 @@
 import javax.inject.Inject
 
 class ManageFeedsActivity : SessionActivity() {
-    private val viewModel: ManageFeedViewModel by viewModels()
     private lateinit var binding: ActivityManageFeedsBinding
+    private lateinit var navController: NavController
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         binding = DataBindingUtil.setContentView(this, R.layout.activity_manage_feeds)
+        navController = findNavController(R.id.nav_host_fragment)
 
         binding.fab.setOnClickListener {
             startSubscribeToFeed()
         }
-
-        viewModel.feedClickedEvent.observe(this, EventObserver {
-            showConfirmationDialog(it)
-        })
-
-        if (savedInstanceState == null) {
-            supportFragmentManager.commit {
-                val f = ManageFeedsFragment.newInstance(supportFragmentManager.fragmentFactory)
-                replace(R.id.container, f)
-            }
-        }
     }
 
     override fun inject() {
@@ -85,15 +75,9 @@
         manageFeedComponent.activityInjector.inject(this)
     }
 
-    private fun showConfirmationDialog(feed: Feed) {
-        val confirmationFragment =
-            ConfirmUnsubscribeFragment.newInstance(supportFragmentManager.fragmentFactory, feed)
-        confirmationFragment.show(supportFragmentManager, null)
-    }
-
     private fun startSubscribeToFeed() {
-        val intent = Intent(this, SubscribeToFeedActivity::class.java)
-        startActivity(intent)
+        val direction = ManageFeedsFragmentDirections.actionSubscribeToFeed()
+        navController.navigate(direction)
     }
 
 }
@@ -120,8 +104,19 @@
         viewModel.feeds.observe(this) {
             adapter.submitList(it)
         }
+
+        viewModel.feedClickedEvent.observe(this, EventObserver {
+            showConfirmationDialog(it)
+        })
     }
 
+    private fun showConfirmationDialog(feed: Feed) {
+        val direction = ManageFeedsFragmentDirections.actionConfirmUnsubscribe(
+            feed.id, feed.title, feed.url)
+        findNavController().navigate(direction)
+    }
+
+
     private inner class FeedsAdapter : PagedListAdapter<Feed, FeedViewHolder>(DiffFeed) {
         override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FeedViewHolder {
             val itemFeedBinding = ItemFeedBinding.inflate(layoutInflater, parent, false)
@@ -159,15 +154,6 @@
         }
     }
 
-    companion object {
-
-        @JvmStatic
-        fun newInstance(fragmentFactory: FragmentFactory): ManageFeedsFragment {
-            return fragmentFactory.instantiate(ManageFeedsFragment::class.java.classLoader!!,
-                ManageFeedsFragment::class.java.name).apply {
-            } as ManageFeedsFragment
-        }
-    }
 }
 
 class ConfirmUnsubscribeFragment @Inject constructor(
@@ -176,17 +162,17 @@
 ) : BaseDialogFragment(viewModelsFactory, fragmentFactory) {
 
     private val viewModel: ManageFeedViewModel by activityViewModels()
+    private val args:ConfirmUnsubscribeFragmentArgs by navArgs()
 
     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
         val inflater = requireActivity().layoutInflater
 
         val binding = DialogUnsubscribeFeedBinding.inflate(inflater, null, false)
-        val arguments = requireArguments()
-        with(arguments) {
-            binding.title = getString(ARG_FEED_TITLE)
-            binding.url = getString(ARG_FEED_URL)
+        with(args) {
+            binding.title = feedTitle
+            binding.url = feedUrl
         }
-        val feedId = arguments.getLong(ARG_FEED_ID)
+        val feedId = args.feedId
         return MaterialAlertDialogBuilder(requireActivity())
             .setView(binding.root)
             .setTitle(R.string.fragment_confirmation_title)
@@ -196,21 +182,4 @@
             .setNegativeButton(R.string.btn_cancel, null)
             .create()
     }
-
-    companion object {
-        const val ARG_FEED_ID = "feed_id"
-        const val ARG_FEED_TITLE = "feed_title"
-        const val ARG_FEED_URL = "feed_url"
-
-        @JvmStatic
-        fun newInstance(fragmentFactory: FragmentFactory, feed: Feed): ConfirmUnsubscribeFragment {
-            return fragmentFactory.instantiate(ConfirmUnsubscribeFragment::class.java.classLoader!!,
-                ConfirmUnsubscribeFragment::class.java.name).apply {
-                arguments = bundleOf(
-                    ARG_FEED_ID to feed.id,
-                    ARG_FEED_TITLE to feed.title,
-                    ARG_FEED_URL to feed.url)
-            } as ConfirmUnsubscribeFragment
-        }
-    }
 }
--- a/manage_feeds/src/main/java/com/geekorum/ttrss/manage_feeds/add_feed/SubscribeToFeedActivity.kt	Fri Jul 05 10:20:03 2019 -0700
+++ b/manage_feeds/src/main/java/com/geekorum/ttrss/manage_feeds/add_feed/SubscribeToFeedActivity.kt	Fri Jul 05 10:53:54 2019 -0700
@@ -36,9 +36,7 @@
 
     private lateinit var binding: ActivitySubscribeToFeedBinding
     private val viewModel: SubscribeToFeedViewModel by viewModels()
-    private val navController: NavController by lazy {
-        findNavController(R.id.nav_host_fragment)
-    }
+    private lateinit var navController: NavController
 
     override fun inject() {
         val manageFeedComponent = DaggerManageFeedComponent.builder()
@@ -50,6 +48,8 @@
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         binding = DataBindingUtil.setContentView(this, R.layout.activity_subscribe_to_feed)
+        navController = findNavController(R.id.nav_host_fragment)
+
         binding.cancel.setOnClickListener {
             if (!navController.popBackStack())
                 finish()
--- a/manage_feeds/src/main/res/layout/activity_manage_feeds.xml	Fri Jul 05 10:20:03 2019 -0700
+++ b/manage_feeds/src/main/res/layout/activity_manage_feeds.xml	Fri Jul 05 10:53:54 2019 -0700
@@ -57,11 +57,12 @@
 
         </com.google.android.material.appbar.AppBarLayout>
 
-        <FrameLayout
-                android:id="@+id/container"
+        <fragment android:id="@+id/nav_host_fragment"
+                android:name="androidx.navigation.fragment.NavHostFragment"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 app:layout_behavior="@string/appbar_scrolling_view_behavior"
+                app:navGraph="@navigation/manage_feeds"
                 />
 
         <com.google.android.material.floatingactionbutton.FloatingActionButton
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/manage_feeds/src/main/res/navigation/manage_feeds.xml	Fri Jul 05 10:53:54 2019 -0700
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+
+    Geekttrss is a RSS feed reader application on the Android Platform.
+
+    Copyright (C) 2017-2019 by Frederic-Charles Barthelery.
+
+    This file is part of Geekttrss.
+
+    Geekttrss is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Geekttrss 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 for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with Geekttrss.  If not, see <http://www.gnu.org/licenses/>.
+
+-->
+<navigation 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/manage_feeds"
+        app:startDestination="@id/manageFeedsFragment">
+
+    <fragment android:id="@+id/manageFeedsFragment"
+            android:name="com.geekorum.ttrss.manage_feeds.ManageFeedsFragment"
+            tools:layout="@layout/fragment_manage_feeds"
+            android:label="ManageFeedsFragment" >
+
+        <action android:id="@+id/action_confirm_unsubscribe"
+                app:destination="@id/confirmUnsubscribeFragment" />
+
+        <action android:id="@+id/action_subscribe_to_feed"
+                app:destination="@id/subscribeToFeedActivity" />
+    </fragment>
+
+    <dialog android:id="@+id/confirmUnsubscribeFragment"
+            android:name="com.geekorum.ttrss.manage_feeds.ConfirmUnsubscribeFragment"
+            tools:layout="@layout/dialog_unsubscribe_feed"
+            android:label="ConfirmUnsubscribeFragment" >
+        <argument android:name="feed_id" app:argType="long" />
+        <argument android:name="feed_title" app:argType="string" />
+        <argument android:name="feed_url" app:argType="string" />
+    </dialog>
+
+    <activity android:id="@+id/subscribeToFeedActivity"
+            android:name="com.geekorum.ttrss.manage_feeds.add_feed.SubscribeToFeedActivity"
+            tools:layout="@layout/activity_subscribe_to_feed"
+            android:label="SubscribeToFeedActivity" />
+</navigation>