Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions workflow-ui/core-android/api/core-android.api
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ public final class com/squareup/workflow1/ui/ScreenViewFactory$DefaultImpls {
public static synthetic fun buildView$default (Lcom/squareup/workflow1/ui/ScreenViewFactory;Lcom/squareup/workflow1/ui/Screen;Lcom/squareup/workflow1/ui/ViewEnvironment;Landroid/content/Context;Landroid/view/ViewGroup;ILjava/lang/Object;)Landroid/view/View;
}

public abstract interface class com/squareup/workflow1/ui/ScreenViewFactoryFinder {
public static final field Companion Lcom/squareup/workflow1/ui/ScreenViewFactoryFinder$Companion;
public abstract fun getViewFactoryForRendering (Lcom/squareup/workflow1/ui/ViewEnvironment;Lcom/squareup/workflow1/ui/Screen;)Lcom/squareup/workflow1/ui/ScreenViewFactory;
}

public final class com/squareup/workflow1/ui/ScreenViewFactoryFinder$Companion : com/squareup/workflow1/ui/ViewEnvironmentKey {
public fun getDefault ()Lcom/squareup/workflow1/ui/ScreenViewFactoryFinder;
public synthetic fun getDefault ()Ljava/lang/Object;
}

public final class com/squareup/workflow1/ui/ScreenViewFactoryFinder$DefaultImpls {
public static fun getViewFactoryForRendering (Lcom/squareup/workflow1/ui/ScreenViewFactoryFinder;Lcom/squareup/workflow1/ui/ViewEnvironment;Lcom/squareup/workflow1/ui/Screen;)Lcom/squareup/workflow1/ui/ScreenViewFactory;
}

