From 91a4f6590b003deb47bd62676ac88c4328ecd65d Mon Sep 17 00:00:00 2001 From: martin bedouret Date: Thu, 24 Mar 2022 17:14:59 -0300 Subject: [PATCH 1/7] initial commit --- .../Settings/Language/Language.container.js | 6 +++-- .../SpeechProvider.container.js | 26 +++++++++++++++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/components/Settings/Language/Language.container.js b/src/components/Settings/Language/Language.container.js index d8e66e1c7..99d78bf19 100644 --- a/src/components/Settings/Language/Language.container.js +++ b/src/components/Settings/Language/Language.container.js @@ -344,7 +344,9 @@ export class LanguageContainer extends Component { continueOnline, firstClick }; - setDownloadingLang(downloadingLangState); + setDownloadingLang( + downloadingLangState + ); /* try { await this.handleSetTtsEngine(ttsName); //after tts change it fires a remounting } catch { @@ -356,7 +358,7 @@ export class LanguageContainer extends Component { }, downloadLangLoading: false }); - } + } */ }; onErrorDialogAcepted = () => { diff --git a/src/providers/SpeechProvider/SpeechProvider.container.js b/src/providers/SpeechProvider/SpeechProvider.container.js index 15778fa9c..8a3a57840 100644 --- a/src/providers/SpeechProvider/SpeechProvider.container.js +++ b/src/providers/SpeechProvider/SpeechProvider.container.js @@ -10,13 +10,17 @@ import { updateLangSpeechStatus, setTtsEngine } from './SpeechProvider.actions'; +import { setDownloadingLang } from '../LanguageProvider/LanguageProvider.actions'; + import { isAndroid } from '../../cordova-util'; export class SpeechProvider extends Component { static propTypes = { children: PropTypes.node.isRequired, ttsEngine: PropTypes.object, - setTtsEngine: PropTypes.func + setTtsEngine: PropTypes.func, + downloadingLang: PropTypes.object, + setDownloadingLang: PropTypes.func }; async componentDidMount() { @@ -26,7 +30,9 @@ export class SpeechProvider extends Component { getTtsEngines, getTtsDefaultEngine, ttsEngine, - setTtsEngine + setTtsEngine, + downloadingLang, + setDownloadingLang } = this.props; if (tts.isSupported()) { @@ -36,8 +42,14 @@ export class SpeechProvider extends Component { getTtsDefaultEngine(); } if (ttsEngine && ttsEngine.name) { + const ttsEnginesName = + downloadingLang?.isdownloading && + downloadingLang.engineName !== ttsEngine.name + ? downloadingLang.engineName + : ttsEngine.name; try { - await setTtsEngine(ttsEngine.name); + await setTtsEngine(ttsEnginesName); + setDownloadingLang({ isdownloading: false }); } catch (err) { console.error(err.message); } @@ -59,7 +71,9 @@ export class SpeechProvider extends Component { } const mapStateToProps = state => ({ - ttsEngine: state.speech.ttsEngine + ttsEngine: state.speech.ttsEngine, + //todo: downloadingVoices + downloadingLang: state.language.downloadingLang }); const mapDispatchToProps = { @@ -67,7 +81,9 @@ const mapDispatchToProps = { getTtsEngines, getTtsDefaultEngine, setTtsEngine, - updateLangSpeechStatus + updateLangSpeechStatus, + //todo: setDownloadingVoices + setDownloadingLang }; export default connect( From f919a40116229a4bf15eb7dac73be30d47f5be81 Mon Sep 17 00:00:00 2001 From: martin bedouret Date: Fri, 25 Mar 2022 10:10:57 -0300 Subject: [PATCH 2/7] wip --- .../Settings/Language/Language.container.js | 119 ++++++++---------- .../SpeechProvider/SpeechProvider.actions.js | 6 +- .../SpeechProvider.container.js | 6 +- 3 files changed, 59 insertions(+), 72 deletions(-) diff --git a/src/components/Settings/Language/Language.container.js b/src/components/Settings/Language/Language.container.js index 99d78bf19..819c68699 100644 --- a/src/components/Settings/Language/Language.container.js +++ b/src/components/Settings/Language/Language.container.js @@ -59,10 +59,6 @@ export class LanguageContainer extends Component { * TTS default engine */ ttsDefaultEngine: PropTypes.object, - /** - * list of available voices - */ - voices: PropTypes.array, /** * Callback fired when language changes */ @@ -83,14 +79,31 @@ export class LanguageContainer extends Component { state = { selectedLang: this.props.lang, openDialog: { open: false, downloadingLangData: {} }, - downloadablesLangs: { - avaliableAndDownloadablesLangs: [], + downloadableLangs: { + avaliableAndDownloadableLangs: [], downloadablesOnly: [] }, downloadLangLoading: true, downloadingLangError: { ttsError: false, langError: false } }; + componentDidMount = async () => { + const { isdownloading } = this.props.downloadingLang; + + this.setState({ + downloadableLangs: isAndroid() + ? this.prepareDownloadableLangs() + : { + avaliableAndDownloadableLangs: [], + downloadablesOnly: [] + } + }); + console.log(this.prepareDownloadableLangs()); + + if (isdownloading) await this.lookDownloadingLang(); + this.setState({ downloadLangLoading: false }); + }; + handleSubmit = async (optionalLang = null) => { const { onLangChange } = this.props; const selectedLang = optionalLang ? optionalLang : this.state.selectedLang; @@ -131,10 +144,10 @@ export class LanguageContainer extends Component { }; downloadableLangClick = (event, downloadingLangData) => { - const { avaliableAndDownloadablesLangs } = this.state.downloadablesLangs; + const { avaliableAndDownloadableLangs } = this.state.downloadableLangs; const { lang: appLang, localLangs } = this.props; const continueOnline = - avaliableAndDownloadablesLangs + avaliableAndDownloadableLangs .map(langTtsData => langTtsData.lang) .filter(availableLang => !localLangs.includes(availableLang)) .includes(downloadingLangData.lang) && @@ -157,10 +170,10 @@ export class LanguageContainer extends Component { }; onDownloadLocalVoiceClick = (event, downloadingLangData) => { - const { avaliableAndDownloadablesLangs } = this.state.downloadablesLangs; + const { avaliableAndDownloadableLangs } = this.state.downloadableLangs; const { localLangs, lang: appLang } = this.props; const continueOnline = - avaliableAndDownloadablesLangs + avaliableAndDownloadableLangs .map(langTtsData => langTtsData.lang) .filter(availableLang => !localLangs.includes(availableLang)) .includes(downloadingLangData.lang) && @@ -206,17 +219,17 @@ export class LanguageContainer extends Component { this.setState({ openDialog: { open: false, downloadingLangData: {} } }); }; - prepareDownloadablesLenguages = () => { + prepareDownloadableLangs = () => { const getDownloadablesLenguages = downloadablesTts => { - const formatLangObject = downloadablesLangs => { - return downloadablesLangs.map(langObject => { + const formatLangObject = downloadableLangs => { + return downloadableLangs.map(langObject => { //const code = ISO6391.getCode(langObject.lang); const { lang } = langObject; const code = lang.slice(0, 2).toLowerCase(); langObject.langCode = code; langObject.nativeName = ISO6391.getNativeName(code); const showLangCode = - downloadablesLangs.filter(language => language.langCode === code) + downloadableLangs.filter(language => language.langCode === code) .length > 1; const langFullCode = showLangCode ? `(${lang})` : ''; @@ -224,10 +237,10 @@ export class LanguageContainer extends Component { return langObject; }); }; - const downloadablesLangsArray = downloadablesTts.map(tts => { + const downloadableLangsArray = downloadablesTts.map(tts => { return { langs: tts.langs, marketId: tts.marketId, ttsName: tts.name }; }); - const identifiedLangsArray = downloadablesLangsArray.map(langObject => + const identifiedLangsArray = downloadableLangsArray.map(langObject => langObject.langs.map(language => { return { lang: language, @@ -237,19 +250,19 @@ export class LanguageContainer extends Component { }) ); const INITIAL_VALUE = []; - const downloadablesLangs = identifiedLangsArray.reduce( + const downloadableLangs = identifiedLangsArray.reduce( (accumulator, currentValue) => accumulator.concat(currentValue), INITIAL_VALUE ); - return formatLangObject(downloadablesLangs); + return formatLangObject(downloadableLangs); }; - const filterAvailablesAndDownloadablesLangs = downloadablesLangs => { + const filterAvailablesAndDownloadableLangs = downloadableLangs => { const { ttsEngines } = this.props; const { langs } = this.props; const ttsEnginesNames = ttsEngines.map(tts => tts.name); - const availableAndDownloadableLang = downloadablesLangs.filter( + const availableAndDownloadableLang = downloadableLangs.filter( ({ lang, ttsName }) => langs.includes(lang) ); @@ -269,16 +282,16 @@ export class LanguageContainer extends Component { ); }; - const downloadablesLangsList = getDownloadablesLenguages(downloadablesTts); - const avaliableAndDownloadablesLangs = filterAvailablesAndDownloadablesLangs( - downloadablesLangsList + const downloadableLangsList = getDownloadablesLenguages(downloadablesTts); + const avaliableAndDownloadableLangs = filterAvailablesAndDownloadableLangs( + downloadableLangsList ); const downloadablesOnly = filterDownloadablesOnlyLangs( - downloadablesLangsList, - avaliableAndDownloadablesLangs + downloadableLangsList, + avaliableAndDownloadableLangs ); return { - avaliableAndDownloadablesLangs, + avaliableAndDownloadableLangs, downloadablesOnly }; }; @@ -305,10 +318,10 @@ export class LanguageContainer extends Component { lang: appLang } = this.props; const { ttsName, lang, marketId } = downloadingLangData; - const { avaliableAndDownloadablesLangs } = this.state.downloadablesLangs; + const { avaliableAndDownloadableLangs } = this.state.downloadableLangs; const continueOnline = - avaliableAndDownloadablesLangs + avaliableAndDownloadableLangs .map(langTtsData => langTtsData.lang) .filter(availableLang => !localLangs.includes(availableLang)) .includes(downloadingLangData.lang) && appLang !== lang @@ -433,21 +446,10 @@ export class LanguageContainer extends Component { } }); return; - } - if (ttsEngine.name !== engineName) { - try { - await this.handleSetTtsEngine(engineName); - } catch { - this.setState({ - downloadingLangError: { - ttsError: false, - langError: true - } - }); - return; - } - } - if (!localLangs.includes(selectedLang)) { + } else if ( + ttsEngine.name !== engineName || + !localLangs.includes(selectedLang) + ) { this.setState({ downloadingLangError: { ttsError: false, @@ -456,11 +458,10 @@ export class LanguageContainer extends Component { }); return; } - const downloadingLangState = { - isdownloading: false - }; - setDownloadingLang(downloadingLangState); + + setDownloadingLang({ isdownloading: false }); this.setState({ selectedLang: selectedLang }); + if (isDiferentTts) return; await this.handleSubmit(selectedLang); showNotification( @@ -469,23 +470,6 @@ export class LanguageContainer extends Component { history.push('/settings'); }; - componentDidMount = async () => { - const { isdownloading } = this.props.downloadingLang; - - this.setState({ - downloadablesLangs: isAndroid() - ? this.prepareDownloadablesLenguages() - : { - //downloadablesLangsList: [] - avaliableAndDownloadablesLangs: [], - downloadablesOnly: [] - } - }); - - if (isdownloading) await this.lookDownloadingLang(); - this.setState({ downloadLangLoading: false }); - }; - render() { const { lang, langs, localLangs, ttsEngines, ttsEngine } = this.props; @@ -493,7 +477,7 @@ export class LanguageContainer extends Component { openDialog, downloadingLangError, downloadLangLoading, - downloadablesLangs + downloadableLangs } = this.state; const { open, downloadingLangData } = openDialog; @@ -512,7 +496,7 @@ export class LanguageContainer extends Component { onClose={this.onClose} onSubmitLang={this.handleSubmit} onSetTtsEngine={this.handleSetTtsEngine} - downloadablesLangs={downloadablesLangs} + downloadableLangs={downloadableLangs} onDownloadableLangClick={this.downloadableLangClick} onUninstalledLangClick={this.onUninstalledLangClick} langOnAvailableTtsClick={this.langOnAvailableTtsClick} @@ -545,8 +529,7 @@ const mapStateToProps = state => ({ localLangs: state.language.localLangs, ttsEngines: state.speech.ttsEngines, ttsEngine: state.speech.ttsEngine, - downloadingLang: state.language.downloadingLang, - voices: state.speech.voices + downloadingLang: state.language.downloadingLang }); const mapDispatchToProps = { diff --git a/src/providers/SpeechProvider/SpeechProvider.actions.js b/src/providers/SpeechProvider/SpeechProvider.actions.js index 54ce28b2b..c17fddb16 100644 --- a/src/providers/SpeechProvider/SpeechProvider.actions.js +++ b/src/providers/SpeechProvider/SpeechProvider.actions.js @@ -74,7 +74,7 @@ export function setTtsEngine(ttsEngineName) { }; } -export function updateLangSpeechStatus(voices) { +export function updateLangSpeechStatus(voices, forceChangeVoice = false) { return async (dispatch, getState) => { try { const supportedLangs = getSupportedLangs(voices); @@ -95,7 +95,9 @@ export function updateLangSpeechStatus(voices) { // last step is to change voice in case it is available if ( - getState().speech.options.lang.substring(0, 2) !== lang.substring(0, 2) + getState().speech.options.lang.substring(0, 2) !== + lang.substring(0, 2) || + forceChangeVoice ) { const uris = voices.map(v => { return v.voiceURI; diff --git a/src/providers/SpeechProvider/SpeechProvider.container.js b/src/providers/SpeechProvider/SpeechProvider.container.js index 8a3a57840..5caf5744a 100644 --- a/src/providers/SpeechProvider/SpeechProvider.container.js +++ b/src/providers/SpeechProvider/SpeechProvider.container.js @@ -35,6 +35,7 @@ export class SpeechProvider extends Component { setDownloadingLang } = this.props; + let forceChangeVoice = false; if (tts.isSupported()) { //if android we have to set the tts engine first if (isAndroid()) { @@ -49,14 +50,15 @@ export class SpeechProvider extends Component { : ttsEngine.name; try { await setTtsEngine(ttsEnginesName); - setDownloadingLang({ isdownloading: false }); + // setDownloadingLang({ isdownloading: false }); + forceChangeVoice = true; } catch (err) { console.error(err.message); } } try { const voices = await getVoices(); - await updateLangSpeechStatus(voices); + await updateLangSpeechStatus(voices, forceChangeVoice); } catch (err) { console.error(err.message); } From 828e9ce7f7b32fa712a183ec70df1c68d7af8987 Mon Sep 17 00:00:00 2001 From: martin bedouret Date: Mon, 28 Mar 2022 12:24:36 -0300 Subject: [PATCH 3/7] wip --- .../Settings/Language/Language.container.js | 79 ++++++++++--------- .../SpeechProvider/SpeechProvider.actions.js | 6 +- .../SpeechProvider.container.js | 6 +- 3 files changed, 45 insertions(+), 46 deletions(-) diff --git a/src/components/Settings/Language/Language.container.js b/src/components/Settings/Language/Language.container.js index 819c68699..24bf97ec6 100644 --- a/src/components/Settings/Language/Language.container.js +++ b/src/components/Settings/Language/Language.container.js @@ -79,8 +79,8 @@ export class LanguageContainer extends Component { state = { selectedLang: this.props.lang, openDialog: { open: false, downloadingLangData: {} }, - downloadableLangs: { - avaliableAndDownloadableLangs: [], + downloadablesLangs: { + avaliableAndDownloadablesLangs: [], downloadablesOnly: [] }, downloadLangLoading: true, @@ -91,14 +91,14 @@ export class LanguageContainer extends Component { const { isdownloading } = this.props.downloadingLang; this.setState({ - downloadableLangs: isAndroid() - ? this.prepareDownloadableLangs() + downloadablesLangs: isAndroid() + ? this.prepareDownloadablesLenguages() : { - avaliableAndDownloadableLangs: [], + //downloadablesLangsList: [] + avaliableAndDownloadablesLangs: [], downloadablesOnly: [] } }); - console.log(this.prepareDownloadableLangs()); if (isdownloading) await this.lookDownloadingLang(); this.setState({ downloadLangLoading: false }); @@ -144,10 +144,10 @@ export class LanguageContainer extends Component { }; downloadableLangClick = (event, downloadingLangData) => { - const { avaliableAndDownloadableLangs } = this.state.downloadableLangs; + const { avaliableAndDownloadablesLangs } = this.state.downloadablesLangs; const { lang: appLang, localLangs } = this.props; const continueOnline = - avaliableAndDownloadableLangs + avaliableAndDownloadablesLangs .map(langTtsData => langTtsData.lang) .filter(availableLang => !localLangs.includes(availableLang)) .includes(downloadingLangData.lang) && @@ -170,10 +170,10 @@ export class LanguageContainer extends Component { }; onDownloadLocalVoiceClick = (event, downloadingLangData) => { - const { avaliableAndDownloadableLangs } = this.state.downloadableLangs; + const { avaliableAndDownloadablesLangs } = this.state.downloadablesLangs; const { localLangs, lang: appLang } = this.props; const continueOnline = - avaliableAndDownloadableLangs + avaliableAndDownloadablesLangs .map(langTtsData => langTtsData.lang) .filter(availableLang => !localLangs.includes(availableLang)) .includes(downloadingLangData.lang) && @@ -219,17 +219,17 @@ export class LanguageContainer extends Component { this.setState({ openDialog: { open: false, downloadingLangData: {} } }); }; - prepareDownloadableLangs = () => { + prepareDownloadablesLenguages = () => { const getDownloadablesLenguages = downloadablesTts => { - const formatLangObject = downloadableLangs => { - return downloadableLangs.map(langObject => { + const formatLangObject = downloadablesLangs => { + return downloadablesLangs.map(langObject => { //const code = ISO6391.getCode(langObject.lang); const { lang } = langObject; const code = lang.slice(0, 2).toLowerCase(); langObject.langCode = code; langObject.nativeName = ISO6391.getNativeName(code); const showLangCode = - downloadableLangs.filter(language => language.langCode === code) + downloadablesLangs.filter(language => language.langCode === code) .length > 1; const langFullCode = showLangCode ? `(${lang})` : ''; @@ -237,10 +237,10 @@ export class LanguageContainer extends Component { return langObject; }); }; - const downloadableLangsArray = downloadablesTts.map(tts => { + const downloadablesLangsArray = downloadablesTts.map(tts => { return { langs: tts.langs, marketId: tts.marketId, ttsName: tts.name }; }); - const identifiedLangsArray = downloadableLangsArray.map(langObject => + const identifiedLangsArray = downloadablesLangsArray.map(langObject => langObject.langs.map(language => { return { lang: language, @@ -250,19 +250,19 @@ export class LanguageContainer extends Component { }) ); const INITIAL_VALUE = []; - const downloadableLangs = identifiedLangsArray.reduce( + const downloadablesLangs = identifiedLangsArray.reduce( (accumulator, currentValue) => accumulator.concat(currentValue), INITIAL_VALUE ); - return formatLangObject(downloadableLangs); + return formatLangObject(downloadablesLangs); }; - const filterAvailablesAndDownloadableLangs = downloadableLangs => { + const filterAvailablesAndDownloadablesLangs = downloadablesLangs => { const { ttsEngines } = this.props; const { langs } = this.props; const ttsEnginesNames = ttsEngines.map(tts => tts.name); - const availableAndDownloadableLang = downloadableLangs.filter( + const availableAndDownloadableLang = downloadablesLangs.filter( ({ lang, ttsName }) => langs.includes(lang) ); @@ -282,16 +282,16 @@ export class LanguageContainer extends Component { ); }; - const downloadableLangsList = getDownloadablesLenguages(downloadablesTts); - const avaliableAndDownloadableLangs = filterAvailablesAndDownloadableLangs( - downloadableLangsList + const downloadablesLangsList = getDownloadablesLenguages(downloadablesTts); + const avaliableAndDownloadablesLangs = filterAvailablesAndDownloadablesLangs( + downloadablesLangsList ); const downloadablesOnly = filterDownloadablesOnlyLangs( - downloadableLangsList, - avaliableAndDownloadableLangs + downloadablesLangsList, + avaliableAndDownloadablesLangs ); return { - avaliableAndDownloadableLangs, + avaliableAndDownloadablesLangs, downloadablesOnly }; }; @@ -318,10 +318,10 @@ export class LanguageContainer extends Component { lang: appLang } = this.props; const { ttsName, lang, marketId } = downloadingLangData; - const { avaliableAndDownloadableLangs } = this.state.downloadableLangs; + const { avaliableAndDownloadablesLangs } = this.state.downloadablesLangs; const continueOnline = - avaliableAndDownloadableLangs + avaliableAndDownloadablesLangs .map(langTtsData => langTtsData.lang) .filter(availableLang => !localLangs.includes(availableLang)) .includes(downloadingLangData.lang) && appLang !== lang @@ -450,18 +450,21 @@ export class LanguageContainer extends Component { ttsEngine.name !== engineName || !localLangs.includes(selectedLang) ) { - this.setState({ - downloadingLangError: { - ttsError: false, - langError: true - } - }); - return; + try { + await this.handleSetTtsEngine(engineName); + } catch { + this.setState({ + downloadingLangError: { + ttsError: false, + langError: true + } + }); + return; + } } setDownloadingLang({ isdownloading: false }); this.setState({ selectedLang: selectedLang }); - if (isDiferentTts) return; await this.handleSubmit(selectedLang); showNotification( @@ -477,7 +480,7 @@ export class LanguageContainer extends Component { openDialog, downloadingLangError, downloadLangLoading, - downloadableLangs + downloadablesLangs } = this.state; const { open, downloadingLangData } = openDialog; @@ -496,7 +499,7 @@ export class LanguageContainer extends Component { onClose={this.onClose} onSubmitLang={this.handleSubmit} onSetTtsEngine={this.handleSetTtsEngine} - downloadableLangs={downloadableLangs} + downloadablesLangs={downloadablesLangs} onDownloadableLangClick={this.downloadableLangClick} onUninstalledLangClick={this.onUninstalledLangClick} langOnAvailableTtsClick={this.langOnAvailableTtsClick} diff --git a/src/providers/SpeechProvider/SpeechProvider.actions.js b/src/providers/SpeechProvider/SpeechProvider.actions.js index c17fddb16..54ce28b2b 100644 --- a/src/providers/SpeechProvider/SpeechProvider.actions.js +++ b/src/providers/SpeechProvider/SpeechProvider.actions.js @@ -74,7 +74,7 @@ export function setTtsEngine(ttsEngineName) { }; } -export function updateLangSpeechStatus(voices, forceChangeVoice = false) { +export function updateLangSpeechStatus(voices) { return async (dispatch, getState) => { try { const supportedLangs = getSupportedLangs(voices); @@ -95,9 +95,7 @@ export function updateLangSpeechStatus(voices, forceChangeVoice = false) { // last step is to change voice in case it is available if ( - getState().speech.options.lang.substring(0, 2) !== - lang.substring(0, 2) || - forceChangeVoice + getState().speech.options.lang.substring(0, 2) !== lang.substring(0, 2) ) { const uris = voices.map(v => { return v.voiceURI; diff --git a/src/providers/SpeechProvider/SpeechProvider.container.js b/src/providers/SpeechProvider/SpeechProvider.container.js index 5caf5744a..8a3a57840 100644 --- a/src/providers/SpeechProvider/SpeechProvider.container.js +++ b/src/providers/SpeechProvider/SpeechProvider.container.js @@ -35,7 +35,6 @@ export class SpeechProvider extends Component { setDownloadingLang } = this.props; - let forceChangeVoice = false; if (tts.isSupported()) { //if android we have to set the tts engine first if (isAndroid()) { @@ -50,15 +49,14 @@ export class SpeechProvider extends Component { : ttsEngine.name; try { await setTtsEngine(ttsEnginesName); - // setDownloadingLang({ isdownloading: false }); - forceChangeVoice = true; + setDownloadingLang({ isdownloading: false }); } catch (err) { console.error(err.message); } } try { const voices = await getVoices(); - await updateLangSpeechStatus(voices, forceChangeVoice); + await updateLangSpeechStatus(voices); } catch (err) { console.error(err.message); } From 3252d4102772219cd116bc50f08efebe228b81c7 Mon Sep 17 00:00:00 2001 From: martin bedouret Date: Wed, 30 Mar 2022 11:49:04 -0300 Subject: [PATCH 4/7] initial draft --- .../Settings/Language/Language.container.js | 59 +++++++++++-------- .../SpeechProvider/SpeechProvider.actions.js | 6 +- .../SpeechProvider.container.js | 6 +- 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/components/Settings/Language/Language.container.js b/src/components/Settings/Language/Language.container.js index 24bf97ec6..dc256652d 100644 --- a/src/components/Settings/Language/Language.container.js +++ b/src/components/Settings/Language/Language.container.js @@ -83,25 +83,32 @@ export class LanguageContainer extends Component { avaliableAndDownloadablesLangs: [], downloadablesOnly: [] }, - downloadLangLoading: true, + downloadLangLoading: false, downloadingLangError: { ttsError: false, langError: false } }; componentDidMount = async () => { const { isdownloading } = this.props.downloadingLang; - this.setState({ downloadablesLangs: isAndroid() ? this.prepareDownloadablesLenguages() : { - //downloadablesLangsList: [] avaliableAndDownloadablesLangs: [], downloadablesOnly: [] } }); + if (isdownloading) { + this.setState({ downloadLangLoading: true }); + } else { + this.setState({ downloadLangLoading: false }); + } + }; - if (isdownloading) await this.lookDownloadingLang(); - this.setState({ downloadLangLoading: false }); + componentDidUpdate = async () => { + const { isdownloading, isUpdated } = this.props.downloadingLang; + if (isdownloading && isUpdated) { + await this.lookDownloadingLang(); + } }; handleSubmit = async (optionalLang = null) => { @@ -208,6 +215,7 @@ export class LanguageContainer extends Component { isDiferentTts: false, engineName: ttsName, marketId: marketId, + isUpdated: false, selectedLang: lang }; this.props.setDownloadingLang(downloadingLangState); @@ -354,6 +362,7 @@ export class LanguageContainer extends Component { engineName: ttsName, marketId: marketId, selectedLang: lang, + isUpdated: false, continueOnline, firstClick }; @@ -437,6 +446,19 @@ export class LanguageContainer extends Component { showNotification } = this.props; + this.setState({ downloadLangLoading: false }); + + if (engineName === ttsEngine.name && localLangs.includes(selectedLang)) { + await setDownloadingLang({ isdownloading: false }); + this.setState({ selectedLang: selectedLang }); + if (isDiferentTts) return; + await this.handleSubmit(selectedLang); + showNotification( + + ); + history.push('/settings'); + return; + } const ttsEnginesNames = ttsEngines.map(tts => tts.name); if (!ttsEnginesNames.includes(engineName)) { this.setState({ @@ -445,32 +467,17 @@ export class LanguageContainer extends Component { ttsError: true } }); - return; } else if ( ttsEngine.name !== engineName || !localLangs.includes(selectedLang) ) { - try { - await this.handleSetTtsEngine(engineName); - } catch { - this.setState({ - downloadingLangError: { - ttsError: false, - langError: true - } - }); - return; - } + this.setState({ + downloadingLangError: { + ttsError: false, + langError: true + } + }); } - - setDownloadingLang({ isdownloading: false }); - this.setState({ selectedLang: selectedLang }); - if (isDiferentTts) return; - await this.handleSubmit(selectedLang); - showNotification( - - ); - history.push('/settings'); }; render() { diff --git a/src/providers/SpeechProvider/SpeechProvider.actions.js b/src/providers/SpeechProvider/SpeechProvider.actions.js index 54ce28b2b..c17fddb16 100644 --- a/src/providers/SpeechProvider/SpeechProvider.actions.js +++ b/src/providers/SpeechProvider/SpeechProvider.actions.js @@ -74,7 +74,7 @@ export function setTtsEngine(ttsEngineName) { }; } -export function updateLangSpeechStatus(voices) { +export function updateLangSpeechStatus(voices, forceChangeVoice = false) { return async (dispatch, getState) => { try { const supportedLangs = getSupportedLangs(voices); @@ -95,7 +95,9 @@ export function updateLangSpeechStatus(voices) { // last step is to change voice in case it is available if ( - getState().speech.options.lang.substring(0, 2) !== lang.substring(0, 2) + getState().speech.options.lang.substring(0, 2) !== + lang.substring(0, 2) || + forceChangeVoice ) { const uris = voices.map(v => { return v.voiceURI; diff --git a/src/providers/SpeechProvider/SpeechProvider.container.js b/src/providers/SpeechProvider/SpeechProvider.container.js index 8a3a57840..380ff1b42 100644 --- a/src/providers/SpeechProvider/SpeechProvider.container.js +++ b/src/providers/SpeechProvider/SpeechProvider.container.js @@ -35,6 +35,7 @@ export class SpeechProvider extends Component { setDownloadingLang } = this.props; + let forceChangeVoice = false; if (tts.isSupported()) { //if android we have to set the tts engine first if (isAndroid()) { @@ -49,17 +50,18 @@ export class SpeechProvider extends Component { : ttsEngine.name; try { await setTtsEngine(ttsEnginesName); - setDownloadingLang({ isdownloading: false }); + forceChangeVoice = true; } catch (err) { console.error(err.message); } } try { const voices = await getVoices(); - await updateLangSpeechStatus(voices); + await updateLangSpeechStatus(voices, forceChangeVoice); } catch (err) { console.error(err.message); } + setDownloadingLang({ ...downloadingLang, isUpdated: true }); } } From d637c0b962e1b7d36611c39d51298402a038d4cc Mon Sep 17 00:00:00 2001 From: martin bedouret Date: Thu, 24 Mar 2022 17:44:26 -0300 Subject: [PATCH 5/7] Revert "Merge pull request #1162 from tomivm/fix/download-Language" This reverts commit 36e96733c6c7b4c034cd506985cdab6f01e8f00b. --- .../Settings/Language/Language.component.js | 2 +- .../Settings/Language/Language.container.js | 54 +++++++------------ .../LanguageProvider.reducer.js | 4 +- .../SpeechProvider/SpeechProvider.actions.js | 15 ++---- src/reducers.js | 13 ++--- 5 files changed, 27 insertions(+), 61 deletions(-) diff --git a/src/components/Settings/Language/Language.component.js b/src/components/Settings/Language/Language.component.js index cfe162a20..6bb0c84d4 100644 --- a/src/components/Settings/Language/Language.component.js +++ b/src/components/Settings/Language/Language.component.js @@ -205,7 +205,7 @@ class Language extends React.Component { const isDownloadable = avaliableAndDownloadablesLangs.filter( downloadableLang => { - return downloadableLang.lang === lang; + return downloadableLang.langCode === lang.slice(0, 2); } )[0]; diff --git a/src/components/Settings/Language/Language.container.js b/src/components/Settings/Language/Language.container.js index 245215978..d8e66e1c7 100644 --- a/src/components/Settings/Language/Language.container.js +++ b/src/components/Settings/Language/Language.container.js @@ -345,6 +345,18 @@ export class LanguageContainer extends Component { firstClick }; setDownloadingLang(downloadingLangState); + try { + await this.handleSetTtsEngine(ttsName); //after tts change it fires a remounting + } catch { + //if tts hasn't any voice SetTtsEngine throw an error and the user would be alerted that should open the tts app + this.setState({ + downloadingLangError: { + ttsError: false, + langError: true + }, + downloadLangLoading: false + }); + } }; onErrorDialogAcepted = () => { @@ -403,7 +415,7 @@ export class LanguageContainer extends Component { const { setDownloadingLang, - + localLangs, ttsEngines, ttsEngine, history, @@ -430,9 +442,9 @@ export class LanguageContainer extends Component { langError: true } }); + return; } } - const localLangs = this.props.localLangs; if (!localLangs.includes(selectedLang)) { this.setState({ downloadingLangError: { @@ -446,14 +458,7 @@ export class LanguageContainer extends Component { isdownloading: false }; setDownloadingLang(downloadingLangState); - this.setState({ - downloadingLangError: { - ttsError: false, - langError: false - }, - selectedLang: selectedLang - }); - this.refreshLanguageList(); + this.setState({ selectedLang: selectedLang }); if (isDiferentTts) return; await this.handleSubmit(selectedLang); showNotification( @@ -462,7 +467,9 @@ export class LanguageContainer extends Component { history.push('/settings'); }; - refreshLanguageList = () => { + componentDidMount = async () => { + const { isdownloading } = this.props.downloadingLang; + this.setState({ downloadablesLangs: isAndroid() ? this.prepareDownloadablesLenguages() @@ -472,35 +479,11 @@ export class LanguageContainer extends Component { downloadablesOnly: [] } }); - }; - - refreshDownloadLanguage = async () => { - const { isdownloading } = this.props.downloadingLang; - - this.refreshLanguageList(); if (isdownloading) await this.lookDownloadingLang(); this.setState({ downloadLangLoading: false }); }; - componentDidMount = async () => { - if (this.props.langsFetched) this.refreshDownloadLanguage(); - }; - - componentDidUpdate = async prevProps => { - const isdownloading = this.props.downloadingLang?.isdownloading; - const langsFetched = this.props.langsFetched; - - if (!prevProps.langsFetched && langsFetched) { - this.setState({ downloadLangLoading: true }); - await this.refreshDownloadLanguage(); - } - if (!isdownloading) return; - if (prevProps.downloadingLang.isdownloading === false) { - await this.refreshDownloadLanguage(); - } - }; - render() { const { lang, langs, localLangs, ttsEngines, ttsEngine } = this.props; @@ -556,7 +539,6 @@ export class LanguageContainer extends Component { const mapStateToProps = state => ({ lang: state.language.lang, - langsFetched: state.language.langsFetched, langs: state.language.langs, localLangs: state.language.localLangs, ttsEngines: state.speech.ttsEngines, diff --git a/src/providers/LanguageProvider/LanguageProvider.reducer.js b/src/providers/LanguageProvider/LanguageProvider.reducer.js index 68a17418d..3b22429ad 100644 --- a/src/providers/LanguageProvider/LanguageProvider.reducer.js +++ b/src/providers/LanguageProvider/LanguageProvider.reducer.js @@ -16,7 +16,6 @@ const initialState = { dir: 'ltr', langs: [], localLangs: [], - langsFetched: false, downloadingLang: { isdownloading: false } }; @@ -45,8 +44,7 @@ function languageProviderReducer(state = initialState, action) { return { ...state, langs: action.langs.sort(), - localLangs: action.localLangs || [], - langsFetched: true + localLangs: action.localLangs || [] }; case SET_DOWNLOADING_LANG: return { ...state, downloadingLang: action.downloadingLangData }; diff --git a/src/providers/SpeechProvider/SpeechProvider.actions.js b/src/providers/SpeechProvider/SpeechProvider.actions.js index 403a5f2c6..54ce28b2b 100644 --- a/src/providers/SpeechProvider/SpeechProvider.actions.js +++ b/src/providers/SpeechProvider/SpeechProvider.actions.js @@ -52,26 +52,19 @@ export function receiveTtsEngine(ttsEngineName) { } export function getTtsEngines() { - const ttsEngines = tts?.getTtsEngines(); + const ttsEngines = tts.getTtsEngines(); return { type: RECEIVE_TTS_ENGINES, ttsEngines }; } -export function setTtsEngine(selectedTtsEngineName) { +export function setTtsEngine(ttsEngineName) { return async dispatch => { dispatch(requestTtsEngine()); try { - const engineAvailable = tts - .getTtsEngines() - .map(tts => tts.name) - .includes(selectedTtsEngineName); - const engineName = engineAvailable - ? selectedTtsEngineName - : tts.getTtsDefaultEngine().name; - const voices = await tts.setTtsEngine(engineName); - dispatch(receiveTtsEngine(engineName)); + const voices = await tts.setTtsEngine(ttsEngineName); + dispatch(receiveTtsEngine(ttsEngineName)); if (!voices.length) { throw new Error('TTS engine does not have a language.'); } diff --git a/src/reducers.js b/src/reducers.js index 47621ad32..c30c9acad 100644 --- a/src/reducers.js +++ b/src/reducers.js @@ -1,4 +1,4 @@ -import { persistCombineReducers, persistReducer } from 'redux-persist'; +import { persistCombineReducers } from 'redux-persist'; import appReducer from './components/App/App.reducer'; import languageProviderReducer from './providers/LanguageProvider/LanguageProvider.reducer'; @@ -11,20 +11,13 @@ import storage from 'redux-persist/lib/storage'; const config = { key: 'root', - storage, - blacklist: ['language'] -}; - -const languagePersistConfig = { - key: 'language', - storage: storage, - blacklist: ['langsFetched'] + storage }; export default function createReducer() { return persistCombineReducers(config, { app: appReducer, - language: persistReducer(languagePersistConfig, languageProviderReducer), + language: languageProviderReducer, speech: speechProviderReducer, board: boardReducer, communicator: communicatorReducer, From b0324c669ed3ba13f86f45561e6957b8721f213c Mon Sep 17 00:00:00 2001 From: martin bedouret Date: Wed, 30 Mar 2022 21:50:23 -0300 Subject: [PATCH 6/7] fix --- .../Settings/Language/Language.container.js | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/src/components/Settings/Language/Language.container.js b/src/components/Settings/Language/Language.container.js index dc256652d..17681296c 100644 --- a/src/components/Settings/Language/Language.container.js +++ b/src/components/Settings/Language/Language.container.js @@ -366,21 +366,7 @@ export class LanguageContainer extends Component { continueOnline, firstClick }; - setDownloadingLang( - downloadingLangState - ); /* - try { - await this.handleSetTtsEngine(ttsName); //after tts change it fires a remounting - } catch { - //if tts hasn't any voice SetTtsEngine throw an error and the user would be alerted that should open the tts app - this.setState({ - downloadingLangError: { - ttsError: false, - langError: true - }, - downloadLangLoading: false - }); - } */ + setDownloadingLang(downloadingLangState); }; onErrorDialogAcepted = () => { From 7204a88e0cd40f1affce71d74d041acd01e60208 Mon Sep 17 00:00:00 2001 From: martin bedouret Date: Wed, 13 Apr 2022 16:02:57 -0300 Subject: [PATCH 7/7] review updates --- src/components/Settings/Language/Language.component.js | 2 +- src/components/Settings/Language/Language.container.js | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/components/Settings/Language/Language.component.js b/src/components/Settings/Language/Language.component.js index 6bb0c84d4..cfe162a20 100644 --- a/src/components/Settings/Language/Language.component.js +++ b/src/components/Settings/Language/Language.component.js @@ -205,7 +205,7 @@ class Language extends React.Component { const isDownloadable = avaliableAndDownloadablesLangs.filter( downloadableLang => { - return downloadableLang.langCode === lang.slice(0, 2); + return downloadableLang.lang === lang; } )[0]; diff --git a/src/components/Settings/Language/Language.container.js b/src/components/Settings/Language/Language.container.js index 17681296c..7c06d376d 100644 --- a/src/components/Settings/Language/Language.container.js +++ b/src/components/Settings/Language/Language.container.js @@ -97,11 +97,7 @@ export class LanguageContainer extends Component { downloadablesOnly: [] } }); - if (isdownloading) { - this.setState({ downloadLangLoading: true }); - } else { - this.setState({ downloadLangLoading: false }); - } + this.setState({ downloadLangLoading: !!isdownloading }); }; componentDidUpdate = async () => { @@ -435,7 +431,7 @@ export class LanguageContainer extends Component { this.setState({ downloadLangLoading: false }); if (engineName === ttsEngine.name && localLangs.includes(selectedLang)) { - await setDownloadingLang({ isdownloading: false }); + setDownloadingLang({ isdownloading: false }); this.setState({ selectedLang: selectedLang }); if (isDiferentTts) return; await this.handleSubmit(selectedLang);