Skip to content

Commit

Permalink
Proper error handlings , returning proper erorr messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihir Soni committed Nov 7, 2015
1 parent a6182ac commit b52005e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 37 deletions.
47 changes: 29 additions & 18 deletions index.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: mihir
* @Date: 2015-11-07
* @Last Modified by: mihir
* @Last Modified time: 2015-11-07
* @Last Modified time: 2015-11-08
*/

'use strict';
Expand All @@ -11,47 +11,58 @@ var AndroidTTS = require('react-native').NativeModules.AndroidTTS;
var RNAndroidTTS = {
speak(args) {
return new Promise((resolve, reject) => {
AndroidTTS.speak(args, (isSpeaking) =>{
console.log(isSpeaking);
if(typeof isSpeaking === 'boolean'){
if (!isSpeaking) {
AndroidTTS.speak(args, (error,result) =>{
console.log(error);
if (error) {
reject(error);
} else {
resolve(result);
}
resolve(true);
} else {
reject(isSpeaking);
}
});
});
});
},
isSpeaking(){
return new Promise((resolve,reject) => {
AndroidTTS.isSpeaking((result) => {
resolve(result);
AndroidTTS.isSpeaking((error,result) => {
if(error) {
reject(error)
} else {
resolve(result);
}
});
})
},
shutDown(){
return new Promise((resolve,reject) => {
AndroidTTS.shutDown((result)=>{
resolve(result)
AndroidTTS.shutDown((error,result)=>{
if(error) {
reject(error)
} else {
resolve(result);
}
});
});
},
stop(){
return new Promise((resolve,reject) => {
AndroidTTS.stop((result)=>{
resolve(result)
AndroidTTS.stop((error,result)=>{
if(error) {
reject(error)
} else {
resolve(result);
}
});
});
},
getLocales() {
return new Promise((resolve, reject) => {
AndroidTTS.getLocale((error, locales) =>{
AndroidTTS.getLocale((error, results) =>{
if (error) {
reject(error);
} else {
resolve(results);
}
resolve(locales);

});
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-android-speech",
"version": "0.0.1",
"version": "0.0.2",
"description": "This is simple module for React Native for Android Text to Speech Engine",
"main": "index.android.js",
"scripts": {
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/mihir/react/tts/ErrorUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.mihir.react.tts;

import android.support.annotation.Nullable;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;

/**
* Created by mihir on 11/8/15.
*/
public class ErrorUtils {

static WritableMap getError(@Nullable String key, String errorMessage) {
WritableMap errorMap = Arguments.createMap();
errorMap.putString("message", errorMessage);
if (key != null) {
errorMap.putString("key", key);
}
return errorMap;
}
}
38 changes: 20 additions & 18 deletions src/main/java/com/mihir/react/tts/RCTTextToSpeech.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
public class RCTTextToSpeech extends ReactContextBaseJavaModule{

private static TextToSpeech tts;
private int MY_DATA_CHECK_CODE = 0;

public RCTTextToSpeech(ReactApplicationContext reactContext) {
super(reactContext);
Expand Down Expand Up @@ -56,21 +55,25 @@ protected void doInBackgroundGuarded(Void... params) {
}
callback.invoke(null,data);
} catch (Exception e) {
callback.invoke(e,null);
callback.invoke(ErrorUtils.getError(null,e.getMessage()),null);
}
}
}.execute();
}

@ReactMethod
public void isSpeaking(final Callback successCallback){
public void isSpeaking(final Callback callback){
new GuardedAsyncTask<Void,Void>(getReactApplicationContext()){
@Override
protected void doInBackgroundGuarded(Void... params){
if(tts.isSpeaking()){
successCallback.invoke(true);
} else {
successCallback.invoke(false);
try {
if (tts.isSpeaking()) {
callback.invoke(null,true);
} else {
callback.invoke(null,false);
}
} catch (Exception e){
callback.invoke(ErrorUtils.getError(null,e.getMessage()),null);
}
}
}.execute();
Expand All @@ -87,17 +90,16 @@ public void onInit(int status) {
}

@ReactMethod
public void stop(final Callback successCallback){
public void stop(final Callback callback){
new GuardedAsyncTask<Void,Void>(getReactApplicationContext()){
@Override
protected void doInBackgroundGuarded(Void... params){
try{
tts.stop();
successCallback.invoke(true);
callback.invoke(null,true);

} catch (Exception e){
tts.stop();
successCallback.invoke(e);
callback.invoke(ErrorUtils.getError(null,e.getMessage()),null);
}
}
}.execute();
Expand All @@ -113,9 +115,9 @@ protected void doInBackgroundGuarded(Void... params){
}
try{
tts.shutdown();
callBack.invoke(true);
callBack.invoke(null,true);
} catch (Exception e){
callBack.invoke(e);
callBack.invoke(ErrorUtils.getError(null,e.getMessage()),null);
}
}
}.execute();
Expand All @@ -138,18 +140,18 @@ protected void doInBackgroundGuarded(Void... params) {
String language = args.hasKey("language") ? args.getString("language") : null;
Boolean forceStop = args.hasKey("forceStop") ? args.getBoolean("forceStop") : null;
Float pitch = args.hasKey("pitch") ? (float) args.getDouble("pitch") : null;

if(tts.isSpeaking()){
//Force to stop and start new speech
if(forceStop != null && forceStop){
tts.stop();
} else {
callback.invoke("TTS is already speaking something , Please shutdown or stop TTS and try again");
callback.invoke(ErrorUtils.getError(null,"TTS is already speaking something , Please shutdown or stop TTS and try again"),null);
return;
}
}
if(args.getString("text") == null || text == ""){
callback.invoke("Text can not be blank");
callback.invoke(ErrorUtils.getError(null,"t can not be blank"),null);
return;
}
try {
if (language != null && language != "") {
Expand All @@ -167,9 +169,9 @@ protected void doInBackgroundGuarded(Void... params) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null,null);
else
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
callback.invoke(true);
callback.invoke(null,true);
} catch (Exception e) {
callback.invoke(false);
callback.invoke(ErrorUtils.getError(null,e.getMessage()),null);
}
}
}.execute();
Expand Down

0 comments on commit b52005e

Please sign in to comment.