From 4037bfae6d3dc3e9c5a9a3236a56aa97526838ee Mon Sep 17 00:00:00 2001 From: Vincent BRIDIER Date: Mon, 2 Jan 2023 12:09:11 +0100 Subject: [PATCH] Fix install await img --- .../src/App/Controllers/InstallController.js | 65 ++--------- ComApp/src/App/Views/InstallScreen.js | 101 ++++++++++++++---- 2 files changed, 87 insertions(+), 79 deletions(-) diff --git a/ComApp/src/App/Controllers/InstallController.js b/ComApp/src/App/Controllers/InstallController.js index fa78f43..d73cb25 100644 --- a/ComApp/src/App/Controllers/InstallController.js +++ b/ComApp/src/App/Controllers/InstallController.js @@ -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) { diff --git a/ComApp/src/App/Views/InstallScreen.js b/ComApp/src/App/Views/InstallScreen.js index 0864bfa..f7975dd 100644 --- a/ComApp/src/App/Views/InstallScreen.js +++ b/ComApp/src/App/Views/InstallScreen.js @@ -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: "" }); }