Skip to content

Deep copy when copying state #20

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions rekotlin-router/src/main/java/org/rekotlinrouter/Routable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ typealias RoutingCompletionHandler = () -> Unit
typealias RouteElementIdentifier = String
typealias Route = List<RouteElementIdentifier>

fun Route.copyRoutes(): Route {
val copiedRoutes = arrayListOf<RouteElementIdentifier>()
this.forEach { route ->
copiedRoutes.add(route)
}
return copiedRoutes
}


interface Routable {

Expand Down
2 changes: 1 addition & 1 deletion rekotlin-router/src/main/java/org/rekotlinrouter/Router.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Router<routerStateType : StateType>(var store: Store<routerStateType>,
routingActions.forEach { routingAction ->
routingSerailActionHandler(routingAction, state)
}
lastNavigationState = state.copy()
lastNavigationState = state.copy(state.route.copyRoutes())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,60 @@ internal class ReKotlinRouterUnitTests {
assertThat(action2Correct).isTrue()
assertThat(routingActions.count()).isEqualTo(2)
}

@Test
//@DisplayName("calculates transitions from an empty route to a multi segment route on when same route list object is modified")
fun test_transition_from_empty_to_multi_segment_route_one_by_one() {

// Given
var lastRoute: Route = arrayListOf()
val newRoute = arrayListOf(mainActivityIdentifier)

// When
val routingActionsOld = Router.routingActionsForTransitionFrom(lastRoute, newRoute)
//how copy works
//lastRoute = newRoute

//deep copy
lastRoute = newRoute.copyRoutes()
newRoute.add(statsActivityIdentifier)

val routingActions = Router.routingActionsForTransitionFrom(lastRoute, newRoute)

// Then
var action1Correct = false
var action2Correct = false

routingActionsOld.forEach { routingAction ->
when (routingAction) {
is push -> {
if (routingAction.responsibleRoutableIndex == 0 && routingAction.segmentToBePushed == mainActivityIdentifier) {
action1Correct = true
}
}
}
}

routingActions.forEach { routingAction ->
when (routingAction) {
is push -> {
if (routingAction.responsibleRoutableIndex == 1 && routingAction.segmentToBePushed == statsActivityIdentifier) {
action2Correct = true
}
}
}
}
assertThat(action1Correct).isTrue()
assertThat(action2Correct).isTrue()
assertThat(routingActionsOld.count()).isEqualTo(1)
assertThat(routingActions.count()).isEqualTo(1)
}

@Test
fun test_copy_routes(){
val route = arrayListOf(mainActivityIdentifier,statsActivityIdentifier)
val copiedRoutes = route.copyRoutes()
assertThat(copiedRoutes.count()).isEqualTo(route.count())
assertThat(copiedRoutes[0]).isEqualTo(route[0])
}
}