1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Added support for navigation

This commit is contained in:
Laurent Cozic 2017-05-09 19:56:01 +00:00
parent 61ea8f2963
commit 7d7ee268f0
4 changed files with 213 additions and 90 deletions

View File

@ -2,12 +2,12 @@ import React, { Component } from 'react';
import { AppRegistry, View, Button, TextInput } from 'react-native'; import { AppRegistry, View, Button, TextInput } from 'react-native';
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { createStore } from 'redux';
import { Provider } from 'react-redux' import { Provider } from 'react-redux'
import { createStore } from 'redux';
import { combineReducers } from 'redux';
import { WebApi } from 'src/web-api.js' import { WebApi } from 'src/web-api.js'
import { Database } from 'src/database.js' import { Database } from 'src/database.js'
//import { Session } from 'src/model/session.js';
import { SessionService } from 'src/services/session-service.js'; import { SessionService } from 'src/services/session-service.js';
@ -16,88 +16,88 @@ import { Log } from 'src/log.js'
import { LoginButton } from 'src/components/login-button.js'; import { LoginButton } from 'src/components/login-button.js';
let debugMode = true; // let debugMode = true;
let clientId = 'A7D301DA7D301DA7D301DA7D301DA7D3'; // let clientId = 'A7D301DA7D301DA7D301DA7D301DA7D3';
let db = new Database(); // let db = new Database();
db.setDebugEnabled(debugMode); // db.setDebugEnabled(debugMode);
db.open(); // db.open();
let defaultState = { // let defaultState = {
'myButtonLabel': 'click', // 'myButtonLabel': 'click',
'counter': 0, // 'counter': 0,
} // }
function shallowcopy(a) { // function shallowcopy(a) {
return Object.assign({}, a); // return Object.assign({}, a);
} // }
let store = createStore(reducer, defaultState); // let store = createStore(reducer, defaultState);
function reducer(state, action) { // function reducer(state, action) {
switch (action.type) { // switch (action.type) {
case 'SET_BUTTON_NAME': // case 'SET_BUTTON_NAME':
var state = shallowcopy(state); // var state = shallowcopy(state);
state.myButtonLabel = action.name; // state.myButtonLabel = action.name;
return state; // return state;
case 'INC_COUNTER': // case 'INC_COUNTER':
var state = shallowcopy(state); // var state = shallowcopy(state);
state.counter++; // state.counter++;
return state; // return state;
} // }
return state; // return state;
} // }
class MyInput extends Component { // class MyInput extends Component {
render() { // render() {
return <TextInput onChangeText={this.props.onChangeText} /> // return <TextInput onChangeText={this.props.onChangeText} />
} // }
} // }
const mapStateToInputProps = function(state) { // const mapStateToInputProps = function(state) {
return {} // return {}
} // }
const mapDispatchToInputProps = function(dispatch) { // const mapDispatchToInputProps = function(dispatch) {
return { // return {
onChangeText(text) { // onChangeText(text) {
dispatch({ // dispatch({
type: 'SET_BUTTON_NAME', // type: 'SET_BUTTON_NAME',
name: text // name: text
}); // });
} // }
} // }
} // }
const MyConnectionInput = connect( // const MyConnectionInput = connect(
mapStateToInputProps, // mapStateToInputProps,
mapDispatchToInputProps // mapDispatchToInputProps
)(MyInput) // )(MyInput)
class App extends Component { // class App extends Component {
render() { // render() {
return ( // return (
<Provider store={store}> // <Provider store={store}>
<View> // <View>
<MyConnectionInput /> // <MyConnectionInput />
<LoginButton /> // <LoginButton />
</View> // </View>
</Provider> // </Provider>
) // )
} // }
} // }
// let api = new WebApi('http://192.168.1.2'); // let api = new WebApi('http://192.168.1.2');
// let sessionService = new SessionService(api); // let sessionService = new SessionService(api);
@ -109,4 +109,96 @@ class App extends Component {
// console.warn(error); // console.warn(error);
// }) // })
AppRegistry.registerComponent('AwesomeProject', () => App); // AppRegistry.registerComponent('AwesomeProject', () => App);
import { StackNavigator } from 'react-navigation';
import { addNavigationHelpers } from 'react-navigation';
class MainScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
};
render() {
const { navigate } = this.props.navigation;
return (
<Button
title="Go to Jane's profile"
onPress={() =>
navigate('Profile', { name: 'Jane' })
}
/>
);
}
}
class ProfileScreen extends React.Component {
static navigationOptions = {
title: 'Profile',
};
render() {
const { navigate } = this.props.navigation;
return (
<Button
title="Go to main"
onPress={() =>
navigate('Main')
}
/>
);
}
}
const AppNavigator = StackNavigator({
Main: {screen: MainScreen},
Profile: {screen: ProfileScreen},
});
class AppComponent extends React.Component {
render() {
return (
<AppNavigator navigation={addNavigationHelpers({
dispatch: this.props.dispatch,
state: this.props.nav,
})} />
);
}
}
const navInitialState = AppNavigator.router.getStateForAction(AppNavigator.router.getActionForPathAndParams('Main'));
const navReducer = (state = navInitialState, action) => {
const nextState = AppNavigator.router.getStateForAction(action, state);
return nextState || state;
};
const appReducer = combineReducers({
nav: navReducer,
});
const mapStateToProps = (state) => ({
nav: state.nav
});
const App = connect(mapStateToProps)(AppComponent);
const store = createStore(appReducer);
class Root extends React.Component {
render() {
return (
<Provider store={store}>
<App />
</Provider>
);
}
}
//AppRegistry.registerComponent('AwesomeProject', () => AppNavigator);
AppRegistry.registerComponent('AwesomeProject', () => Root);

