Skip to content

Commit fee3d7b

Browse files
authored
Merge pull request #1 from ReKotlin/master
Merge from Head Fork
2 parents 1a8a8d2 + f41f649 commit fee3d7b

File tree

8 files changed

+118
-39
lines changed

8 files changed

+118
-39
lines changed

CONTRIBUTING.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Contributing to ReKotlin-Router
2+
3+
If you would like to contribute code you can do so through GitHub by forking the repository and sending a pull request (on a branch other than master or gh-pages).
4+
5+
When submitting code, please make every effort to follow existing conventions and style in order to keep the code as readable as possible.
6+
7+
## License
8+
9+
By contributing your code, you agree to license your contribution under the terms of the MIT : https://github.com/ReKotlin/rekotlin-router/blob/master/LICENSE.md
10+
11+
All files are released with the MIT license.

README.md

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# ReKotlin-Router [![CircleCI](https://circleci.com/gh/kmmraj/rekotlin-router.svg?style=svg)](https://circleci.com/gh/kmmraj/rekotlin-router) [![License MIT](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/ReKotlin/rekotlin-router/blob/master/LICENSE.md)
1+
2+
# ReKotlin-Router [![Build Status](https://travis-ci.org/ReKotlin/rekotlin-router.svg?branch=master)](https://travis-ci.org/ReKotlin/rekotlin-router) [![License MIT](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/ReKotlin/rekotlin-router/blob/master/LICENSE.md)
3+
24

35
A declarative router for [ReKotlin](https://github.com/GeoThings/ReKotlin). Allows developers to declare routes in a similar manner as URLs are used on the web.
46

@@ -195,7 +197,27 @@ For general usage questions please use the [mailing list][list] or [StackOverflo
195197

196198
# Contributing
197199

198-
There's still a lot of work to do here! We would love to see you involved! You can find all the details on how to get started in the [Contributing Guide](/CONTRIBUTING.md).
200+
There's still a lot of work to do here! We would love to see you involved!
201+
202+
### Submitting patches
203+
204+
The best way to submit a patch is to [fork the project on github](https://help.github.com/articles/fork-a-repo/) then send us a
205+
[pull request](https://help.github.com/articles/creating-a-pull-request/) via [github](https://github.com).
206+
207+
If you create your own fork, it might help to enable rebase by default
208+
when you pull by executing
209+
``` bash
210+
git config --global pull.rebase true
211+
```
212+
This will avoid your local repo having too many merge commits
213+
which will help keep your pull request simple and easy to apply.
214+
215+
Before submitting the pull request, make sure all existing tests are passing, and add the new test if it is required.
216+
217+
### New functionality
218+
If you want to add new functionality, please file a new proposal issue first to make sure that it is not in progress already. If you have any questions, feel free to create a question issue.
219+
220+
You can find all the details on how to get started in the [Contributing Guide](/CONTRIBUTING.md).
199221

200222
## Compiling & Running tests
201223

build.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ version '0.1.0-SNAPSHOT'
55

66

77
buildscript {
8-
98
ext {
109
kotlin_version = '1.1.50'
1110
dokka_version = '0.9.15'

rekotlin-router/build.gradle

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ apply plugin: 'maven-publish'
99

1010

1111
group 'org.rekotlin-router'
12-
version '0.1.2'
12+
version '0.1.6'
13+
1314

1415
ext {
1516
junitPlugin_version = '1.0.0'
@@ -57,8 +58,7 @@ android {
5758
minSdkVersion 16
5859
targetSdkVersion 25
5960
versionCode 1
60-
versionName "0.1.2"
61-
61+
versionName "0.1.6"
6262
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
6363

6464
}
@@ -138,15 +138,15 @@ bintray {
138138
key = System.getenv("bintrayApiKey")
139139
publications = ['rekotlinRouter']
140140
pkg {
141-
repo = 'rekotlin-router'
142-
name = 'rekotlin-router'
141+
repo = libraryName
142+
name = libraryName
143143
websiteUrl = siteUrl
144144
vcsUrl = gitUrl
145-
licenses = ["MIT"]
145+
licenses = [licenseName]
146146
publish = true
147147
version {
148148
name = android.defaultConfig.versionName
149-
desc = 'Redux based declarative routing for Android'
149+
desc = libraryDescription
150150
released = new Date()
151151
}
152152
}

rekotlin-router/src/main/java/org/rekotlinrouter/NavigationReducer.kt

+27-16
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ class NavigationReducer {
1515
companion object NavRed {
1616

1717
fun handleAction(action: Action, state: NavigationState?): NavigationState {
18-
var navigationState = state ?: NavigationState()
18+
var navigationState = state ?: NavigationState()
1919

20-
when(action) {
21-
is SetRouteAction -> {
22-
navigationState = setRoute(navigationState,action)
23-
}
24-
is SetRouteSpecificData -> {
25-
navigationState = setRouteSpecificData(navigationState, action.route, action.data)
26-
}
27-
else -> {
28-
navigationState = NavigationState()
29-
}
20+
when(action) {
21+
is SetRouteAction -> {
22+
navigationState = setRoute(navigationState,action)
3023
}
31-
return navigationState
24+
is SetRouteSpecificData -> {
25+
navigationState = setRouteSpecificData(navigationState, action.route, action.data)
26+
}
27+
else -> {
28+
navigationState = NavigationState()
29+
}
30+
}
31+
return navigationState
3232
}
3333

3434
fun setRoute(state: NavigationState, setRouteAction: SetRouteAction): NavigationState {
@@ -43,10 +43,21 @@ class NavigationReducer {
4343
fun setRouteSpecificData(state: NavigationState, route: Route, data: Any): NavigationState {
4444
val routeString = FullRoute(route).routeString
4545

46-
if (state.routeSpecificState.containsKey(routeString)) {
47-
state.routeSpecificState.replace(routeString, data)
48-
} else {
49-
state.routeSpecificState.put(routeString, data)
46+
state.routeSpecificState[routeString] = data
47+
48+
return state
49+
}
50+
51+
fun reduce(action: Action, oldState: NavigationState?): NavigationState {
52+
val state = oldState ?: NavigationReducer.handleAction(action = action, state = oldState)
53+
when (action) {
54+
is SetRouteAction -> {
55+
return NavigationReducer.handleAction(action = action, state = state)
56+
}
57+
58+
is SetRouteSpecificData -> {
59+
return NavigationReducer.handleAction(action = action, state = state)
60+
}
5061
}
5162
return state
5263
}

rekotlin-router/src/main/java/org/rekotlinrouter/Router.kt

+12-12
Original file line numberDiff line numberDiff line change
@@ -146,28 +146,28 @@ class Router<routerStateType: StateType> (var store: Store<routerStateType>,
146146
routeBuildingIndex -= 1
147147
}
148148

149+
// This is the 3. case:
150+
// "The new route has a different element after the commonSubroute, we need to replace
151+
// the old route element with the new one"
152+
if((oldRoute.count() > (commonSubroute + 1))
153+
&& (newRoute.count() > (commonSubroute + 1))) {
154+
val changeAction = change(routableIndexForRouteSegment(commonSubroute),
155+
oldRoute[commonSubroute + 1],
156+
newRoute[commonSubroute + 1])
157+
158+
routingActions.add(changeAction)
159+
}
149160
// This is the 1. case:
150161
// "The old route had an element after the commonSubroute and the new route does not
151162
// we need to pop the route segment after the commonSubroute"
152-
if(oldRoute.count() > newRoute.count()) {
163+
else if(oldRoute.count() > newRoute.count()) {
153164
val popAction = pop(routableIndexForRouteSegment(routeBuildingIndex - 1),
154165
oldRoute[routeBuildingIndex])
155166

156167
//routingActions = routingActions.plus(popAction)
157168
routingActions.add(popAction)
158169
routeBuildingIndex -= 1
159170
}
160-
// This is the 3. case:
161-
// "The new route has a different element after the commonSubroute, we need to replace
162-
// the old route element with the new one"
163-
else if((oldRoute.count() > (commonSubroute + 1))
164-
&& (newRoute.count() > (commonSubroute + 1))) {
165-
val changeAction = change(routableIndexForRouteSegment(commonSubroute),
166-
oldRoute[commonSubroute + 1],
167-
newRoute[commonSubroute + 1])
168-
169-
routingActions.add(changeAction)
170-
}
171171

172172
// Push remainder of elements in new Route that weren't in old Route, this covers
173173
// the 2. case:
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
<resources>
2-
<string name="app_name">rekrouter</string>
32
</resources>

rekotlin-router/src/test/java/org/rekotlinrouter/ReKotlinRouterUnitTests.kt

+37
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,43 @@ internal class ReKotlinRouterUnitTests {
153153
assertThat(new).isEqualTo(statsActivityIdentifier)
154154
}
155155

156+
@Test
157+
// @DisplayName("generates a pop action followed by a change action on root when whole route changes")
158+
fun test_change_action_on_root_when_no_common_subroute() {
159+
// Given
160+
val oldRoute = arrayListOf(mainActivityIdentifier,counterActivityIdentifier)
161+
val newRoute = arrayListOf(statsActivityIdentifier)
162+
163+
// When
164+
val routingActions = Router.routingActionsForTransitionFrom(oldRoute, newRoute)
165+
166+
// Then
167+
var action1Correct: Boolean = false
168+
var action2Correct: Boolean = false
169+
170+
routingActions.forEach { routingAction ->
171+
when(routingAction) {
172+
is pop -> {
173+
if(routingAction.responsibleRoutableIndex == 1
174+
&& routingAction.segmentToBePopped == counterActivityIdentifier){
175+
action1Correct = true
176+
}
177+
}
178+
is change -> {
179+
if(routingAction.responsibleRoutableIndex == 0
180+
&& routingAction.segmentToBeReplaced == mainActivityIdentifier
181+
&& routingAction.newSegment == statsActivityIdentifier){
182+
action2Correct = true
183+
}
184+
}
185+
}
186+
}
187+
188+
assertThat(action1Correct).isTrue()
189+
assertThat(action2Correct).isTrue()
190+
assertThat(routingActions.count()).isEqualTo(2)
191+
}
192+
156193
@Test
157194
// @DisplayName("calculates no actions for transition from empty route to empty route")
158195
fun test_no_action_when_transistion_from_empty_to_empty_route(){

0 commit comments

Comments
 (0)