This commit is contained in:
Vincent BRIDIER 2022-12-30 16:44:51 +01:00
parent 793ef4a1fa
commit 4e68fdb696
No known key found for this signature in database
GPG key ID: 8571BFA20420D512
7 changed files with 114 additions and 37 deletions

View file

@ -54,13 +54,10 @@ class App extends React.Component {
componentDidMount() {
const unsubscribe = store.subscribe(this.handleChange.bind(this));
if (SyncController.isLogged()) {
store.dispatch({ type: 'SET_CURRENT_USER' });
store.dispatch({ type: 'SET_LOGGED', data: true });
}
this.setState({
isSetup: store.getState().app.isSetup,
isLogged: store.getState().app.logged,
mainColor: store.getState().app.mainColor
});
}
@ -77,7 +74,7 @@ class App extends React.Component {
return (
<Provider store={store}>
<NavigationContainer>
<Stack.Navigator>
<Stack.Navigator initialRouteName="HomeScreen">
{this.state.isSetup === false &&
<Stack.Screen
name="InstallScreen"
@ -171,7 +168,7 @@ class App extends React.Component {
</TouchableOpacity>
<TouchableOpacity
onPress={() => {
store.dispatch({ type: 'SET_LOGGED', data: false });
store.dispatch({ type: 'SET_CURRENT_USER', data: false });
}}
style={{ marginRight: 25 }}>
<Icon name="sign-out-alt" size={20} />

View file

@ -154,6 +154,49 @@ class SyncController {
}
return false;
}
needSync(callback) {
NetInfo.fetch().then(state => {
if (state.isConnected) {
axios({
method: 'get',
url: endpoint + '/version',
responseType: 'json',
params: {
token: api_token,
},
})
.then(response => {
const localVersion = Setting.get('version');
if (
localVersion === undefined ||
localVersion === false ||
localVersion.value !== response.data.version
) {
return callback();
}
return false;
})
.catch(function (error) {
console.log(error);
});
}
return false;
});
return false;
}
setInstalled() {
Setting.add({ key: 'installed', value: "true" });
}
isInstalled() {
const test = Setting.get('installed');
if (test === false || test === undefined) {
return false;
}
return true;
}
}
export default new SyncController();

View file

@ -43,18 +43,19 @@ const reducer = (state = initialState, action) => {
isSetup: saved === undefined ? false : saved.value === "true" ? true : false
};
case 'SET_CURRENT_USER':
let user = Setting.get('isLogged').value
return {
...state,
currentUser: JSON.parse(user),
};
case 'SET_LOGGED':
let user = false;
let isLogged = false;
if (action.data === false) {
Setting.delete('isLogged');
} else {
userObj = Setting.get('isLogged').value
user = JSON.parse(userObj)
isLogged = true
}
return {
...state,
logged: action.data,
currentUser: user !== false ? user : initialState.currentUser,
logged: isLogged
};
// case 'UPDATE_LOCALE':
// let nxt;

View file

@ -8,7 +8,6 @@ const reducer = (state = initialState, action) => {
switch (action.type) {
case INIT_DIRECTORIES: {
return {
...state,
global: Directory.getAll(
action.user.role,
action.user.authorizedFolders,

View file

@ -4,15 +4,19 @@ import { View, ImageBackground, TouchableOpacity, Text, Image } from 'react-nati
import commonStyles from '../../Resources/CommonStyles';
// Redux
import { connect } from 'react-redux';
import { initDirectories } from '../Store/Actions/directories';
import { initDocuments } from '../Store/Actions/documents';
import { initContact } from '../Store/Actions/contacts';
import { initDirectories } from '../Store/Actions/directories';
// Style
import styles from '../../Resources/Styles/HomeStyle';
// Translations
import i18n from "../../I18n";
// FileSytem
import * as FileSystem from 'expo-file-system';
// NetInfo
import NetInfo from '@react-native-community/netinfo';
// Controller
import SyncController from '../Controllers/SyncController';
function mapStateToProps(state) {
return {
@ -25,21 +29,45 @@ function mapStateToProps(state) {
const mapDispatchToProps = dispatch => {
return {
init: () => dispatch({ type: 'INIT' }),
initBtns: () => dispatch({ type: 'INIT_HOME_BTNS' }),
initDirectories: (lang, user) => dispatch(initDirectories(lang, user)),
initDocuments: (lang, directories) => dispatch(initDocuments(lang, directories)),
initContact: currentUser => dispatch(initContact(currentUser)),
initDirectories: (lang, user) => dispatch(initDirectories(lang, user)),
};
};
class HomeScreen extends React.Component {
state = {
lastSyncCall: '1970-01-01',
};
componentDidMount() {
this.props.init();
// Si on est sur une installation
if (!SyncController.isInstalled()) {
return this.openSync();
}
this.props.initBtns();
this.props.initDirectories(this.props.locale.value, this.props.currentUser);
this.props.initContact(this.props.currentUser);
this.props.initDirectories(this.props.locale.value, this.props.currentUser);
}
unsubscribe() {
NetInfo.addEventListener(state => {
if (state.isConnected === true) {
let seconds =
Math.abs(new Date(this.state.lastSyncCall) - Date.now()) / 1000;
if (seconds > 30) {
this.setState({ lastSyncCall: Date.now() });
SyncController.needSync(this.openSync.bind(this));
}
}
});
}
openSync() {
this.props.navigation.navigate('SyncScreen', { forceSync: true });
}
render() {

View file

@ -2,7 +2,6 @@ import React from 'react';
import { View, ImageBackground, TextInput, Button, Image, Alert } from 'react-native';
// Redux
import { connect } from 'react-redux';
import { initContact } from '../Store/Actions/contacts';
// Common Styles
import commonStyles from '../../Resources/CommonStyles';
// Style
@ -28,12 +27,7 @@ function mapStateToProps(state) {
const mapDispatchToProps = dispatch => {
return {
initContact: currentUser => dispatch(initContact(currentUser)),
setCurrentUser: user => dispatch({
type: 'SET_CURRENT_USER',
data: JSON.stringify(user),
}),
setIsLogged: () => dispatch({ type: 'SET_LOGGED', data: true })
setCurrentUser: () => dispatch({ type: 'SET_CURRENT_USER' }),
};
};
@ -43,6 +37,12 @@ class LoginScreen extends React.Component {
password: 'test',
};
componentDidMount() {
if (SyncController.isLogged()) {
this.props.setCurrentUser();
}
}
render() {
return (
<ImageBackground source={{ uri: FileSystem.documentDirectory + 'BackgroundImage.png' }} style={commonStyles.background}>
@ -101,12 +101,12 @@ class LoginScreen extends React.Component {
}
const lastUser = Setting.get('lastUser');
// Si on est sur une installation
if (lastUser === false || lastUser === undefined) {
this.loginAction(user, true);
return;
if (lastUser === false || lastUser == undefined) {
return this.loginAction(user);
}
// TODO : a deplacer dans home screen
// Si on a deja une install avec une précédente connexion
if (lastUser.value === user.id.toString()) {
if (lastUser !== false && lastUser !== undefined && lastUser.value === user.id.toString()) {
this.loginAction(user);
} else {
// Si user !== last user && last user not synced
@ -129,9 +129,9 @@ class LoginScreen extends React.Component {
loginAction(user, forceSync = false) {
this.setState({ login: '', password: '' });
LoginController.login(user);
this.props.setCurrentUser(user);
this.props.setIsLogged();
if (forceSync === true) {
this.props.setCurrentUser();
if (forceSync === true) {
this.props.navigation.navigate('SyncScreen', { forceSync: true });
}
}

View file

@ -22,14 +22,17 @@ function mapStateToProps(state) {
syncVersion: state.app.syncVersion,
currentUser: state.app.currentUser,
locale: state.locale.locale,
mainColor: state.app.mainColor
};
}
const mapDispatchToProps = dispatch => {
return {
init: () => dispatch({ type: 'INIT' }),
initUsers: () => dispatch({ type: 'INIT_USERS' }),
initDirectories: (lang, user) => dispatch(initDirectories(lang, user)),
initContact: currentUser => dispatch(initContact(currentUser)),
initBtns: () => dispatch({ type: 'INIT_HOME_BTNS' }),
updateVersion: version => dispatch({
type: 'UPDATE_VERSION',
data: version,
@ -48,12 +51,14 @@ class SyncScreen extends React.Component {
downloadProgress: 0,
downloadingFile: false,
processing: false,
newApp: false
newApp: false,
forced: false
};
componentDidMount() {
const { forceSync } = this.props.route.params;
if (forceSync === true) {
this.setState({ forced: true });
this.sync();
}
}
@ -291,11 +296,15 @@ class SyncScreen extends React.Component {
}
}
finishSync(version) {
finishSync() {
this.props.init();
this.props.initDirectories(this.props.locale.value, this.props.currentUser);
this.props.initUsers();
this.props.initContact(this.props.currentUser);
this.props.initDirectories(this.props.locale.value, this.props.currentUser);
this.props.initBtns();
if (this.state.forced) {
SyncController.setInstalled();
}
SyncController.updateVersion(version);
this.setState({ txtStep: i18n.t('sync_s.update_completed') + '.', processing: false });
if (this.state.newApp === true) {