-
Notifications
You must be signed in to change notification settings - Fork 112
Completes our navigation story #1313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
52 changes: 52 additions & 0 deletions
52
...ow-ui/core-common/src/main/java/com/squareup/workflow1/ui/navigation/NavigationMonitor.kt
This file contains hidden or 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,52 @@ | ||
| package com.squareup.workflow1.ui.navigation | ||
|
|
||
| import com.squareup.workflow1.ui.Compatible | ||
| import com.squareup.workflow1.ui.unwrap | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.onEach | ||
|
|
||
| /** | ||
| * Reports navigation across a series of calls to [update], probably made | ||
| * for each rendering posted by | ||
| * [renderWorkflowIn][com.squareup.workflow1.renderWorkflowIn]. | ||
| * | ||
| * Takes advantage of [unwrap()] and [Compatible.keyFor] to provide navigation | ||
| * logging by reporting the top (read: last-most, inner-most) sub-rendering, | ||
| * which conventionally is the one that is visible and accessible to the user. | ||
| * | ||
| * Reports each time the [Compatible.keyFor] the top is unequal to the previous one, | ||
| * which conventionally indicates that a new view object will replace the previous one. | ||
| */ | ||
| public class NavigationMonitor( | ||
| skipFirstScreen: Boolean = false, | ||
| private val onNavigate: (Any) -> Unit = { println(it) } | ||
| ) { | ||
| @Volatile | ||
| private var lastKey: String? = if (skipFirstScreen) null else "" | ||
|
|
||
| /** | ||
| * Uses [unwrap] to find the topmost element of [rendering] and | ||
| * reports it with [onNavigate] if [Compatible.keyFor] reveals that | ||
| * it is of a different kind from the previous top. | ||
| */ | ||
| public fun update(rendering: Any) { | ||
| val unwrapped = rendering.unwrap() | ||
|
|
||
| Compatible.keyFor(unwrapped).takeIf { it != lastKey }?.let { newKey -> | ||
| if (lastKey != null) onNavigate(unwrapped) | ||
| lastKey = newKey | ||
rjrjr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates a [NavigationMonitor] and [updates it][NavigationMonitor.update] | ||
| * with [each element collected][Flow.onEach] by the receiving [Flow]. | ||
| */ | ||
| public fun <T : Any> Flow<T>.reportNavigation( | ||
| skipFirstScreen: Boolean = false, | ||
| onNavigate: (Any) -> Unit = { println(it) } | ||
| ): Flow<T> { | ||
| val monitor = NavigationMonitor(skipFirstScreen, onNavigate) | ||
| return onEach { monitor.update(it) } | ||
| } | ||
189 changes: 189 additions & 0 deletions
189
...i/core-common/src/test/java/com/squareup/workflow1/ui/navigation/NavigationMonitorTest.kt
This file contains hidden or 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,189 @@ | ||
| package com.squareup.workflow1.ui.navigation | ||
|
|
||
| import com.squareup.workflow1.ui.Compatible | ||
| import com.squareup.workflow1.ui.Compatible.Companion.keyFor | ||
| import com.squareup.workflow1.ui.Container | ||
| import com.squareup.workflow1.ui.Screen | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertNotEquals | ||
| import kotlin.test.assertNull | ||
| import kotlin.test.assertSame | ||
|
|
||
| class NavigationMonitorTest { | ||
| private data class NotScreen( | ||
| val name: String, | ||
| val baggage: String = "" | ||
| ) : Compatible { | ||
| override val compatibilityKey: String = keyFor(this, name) | ||
| } | ||
|
|
||
| private data class TestScreen( | ||
| val name: String, | ||
| val baggage: String = "" | ||
| ) : Screen, Compatible { | ||
| override val compatibilityKey: String = keyFor(this, name) | ||
| } | ||
|
|
||
| private data class TestContainer<T : Any>( | ||
| val content: List<T> | ||
| ) : Container<Any, T> { | ||
| override fun asSequence(): Sequence<T> = content.asSequence() | ||
|
|
||
| override fun <D : Any> map(transform: (T) -> D): Container<Any, D> = error("not relevant") | ||
| } | ||
|
|
||
| private class TestOverlay<T : Screen>( | ||
| override val content: T | ||
| ) : ScreenOverlay<T> { | ||
| override fun <ContentU : Screen> map(transform: (T) -> ContentU) = error("not relevant") | ||
| } | ||
|
|
||
| private var lastTop: Any? = null | ||
| private var updates = 0 | ||
|
|
||
| private fun onUpdate(top: Any) { | ||
| lastTop = top | ||
| updates++ | ||
| } | ||
|
|
||
| private val monitor = NavigationMonitor(onNavigate = ::onUpdate) | ||
|
|
||
| @Test | ||
| fun `reports first by default`() { | ||
| val screen = TestScreen("first") | ||
| assertNull(lastTop) | ||
| monitor.update(screen) | ||
| assertSame(screen, lastTop) | ||
| } | ||
|
|
||
| @Test | ||
| fun `can skip first`() { | ||
| val monitor = NavigationMonitor(skipFirstScreen = true, ::onUpdate) | ||
|
|
||
| assertNull(lastTop) | ||
| monitor.update(TestScreen("first")) | ||
| assertNull(lastTop) | ||
|
|
||
| monitor.update(TestScreen("second")) | ||
| assertEquals(TestScreen("second"), lastTop) | ||
| } | ||
|
|
||
| @Test | ||
| fun `reports only on compatibility change`() { | ||
| val type1Instance1 = TestScreen("first") | ||
| assertEquals(0, updates) | ||
|
|
||
| monitor.update(type1Instance1) | ||
| assertEquals(1, updates) | ||
|
|
||
| val type1Instance2 = type1Instance1.copy(baggage = "baggage") | ||
| assertNotEquals(type1Instance1, type1Instance2) | ||
| monitor.update(type1Instance2) | ||
| assertEquals(1, updates) | ||
| assertSame(type1Instance1, lastTop) | ||
|
|
||
| val type2 = TestScreen("second") | ||
| monitor.update(type2) | ||
| assertEquals(2, updates) | ||
| assertSame(type2, lastTop) | ||
| } | ||
|
|
||
| @Test | ||
| fun `handles non-Screens`() { | ||
| val first = NotScreen("first") | ||
|
|
||
| monitor.update(first) | ||
| assertSame(first, lastTop) | ||
|
|
||
| monitor.update(first.copy(baggage = "fnord")) | ||
| assertSame(first, lastTop) | ||
| assertEquals(1, updates) | ||
|
|
||
| monitor.update(NotScreen("second", baggage = "fnord")) | ||
| assertEquals(NotScreen("second", baggage = "fnord"), lastTop) | ||
| assertEquals(2, updates) | ||
| } | ||
|
|
||
| @Test | ||
| fun unwraps() { | ||
| monitor.update(container(TestScreen("0"), TestScreen("1"), TestScreen("2"))) | ||
| assertEquals(TestScreen("2"), lastTop) | ||
| assertEquals(1, updates) | ||
|
|
||
| monitor.update(container(TestScreen("0"), TestScreen("1"), TestScreen("2"))) | ||
| assertEquals(TestScreen("2"), lastTop) | ||
| assertEquals(1, updates) | ||
|
|
||
| monitor.update(container(TestScreen("0"), TestScreen("Hidden Update"), TestScreen("2"))) | ||
| assertEquals(TestScreen("2"), lastTop) | ||
| assertEquals(1, updates) | ||
|
|
||
| monitor.update(container(TestScreen("0"), TestScreen("Hidden Update"), TestScreen("3"))) | ||
| assertEquals(TestScreen("3"), lastTop) | ||
| assertEquals(2, updates) | ||
|
|
||
| monitor.update(container(TestScreen("3", "baggage"))) | ||
| assertEquals(TestScreen("3"), lastTop) | ||
| assertEquals(2, updates) | ||
| } | ||
|
|
||
| @Test | ||
| fun `stock navigation types play nice`() { | ||
| val body = TestScreen("Body") | ||
|
|
||
| monitor.update(bodyAndOverlays(body)) | ||
| assertSame(body, lastTop) | ||
|
|
||
| monitor.update(bodyAndOverlays(body.copy(baggage = "updated"))) | ||
| assertSame(body, lastTop) | ||
|
|
||
| val firstWindowBody = TestScreen("first window") | ||
| monitor.update(bodyAndOverlays(body, TestOverlay(firstWindowBody))) | ||
| assertSame(firstWindowBody, lastTop) | ||
|
|
||
| val wizardOne = TestScreen("wizard one") | ||
| monitor.update( | ||
| bodyAndOverlays( | ||
| body, | ||
| TestOverlay(firstWindowBody), | ||
| TestOverlay(BackStackScreen(wizardOne)) | ||
| ) | ||
| ) | ||
| assertSame(wizardOne, lastTop) | ||
|
|
||
| monitor.update( | ||
| bodyAndOverlays( | ||
| body, | ||
| TestOverlay(firstWindowBody), | ||
| TestOverlay(BackStackScreen(wizardOne.copy(baggage = "updated"))) | ||
| ) | ||
| ) | ||
| assertSame(wizardOne, lastTop) | ||
|
|
||
| val wizardTwo = TestScreen("wizard two") | ||
| monitor.update( | ||
| bodyAndOverlays( | ||
| body, | ||
| TestOverlay(firstWindowBody), | ||
| TestOverlay( | ||
| BackStackScreen( | ||
| wizardOne.copy(baggage = "updated"), | ||
| wizardTwo | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
| assertSame(wizardTwo, lastTop) | ||
| } | ||
|
|
||
| private fun <T : Any> container(vararg elements: T): TestContainer<T> = | ||
| TestContainer(elements.toList()) | ||
|
|
||
| private fun bodyAndOverlays( | ||
| body: Screen, | ||
| vararg overlays: Overlay | ||
| ): BodyAndOverlaysScreen<*, *> { | ||
| return BodyAndOverlaysScreen(body, overlays.asList()) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pyricau @steve-the-edwards We have only ever used our flavor of this on the main thread with the Main.Immediate dispatcher, so this simple implementation has been just fine. Should we stick with what we know, or should this field be marked
@Volatile, or…?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My gut is to leave it this way. It's such a trivial class that anyone doing something more interesting and finding flaws could so easily fork it.
Should I put something in the kdoc warning of its limitations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enforce that it's single threaded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or make it volatile it's simple enough and not that expensive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's the sanity check I was hoping for, "
@Volatileis just fine."