Skip to content

Commit

Permalink
Added interceptCloseInstructionForAndroidxNavigation to allow deleg…
Browse files Browse the repository at this point in the history
…ation of backstack commands to AndroidX Navigation
  • Loading branch information
isaac-udy committed Oct 10, 2022
1 parent 79e26a8 commit 4186715
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
1 change: 1 addition & 0 deletions enro-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {
implementation deps.androidx.activity
implementation deps.androidx.recyclerview

compileOnly deps.androidx.navigation.fragment
compileOnly deps.hilt.android
kapt deps.hilt.compiler
kapt deps.hilt.androidCompiler
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dev.enro.core.fragment

import androidx.fragment.app.Fragment
import androidx.navigation.fragment.NavHostFragment
import dev.enro.core.NavigationContext
import dev.enro.core.fragment

/**
* In applications that contain both AndroidX Navigation and Enro, the back pressed behaviour
* that is set by the NavigationHandleViewModel takes precedence over the back pressed behaviours that
* are set by AndroidX Navigation.
*
* This method checks whether or not a given NavigationContext<out Fragment> is a part of the AndroidX Navigation,
* by checking whether or not the parent fragment is a NavHostFragment. If we see that it is a NavHostFragment,
* we'll execute popBackStack (which is the same behaviour the back pressed behaviour set by AndroidX Navigation),
* and then return true.
*
* If we decide that the NavigationContext<out Fragment> does **not** belong to AndroidX Navigation, and
* is either part of Enro, or not part of any navigation framework, then we return false, to indicate that no
* action was performed.
*/
internal fun interceptCloseInstructionForAndroidxNavigation(context: NavigationContext<out Fragment>): Boolean {
if (!isAndroidxNavigationOnTheClasspath) return false
val parent = context.fragment.parentFragment
if (parent is NavHostFragment) {
parent.navController.popBackStack()
return true
}
return false
}

private val isAndroidxNavigationOnTheClasspath by lazy {
runCatching { NavHostFragment::class.java }
.map { true }
.getOrDefault(false)
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ object DefaultFragmentExecutor : NavigationExecutor<Any, Fragment, NavigationKey
}

override fun close(context: NavigationContext<out Fragment>) {
if (interceptCloseInstructionForAndroidxNavigation(context)) return

if(!tryExecutePendingTransitions(context.fragment.parentFragmentManager)) {
mainThreadHandler.post {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,14 @@ import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import androidx.core.view.children
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso
import dev.enro.TestFragment
import dev.enro.application
import dev.enro.core.controller.navigationController
import dev.enro.expectFragment
import org.junit.After
import org.junit.Before
import org.junit.Test

class JetpackNavigationInteropTest {

val override = createOverride<Any, Fragment> {
closed {
when (val parent = it.contextReference.parentFragment) {
is NavHostFragment -> parent.navController.popBackStack()
else -> defaultClosed(it)
}
}
}

@Before
fun before() {
application.navigationController.addOverride(override)
}

@After
fun after() {
application.navigationController.removeOverride(override)
}
class AndroidxNavigationInteropTest {

@Test
fun whenBackButtonIsPressed_thenJetpackNavigationReceivesBackButtonPress() {
Expand Down

0 comments on commit 4186715

Please sign in to comment.