Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
thalia
ThaliApp
Commits
efeec8fa
Commit
efeec8fa
authored
Feb 15, 2017
by
Wietse Kuipers
Browse files
Add back button functionality on Android
parent
1268db9d
Changes
5
Hide whitespace changes
Inline
Side-by-side
app/actions/actionTypes.js
View file @
efeec8fa
export
const
LOGIN
=
'
LOGIN
'
;
export
const
NAVIGATE
=
'
NAVIGATE
'
;
export
const
BACK
=
'
BACK
'
;
app/actions/navigation.js
View file @
efeec8fa
...
...
@@ -6,3 +6,9 @@ export function navigate(scene) {
scene
,
};
}
export
function
back
()
{
return
{
type
:
types
.
BACK
,
};
}
app/app.js
View file @
efeec8fa
import
React
from
'
react
'
;
import
{
BackAndroid
}
from
'
react-native
'
;
import
{
createStore
,
applyMiddleware
,
combineReducers
}
from
'
redux
'
;
import
{
Provider
}
from
'
react-redux
'
;
import
thunk
from
'
redux-thunk
'
;
import
*
as
reducers
from
'
./reducers
'
;
import
ReduxNavigator
from
'
./components/navigator
'
;
import
{
back
}
from
'
./actions/navigation
'
;
const
createStoreWithMiddleware
=
applyMiddleware
(
thunk
)(
createStore
);
const
reducer
=
combineReducers
(
reducers
);
const
store
=
createStoreWithMiddleware
(
reducer
);
BackAndroid
.
addEventListener
(
'
hardwareBackPress
'
,
()
=>
{
if
(
store
.
getState
().
navigation
.
previousScenes
.
length
===
0
)
{
return
false
;
}
store
.
dispatch
(
back
());
return
true
;
});
const
Main
=
()
=>
<
Provider
store
=
{
store
}
>
<
ReduxNavigator
/>
...
...
app/components/navigator.js
View file @
efeec8fa
...
...
@@ -14,7 +14,6 @@ const mapDispatchToProps = dispatch => ({
navigate
:
scene
=>
dispatch
(
actions
.
navigate
(
scene
)),
});
const
ReduxNavigator
=
(
props
)
=>
{
const
currentScene
=
props
.
currentScene
;
switch
(
currentScene
)
{
...
...
@@ -31,5 +30,4 @@ ReduxNavigator.propTypes = {
currentScene
:
React
.
PropTypes
.
string
.
isRequired
,
};
export
default
connect
(
mapStateToProps
,
mapDispatchToProps
)(
ReduxNavigator
);
app/reducers/navigation.js
View file @
efeec8fa
import
*
as
types
from
'
../actions/actionTypes
'
;
const
initialState
=
{
previousScenes
:
[],
currentScene
:
'
login
'
,
};
export
default
function
navigate
(
state
=
initialState
,
action
=
{})
{
const
{
currentScene
,
previousScenes
}
=
state
;
switch
(
action
.
type
)
{
case
types
.
LOGIN
:
if
(
action
.
success
)
{
return
{
...
state
,
currentScene
:
'
welcome
'
};
return
{
previousScenes
:
[
...
previousScenes
,
currentScene
,
],
currentScene
:
'
welcome
'
,
};
}
return
{
...
state
};
case
types
.
BACK
:
{
const
scene
=
previousScenes
.
pop
();
return
{
previousScenes
,
currentScene
:
scene
,
};
}
default
:
return
{
...
state
};
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment