-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces
fun Screen.deepMap(Screen): Screen
Introduces and uses `Screen`-specific subtypes of the `Container` and `Wrapper` interfaces. This allows the inroduction of `Screen.deepMap()`, which allows us to apply a transformation to the "real" `Screen`s collected in a `Container`, no matter how deeply wrapped they are.
- Loading branch information
Showing
13 changed files
with
156 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
workflow-ui/container-android/src/test/java/com/squareup/workflow1/ui/ScreenContainerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
@file:OptIn(WorkflowUiExperimentalApi::class) | ||
|
||
package com.squareup.workflow1.ui | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import com.squareup.workflow1.ui.container.BackStackScreen | ||
import com.squareup.workflow1.ui.container.EnvironmentScreen | ||
import com.squareup.workflow1.ui.container.withEnvironment | ||
import org.junit.Test | ||
|
||
internal class ScreenContainerTest { | ||
object MyScreen : Screen | ||
|
||
@Test | ||
fun deepMapRecurses() { | ||
val backStack = BackStackScreen(NamedScreen(MyScreen, "name")) | ||
@Suppress("UNCHECKED_CAST") | ||
val mappedBackStack = backStack | ||
.deepMap { it.withEnvironment() } as BackStackScreen<NamedScreen<EnvironmentScreen<MyScreen>>> | ||
|
||
assertThat(mappedBackStack.top.content.content).isSameInstanceAs(MyScreen) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/UiFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.squareup.workflow1.ui | ||
|
||
@WorkflowUiExperimentalApi | ||
public fun interface VisualFactory<ContextT, in RenderingT, VisualT> { | ||
/** | ||
* Given a ui model ([rendering]), creates a [VisualHolder] which pairs: | ||
* | ||
* - a native view system object of type [VisualT] -- a [visual][VisualHolder.visual] | ||
* - an [update function][VisualHolder.update] to apply [RenderingT] instances to | ||
* the new [VisualT] instance. | ||
* | ||
* This method must not call [VisualHolder.update], to ensure that callers have | ||
* complete control over the lifecycle of the new [VisualT]. | ||
* | ||
* @param getFactory can be used to make recursive calls to build VisualT | ||
* instances for sub-parts of [rendering] | ||
*/ | ||
public fun createOrNull( | ||
rendering: RenderingT, | ||
context: ContextT, | ||
environment: ViewEnvironment, | ||
getFactory: (ViewEnvironment) -> VisualFactory<ContextT, Any, VisualT> | ||
): VisualHolder<RenderingT, VisualT>? | ||
} |
24 changes: 24 additions & 0 deletions
24
workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/VisualHolder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.squareup.workflow1.ui | ||
|
||
@WorkflowUiExperimentalApi | ||
public interface VisualHolder<in RenderingT, out VisualT> { | ||
public val visual: VisualT | ||
|
||
public fun update(rendering: RenderingT): Boolean | ||
|
||
public companion object { | ||
public operator fun <RenderingT, VisualT> invoke( | ||
visual: VisualT, | ||
onUpdate: (RenderingT) -> Unit | ||
): VisualHolder<RenderingT, VisualT> { | ||
return object : VisualHolder<RenderingT, VisualT> { | ||
override val visual = visual | ||
|
||
override fun update(rendering: RenderingT): Boolean { | ||
onUpdate(rendering) | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/ScreenContainer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.squareup.workflow1.ui | ||
|
||
@WorkflowUiExperimentalApi | ||
public interface ScreenContainer<C : Screen> : Container<Screen, C>, Screen { | ||
public override fun <D : Screen> map(transform: (C) -> D): ScreenContainer<D> | ||
} | ||
|
||
@WorkflowUiExperimentalApi | ||
public interface ScreenWrapper<C : Screen> : ScreenContainer<C>, Wrapper<Screen, C>, Screen { | ||
public override fun <D : Screen> map(transform: (C) -> D): ScreenWrapper<D> | ||
} | ||
|
||
/** | ||
* Applies [transform] to the receiver unless it is a [ScreenContainer]. In that case, | ||
* makes a recursive call to [ScreenContainer.map]. | ||
* | ||
* For example, consider this snippet: | ||
* | ||
* val backStack = BackStackScreen(SomeWrapper(theRealScreen)) | ||
* val loggingBackStack = backStack.deepMap { WithLogging(it) } | ||
* | ||
* `loggingBackStack` will have a structure like so: | ||
* | ||
* BackStackScreen(SomeWrapper(WithLogging(theRealScreen))) | ||
*/ | ||
@WorkflowUiExperimentalApi | ||
public fun Screen.deepMap(transform: (Screen) -> Screen): Screen { | ||
return if (this is ScreenContainer<*>) map { it.deepMap(transform) } | ||
else transform(this) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters