-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a render binding function that works with AndroidX ViewBind…
…ings. Closes #984.
- Loading branch information
1 parent
864ad23
commit c15e266
Showing
9 changed files
with
121 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...in/workflow-ui/core-android/src/main/java/com/squareup/workflow/ui/AndroidXViewBinding.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.squareup.workflow.ui | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.viewbinding.ViewBinding | ||
|
||
typealias ViewBindingInflater<BindingT> = (LayoutInflater, ViewGroup?, Boolean) -> BindingT | ||
|
||
/** | ||
* Creates a [RenderBinding] that [inflates][bindingInflater] a [ViewBinding] ([BindingT]) to show | ||
* renderings of type [RenderingT], using [showRendering]. | ||
* | ||
* ``` | ||
* val HelloBinding: RenderBinding<Rendering> = | ||
* bindViewBinding(HelloGoodbyeLayoutBinding::inflate) { rendering, containerHints -> | ||
* helloMessage.text = rendering.message | ||
* helloMessage.setOnClickListener { rendering.onClick(Unit) } | ||
* } | ||
* ``` | ||
* | ||
* If you need to initialize your view before [showRendering] is called, create a [LayoutRunner] | ||
* and create a binding using `LayoutRunner.bind` instead. | ||
*/ | ||
inline fun <BindingT : ViewBinding, reified RenderingT : Any> LayoutRunner.Companion.bind( | ||
noinline bindingInflater: ViewBindingInflater<BindingT>, | ||
crossinline showRendering: BindingT.(RenderingT, ContainerHints) -> Unit | ||
): RenderBinding<RenderingT> = bind(bindingInflater) { binding -> | ||
object : LayoutRunner<RenderingT> { | ||
override fun showRendering( | ||
rendering: RenderingT, | ||
containerHints: ContainerHints | ||
) = binding.showRendering(rendering, containerHints) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters