Skip to content

Commit 73c2eee

Browse files
authored
Merge pull request #742 from square/ray/peekaboo
`View.environmentOrNull`, `View.screenOrNull`.
2 parents b2d869d + d06fd71 commit 73c2eee

File tree

6 files changed

+37
-9
lines changed

6 files changed

+37
-9
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android.useAndroidX=true
88
systemProp.org.gradle.internal.publish.checksums.insecure=true
99

1010
GROUP=com.squareup.workflow1
11-
VERSION_NAME=1.8.0-uiUpdate04-SNAPSHOT
11+
VERSION_NAME=1.8.0-uiUpdate05-SNAPSHOT
1212

1313
POM_DESCRIPTION=Square Workflow
1414

workflow-ui/core-android/api/core-android.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ public final class com/squareup/workflow1/ui/ViewShowRenderingKt {
276276
public static final fun bindShowRendering (Landroid/view/View;Ljava/lang/Object;Lcom/squareup/workflow1/ui/ViewEnvironment;Lkotlin/jvm/functions/Function2;)V
277277
public static final fun canShowRendering (Landroid/view/View;Ljava/lang/Object;)Z
278278
public static final fun getEnvironment (Landroid/view/View;)Lcom/squareup/workflow1/ui/ViewEnvironment;
279+
public static final fun getEnvironmentOrNull (Landroid/view/View;)Lcom/squareup/workflow1/ui/ViewEnvironment;
279280
public static final synthetic fun getRendering (Landroid/view/View;)Ljava/lang/Object;
281+
public static final fun getScreenOrNull (Landroid/view/View;)Lcom/squareup/workflow1/ui/Screen;
280282
public static final fun getShowRendering (Landroid/view/View;)Lkotlin/jvm/functions/Function2;
281283
public static final fun getStarter (Landroid/view/View;)Lkotlin/jvm/functions/Function1;
282284
public static final fun getStarterOrNull (Landroid/view/View;)Lkotlin/jvm/functions/Function1;

workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/RealScreenViewHolder.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal class RealScreenViewHolder<ScreenT : Screen>(
1515
override val runner: ScreenViewRunner<ScreenT> =
1616
ScreenViewRunner { newScreen, newEnvironment ->
1717
_environment = newEnvironment
18+
view.setTag(R.id.workflow_environment, newEnvironment)
1819
viewRunner.showRendering(newScreen, newEnvironment)
1920
}
2021
}

workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/ViewShowRendering.kt

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.squareup.workflow1.ui
22

33
import android.view.View
4+
import com.squareup.workflow1.ui.ScreenViewHolder.Companion.Showing
45
import com.squareup.workflow1.ui.WorkflowViewState.New
56
import com.squareup.workflow1.ui.WorkflowViewState.Started
67

@@ -59,7 +60,7 @@ public fun <RenderingT : Any> View.bindShowRendering(
5960
* - It is an error to call [View.showRendering] without having called this method first.
6061
*/
6162
@WorkflowUiExperimentalApi
62-
@Deprecated("Use ScreenViewFactory.start to create a ScreenViewHolder")
63+
@Deprecated("Use ScreenViewFactory.startShowing to create a ScreenViewHolder")
6364
public fun View.start() {
6465
val current = workflowViewStateAsNew
6566
workflowViewState = Started(current.showing, current.environment, current.showRendering)
@@ -118,7 +119,10 @@ public fun <RenderingT : Any> View.showRendering(
118119
* @throws ClassCastException if the current rendering is not of type [RenderingT]
119120
*/
120121
@WorkflowUiExperimentalApi
121-
@Deprecated("Replaced by ViewEnvironment[Screen]")
122+
@Deprecated(
123+
"Replaced by View.screenOrNull",
124+
ReplaceWith("screenOrNull", "com.squareup.workflow1.ui.screenOrNull")
125+
)
122126
public inline fun <reified RenderingT : Any> View.getRendering(): RenderingT? {
123127
// Can't use a val because of the parameter type.
124128
return when (val showing = workflowViewStateOrNull?.showing) {
@@ -127,14 +131,34 @@ public inline fun <reified RenderingT : Any> View.getRendering(): RenderingT? {
127131
}
128132
}
129133

134+
/**
135+
* Returns the most recent [Screen] rendering [shown][ScreenViewHolder.show] in this view,
136+
* or `null` if the receiver was not created via [ScreenViewFactory.startShowing].
137+
*/
138+
@WorkflowUiExperimentalApi
139+
public val View.screenOrNull: Screen?
140+
get() = environmentOrNull?.get(Showing)
141+
130142
/**
131143
* Returns the most recent [ViewEnvironment] applied to this view, or null if [bindShowRendering]
132144
* has never been called.
133145
*/
134146
@WorkflowUiExperimentalApi
135-
@Deprecated("Replaced by ScreenViewHolder.environment")
147+
@Deprecated(
148+
"Replaced by View.environmentOrNull",
149+
ReplaceWith("environmentOrNull", "com.squareup.workflow1.ui.environmentOrNull")
150+
)
136151
public val View.environment: ViewEnvironment?
152+
get() = environmentOrNull
153+
154+
/**
155+
* Returns the most recent [ViewEnvironment] applied to this view, or null if [bindShowRendering]
156+
* has never been called.
157+
*/
158+
@WorkflowUiExperimentalApi
159+
public val View.environmentOrNull: ViewEnvironment?
137160
get() = workflowViewStateOrNull?.environment
161+
?: getTag(R.id.workflow_environment) as? ViewEnvironment
138162

139163
/**
140164
* Returns the function set by the most recent call to [bindShowRendering], or null

workflow-ui/core-android/src/main/res/values/ids.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@
2525
<item name="workflow_layout" type="id"/>
2626
<!-- View Tag used by ModalScreenOverlayDialogFactory to find the content ScreenViewHolder. -->
2727
<item name="workflow_modal_dialog_content" type="id"/>
28-
<!-- View Tag used for ScreenViewRunner. -->
29-
<item name="workflow_view_runner" type="id"/>
28+
<!-- View Tag for the ViewEnvironment that last updated this view. -->
29+
<item name="workflow_environment" type="id"/>
3030
</resources>

workflow-ui/radiography/src/main/java/com/squareup/workflow1/ui/radiography/WorkflowViewRenderer.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import com.squareup.workflow1.ui.Named
77
import com.squareup.workflow1.ui.NamedScreen
88
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
99
import com.squareup.workflow1.ui.getRendering
10+
import com.squareup.workflow1.ui.screenOrNull
1011
import radiography.AttributeAppendable
1112
import radiography.ScannableView
1213
import radiography.ScannableView.AndroidView
1314
import radiography.ViewStateRenderer
1415
import radiography.ViewStateRenderers
1516

1617
/**
17-
* Renders information about views that were created by view factories, i.e. views with associated
18-
* rendering tags.
18+
* Renders information about views that were created by workflow-ui, i.e. views
19+
* that return non-null values from [getRendering] or [screenOrNull].
1920
*/
2021
@Suppress("unused")
2122
public val ViewStateRenderers.WorkflowViewRenderer: ViewStateRenderer
@@ -26,7 +27,7 @@ private object WorkflowViewRendererImpl : ViewStateRenderer {
2627

2728
override fun AttributeAppendable.render(view: ScannableView) {
2829
val androidView = (view as? AndroidView)?.view ?: return
29-
val rendering = androidView.getRendering<Any>() ?: return
30+
val rendering = androidView.getRendering<Any>() ?: androidView.screenOrNull ?: return
3031
renderRendering(rendering)
3132
}
3233

0 commit comments

Comments
 (0)