This repository was archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Inline WorkflowViewStub logic in ViewFactory.showRendering to avoid unnecessary intermediate Views. #29
Merged
Merged
Inline WorkflowViewStub logic in ViewFactory.showRendering to avoid unnecessary intermediate Views. #29
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -22,24 +22,22 @@ import androidx.compose.Composable | |
import androidx.compose.CompositionReference | ||
import androidx.compose.Recomposer | ||
import androidx.compose.compositionReference | ||
import androidx.compose.currentComposer | ||
import androidx.ui.core.setContent | ||
import com.squareup.workflow.ui.ViewEnvironment | ||
import com.squareup.workflow.ui.ViewEnvironmentKey | ||
|
||
/** | ||
* Holds a [CompositionReference] and a [Recomposer] that can be used to [setContent] to create a | ||
* composition that is a child of another composition. Child compositions get ambients and other | ||
* compose context from their parent, which allows ambients provided around a [showRendering] call | ||
* to be read by nested [bindCompose] factories. | ||
* Holds a [CompositionReference] and that can be passed to [setOrSubcomposeContent] to create a | ||
* composition that is a child of another composition. Subcompositions get ambients and other | ||
* compose context from their parent, and propagate invalidations, which allows ambients provided | ||
* around a [showRendering] call to be read by nested Compose-based view factories. | ||
* | ||
* When [showRendering] is called, it will store an instance of this class in the [ViewEnvironment]. | ||
* [ComposeViewFactory] will then pull the continuation out of the environment and use it to link | ||
* its composition to the outer one. | ||
* [ComposeViewFactory] pulls the reference out of the environment and uses it to link its | ||
* composition to the outer one. | ||
*/ | ||
internal data class ParentComposition( | ||
val reference: CompositionReference? = null, | ||
val recomposer: Recomposer? = null | ||
internal class ParentComposition( | ||
var reference: CompositionReference? = null | ||
) { | ||
companion object : ViewEnvironmentKey<ParentComposition>(ParentComposition::class) { | ||
override val default: ParentComposition get() = ParentComposition() | ||
|
@@ -50,31 +48,29 @@ internal data class ParentComposition( | |
* Creates a [ParentComposition] from the current point in the composition and adds it to this | ||
* [ViewEnvironment]. | ||
*/ | ||
@Composable internal fun ViewEnvironment.withParentComposition(): ViewEnvironment { | ||
val compositionReference = ParentComposition( | ||
reference = compositionReference(), | ||
recomposer = currentComposer.recomposer | ||
) | ||
@Composable internal fun ViewEnvironment.withParentComposition( | ||
reference: CompositionReference = compositionReference() | ||
): ViewEnvironment { | ||
val compositionReference = ParentComposition(reference = reference) | ||
return this + (ParentComposition to compositionReference) | ||
} | ||
|
||
/** | ||
* Starts composing [content] into this [ViewGroup]. | ||
* | ||
* If there is a [ParentComposition] present in [initialViewEnvironment], it will start the | ||
* composition as a subcomposition of that continuation. | ||
* If [parentComposition] is not null, [content] will be installed as a _subcomposition_ of the | ||
* parent composition, meaning that it will propagate ambients and invalidation. | ||
* | ||
* This function corresponds to [withParentComposition]. | ||
*/ | ||
internal fun ViewGroup.setOrContinueContent( | ||
initialViewEnvironment: ViewEnvironment, | ||
internal fun ViewGroup.setOrSubcomposeContent( | ||
parentComposition: CompositionReference?, | ||
content: @Composable() () -> Unit | ||
) { | ||
val (compositionReference, recomposer) = initialViewEnvironment[ParentComposition] | ||
if (compositionReference != null && recomposer != null) { | ||
if (parentComposition != null) { | ||
// Somewhere above us in the workflow rendering tree, there's another bindCompose factory. | ||
// We need to link to its composition reference so we inherit its ambients. | ||
setContent(recomposer, compositionReference, content) | ||
setContent(Recomposer.current(), parentComposition, content) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Everything is just using the main thread Recomposer, no need to pass it around ourselves. |
||
} else { | ||
// This is the first bindCompose factory in the rendering tree, so it won't be a child | ||
// composition. | ||
|
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems more idiomatic to wrap the update itself in a framed, the Compose code does this internally a bunch.