Skip to content

Commit

Permalink
Introduced global plugins, added GlobalNodeLifecycleAware and expose …
Browse files Browse the repository at this point in the history
…view
  • Loading branch information
LachlanMcKee committed Nov 18, 2024
1 parent 9a00ba8 commit 2df4805
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
org.gradle.caching=true
org.gradle.parallel=true

VERSION_NAME=0.40.2
VERSION_NAME=0.40.3

android.useAndroidX=true
android.defaults.buildfeatures.buildconfig=true
Expand Down
11 changes: 10 additions & 1 deletion libraries/rib-base/src/main/java/com/badoo/ribs/core/Node.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.badoo.ribs.core.modality.BuildContext
import com.badoo.ribs.core.modality.BuildParams
import com.badoo.ribs.core.plugin.AndroidLifecycleAware
import com.badoo.ribs.core.plugin.BackPressHandler
import com.badoo.ribs.core.plugin.GlobalNodeLifecycleAware
import com.badoo.ribs.core.plugin.NodeAware
import com.badoo.ribs.core.plugin.NodeLifecycleAware
import com.badoo.ribs.core.plugin.Plugin
Expand Down Expand Up @@ -100,7 +101,7 @@ open class Node<V : RibView> @VisibleForTesting internal constructor(
}

val plugins: List<Plugin> =
buildContext.defaultPlugins(this) + plugins + if (this is Plugin) listOf(this) else emptyList()
buildContext.defaultPlugins(this) + RIBs.globalPlugins + plugins + if (this is Plugin) listOf(this) else emptyList()

internal open val activationMode: ActivationMode =
buildContext.activationMode
Expand Down Expand Up @@ -136,6 +137,7 @@ open class Node<V : RibView> @VisibleForTesting internal constructor(

internal fun onBuildFinished() {
plugins.filterIsInstance<NodeLifecycleAware>().forEach { it.onBuild() }
plugins.filterIsInstance<GlobalNodeLifecycleAware>().forEach { it.onBuild(this) }
parent?.onChildBuilt(this)
}

Expand All @@ -148,6 +150,9 @@ open class Node<V : RibView> @VisibleForTesting internal constructor(
plugins
.filterIsInstance<NodeLifecycleAware>()
.forEach { it.onCreate(lifecycleManager.ribLifecycle.lifecycle) }
plugins
.filterIsInstance<GlobalNodeLifecycleAware>()
.forEach { it.onCreate(this, lifecycleManager.ribLifecycle.lifecycle) }
lifecycleManager.onCreate()
}

Expand Down Expand Up @@ -218,6 +223,7 @@ open class Node<V : RibView> @VisibleForTesting internal constructor(

lifecycleManager.onDestroy()
plugins.filterIsInstance<NodeLifecycleAware>().forEach { it.onDestroy() }
plugins.filterIsInstance<GlobalNodeLifecycleAware>().forEach { it.onDestroy(this) }
if (!isRecreating) {
retainedInstanceStore.removeAll(identifier)
}
Expand Down Expand Up @@ -247,6 +253,7 @@ open class Node<V : RibView> @VisibleForTesting internal constructor(

internal fun onAttachFinished() {
plugins.filterIsInstance<NodeLifecycleAware>().forEach { it.onAttach() }
plugins.filterIsInstance<GlobalNodeLifecycleAware>().forEach { it.onAttach(this) }
}

open fun onAttachChildNode(child: Node<*>) {
Expand Down Expand Up @@ -407,6 +414,8 @@ open class Node<V : RibView> @VisibleForTesting internal constructor(
override val lifecycle: Lifecycle
get() = lifecycleManager.lifecycle

fun getView() : V? = view

fun <P> plugins(pClass: Class<P>): List<P> =
plugins.filterIsInstance(pClass)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ interface NodeLifecycleAware : Plugin {
fun onDestroy() {}
}

interface GlobalNodeLifecycleAware : Plugin {
fun onBuild(node: Node<*>) {}

fun onCreate(node: Node<*>, nodeLifecycle: Lifecycle) {}

fun onAttach(node: Node<*>) {}

fun onDestroy(node: Node<*>) {}
}

interface ViewAware<V : RibView> : Plugin {
fun onViewCreated(view: V, viewLifecycle: Lifecycle) {}
}
Expand Down
6 changes: 6 additions & 0 deletions libraries/rib-base/src/main/java/com/badoo/ribs/util/RIBs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.badoo.ribs.util
import android.util.Log
import androidx.annotation.VisibleForTesting
import com.badoo.ribs.android.requestcode.RequestCodeBasedEventStream
import com.badoo.ribs.core.plugin.Plugin

object RIBs {

Expand All @@ -28,6 +29,11 @@ object RIBs {
}
}

/**
* Plugins that are applied to all Nodes.
*/
var globalPlugins: List<Plugin> = emptyList()

@VisibleForTesting
fun clearErrorHandler() {
_errorHandler = null
Expand Down

0 comments on commit 2df4805

Please sign in to comment.