Skip to content

Commit

Permalink
Brings back View.environment, adds View.getScreen.
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
rjrjr committed Apr 26, 2022
1 parent b2d869d commit 609a3aa
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions workflow-ui/core-android/api/core-android.api
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ public final class com/squareup/workflow1/ui/ViewShowRenderingKt {
public static final fun canShowRendering (Landroid/view/View;Ljava/lang/Object;)Z
public static final fun getEnvironment (Landroid/view/View;)Lcom/squareup/workflow1/ui/ViewEnvironment;
public static final synthetic fun getRendering (Landroid/view/View;)Ljava/lang/Object;
public static final fun getScreen (Landroid/view/View;)Lcom/squareup/workflow1/ui/Screen;
public static final fun getShowRendering (Landroid/view/View;)Lkotlin/jvm/functions/Function2;
public static final fun getStarter (Landroid/view/View;)Lkotlin/jvm/functions/Function1;
public static final fun getStarterOrNull (Landroid/view/View;)Lkotlin/jvm/functions/Function1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal class RealScreenViewHolder<ScreenT : Screen>(
override val runner: ScreenViewRunner<ScreenT> =
ScreenViewRunner { newScreen, newEnvironment ->
_environment = newEnvironment
view.setTag(R.id.workflow_environment, newEnvironment)
viewRunner.showRendering(newScreen, newEnvironment)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.squareup.workflow1.ui

import android.view.View
import com.squareup.workflow1.ui.ScreenViewHolder.Companion.Showing
import com.squareup.workflow1.ui.WorkflowViewState.New
import com.squareup.workflow1.ui.WorkflowViewState.Started

Expand Down Expand Up @@ -59,7 +60,7 @@ public fun <RenderingT : Any> View.bindShowRendering(
* - It is an error to call [View.showRendering] without having called this method first.
*/
@WorkflowUiExperimentalApi
@Deprecated("Use ScreenViewFactory.start to create a ScreenViewHolder")
@Deprecated("Use ScreenViewFactory.startShowing to create a ScreenViewHolder")
public fun View.start() {
val current = workflowViewStateAsNew
workflowViewState = Started(current.showing, current.environment, current.showRendering)
Expand Down Expand Up @@ -118,7 +119,7 @@ public fun <RenderingT : Any> View.showRendering(
* @throws ClassCastException if the current rendering is not of type [RenderingT]
*/
@WorkflowUiExperimentalApi
@Deprecated("Replaced by ViewEnvironment[Screen]")
@Deprecated("Replaced by View.getScreen()")
public inline fun <reified RenderingT : Any> View.getRendering(): RenderingT? {
// Can't use a val because of the parameter type.
return when (val showing = workflowViewStateOrNull?.showing) {
Expand All @@ -127,14 +128,23 @@ public inline fun <reified RenderingT : Any> View.getRendering(): RenderingT? {
}
}

/**
* Returns the most recent [Screen] rendering [shown][ScreenViewHolder.show] in this view,
* or `null` if the receiver was not created via [ScreenViewFactory.startShowing].
*/
@WorkflowUiExperimentalApi
public fun View.getScreen(): Screen? {
return environment?.get(Showing)
}

/**
* Returns the most recent [ViewEnvironment] applied to this view, or null if [bindShowRendering]
* has never been called.
*/
@WorkflowUiExperimentalApi
@Deprecated("Replaced by ScreenViewHolder.environment")
public val View.environment: ViewEnvironment?
get() = workflowViewStateOrNull?.environment
?: getTag(R.id.workflow_environment) as? ViewEnvironment

/**
* Returns the function set by the most recent call to [bindShowRendering], or null
Expand Down
4 changes: 2 additions & 2 deletions workflow-ui/core-android/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
<item name="workflow_layout" type="id"/>
<!-- View Tag used by ModalScreenOverlayDialogFactory to find the content ScreenViewHolder. -->
<item name="workflow_modal_dialog_content" type="id"/>
<!-- View Tag used for ScreenViewRunner. -->
<item name="workflow_view_runner" type="id"/>
<!-- View Tag for the ViewEnvironment that last updated this view. -->
<item name="workflow_environment" type="id"/>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import com.squareup.workflow1.ui.Named
import com.squareup.workflow1.ui.NamedScreen
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.getRendering
import com.squareup.workflow1.ui.getScreen
import radiography.AttributeAppendable
import radiography.ScannableView
import radiography.ScannableView.AndroidView
import radiography.ViewStateRenderer
import radiography.ViewStateRenderers

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

override fun AttributeAppendable.render(view: ScannableView) {
val androidView = (view as? AndroidView)?.view ?: return
val rendering = androidView.getRendering<Any>() ?: return
val rendering = androidView.getRendering<Any>() ?: androidView.getScreen() ?: return
renderRendering(rendering)
}

Expand Down

0 comments on commit 609a3aa

Please sign in to comment.