Fix install await img

This commit is contained in:
Vincent BRIDIER 2023-01-02 12:09:11 +01:00
parent 4e68fdb696
commit 4037bfae6d
No known key found for this signature in database
GPG key ID: 8571BFA20420D512
2 changed files with 87 additions and 79 deletions

View file

@ -9,68 +9,19 @@ import NetInfo from "@react-native-community/netinfo";
import i18n from "../../I18n";
// Database
import { Setting } from "../../Database";
// FileSytem
import * as FileSystem from 'expo-file-system';
const api_token = 'toto';
class InstallController {
getConfig(url) {
NetInfo.fetch().then(state => {
if (state.isConnected) {
axios({
method: 'GET',
url: url + '/api/settings',
responseType: 'json',
params: {
token: api_token
},
})
.then(function (response) {
if (response.data) {
if (response.data.status === "OK") {
_.forEach(response.data.datas, function (value, key) {
switch (key) {
case "MainColor":
Setting.upsert({ key: key, value: value });
break;
case "SquaredLogo":
case "RectangleLogo":
case "BackgroundImage":
case "HomeButtonImage":
FileSystem.downloadAsync(
url + "/" + value,
FileSystem.documentDirectory + key + '.png'
)
.then(({ uri }) => {
console.log('Finished downloading to ', uri);
})
.catch(error => {
Alert.alert(i18n.t("alert.error"), i18n.t("install.resource_dl_error") + key);
console.error(error);
return false;
});
break;
}
});
} else {
Alert.alert(i18n.t("alert.error"), i18n.t("error.error_has_occurred"));
return false;
}
}
})
.catch(function (error) {
console.log(error)
Alert.alert(i18n.t("alert.error"), i18n.t("error.error_has_occurred"));
return false;
})
} else {
Alert.alert(i18n.t("alert.error"), i18n.t("error.tablet_not_connected_internet"));
return false;
}
async getConfig(url) {
return axios({
method: 'GET',
url: url + '/api/settings',
responseType: 'json',
params: {
token: api_token
},
});
return true;
}
getAppConfig(url) {

View file

@ -4,6 +4,8 @@ import { ImageBackground, View, Image, Text, TextInput, Button, Alert } from 're
import { connect } from 'react-redux';
// Helpers
import Helpers from '../Lib/Helpers';
// Lodash
import _ from 'lodash';
// Common Styles
import commonStyles from '../../Resources/CommonStyles';
// Style
@ -14,7 +16,9 @@ import i18n from "../../I18n";
import InstallController from '../Controllers/InstallController';
import SyncController from '../Controllers/SyncController';
// Database
import { User } from '../../Database';
import { User, Setting } from '../../Database';
// FileSytem
import * as FileSystem from 'expo-file-system';
function mapStateToProps(state) {
return {
@ -68,29 +72,82 @@ class InstallScreen extends React.Component {
}
async install() {
let url = this.state.url;
if (!SyncController.checkConnection()) {
Alert.alert(
i18n.t('global.warning'),
i18n.t('error.must_be_connected_internet_sync')
);
return;
}
this.setState({ processing: true, txtStep: i18n.t("install.get_app_setting") });
if (InstallController.getConfig(this.state.url)) {
this.setState({ txtStep: i18n.t("install.get_content_setting") });
if (InstallController.getAppConfig(this.state.url)) {
// getUsers
this.setState({ txtStep: i18n.t('sync_s.updating_users') + '...' });
const resUsers = await SyncController.getUsers().then(res => {
User.clear();
return res.data;
}).catch(error => {
console.log(error);
Alert.alert(
i18n.t('error.error'),
i18n.t('error.error_has_occurred')
);
return
});
resUsers.forEach(user => {
User.add(user);
});
this.props.dispatch({ type: 'SET_ISSETUP' });
this.props.navigation.navigate('LoginScreen');
const toDL = await InstallController.getConfig(url).then(response => {
let toDL = [];
if (response.data) {
if (response.data.status === "OK") {
_.forEach(response.data.datas, function (value, key) {
switch (key) {
case "MainColor":
Setting.upsert({ key: key, value: value });
break;
case "SquaredLogo":
case "RectangleLogo":
case "BackgroundImage":
case "HomeButtonImage":
toDL.push({
source: url + "/" + value,
dest: FileSystem.documentDirectory + key + '.png'
});
break;
}
});
return toDL;
} else {
Alert.alert(i18n.t("alert.error"), i18n.t("error.error_has_occurred"));
return false;
}
}
}).catch(function (error) {
console.log(error)
Alert.alert(i18n.t("alert.error"), i18n.t("error.error_has_occurred"));
return false;
});
for await (const file of toDL) {
await FileSystem.downloadAsync(
file.source,
file.dest
)
.then(({ uri }) => {
console.log('Finished downloading to ', uri);
})
.catch(error => {
Alert.alert(i18n.t("alert.error"), i18n.t("install.resource_dl_error") + file.dest);
console.error(error);
return false;
});
}
this.setState({ txtStep: i18n.t("install.get_content_setting") });
if (InstallController.getAppConfig(this.state.url)) {
// getUsers
this.setState({ txtStep: i18n.t('sync_s.updating_users') + '...' });
const resUsers = await SyncController.getUsers().then(res => {
User.clear();
return res.data;
}).catch(error => {
console.log(error);
Alert.alert(
i18n.t('error.error'),
i18n.t('error.error_has_occurred')
);
return
});
resUsers.forEach(user => {
User.add(user);
});
this.props.dispatch({ type: 'SET_ISSETUP' });
this.props.navigation.navigate('LoginScreen');
}
this.setState({ processing: false, txtStep: "" });
}