public final class com/squareup/workflow1/ui/ScreenViewFactoryKt {
public static final fun buildView (Lcom/squareup/workflow1/ui/Screen;Lcom/squareup/workflow1/ui/ViewEnvironment;Landroid/content/Context;Landroid/view/ViewGroup;Lcom/squareup/workflow1/ui/ViewStarter;)Landroid/view/View;
public static synthetic fun buildView$default (Lcom/squareup/workflow1/ui/Screen;Lcom/squareup/workflow1/ui/ViewEnvironment;Landroid/content/Context;Landroid/view/ViewGroup;Lcom/squareup/workflow1/ui/ViewStarter;ILjava/lang/Object;)Landroid/view/View;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.view.ViewGroup
import com.squareup.workflow1.ui.container.BackStackScreen
import kotlin.reflect.KClass

@Deprecated("Use ViewEnvironment.getViewFactoryForRendering()")
@Deprecated("Use ScreenViewFactoryFinder.getViewFactoryForRendering()")
@WorkflowUiExperimentalApi
public fun <RenderingT : Any>
ViewRegistry.getFactoryForRendering(rendering: RenderingT): ViewFactory<RenderingT> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ package com.squareup.workflow1.ui
import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.squareup.workflow1.ui.container.BackStackScreen
import com.squareup.workflow1.ui.container.BackStackScreenViewFactory
import com.squareup.workflow1.ui.container.BodyAndModalsContainer
import com.squareup.workflow1.ui.container.BodyAndModalsScreen
import com.squareup.workflow1.ui.container.EnvironmentScreen
import com.squareup.workflow1.ui.container.EnvironmentScreenViewFactory

/**
* Factory for [View] instances that can show renderings of type [RenderingT] : [Screen].
Expand Down Expand Up @@ -63,7 +57,9 @@ public fun <ScreenT : Screen> ScreenT.buildView(
container: ViewGroup? = null,
viewStarter: ViewStarter? = null,
): View {
val viewFactory = viewEnvironment.getViewFactoryForRendering(this)
val viewFactory = viewEnvironment[ScreenViewFactoryFinder].getViewFactoryForRendering(
viewEnvironment, this
)

return viewFactory.buildView(this, viewEnvironment, contextForNewView, container).also { view ->
checkNotNull(view.workflowViewStateOrNull) {
Expand Down Expand Up @@ -97,28 +93,3 @@ public fun interface ViewStarter {
doStart: () -> Unit
)
}

@WorkflowUiExperimentalApi
internal fun <ScreenT : Screen>
ViewEnvironment.getViewFactoryForRendering(rendering: ScreenT): ScreenViewFactory<ScreenT> {
val entry = get(ViewRegistry).getEntryFor(rendering::class)

@Suppress("UNCHECKED_CAST", "DEPRECATION")
return (entry as? ScreenViewFactory<ScreenT>)
?: (rendering as? AndroidScreen<*>)?.viewFactory as? ScreenViewFactory<ScreenT>
?: (rendering as? AsScreen<*>)?.let { AsScreenViewFactory as ScreenViewFactory<ScreenT> }
?: (rendering as? BackStackScreen<*>)?.let {
BackStackScreenViewFactory as ScreenViewFactory<ScreenT>
}
?: (rendering as? BodyAndModalsScreen<*, *>)?.let {
BodyAndModalsContainer as ScreenViewFactory<ScreenT>
}
?: (rendering as? NamedScreen<*>)?.let { NamedScreenViewFactory as ScreenViewFactory<ScreenT> }
?: (rendering as? EnvironmentScreen<*>)?.let {
EnvironmentScreenViewFactory as ScreenViewFactory<ScreenT>
}
?: throw IllegalArgumentException(
"A ScreenViewFactory should have been registered to display $rendering, " +
"or that class should implement AndroidScreen. Instead found $entry."
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.squareup.workflow1.ui

import com.squareup.workflow1.ui.container.BackStackScreen
import com.squareup.workflow1.ui.container.BackStackScreenViewFactory
import com.squareup.workflow1.ui.container.BodyAndModalsContainer
import com.squareup.workflow1.ui.container.BodyAndModalsScreen
import com.squareup.workflow1.ui.container.EnvironmentScreen
import com.squareup.workflow1.ui.container.EnvironmentScreenViewFactory

/**
* [ViewEnvironment] service object used by [Screen.buildView] to find the right
* [ScreenViewFactory]. The default implementation makes [AndroidScreen] work
* and provides default bindings for [NamedScreen], [EnvironmentScreen], [BackStackScreen],
* etc.
*
* Here is how this hook could be used to provide a custom view to handle [BackStackScreen]:
*
* object MyViewFactory : ScreenViewFactory<BackStackScreen<*>>
* by ManualScreenViewFactory(
* type = BackStackScreen::class,
* viewConstructor = { initialRendering, initialEnv, context, _ ->
* MyBackStackContainer(context)
* .apply {
* layoutParams = (LayoutParams(MATCH_PARENT, MATCH_PARENT))
* bindShowRendering(initialRendering, initialEnv, ::update)
* }
* }
* )
*
* object MyFinder : ScreenViewFactoryFinder {
* @Suppress("UNCHECKED_CAST")
* if (rendering is BackStackScreen<*>)
* return MyViewFactory as ScreenViewFactory<ScreenT>
* return super.getViewFactoryForRendering(environment, rendering)
* }
*
* class MyViewModel(savedState: SavedStateHandle) : ViewModel() {
* val renderings: StateFlow<MyRootRendering> by lazy {
* val customized = ViewEnvironment() + (ScreenViewFactoryFinder to MyFinder)
* renderWorkflowIn(
* workflow = MyRootWorkflow.withEnvironment(customized),
* scope = viewModelScope,
* savedStateHandle = savedState
* )
* }
* }
*/

@WorkflowUiExperimentalApi
public interface ScreenViewFactoryFinder {
public fun <ScreenT : Screen> getViewFactoryForRendering(
environment: ViewEnvironment,
rendering: ScreenT
): ScreenViewFactory<ScreenT> {
val entry = environment[ViewRegistry].getEntryFor(rendering::class)

@Suppress("UNCHECKED_CAST", "DEPRECATION")
return (entry as? ScreenViewFactory<ScreenT>)
?: (rendering as? AndroidScreen<*>)?.viewFactory as? ScreenViewFactory<ScreenT>
?: (rendering as? AsScreen<*>)?.let { AsScreenViewFactory as ScreenViewFactory<ScreenT> }
?: (rendering as? BackStackScreen<*>)?.let {
BackStackScreenViewFactory as ScreenViewFactory<ScreenT>
}
?: (rendering as? BodyAndModalsScreen<*, *>)?.let {
BodyAndModalsContainer as ScreenViewFactory<ScreenT>
}
?: (rendering as? NamedScreen<*>)?.let {
NamedScreenViewFactory as ScreenViewFactory<ScreenT>
}
?: (rendering as? EnvironmentScreen<*>)?.let {
EnvironmentScreenViewFactory as ScreenViewFactory<ScreenT>
}
?: throw IllegalArgumentException(
"A ScreenViewFactory should have been registered to display $rendering, " +
"or that class should implement AndroidScreen. Instead found $entry."
)
}

public companion object : ViewEnvironmentKey<ScreenViewFactoryFinder>(
ScreenViewFactoryFinder::class
) {
override val default: ScreenViewFactoryFinder
get() = object : ScreenViewFactoryFinder {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal class ScreenViewFactoryTest {

return mock {
on {
getTag(eq(com.squareup.workflow1.ui.R.id.workflow_ui_view_state))
getTag(eq(R.id.workflow_ui_view_state))
} doReturn (WorkflowViewState.New(initialRendering, initialViewEnvironment, { _, _ -> }))
}
}
Expand Down