(android) Result collector improvements

This commit is contained in:
Ivan Pusic 2018-05-16 18:49:51 +02:00
parent d0e583728c
commit 5b8bb6aced
2 changed files with 32 additions and 14 deletions

View File

@ -92,9 +92,9 @@ class PickerModule extends ReactContextBaseJavaModule implements ActivityEventLi
private Uri mCameraCaptureURI;
private String mCurrentPhotoPath;
private ResultCollector resultCollector;
private ResultCollector resultCollector = new ResultCollector();
private Compression compression = new Compression();
private ReactApplicationContext reactContext = null;
private ReactApplicationContext reactContext;
PickerModule(ReactApplicationContext reactContext) {
super(reactContext);
@ -104,7 +104,7 @@ class PickerModule extends ReactContextBaseJavaModule implements ActivityEventLi
private String getTmpDir(Activity activity) {
String tmpDir = activity.getCacheDir() + "/react-native-image-crop-picker";
Boolean created = new File(tmpDir).mkdir();
new File(tmpDir).mkdir();
return tmpDir;
}
@ -278,7 +278,7 @@ class PickerModule extends ReactContextBaseJavaModule implements ActivityEventLi
}
setConfiguration(options);
resultCollector = new ResultCollector(promise, multiple);
resultCollector.setup(promise, multiple);
permissionsCheck(activity, promise, Arrays.asList(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE), new Callable<Void>() {
@Override
@ -352,7 +352,7 @@ class PickerModule extends ReactContextBaseJavaModule implements ActivityEventLi
}
setConfiguration(options);
resultCollector = new ResultCollector(promise, multiple);
resultCollector.setup(promise, multiple);
permissionsCheck(activity, promise, Collections.singletonList(Manifest.permission.WRITE_EXTERNAL_STORAGE), new Callable<Void>() {
@Override
@ -373,7 +373,7 @@ class PickerModule extends ReactContextBaseJavaModule implements ActivityEventLi
}
setConfiguration(options);
resultCollector = new ResultCollector(promise, false);
resultCollector.setup(promise, false);
Uri uri = Uri.parse(options.getString("path"));
startCropping(activity, uri);

View File

@ -19,12 +19,16 @@ class ResultCollector {
private boolean multiple;
private AtomicInteger waitCounter;
private WritableArray arrayResult;
private boolean resultSent = false;
private boolean resultSent;
ResultCollector(Promise promise, boolean multiple) {
synchronized void setup(Promise promise, boolean multiple) {
this.promise = promise;
this.multiple = multiple;
this.resultSent = false;
this.waitCount = 0;
this.waitCounter = new AtomicInteger(0);
if (multiple) {
this.arrayResult = new WritableNativeArray();
}
@ -32,14 +36,28 @@ class ResultCollector {
// if user has provided "multiple" option, we will wait for X number of result to come,
// and also return result as an array
void setWaitCount(int waitCount) {
synchronized void setWaitCount(int waitCount) {
this.waitCount = waitCount;
this.waitCounter = new AtomicInteger(0);
}
synchronized void notifySuccess(WritableMap result) {
synchronized private boolean isRequestValid() {
if (resultSent) {
Log.w("image-crop-picker", "Skipping result, already sent...");
return false;
}
if (promise == null) {
Log.w("image-crop-picker", "Trying to notify success but promise is not set");
return false;
}
return true;
}
synchronized void notifySuccess(WritableMap result) {
if (!isRequestValid()) {
return;
}
if (multiple) {
@ -57,8 +75,8 @@ class ResultCollector {
}
synchronized void notifyProblem(String code, String message) {
if (resultSent) {
Log.w("image-crop-picker", "Skipping result, already sent...");
if (!isRequestValid()) {
return;
}
Log.e("image-crop-picker", "Promise rejected. " + message);
@ -67,8 +85,8 @@ class ResultCollector {
}
synchronized void notifyProblem(String code, Throwable throwable) {
if (resultSent) {
Log.w("image-crop-picker", "Skipping result, already sent...");
if (!isRequestValid()) {
return;
}
Log.e("image-crop-picker", "Promise rejected. " + throwable.getMessage());