Skip to content
Permalink
Browse files

Trying out Navigation library

  • Loading branch information...
chrisbanes committed Mar 9, 2019
1 parent 4e0a370 commit f6ec834e5f8ed5346374f7ade3fe8f66674ee918
@@ -4,6 +4,7 @@
/.idea/workspace.xml
/.idea/libraries
/.idea/caches
/.idea/navEditor.xml
.DS_Store
build/
captures
@@ -182,6 +182,8 @@ dependencies {
implementation Libs.AndroidX.constraintlayout
implementation Libs.AndroidX.coreKtx
implementation Libs.AndroidX.Fragment.fragmentKtx
implementation Libs.AndroidX.Navigation.fragment
implementation Libs.AndroidX.Navigation.ui

implementation Libs.Google.material

@@ -25,20 +25,19 @@ import android.view.ViewGroup
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.net.toUri
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import app.tivi.R
import app.tivi.SharedElementHelper
import app.tivi.databinding.FragmentHomeBinding
import app.tivi.extensions.updateConstraintSets
import app.tivi.home.HomeActivity
import app.tivi.home.HomeNavigatorViewModel
import app.tivi.home.discover.DiscoverFragment
import app.tivi.home.followed.FollowedFragment
import app.tivi.home.popular.PopularShowsFragment
import app.tivi.home.trending.TrendingShowsFragment
import app.tivi.home.watched.WatchedFragment
import app.tivi.trakt.TraktAuthState
import app.tivi.ui.glide.GlideApp
import app.tivi.ui.glide.asGlideTarget
@@ -104,10 +103,10 @@ class HomeNavigationFragment : TiviMvRxFragment() {
.asGlideTarget(binding.homeToolbar)

homeNavigatorViewModel.showPopularCall.observeEvent(this) {
showStackFragment(PopularShowsFragment(), it)
getNavController().navigate(R.id.action_discover_to_popular)
}
homeNavigatorViewModel.showTrendingCall.observeEvent(this) {
showStackFragment(TrendingShowsFragment(), it)
getNavController().navigate(R.id.action_discover_to_popular)
}
}

@@ -140,40 +139,24 @@ class HomeNavigationFragment : TiviMvRxFragment() {
}

private fun showNavigationItem(item: HomeNavigationItem) {
val newFragment: Fragment = when (item) {
HomeNavigationItem.DISCOVER -> DiscoverFragment()
HomeNavigationItem.FOLLOWED -> FollowedFragment()
HomeNavigationItem.WATCHED -> WatchedFragment()
when (item) {
HomeNavigationItem.DISCOVER -> {
getNavController().navigate(R.id.discover)
}
HomeNavigationItem.FOLLOWED -> {
getNavController().navigate(R.id.followed)
}
HomeNavigationItem.WATCHED -> {
getNavController().navigate(R.id.watched)
}
}

val currentFragment = childFragmentManager.findFragmentById(R.id.home_content)

if (currentFragment == null || currentFragment::class != newFragment::class) {
childFragmentManager.popBackStackImmediate(ROOT_FRAGMENT, 0)

childFragmentManager.beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.replace(R.id.home_content, newFragment, ROOT_FRAGMENT)
.commit()

// Close the menu if we've changed fragments
binding.homeRoot.transitionToStart()
}
// Close the menu if we've changed fragments
binding.homeRoot.transitionToStart()
}

private fun showStackFragment(fragment: Fragment, sharedElements: SharedElementHelper? = null) {
childFragmentManager.beginTransaction()
.setReorderingAllowed(true)
.replace(R.id.home_content, fragment)
.addToBackStack(null)
.apply {
if (sharedElements != null && !sharedElements.isEmpty()) {
sharedElements.applyToTransaction(this)
} else {
setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
}
}
.commit()
private fun getNavController(): NavController {
return (childFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment).navController
}

private fun onMenuItemClicked(item: MenuItem) = when (item.itemId) {
@@ -81,10 +81,13 @@
app:layout_constraintTop_toBottomOf="@id/home_toolbar"
app:materialBackdropBackgroundRadius="@{ @dimen/spacing_normal }">

<FrameLayout
android:id="@+id/home_content"
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />

</FrameLayout>

@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2019 Google, Inc.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_graph"
app:startDestination="@id/discover">

<fragment
android:id="@+id/watched"
android:name="app.tivi.home.watched.WatchedFragment"
android:label="WatchedFragment" />
<fragment
android:id="@+id/discover"
android:name="app.tivi.home.discover.DiscoverFragment"
android:label="DiscoverFragment" >
<action
android:id="@+id/action_discover_to_trending"
app:destination="@id/trending"
app:popUpTo="@+id/discover" />
<action
android:id="@+id/action_discover_to_popular"
app:destination="@id/popular"
app:popUpTo="@+id/discover" />
</fragment>
<fragment
android:id="@+id/followed"
android:name="app.tivi.home.followed.FollowedFragment"
android:label="FollowedFragment" />
<fragment
android:id="@+id/trending"
android:name="app.tivi.home.trending.TrendingShowsFragment"
android:label="TrendingShowsFragment" />
<fragment
android:id="@+id/popular"
android:name="app.tivi.home.popular.PopularShowsFragment"
android:label="PopularShowsFragment" />

</navigation>
@@ -75,6 +75,13 @@ object Libs {
val recyclerview = "androidx.recyclerview:recyclerview:1.0.0"
val emoji = "androidx.emoji:emoji:1.0.0"

object Navigation {
private const val version = "2.0.0-rc02"
val fragment = "androidx.navigation:navigation-fragment-ktx:$version"
val ui = "androidx.navigation:navigation-ui-ktx:$version"
val safeArgs = "androidx.navigation:navigation-safe-args-gradle-plugin:$version"
}

object Fragment {
private const val version = "1.1.0-alpha03"
val fragment = "androidx.fragment:fragment:$version"

0 comments on commit f6ec834

Please sign in to comment.
You can’t perform that action at this time.