Skip to content

Commit 3678f62

Browse files
committed
Brings back View.environment, adds View.getScreen.
I'd like to bring back the ability to peek at a View and see if/how workflow-ui created it, even though we no longer need that ability at runtime. I don't see how we can preserve our Radiography support without it, and it's also handy for espresso testing (#741).
1 parent 12928cc commit 3678f62

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ public final class com/squareup/workflow1/ui/ViewShowRenderingKt {
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;
279279
public static final synthetic fun getRendering (Landroid/view/View;)Ljava/lang/Object;
280+
public static final fun getScreen (Landroid/view/View;)Lcom/squareup/workflow1/ui/Screen;
280281
public static final fun getShowRendering (Landroid/view/View;)Lkotlin/jvm/functions/Function2;
281282
public static final fun getStarter (Landroid/view/View;)Lkotlin/jvm/functions/Function1;
282283
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: 13 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,7 @@ 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("Replaced by View.getScreen()")
122123
public inline fun <reified RenderingT : Any> View.getRendering(): RenderingT? {
123124
// Can't use a val because of the parameter type.
124125
return when (val showing = workflowViewStateOrNull?.showing) {
@@ -127,14 +128,23 @@ public inline fun <reified RenderingT : Any> View.getRendering(): RenderingT? {
127128
}
128129
}
129130

131+
/**
132+
* Returns the most recent [Screen] rendering [shown][ScreenViewHolder.show] in this view,
133+
* or `null` if the receiver was not created via [ScreenViewFactory.startShowing].
134+
*/
135+
@WorkflowUiExperimentalApi
136+
public fun View.getScreen(): Screen? {
137+
return environment?.get(Showing)
138+
}
139+
130140
/**
131141
* Returns the most recent [ViewEnvironment] applied to this view, or null if [bindShowRendering]
132142
* has never been called.
133143
*/
134144
@WorkflowUiExperimentalApi
135-
@Deprecated("Replaced by ScreenViewHolder.environment")
136145
public val View.environment: ViewEnvironment?
137146
get() = workflowViewStateOrNull?.environment
147+
?: getTag(R.id.workflow_environment) as? ViewEnvironment
138148

139149
/**
140150
* 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.getScreen
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 [getScreen].
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.getScreen() ?: return
3031
renderRendering(rendering)
3132
}
3233

0 commit comments

Comments
 (0)