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 StackNavigator#pushToFront extension function #593

Merged
merged 1 commit into from
Jan 10, 2024
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
2 changes: 2 additions & 0 deletions decompose/api/android/decompose.api
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ public final class com/arkivanov/decompose/router/stack/StackNavigatorExtKt {
public static synthetic fun push$default (Lcom/arkivanov/decompose/router/stack/StackNavigator;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)V
public static final fun pushNew (Lcom/arkivanov/decompose/router/stack/StackNavigator;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)V
public static synthetic fun pushNew$default (Lcom/arkivanov/decompose/router/stack/StackNavigator;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
public static final fun pushToFront (Lcom/arkivanov/decompose/router/stack/StackNavigator;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)V
public static synthetic fun pushToFront$default (Lcom/arkivanov/decompose/router/stack/StackNavigator;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)V
public static final fun replaceAll (Lcom/arkivanov/decompose/router/stack/StackNavigator;[Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)V
public static synthetic fun replaceAll$default (Lcom/arkivanov/decompose/router/stack/StackNavigator;[Ljava/lang/Object;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)V
public static final fun replaceCurrent (Lcom/arkivanov/decompose/router/stack/StackNavigator;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)V
Expand Down
2 changes: 2 additions & 0 deletions decompose/api/jvm/decompose.api
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ public final class com/arkivanov/decompose/router/stack/StackNavigatorExtKt {
public static synthetic fun push$default (Lcom/arkivanov/decompose/router/stack/StackNavigator;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)V
public static final fun pushNew (Lcom/arkivanov/decompose/router/stack/StackNavigator;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)V
public static synthetic fun pushNew$default (Lcom/arkivanov/decompose/router/stack/StackNavigator;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
public static final fun pushToFront (Lcom/arkivanov/decompose/router/stack/StackNavigator;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)V
public static synthetic fun pushToFront$default (Lcom/arkivanov/decompose/router/stack/StackNavigator;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)V
public static final fun replaceAll (Lcom/arkivanov/decompose/router/stack/StackNavigator;[Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)V
public static synthetic fun replaceAll$default (Lcom/arkivanov/decompose/router/stack/StackNavigator;[Ljava/lang/Object;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)V
public static final fun replaceCurrent (Lcom/arkivanov/decompose/router/stack/StackNavigator;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ fun <C : Any> StackNavigator<C>.pushNew(
)
}

/**
* Pushes the provided [configuration] to the top of the stack,
* removing the [configuration] from the back stack, if any.
*
* This API works similar to [bringToFront], except it compares configurations
* by equality rather than by configuration class.
*
* @param onComplete called when the navigation is finished (either synchronously or asynchronously).
*/
@ExperimentalDecomposeApi
fun <C : Any> StackNavigator<C>.pushToFront(
configuration: C,
onComplete: () -> Unit = {},
) {
navigate(
transformer = { stack -> stack - configuration + configuration },
onComplete = { _, _ -> onComplete() },
)
}

/**
* Pops the latest configuration at the top of the stack.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.arkivanov.decompose.router.stack

import kotlin.test.Test
import kotlin.test.assertEquals

@Suppress("TestFunctionName")
class RouterPushToFrontTest {

@Test
fun WHEN_does_not_contain_THEN_new_configuration_pushed() {
val navigator = TestStackNavigator(listOf(1, 2))

navigator.pushToFront(3)

assertEquals(listOf(1, 2, 3), navigator.configurations)
}

@Test
fun WHEN_exists_with_same_value_at_start_THEN_existing_configuration_moved_to_end() {
val navigator = TestStackNavigator(listOf(1, 2, 3))

navigator.pushToFront(1)

assertEquals(listOf(2, 3, 1), navigator.configurations)
}

@Test
fun WHEN_exists_with_same_value_in_the_middle_THEN_existing_configuration_moved_to_end() {
val navigator = TestStackNavigator(listOf(1, 2, 3))

navigator.pushToFront(2)

assertEquals(listOf(1, 3, 2), navigator.configurations)
}

@Test
fun WHEN_exists_with_same_value_at_the_end_THEN_not_changed() {
val navigator = TestStackNavigator(listOf(1, 2, 3))

navigator.pushToFront(3)

assertEquals(listOf(1, 2, 3), navigator.configurations)
}
}