-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #276 from CherryPerry/ribs-appyx-back-fix
Fix back press invocation order in RIBs-Appyx integration
- Loading branch information
Showing
11 changed files
with
253 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
libraries/interop-ribs/src/androidTest/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<application> | ||
|
||
<activity | ||
android:name="com.bumble.appyx.interop.ribs.AppyxRibsInteropActivity" | ||
android:theme="@style/Theme.AppCompat.Light.NoActionBar" /> | ||
|
||
</application> | ||
|
||
</manifest> |
20 changes: 20 additions & 0 deletions
20
...rop-ribs/src/androidTest/kotlin/com/bumble/appyx/interop/ribs/AppyxRibsInteropActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.bumble.appyx.interop.ribs | ||
|
||
import android.os.Bundle | ||
import android.view.ViewGroup | ||
import com.badoo.ribs.core.Rib | ||
import com.badoo.ribs.core.modality.BuildContext | ||
|
||
class AppyxRibsInteropActivity : InteropActivity() { | ||
|
||
lateinit var ribsNode: RibsNode | ||
|
||
override val rootViewGroup: ViewGroup | ||
get() = findViewById(android.R.id.content) | ||
|
||
override fun createRib(savedInstanceState: Bundle?): Rib = | ||
RibsNodeBuilder() | ||
.build(BuildContext.root(savedInstanceState), appyxIntegrationPoint) | ||
.also { ribsNode = it } | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
.../interop-ribs/src/androidTest/kotlin/com/bumble/appyx/interop/ribs/InteropNodeImplTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.bumble.appyx.interop.ribs | ||
|
||
import androidx.compose.ui.test.junit4.createAndroidComposeRule | ||
import androidx.compose.ui.test.onNodeWithTag | ||
import androidx.test.espresso.Espresso | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class InteropNodeImplTest { | ||
|
||
@get:Rule | ||
val rule = createAndroidComposeRule(AppyxRibsInteropActivity::class.java) | ||
|
||
@Test | ||
fun appyx_back_press_is_handled_before_rib_parent() { | ||
// push a new child into the backstack | ||
var newChild = "" | ||
var oldChild = "" | ||
rule.activityRule.scenario.onActivity { | ||
oldChild = it.ribsNode.current() | ||
newChild = it.ribsNode.push() | ||
} | ||
|
||
Espresso.pressBack() | ||
|
||
// the back press is swollen by the child node, newChild is still displayed | ||
rule.onNodeWithTag(newChild).assertExists() | ||
|
||
Espresso.pressBack() | ||
|
||
// the back press is handled by RIBs now and the old child is displayed | ||
rule.onNodeWithTag(oldChild).assertExists() | ||
} | ||
|
||
} |
112 changes: 112 additions & 0 deletions
112
libraries/interop-ribs/src/androidTest/kotlin/com/bumble/appyx/interop/ribs/RibsNode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.bumble.appyx.interop.ribs | ||
|
||
import android.os.Parcelable | ||
import android.view.ViewGroup | ||
import android.widget.FrameLayout | ||
import androidx.activity.compose.BackHandler | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.testTag | ||
import androidx.lifecycle.Lifecycle | ||
import com.badoo.ribs.builder.Builder | ||
import com.badoo.ribs.core.Node | ||
import com.badoo.ribs.core.modality.BuildParams | ||
import com.badoo.ribs.core.plugin.Plugin | ||
import com.badoo.ribs.core.view.AndroidRibView2 | ||
import com.badoo.ribs.core.view.ViewFactory | ||
import com.badoo.ribs.routing.Routing | ||
import com.badoo.ribs.routing.resolution.ChildResolution | ||
import com.badoo.ribs.routing.resolution.Resolution | ||
import com.badoo.ribs.routing.router.Router | ||
import com.badoo.ribs.routing.source.backstack.BackStack | ||
import com.badoo.ribs.routing.source.backstack.operation.push | ||
import com.bumble.appyx.core.integrationpoint.IntegrationPoint | ||
import com.bumble.appyx.core.modality.BuildContext | ||
import kotlinx.parcelize.Parcelize | ||
import java.util.UUID | ||
|
||
class RibsNode( | ||
buildParams: BuildParams<*>, | ||
private val backStack: BackStack<RibsNodeRouter.Configuration>, | ||
plugins: List<Plugin>, | ||
) : Node<RibsNode.View>( | ||
buildParams = buildParams, | ||
viewFactory = View.Factory(), | ||
plugins = plugins, | ||
) { | ||
|
||
fun current(): String = | ||
backStack.activeConfiguration.id | ||
|
||
fun push(): String { | ||
val id = UUID.randomUUID().toString() | ||
backStack.push(RibsNodeRouter.Configuration(id)) | ||
return id | ||
} | ||
|
||
class View( | ||
androidView: ViewGroup, | ||
lifecycle: Lifecycle, | ||
) : AndroidRibView2( | ||
androidView, | ||
lifecycle | ||
) { | ||
class Factory : ViewFactory<View> { | ||
override fun invoke(context: ViewFactory.Context): View = | ||
View(FrameLayout(context.parent.context), context.lifecycle) | ||
} | ||
} | ||
} | ||
|
||
class RibsNodeBuilder : Builder<IntegrationPoint, RibsNode>() { | ||
override fun build(buildParams: BuildParams<IntegrationPoint>): RibsNode { | ||
val backStack = BackStack( | ||
RibsNodeRouter.Configuration(UUID.randomUUID().toString()), | ||
buildParams | ||
) | ||
val router = RibsNodeRouter(buildParams, buildParams.payload, backStack) | ||
return RibsNode(buildParams, backStack, listOf(router)) | ||
} | ||
} | ||
|
||
class RibsNodeRouter( | ||
buildParams: BuildParams<*>, | ||
private val integrationPoint: IntegrationPoint, | ||
backStack: BackStack<Configuration>, | ||
) : Router<RibsNodeRouter.Configuration>( | ||
buildParams = buildParams, | ||
routingSource = backStack | ||
) { | ||
@Parcelize | ||
data class Configuration(val id: String) : Parcelable | ||
|
||
override fun resolve(routing: Routing<Configuration>): Resolution = | ||
ChildResolution.child { | ||
InteropBuilder( | ||
nodeFactory = { buildContext -> AppyxNode(buildContext, routing.configuration.id) }, | ||
integrationPoint = integrationPoint, | ||
).build(it) | ||
} | ||
} | ||
|
||
class AppyxNode( | ||
buildContext: BuildContext, | ||
private val s: String, | ||
) : com.bumble.appyx.core.node.Node( | ||
buildContext, | ||
) { | ||
var shouldInterceptBackPress by mutableStateOf(true) | ||
|
||
@Composable | ||
override fun View(modifier: Modifier) { | ||
Box(modifier = modifier.testTag(s)) { | ||
BackHandler(shouldInterceptBackPress) { | ||
shouldInterceptBackPress = false | ||
} | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ies/interop-ribs/src/main/kotlin/com/bumble/appyx/interop/ribs/InteropBackPressHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.bumble.appyx.interop.ribs | ||
|
||
import androidx.activity.OnBackPressedDispatcher | ||
import androidx.activity.OnBackPressedDispatcherOwner | ||
import androidx.lifecycle.Lifecycle | ||
import com.badoo.ribs.core.plugin.BackPressHandler | ||
import com.badoo.ribs.core.plugin.NodeLifecycleAware | ||
|
||
/** | ||
* When we put Appyx into RIBs, we have the following invocation order of back handlers: | ||
* 1. All active RIBs handlers using RIBs back press API. | ||
* 2. All active Appyx handlers using AndroidX back press API. | ||
* | ||
* The reason is RIBs integration point implementation | ||
* where `super<Activity>.onBackPressed()` is invoked after `Node.handleBackPress()`. | ||
* | ||
* Let's collect all Appyx back press handlers into a separate [OnBackPressedDispatcher] | ||
* and use it for `handleBackPress()`. | ||
*/ | ||
internal class InteropBackPressHandler : | ||
BackPressHandler, | ||
OnBackPressedDispatcherOwner, | ||
NodeLifecycleAware { | ||
|
||
private val dispatcher = OnBackPressedDispatcher() | ||
private lateinit var lifecycle: Lifecycle | ||
|
||
override fun onCreate(nodeLifecycle: Lifecycle) { | ||
lifecycle = nodeLifecycle | ||
} | ||
|
||
override fun handleBackPress(): Boolean = | ||
if (dispatcher.hasEnabledCallbacks()) { | ||
dispatcher.onBackPressed() | ||
true | ||
} else { | ||
false | ||
} | ||
|
||
override fun getLifecycle(): Lifecycle = lifecycle | ||
|
||
override fun getOnBackPressedDispatcher(): OnBackPressedDispatcher = | ||
dispatcher | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters