Skip to content

Commit

Permalink
Introduced global plugins, added GlobalNodeLifecycleAware
Browse files Browse the repository at this point in the history
  • Loading branch information
LachlanMcKee committed Nov 18, 2024
1 parent 7911ad9 commit 2c37b94
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions libraries/core/src/main/kotlin/com/bumble/appyx/Appyx.kt
Original file line number Diff line number Diff line change
@@ -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<Plugin> = emptyList()

fun reportException(exception: Exception) {
val handler = exceptionHandler
if (handler != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Plugin> = plugins + listOfNotNull(this as? Plugin)
val plugins: List<Plugin> = plugins + Appyx.globalPlugins + listOfNotNull(this as? Plugin)

val ancestryInfo: AncestryInfo =
buildContext.ancestryInfo
Expand Down Expand Up @@ -115,6 +116,7 @@ open class Node @VisibleForTesting internal constructor(
updateLifecycleState(Lifecycle.State.CREATED)
plugins<NodeReadyObserver<Node>>().forEach { it.init(this) }
plugins<NodeLifecycleAware>().forEach { it.onCreate(lifecycle) }
plugins<GlobalNodeLifecycleAware>().forEach { it.onCreate(this, lifecycle) }
}

@Composable
Expand Down Expand Up @@ -179,6 +181,7 @@ open class Node @VisibleForTesting internal constructor(
retainedInstanceStore.clearStore(id)
}
plugins<Destroyable>().forEach { it.destroy() }
plugins<GlobalNodeLifecycleAware>().forEach { it.onDestroy(this) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 2c37b94

Please sign in to comment.