Skip to content

Commit dd08b41

Browse files
committed
Brings back the convenience overload of toUnwrappingViewFactory.
Turns out there are still a decent number of use cases for this.
1 parent 2d36ba1 commit dd08b41

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ public final class com/squareup/workflow1/ui/ScreenViewFactoryFinder$DefaultImpl
184184
public final class com/squareup/workflow1/ui/ScreenViewFactoryKt {
185185
public static final fun startShowing (Lcom/squareup/workflow1/ui/ScreenViewFactory;Lcom/squareup/workflow1/ui/Screen;Lcom/squareup/workflow1/ui/ViewEnvironment;Landroid/content/Context;Landroid/view/ViewGroup;Lcom/squareup/workflow1/ui/ViewStarter;)Lcom/squareup/workflow1/ui/ScreenViewHolder;
186186
public static synthetic fun startShowing$default (Lcom/squareup/workflow1/ui/ScreenViewFactory;Lcom/squareup/workflow1/ui/Screen;Lcom/squareup/workflow1/ui/ViewEnvironment;Landroid/content/Context;Landroid/view/ViewGroup;Lcom/squareup/workflow1/ui/ViewStarter;ILjava/lang/Object;)Lcom/squareup/workflow1/ui/ScreenViewHolder;
187+
public static final synthetic fun toUnwrappingViewFactory (Lcom/squareup/workflow1/ui/ScreenViewFactory;Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/ScreenViewFactory;
187188
public static final synthetic fun toUnwrappingViewFactory (Lcom/squareup/workflow1/ui/ScreenViewFactory;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function4;)Lcom/squareup/workflow1/ui/ScreenViewFactory;
188189
public static final fun toViewFactory (Lcom/squareup/workflow1/ui/Screen;Lcom/squareup/workflow1/ui/ViewEnvironment;)Lcom/squareup/workflow1/ui/ScreenViewFactory;
189190
public static final fun viewBindingLayoutInflater (Landroid/content/Context;Landroid/view/ViewGroup;)Landroid/view/LayoutInflater;

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,25 @@ public fun interface ViewStarter {
377377
* Transforms a [ScreenViewFactory] of [WrappedT] into one that can handle
378378
* instances of [WrapperT].
379379
*
380-
* It is usually more convenient to call [ScreenViewFactory.forWrapper].
380+
* @see [ScreenViewFactory.forWrapper].
381+
*/
382+
@WorkflowUiExperimentalApi
383+
public inline fun <
384+
reified WrapperT : Screen,
385+
WrappedT : Screen
386+
> ScreenViewFactory<WrappedT>.toUnwrappingViewFactory(
387+
crossinline unwrap: (wrapperScreen: WrapperT) -> WrappedT
388+
): ScreenViewFactory<WrapperT> {
389+
return toUnwrappingViewFactory(unwrap) { _, wrapperScreen, environment, showUnwrappedScreen ->
390+
showUnwrappedScreen(unwrap(wrapperScreen), environment)
391+
}
392+
}
393+
394+
/**
395+
* Transforms a [ScreenViewFactory] of [WrappedT] into one that can handle
396+
* instances of [WrapperT].
397+
*
398+
* @see [ScreenViewFactory.forWrapper].
381399
*/
382400
@WorkflowUiExperimentalApi
383401
public inline fun <

workflow-ui/core-android/src/test/java/com/squareup/workflow1/ui/ScreenViewFactoryTest.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package com.squareup.workflow1.ui
55
import android.content.Context
66
import android.view.ViewGroup
77
import com.google.common.truth.Truth.assertThat
8+
import com.squareup.workflow1.ui.ScreenViewFactory.Companion.fromCode
89
import com.squareup.workflow1.ui.ViewRegistry.Entry
910
import org.junit.Test
1011
import org.mockito.kotlin.mock
@@ -60,6 +61,27 @@ internal class ScreenViewFactoryTest {
6061
assertThat(overrideViewRenderingFactory.updated).isTrue()
6162
}
6263

64+
@Test fun `convenience wrapper is convenient`() {
65+
val env = ViewEnvironment.EMPTY + ViewRegistry()
66+
val screen = MyWrapper(MyAndroidScreen())
67+
68+
screen.toViewFactory(env).startShowing(screen, env, mock())
69+
assertThat(screen.wrapped.viewFactory.built).isTrue()
70+
assertThat(screen.wrapped.viewFactory.updated).isTrue()
71+
}
72+
73+
@OptIn(WorkflowUiExperimentalApi::class)
74+
private class MyWrapper(
75+
val wrapped: MyAndroidScreen
76+
) : AndroidScreen<MyWrapper> {
77+
override val viewFactory =
78+
fromCode<MyWrapper> { initialScreen, initialEnvironment, _, _ ->
79+
wrapped.viewFactory.toUnwrappingViewFactory<MyWrapper, MyAndroidScreen>(
80+
unwrap = { wrapped }
81+
).startShowing(initialScreen, initialEnvironment, mock())
82+
}
83+
}
84+
6385
private class TestViewFactory<T : Screen>(
6486
override val type: KClass<in T>
6587
) : ScreenViewFactory<T> {

0 commit comments

Comments
 (0)