View File

@ -8,7 +8,8 @@
}, },
"dependencies": { "dependencies": {
"react": "16.0.0-alpha.6", "react": "16.0.0-alpha.6",
"react-native": "0.44.0" "react-native": "0.44.0",
"react-navigation": "^1.0.0-beta.9"
}, },
"devDependencies": { "devDependencies": {
"babel-jest": "19.0.0", "babel-jest": "19.0.0",
@ -18,7 +19,7 @@
"redux": "3.6.0", "redux": "3.6.0",
"react-redux": "4.4.8", "react-redux": "4.4.8",
"query-string": "4.3.4", "query-string": "4.3.4",
"react-native-sqlite-storage": "3.3.*", "react-native-sqlite-storage": "3.3.*"
}, },
"jest": { "jest": {
"preset": "react-native" "preset": "react-native"

View File

@ -6,14 +6,15 @@ import { _ } from 'src/locale.js';
class LoginButtonComponent extends Component { class LoginButtonComponent extends Component {
render() { render() {
return <Button onPress={this.props.onPress} title={_(this.props.label)} /> return <Button onPress={this.props.onPress} title={_("Login")} />
} }
} }
const LoginButton = connect( const LoginButton = connect(
(state) => { (state) => {
return { label: state.myButtonLabel }; return {};
//return { label: state.myButtonLabel };
}, },
(dispatch) => { (dispatch) => {
return { return {

View File

@ -0,0 +1,29 @@
import React, { Component } from 'react';
import { connect } from 'react-redux'
import { Button } from 'react-native';
import { _ } from 'src/locale.js';
class OfflineButtonComponent extends Component {
render() {
return <Button onPress={this.props.onPress} title={_(this.props.label)} />
}
}
const OfflineButton = connect(
(state) => {
//return { label: state.myButtonLabel };
},
(dispatch) => {
return {
onPress: function() {
dispatch({
type: 'WORK_OFFLINE'
});
}
}
}
)(OfflineButtonComponent)
export { OfflineButton };