Skip to content

Commit

Permalink
Add a Config.extension(block)
Browse files Browse the repository at this point in the history
Allows for extension code to optionally add an extension
if not present. This might be needed in some cases
where a feature (like a DSL that help define row types)
need to access a common-shared object associated to the
recycler.
  • Loading branch information
Helios Alonso Cabanillas committed Apr 20, 2021
1 parent 591ab4f commit 5886c3d
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/src/main/java/com/squareup/cycler/Recycler.kt
Expand Up @@ -261,7 +261,7 @@ class Recycler<I : Any> internal constructor(

@PublishedApi internal val rowSpecs = mutableListOf<RowSpec<I, *, *>>()
@PublishedApi internal val extraItemSpecs = mutableListOf<RowSpec<Any, *, *>>()
internal val extensionSpecs = mutableListOf<ExtensionSpec<I>>()
@PublishedApi internal val extensionSpecs = mutableListOf<ExtensionSpec<I>>()

internal var hasStableIdProvider = false
internal var stableIdProvider: StableIdProvider<I> = {
Expand Down Expand Up @@ -380,13 +380,27 @@ class Recycler<I : Any> internal constructor(
}

/**
* General method which is called from Extension code. DON'T USE DIRECTLY.
* General method which is called from extensions' code.
* Adds an [ExtensionSpec] to the [Config].
* @hide
*/
fun extension(spec: ExtensionSpec<I>) {
extensionSpecs.add(spec)
}

/**
* General method which is called from extensions' code.
* Returns the [ExtensionSpec] of type [E]. If not present [block] will be
* used to create it and add it.
* @hide
*/
inline fun <reified E : ExtensionSpec<I>> extension(block: () -> E): E {
return extensionSpecs
.filterIsInstance<E>()
.firstOrNull()
?: block().also(extensionSpecs::add)
}

/**
* Public method only for inlining, don't use directly. Use [Recycler.create] instead.
* @hide
Expand Down

0 comments on commit 5886c3d

Please sign in to comment.