Skip to content

Commit

Permalink
Extracts ScreenViewFactory.map from ScreenViewFactory.forWrapper
Browse files Browse the repository at this point in the history
Coupling `Wrapper` to `ScreenViewFactory.forWrapper` was a mistake,
made simple transformations that weren't in the strict `Wrapper`
shape more difficult.

Also some compulsive tidying of sample code.

Fixes #973
  • Loading branch information
rjrjr committed Mar 21, 2023
1 parent 93f4bac commit 97e44e3
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,3 @@ fun MayBeLoadingScreen(
loaders.map { FullScreenOverlay(it) }
)
}

@OptIn(WorkflowUiExperimentalApi::class)
val MayBeLoadingScreen.baseScreen: OverviewDetailScreen
get() = body.content

@OptIn(WorkflowUiExperimentalApi::class)
val MayBeLoadingScreen.loaders: List<LoaderSpinner>
get() = overlays.map { it.content }
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.squareup.sample.container.panel

import com.squareup.workflow1.ui.Compatible
import com.squareup.workflow1.ui.Compatible.Companion.keyFor
import com.squareup.workflow1.ui.Screen
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.Wrapper

/**
* Show a scrim over some [content], which is invisible if [dimmed] is false,
* visible if it is true.
*/
@OptIn(WorkflowUiExperimentalApi::class)
class ScrimScreen<T : Screen>(
val content: T,
class ScrimScreen<C : Screen>(
override val content: C,
val dimmed: Boolean
) : Screen, Compatible {
override val compatibilityKey = keyFor(content, "ScrimScreen")
) : Wrapper<Screen, C>, Screen {
override fun <D : Screen> map(transform: (C) -> D) = ScrimScreen(transform(content), dimmed)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.internal.test.IdlingDispatcherRule
import leakcanary.DetectLeaksAfterTestSuccess
import org.junit.Rule
Expand All @@ -19,7 +18,6 @@ import org.junit.rules.RuleChain
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@OptIn(WorkflowUiExperimentalApi::class)
class HelloBackButtonEspressoTest {

private val scenarioRule = ActivityScenarioRule(HelloBackButtonActivity::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package com.squareup.sample.hellobackbutton

import android.os.Parcelable
import com.squareup.sample.hellobackbutton.AreYouSureWorkflow.Finished
import com.squareup.sample.hellobackbutton.AreYouSureWorkflow.Rendering
import com.squareup.sample.hellobackbutton.AreYouSureWorkflow.State
import com.squareup.sample.hellobackbutton.AreYouSureWorkflow.State.Quitting
import com.squareup.sample.hellobackbutton.AreYouSureWorkflow.State.Running
import com.squareup.workflow1.Snapshot
import com.squareup.workflow1.StatefulWorkflow
import com.squareup.workflow1.WorkflowAction.Companion.noAction
import com.squareup.workflow1.action
import com.squareup.workflow1.ui.AndroidScreen
import com.squareup.workflow1.ui.Screen
import com.squareup.workflow1.ui.ScreenViewFactory
import com.squareup.workflow1.ui.ScreenViewFactory.Companion.map
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.container.AlertOverlay
import com.squareup.workflow1.ui.container.AlertOverlay.Button.NEGATIVE
Expand All @@ -27,12 +32,21 @@ import kotlinx.parcelize.Parcelize
*/
@OptIn(WorkflowUiExperimentalApi::class)
object AreYouSureWorkflow :
StatefulWorkflow<Unit, State, Finished, BodyAndOverlaysScreen<*, AlertOverlay>>() {
StatefulWorkflow<Unit, State, Finished, Rendering>() {
override fun initialState(
props: Unit,
snapshot: Snapshot?
): State = snapshot?.toParcelable() ?: Running

class Rendering(
val base: Screen,
val alert: AlertOverlay? = null
) : AndroidScreen<Rendering> {
override val viewFactory: ScreenViewFactory<Rendering> = map { newRendering ->
BodyAndOverlaysScreen(newRendering.base, listOfNotNull(newRendering.alert))
}
}

@Parcelize
enum class State : Parcelable {
Running,
Expand All @@ -45,12 +59,12 @@ object AreYouSureWorkflow :
renderProps: Unit,
renderState: State,
context: RenderContext
): BodyAndOverlaysScreen<*, AlertOverlay> {
): Rendering {
val ableBakerCharlie = context.renderChild(HelloBackButtonWorkflow, Unit) { noAction() }

return when (renderState) {
Running -> {
BodyAndOverlaysScreen(
Rendering(
BackButtonScreen(ableBakerCharlie) {
// While we always provide a back button handler, by default the view code
// associated with BackButtonScreen ignores ours if the view created for the
Expand Down Expand Up @@ -80,7 +94,7 @@ object AreYouSureWorkflow :
}
)

BodyAndOverlaysScreen(ableBakerCharlie, alert)
Rendering(ableBakerCharlie, alert)
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@file:Suppress("ktlint:filename")

package com.squareup.sample.hellobackbutton

import com.squareup.sample.hellobackbutton.databinding.HelloBackButtonLayoutBinding
import com.squareup.workflow1.ui.AndroidScreen
import com.squareup.workflow1.ui.ScreenViewFactory
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.backPressedHandler

@OptIn(WorkflowUiExperimentalApi::class)
data class HelloBackButtonScreen(
val message: String,
val onClick: () -> Unit,
val onBackPressed: (() -> Unit)?
) : AndroidScreen<HelloBackButtonScreen> {
override val viewFactory: ScreenViewFactory<HelloBackButtonScreen> =
ScreenViewFactory.fromViewBinding(HelloBackButtonLayoutBinding::inflate) { rendering, _ ->
helloMessage.text = rendering.message
helloMessage.setOnClickListener { rendering.onClick() }
helloMessage.backPressedHandler = rendering.onBackPressed
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ import com.squareup.workflow1.ui.toParcelable
import com.squareup.workflow1.ui.toSnapshot
import kotlinx.parcelize.Parcelize

object HelloBackButtonWorkflow : StatefulWorkflow<
Unit,
State,
Nothing,
HelloBackButtonScreen
>() {
object HelloBackButtonWorkflow : StatefulWorkflow<Unit, State, Nothing, HelloBackButtonScreen>() {

@Parcelize
enum class State : Parcelable {
Expand Down
14 changes: 11 additions & 3 deletions workflow-ui/core-android/api/core-android.api
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,17 @@ public final class com/squareup/workflow1/ui/ScreenViewFactoryKt {
}

public abstract interface class com/squareup/workflow1/ui/ScreenViewHolder {
public static final field Companion Lcom/squareup/workflow1/ui/ScreenViewHolder$Companion;
public abstract fun getEnvironment ()Lcom/squareup/workflow1/ui/ViewEnvironment;
public abstract fun getRunner ()Lcom/squareup/workflow1/ui/ScreenViewRunner;
public abstract fun getView ()Landroid/view/View;
}

public final class com/squareup/workflow1/ui/ScreenViewHolder$Companion {
public final fun invoke (Lcom/squareup/workflow1/ui/ViewEnvironment;Landroid/view/View;Lcom/squareup/workflow1/ui/ScreenViewRunner;)Lcom/squareup/workflow1/ui/ScreenViewHolder;
}

public final class com/squareup/workflow1/ui/ScreenViewHolderKt {
public static final fun ScreenViewHolder (Lcom/squareup/workflow1/ui/ViewEnvironment;Landroid/view/View;Lcom/squareup/workflow1/ui/ScreenViewRunner;)Lcom/squareup/workflow1/ui/ScreenViewHolder;
public static final fun canShow (Lcom/squareup/workflow1/ui/ScreenViewHolder;Lcom/squareup/workflow1/ui/Screen;)Z
public static final fun getShowing (Lcom/squareup/workflow1/ui/ScreenViewHolder;)Lcom/squareup/workflow1/ui/Screen;
public static final fun show (Lcom/squareup/workflow1/ui/ScreenViewHolder;Lcom/squareup/workflow1/ui/Screen;Lcom/squareup/workflow1/ui/ViewEnvironment;)V
Expand Down Expand Up @@ -429,16 +433,20 @@ public final class com/squareup/workflow1/ui/container/OverlayDialogFactoryKt {
}

public abstract interface class com/squareup/workflow1/ui/container/OverlayDialogHolder {
public static final field Companion Lcom/squareup/workflow1/ui/container/OverlayDialogHolder$Companion;
public abstract fun getDialog ()Landroid/app/Dialog;
public abstract fun getEnvironment ()Lcom/squareup/workflow1/ui/ViewEnvironment;
public abstract fun getOnBackPressed ()Lkotlin/jvm/functions/Function0;
public abstract fun getOnUpdateBounds ()Lkotlin/jvm/functions/Function1;
public abstract fun getRunner ()Lkotlin/jvm/functions/Function2;
}

public final class com/squareup/workflow1/ui/container/OverlayDialogHolder$Companion {
public final fun invoke (Lcom/squareup/workflow1/ui/ViewEnvironment;Landroid/app/Dialog;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function2;)Lcom/squareup/workflow1/ui/container/OverlayDialogHolder;
public static synthetic fun invoke$default (Lcom/squareup/workflow1/ui/container/OverlayDialogHolder$Companion;Lcom/squareup/workflow1/ui/ViewEnvironment;Landroid/app/Dialog;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lcom/squareup/workflow1/ui/container/OverlayDialogHolder;
}

public final class com/squareup/workflow1/ui/container/OverlayDialogHolderKt {
public static final fun OverlayDialogHolder (Lcom/squareup/workflow1/ui/ViewEnvironment;Landroid/app/Dialog;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function2;)Lcom/squareup/workflow1/ui/container/OverlayDialogHolder;
public static synthetic fun OverlayDialogHolder$default (Lcom/squareup/workflow1/ui/ViewEnvironment;Landroid/app/Dialog;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lcom/squareup/workflow1/ui/container/OverlayDialogHolder;
public static final fun canShow (Lcom/squareup/workflow1/ui/container/OverlayDialogHolder;Lcom/squareup/workflow1/ui/container/Overlay;)Z
public static final fun getShowing (Lcom/squareup/workflow1/ui/container/OverlayDialogHolder;)Lcom/squareup/workflow1/ui/container/Overlay;
public static final fun show (Lcom/squareup/workflow1/ui/container/OverlayDialogHolder;Lcom/squareup/workflow1/ui/container/Overlay;Lcom/squareup/workflow1/ui/ViewEnvironment;)V
Expand Down
Loading

0 comments on commit 97e44e3

Please sign in to comment.