Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/full refactor downl lang review #1229

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 49 additions & 54 deletions src/components/Settings/Language/Language.container.js
Original file line number Diff line number Diff line change
@@ -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
*/
@@ -87,10 +83,34 @@ 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()
: {
avaliableAndDownloadablesLangs: [],
downloadablesOnly: []
}
});
if (isdownloading) {
this.setState({ downloadLangLoading: true });
} else {
this.setState({ downloadLangLoading: false });
}
};

componentDidUpdate = async () => {
const { isdownloading, isUpdated } = this.props.downloadingLang;
if (isdownloading && isUpdated) {
await this.lookDownloadingLang();
}
};

handleSubmit = async (optionalLang = null) => {
const { onLangChange } = this.props;
const selectedLang = optionalLang ? optionalLang : this.state.selectedLang;
@@ -195,6 +215,7 @@ export class LanguageContainer extends Component {
isDiferentTts: false,
engineName: ttsName,
marketId: marketId,
isUpdated: false,
selectedLang: lang
};
this.props.setDownloadingLang(downloadingLangState);
@@ -341,10 +362,13 @@ export class LanguageContainer extends Component {
engineName: ttsName,
marketId: marketId,
selectedLang: lang,
isUpdated: false,
continueOnline,
firstClick
};
setDownloadingLang(downloadingLangState);
setDownloadingLang(
downloadingLangState
); /*
try {
await this.handleSetTtsEngine(ttsName); //after tts change it fires a remounting
} catch {
@@ -356,7 +380,7 @@ export class LanguageContainer extends Component {
},
downloadLangLoading: false
});
}
} */
};

onErrorDialogAcepted = () => {
@@ -422,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(
<FormattedMessage {...messages.instaledLangSuccesNotification} />
);
history.push('/settings');
return;
}
const ttsEnginesNames = ttsEngines.map(tts => tts.name);
if (!ttsEnginesNames.includes(engineName)) {
this.setState({
@@ -430,58 +467,17 @@ export class LanguageContainer extends Component {
ttsError: true
}
});
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,
langError: true
}
});
return;
}
const downloadingLangState = {
isdownloading: false
};
setDownloadingLang(downloadingLangState);
this.setState({ selectedLang: selectedLang });
if (isDiferentTts) return;
await this.handleSubmit(selectedLang);
showNotification(
<FormattedMessage {...messages.instaledLangSuccesNotification} />
);
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() {
@@ -543,8 +539,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 = {
6 changes: 4 additions & 2 deletions src/providers/SpeechProvider/SpeechProvider.actions.js
Original file line number Diff line number Diff line change
@@ -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;
30 changes: 24 additions & 6 deletions src/providers/SpeechProvider/SpeechProvider.container.js
Original file line number Diff line number Diff line change
@@ -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,28 +30,38 @@ export class SpeechProvider extends Component {
getTtsEngines,
getTtsDefaultEngine,
ttsEngine,
setTtsEngine
setTtsEngine,
downloadingLang,
setDownloadingLang
} = this.props;

let forceChangeVoice = false;
if (tts.isSupported()) {
//if android we have to set the tts engine first
if (isAndroid()) {
getTtsEngines();
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);
forceChangeVoice = true;
tomivm marked this conversation as resolved.
Show resolved Hide resolved
} 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 });
}
}

@@ -59,15 +73,19 @@ export class SpeechProvider extends Component {
}

const mapStateToProps = state => ({
ttsEngine: state.speech.ttsEngine
ttsEngine: state.speech.ttsEngine,
//todo: downloadingVoices
downloadingLang: state.language.downloadingLang
});

const mapDispatchToProps = {
getVoices,
getTtsEngines,
getTtsDefaultEngine,
setTtsEngine,
updateLangSpeechStatus
updateLangSpeechStatus,
//todo: setDownloadingVoices
setDownloadingLang
};

export default connect(