From 2c37b940e231103957494aa48e2a7c87c7eeb799 Mon Sep 17 00:00:00 2001 From: Lachlan McKee Date: Mon, 18 Nov 2024 12:43:28 +0000 Subject: [PATCH] Introduced global plugins, added GlobalNodeLifecycleAware --- gradle.properties | 2 +- libraries/core/src/main/kotlin/com/bumble/appyx/Appyx.kt | 6 ++++++ .../core/src/main/kotlin/com/bumble/appyx/core/node/Node.kt | 5 ++++- .../src/main/kotlin/com/bumble/appyx/core/plugin/Plugins.kt | 5 +++++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index 924b999b2..d7a6d488d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,4 +3,4 @@ org.gradle.caching=true org.gradle.parallel=true android.useAndroidX=true kotlin.code.style=official -library.version=1.5.0 +library.version=1.5.1 diff --git a/libraries/core/src/main/kotlin/com/bumble/appyx/Appyx.kt b/libraries/core/src/main/kotlin/com/bumble/appyx/Appyx.kt index 51a57902e..3102d99a5 100644 --- a/libraries/core/src/main/kotlin/com/bumble/appyx/Appyx.kt +++ b/libraries/core/src/main/kotlin/com/bumble/appyx/Appyx.kt @@ -1,12 +1,18 @@ package com.bumble.appyx import com.bumble.appyx.core.children.ChildEntry +import com.bumble.appyx.core.plugin.Plugin object Appyx { var exceptionHandler: ((Exception) -> Unit)? = null var defaultChildKeepMode: ChildEntry.KeepMode = ChildEntry.KeepMode.KEEP + /** + * Plugins that are applied to all Nodes. + */ + var globalPlugins: List = emptyList() + fun reportException(exception: Exception) { val handler = exceptionHandler if (handler != null) { diff --git a/libraries/core/src/main/kotlin/com/bumble/appyx/core/node/Node.kt b/libraries/core/src/main/kotlin/com/bumble/appyx/core/node/Node.kt index 2ffcceef7..7c5b19e62 100644 --- a/libraries/core/src/main/kotlin/com/bumble/appyx/core/node/Node.kt +++ b/libraries/core/src/main/kotlin/com/bumble/appyx/core/node/Node.kt @@ -27,6 +27,7 @@ import com.bumble.appyx.core.modality.AncestryInfo import com.bumble.appyx.core.modality.BuildContext import com.bumble.appyx.core.plugin.BackPressHandler import com.bumble.appyx.core.plugin.Destroyable +import com.bumble.appyx.core.plugin.GlobalNodeLifecycleAware import com.bumble.appyx.core.plugin.NodeLifecycleAware import com.bumble.appyx.core.plugin.NodeReadyObserver import com.bumble.appyx.core.plugin.Plugin @@ -57,7 +58,7 @@ open class Node @VisibleForTesting internal constructor( @Suppress("LeakingThis") // Implemented in the same way as in androidx.Fragment private val nodeLifecycle = NodeLifecycleImpl(this) - val plugins: List = plugins + listOfNotNull(this as? Plugin) + val plugins: List = plugins + Appyx.globalPlugins + listOfNotNull(this as? Plugin) val ancestryInfo: AncestryInfo = buildContext.ancestryInfo @@ -115,6 +116,7 @@ open class Node @VisibleForTesting internal constructor( updateLifecycleState(Lifecycle.State.CREATED) plugins>().forEach { it.init(this) } plugins().forEach { it.onCreate(lifecycle) } + plugins().forEach { it.onCreate(this, lifecycle) } } @Composable @@ -179,6 +181,7 @@ open class Node @VisibleForTesting internal constructor( retainedInstanceStore.clearStore(id) } plugins().forEach { it.destroy() } + plugins().forEach { it.onDestroy(this) } } } diff --git a/libraries/core/src/main/kotlin/com/bumble/appyx/core/plugin/Plugins.kt b/libraries/core/src/main/kotlin/com/bumble/appyx/core/plugin/Plugins.kt index c4eaa4ed7..c0176bd26 100644 --- a/libraries/core/src/main/kotlin/com/bumble/appyx/core/plugin/Plugins.kt +++ b/libraries/core/src/main/kotlin/com/bumble/appyx/core/plugin/Plugins.kt @@ -23,6 +23,11 @@ interface NodeLifecycleAware : Plugin { fun onCreate(lifecycle: Lifecycle) {} } +interface GlobalNodeLifecycleAware : Plugin { + fun onCreate(node: Node, lifecycle: Lifecycle) {} + fun onDestroy(node: Node) {} +} + interface UpNavigationHandler : Plugin { fun handleUpNavigation(): Boolean = false }