Skip to content
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

Added Navigator.bringToFront extension function #208

Merged
merged 1 commit into from
Aug 10, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ inline fun <C : Any> Navigator<C>.popWhile(crossinline predicate: (C) -> Boolean
fun <C : Any> Navigator<C>.replaceCurrent(configuration: C) {
navigate { it.dropLast(1) + configuration }
}

/**
* Removes all components with configurations of [configuration]'s class, and adds the provided [configuration] to the top of the stack.
* The operation is performed as one transaction. If there is already a component with the same configuration, it will not be recreated.
*/
fun <C : Any> Navigator<C>.bringToFront(configuration: C) {
navigate { stack ->
stack.filterNot { it::class == configuration::class } + configuration
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.arkivanov.decompose.router

import com.arkivanov.decompose.bringToFront
import kotlin.test.Test
import kotlin.test.assertEquals

@Suppress("TestFunctionName")
class NavigatorBringToFrontTest {

@Test
fun WHEN_does_not_contain_THEN_new_configuration_pushed() {
val navigator = TestNavigator(listOf(Config.A, Config.B))

navigator.bringToFront(Config.C(0))

assertEquals(listOf(Config.A, Config.B, Config.C(0)), navigator.stack)
}

@Test
fun WHEN_exists_with_same_value_at_start_THEN_existing_configuration_moved_to_end() {
val navigator = TestNavigator(listOf(Config.C(0), Config.A, Config.B))

navigator.bringToFront(Config.C(0))

assertEquals(listOf(Config.A, Config.B, Config.C(0)), navigator.stack)
}

@Test
fun WHEN_exists_with_same_value_in_the_middle_THEN_existing_configuration_moved_to_end() {
val navigator = TestNavigator(listOf(Config.A, Config.C(0), Config.B))

navigator.bringToFront(Config.C(0))

assertEquals(listOf(Config.A, Config.B, Config.C(0)), navigator.stack)
}

@Test
fun WHEN_exists_with_same_value_at_the_end_THEN_existing_configuration_moved_to_end() {
val navigator = TestNavigator(listOf(Config.A, Config.B, Config.C(0)))

navigator.bringToFront(Config.C(0))

assertEquals(listOf(Config.A, Config.B, Config.C(0)), navigator.stack)
}

@Test
fun WHEN_exists_with_different_value_at_start_THEN_removed_and_new_added_to_end() {
val navigator = TestNavigator(listOf(Config.C(0), Config.A, Config.B))

navigator.bringToFront(Config.C(1))

assertEquals(listOf(Config.A, Config.B, Config.C(1)), navigator.stack)
}

@Test
fun WHEN_exists_with_different_value_in_the_middle_THEN_removed_and_new_added_to_end() {
val navigator = TestNavigator(listOf(Config.A, Config.C(0), Config.B))

navigator.bringToFront(Config.C(1))

assertEquals(listOf(Config.A, Config.B, Config.C(1)), navigator.stack)
}

@Test
fun WHEN_exists_with_different_value_at_the_end_THEN_removed_and_new_added_to_end() {
val navigator = TestNavigator(listOf(Config.A, Config.B, Config.C(0)))

navigator.bringToFront(Config.C(1))

assertEquals(listOf(Config.A, Config.B, Config.C(1)), navigator.stack)
}

@Test
fun WHEN_multiple_exist_THEN_all_removed_and_new_added_to_end() {
val navigator = TestNavigator(listOf(Config.C(0), Config.A, Config.C(1), Config.B))

navigator.bringToFront(Config.C(1))

assertEquals(listOf(Config.A, Config.B, Config.C(1)), navigator.stack)
}

private sealed class Config {
object A : Config()
object B : Config()
data class C(val value: Int) : Config()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.arkivanov.decompose.router

import com.arkivanov.decompose.Navigator

class TestNavigator<C : Any>(
var stack: List<C> = emptyList()
) : Navigator<C> {

override fun navigate(transformer: (stack: List<C>) -> List<C>) {
stack = stack.let(transformer)
}
}