Merge pull request #170 from corbt/promises

Switch from callbacks to promises
This commit is contained in:
Loch Wansbrough
2016-01-31 18:16:07 -08:00
6 changed files with 58 additions and 67 deletions
@@ -150,10 +150,10 @@ public class RCTCameraModule extends ReactContextBaseJavaModule {
}
@ReactMethod
public void capture(final ReadableMap options, final Callback callback) {
public void capture(final ReadableMap options, final Promise promise) {
Camera camera = RCTCamera.getInstance().acquireCameraInstance(options.getInt("type"));
if (null == camera) {
callback.invoke("No camera found.", null);
promise.reject("No camera found.");
return;
}
camera.takePicture(null, null, new Camera.PictureCallback() {
@@ -162,7 +162,7 @@ public class RCTCameraModule extends ReactContextBaseJavaModule {
switch (options.getInt("target")) {
case RCT_CAMERA_CAPTURE_TARGET_MEMORY:
String encoded = Base64.encodeToString(data, Base64.DEFAULT);
callback.invoke(null, encoded);
promise.resolve(encoded);
break;
case RCT_CAMERA_CAPTURE_TARGET_CAMERA_ROLL:
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
@@ -171,12 +171,12 @@ public class RCTCameraModule extends ReactContextBaseJavaModule {
_reactContext.getContentResolver(),
bitmap, options.getString("title"),
options.getString("description"));
callback.invoke(null, url);
promise.resolve(url);
break;
case RCT_CAMERA_CAPTURE_TARGET_DISK:
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null) {
callback.invoke("Error creating media file.", null);
promise.reject("Error creating media file.");
return;
}
@@ -185,11 +185,11 @@ public class RCTCameraModule extends ReactContextBaseJavaModule {
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
callback.invoke("File not found: " + e.getMessage(), null);
promise.reject("File not found: " + e.getMessage());
} catch (IOException e) {
callback.invoke("Error accessing file: " + e.getMessage(), null);
promise.reject("Error accessing file: " + e.getMessage());
}
callback.invoke(null, Uri.fromFile(pictureFile).toString());
promise.resolve(Uri.fromFile(pictureFile).toString());
break;
case RCT_CAMERA_CAPTURE_TARGET_TEMP:
File tempFile = getTempMediaFile(MEDIA_TYPE_IMAGE);
@@ -216,7 +216,7 @@ public class RCTCameraModule extends ReactContextBaseJavaModule {
}
@ReactMethod
public void stopCapture(final ReadableMap options, final Callback callback) {
public void stopCapture(final ReadableMap options, final Promise promise) {
// TODO: implement video capture
}