Skip to content

Commit

Permalink
Merge pull request #214 from zsoltk/plugin-utils
Browse files Browse the repository at this point in the history
Plugin utils
  • Loading branch information
zsoltk authored Sep 2, 2020
2 parents eb786e8 + f62e2f0 commit 57901e2
Show file tree
Hide file tree
Showing 27 changed files with 635 additions and 40 deletions.
2 changes: 1 addition & 1 deletion detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ naming:
active: true
PackageNaming:
active: true
packagePattern: '^[a-z]+(\.[a-z][a-z0-9]*)*$'
packagePattern: '^[a-z]+(\.[a-z][a-z0-9_]*)*$'
ClassNaming:
active: true
classPattern: '[A-Z$][a-zA-Z$0-9]*'
Expand Down
2 changes: 2 additions & 0 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def versions = [
androidxTestUiAutoVersion:'2.2.0',
retrofitVersion: '2.8.1',
okhttp: '3.14.7',
leakCanary: '2.4'
]

def apt = [
Expand Down Expand Up @@ -114,6 +115,7 @@ def external = [
apacheCommons: 'org.apache.commons:commons-lang3:3.0',
gson: 'com.google.code.gson:gson:2.8.5',
coil: 'io.coil-kt:coil:0.11.0',
leakCanary: "com.squareup.leakcanary:leakcanary-android:${versions.leakCanary}"
]

def retrofit = [
Expand Down
10 changes: 6 additions & 4 deletions libraries/rib-base/src/main/java/com/badoo/ribs/core/Node.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import com.badoo.ribs.util.RIBs
open class Node<V : RibView>(
val buildParams: BuildParams<*>,
private val viewFactory: ((RibView) -> V?)?, // TODO V? vs V
private val plugins: List<Plugin> = emptyList()
plugins: List<Plugin> = emptyList()
) : Rib, LifecycleOwner {

companion object {
Expand Down Expand Up @@ -81,6 +81,8 @@ open class Node<V : RibView>(
is AncestryInfo.Child -> ancestryInfo.anchor
}

val plugins: List<Plugin> = buildContext.defaultPlugins(this) + plugins

internal open val activationMode: ActivationMode =
buildContext.activationMode

Expand All @@ -107,11 +109,11 @@ open class Node<V : RibView>(

private var isPendingViewDetach: Boolean = false
private var isPendingDetach: Boolean = false
private val isActive: Boolean
val isActive: Boolean
get() = isAttachedToView && !isPendingViewDetach && !isPendingDetach

init {
plugins.filterIsInstance<NodeAware>().forEach { it.init(this) }
this.plugins.filterIsInstance<NodeAware>().forEach { it.init(this) }
}

internal fun onBuild() {
Expand Down Expand Up @@ -244,7 +246,7 @@ open class Node<V : RibView>(
}

private fun detachChildView(child: Node<*>, notifyPlugins: Boolean) {
if (isAttachedToView) {
if (isAttachedToView && child.isAttachedToView) {
view?.let { it.detachChild(child) }
?: parent?.detachChildView(child, false)
?: rootHost!!.detachChild(child)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.badoo.ribs.core.customisation

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import com.badoo.ribs.core.view.RibView

fun <T> RibView.inflate(@LayoutRes layoutResourceId: Int): T =
androidView.inflate(layoutResourceId)

fun <T> ViewGroup.inflate(@LayoutRes layoutResourceId: Int): T =
LayoutInflater
.from(androidView.context)
.from(context)
.inflate(
layoutResourceId,
androidView,
this,
false
) as T
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package com.badoo.ribs.core.modality

import android.os.Bundle
import com.badoo.ribs.core.Node
import com.badoo.ribs.core.customisation.RibCustomisationDirectory
import com.badoo.ribs.core.customisation.RibCustomisationDirectoryImpl
import com.badoo.ribs.core.plugin.Plugin
Expand All @@ -15,7 +16,7 @@ data class BuildContext(
val activationMode: ActivationMode = ActivationMode.ATTACH_TO_PARENT,
val savedInstanceState: Bundle?,
val customisations: RibCustomisationDirectory,
val plugins: List<Plugin> = emptyList()
val defaultPlugins: (Node<*>) -> List<Plugin> = { emptyList() }
) {
companion object {
/**
Expand All @@ -24,13 +25,13 @@ data class BuildContext(
fun root(
savedInstanceState: Bundle?,
customisations: RibCustomisationDirectory = RibCustomisationDirectoryImpl(),
rootPlugins: List<Plugin> = emptyList()
) =
defaultPlugins: (Node<*>) -> List<Plugin> = { emptyList() }
): BuildContext =
BuildContext(
ancestryInfo = AncestryInfo.Root,
savedInstanceState = savedInstanceState,
customisations = customisations,
plugins = rootPlugins
defaultPlugins = defaultPlugins
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ internal sealed class RoutingContext<C : Parcelable> {
savedInstanceState = null,
customisations = parentNode.buildContext.customisations.getSubDirectoryOrSelf(
parentNode::class
)
),
defaultPlugins = parentNode.buildContext.defaultPlugins
)

override fun withActivationState(activationState: ActivationState) =
Expand Down
4 changes: 3 additions & 1 deletion libraries/rib-base/src/main/java/com/badoo/ribs/util/RIBs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ object RIBs {

override fun handleNonFatalWarning(warningMessage: String, throwable: Throwable?) {}

override fun handleDebugMessage(format: String, vararg args: Any) {}
override fun handleDebugMessage(format: String, vararg args: Any) {
Log.d(TAG, format.format(args))
}
}
}
23 changes: 23 additions & 0 deletions libraries/rib-base/src/main/res/layout/debug_controls_host.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/debug_controls_host"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:textSize="18sp"
tools:text="Debug controls" />

<FrameLayout
android:id="@+id/children_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />

</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class NodePluginSubtreeViewChangeAwareTest : NodePluginTest() {

node.onCreateView(parentView)
node.onAttachToView()
childNode.onAttachToView()
node.detachChildView(childNode)

plugins.forEach {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.badoo.ribs.core.plugin.utils.debug

import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.badoo.mobile.rib.base.R
import com.badoo.ribs.core.Rib
import com.badoo.ribs.core.customisation.inflate
import com.badoo.ribs.core.plugin.NodeAware
import com.badoo.ribs.core.plugin.NodeAwareImpl
import com.badoo.ribs.core.plugin.RibAware
import com.badoo.ribs.core.plugin.RibAwareImpl
import com.badoo.ribs.core.plugin.ViewLifecycleAware

@SuppressWarnings("LongParameterList")
abstract class AbstractDebugControls<T : Rib> internal constructor(
private val viewFactory: ((ViewGroup) -> View)? = null,
private var viewGroupForChildren: (() -> ViewGroup)? = null,
private var growthDirection: GrowthDirection? = null,
private val nodeAware: NodeAware = NodeAwareImpl(),
private val ribAware: RibAware<T> = RibAwareImpl()
) : NodeAware by nodeAware,
RibAware<T> by ribAware,
ViewLifecycleAware {

abstract val label: String
private var container: ViewGroup? = null
private var target: ViewGroup? = null
protected var debugView: View? = null

final override fun onAttachToView() {
node.pluginUp<AbstractDebugControls<*>>()?.let { parent ->
if (viewGroupForChildren == null) viewGroupForChildren = parent.viewGroupForChildren
if (growthDirection == null) growthDirection = parent.growthDirection
}

target = viewGroupForChildren?.invoke()
target?.let { target ->
debugView = viewFactory?.invoke(target)
debugView?.let { debugView ->
container = target.inflate<ViewGroup>(R.layout.debug_controls_host).also {
it.findViewById<TextView>(R.id.label)?.text = label
it.findViewById<ViewGroup>(R.id.children_container).addView(debugView)
}

target.addView(container, if (growthDirection == GrowthDirection.TOP) 0 else -1)
onDebugViewCreated(debugView)
}
}
}

final override fun onDetachFromView() {
super.onDetachFromView()
if (target != null) {
debugView?.let {
target?.removeView(container)
onDebugViewDestroyed(it)
}
debugView = null
target = null
}
}

open fun onDebugViewCreated(debugView: View) {}

open fun onDebugViewDestroyed(debugView: View) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.badoo.ribs.core.plugin.utils.debug

import android.view.View
import android.view.ViewGroup
import com.badoo.ribs.core.Rib

abstract class DebugControls<T : Rib>(
override val label: String,
viewFactory: ((ViewGroup) -> View)
) : AbstractDebugControls<T>(
viewFactory = viewFactory
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.badoo.ribs.core.plugin.utils.debug

import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import com.badoo.mobile.rib.debugutils.R
import com.badoo.ribs.core.Node
import com.badoo.ribs.core.Rib
import com.badoo.ribs.core.customisation.inflate
import com.badoo.ribs.debug.TreePrinter

class DebugControlsHost(
viewGroupForChildren: (() -> ViewGroup),
growthDirection: GrowthDirection = GrowthDirection.TOP,
includeDefaultDebugTools: Boolean = true,
private val defaultTreePrinterFormat: (Node<*>) -> String = TreePrinter.FORMAT_SIMPLE_INSTANCE
) : AbstractDebugControls<Rib>(
viewFactory = if (includeDefaultDebugTools) defaultDebugTools else null,
viewGroupForChildren = viewGroupForChildren,
growthDirection = growthDirection
) {
companion object {
private val defaultDebugTools: ((ViewGroup) -> View) = { it.inflate(R.layout.debug_root) }
}

override val label: String = "Debug root"

override fun onDebugViewCreated(debugView: View) {
super.onDebugViewCreated(debugView)
debugView.findViewById<Button>(R.id.debug_tree).setOnClickListener {
TreePrinter.printNodeSubtree(node, defaultTreePrinterFormat)
Toast.makeText(debugView.context, "Check logs", Toast.LENGTH_SHORT).show()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.badoo.ribs.core.plugin.utils.debug

enum class GrowthDirection {
TOP, BOTTOM
}
Loading

0 comments on commit 57901e2

Please sign in to comment.