Skip to content

Commit

Permalink
fix: do not crash on double select (#476)
Browse files Browse the repository at this point in the history
* fix: do not crash on double select

* docs: clarify isInProgress()
  • Loading branch information
vonovak authored Oct 2, 2021
1 parent 605541c commit b949b47
Show file tree
Hide file tree
Showing 8 changed files with 393 additions and 294 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ The base64 encoded content of the picked file if the option `readContent` was se

If the user cancels the document picker without choosing a file (by pressing the system back button on Android or the Cancel button on iOS) the Promise will be rejected with a cancellation error. You can check for this error using `DocumentPicker.isCancel(err)` allowing you to ignore it and cleanup any parts of your interface that may not be needed anymore.

#### `DocumentPicker.isInProgress(err)`

If the user somehow manages to open multiple file pickers (eg. due the app being unresponsive), then only the picked result from the last opened picker will be considered and the promises form previous opened pickers will be rejected with an error that you can check using `DocumentPicker.isInProgress()`.

This behavior might change in future to allow opening only a single picker at a time. The internal logic is currently implemented only on iOS.

#### [iOS only] `DocumentPicker.releaseSecureAccess(uris: Array<string>)`

If `mode` is set to `open` iOS is giving you a secure access to a file located outside from your sandbox.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class DocumentPickerModule extends ReactContextBaseJavaModule {

private static final String OPTION_TYPE = "type";
private static final String OPTION_MULTIPLE = "allowMultiSelection";
private static final String OPTION_COPYTO = "copyTo";
private static final String OPTION_COPY_TO = "copyTo";

private static final String FIELD_URI = "uri";
private static final String FIELD_FILE_COPY_URI = "fileCopyUri";
Expand All @@ -67,14 +67,15 @@ public class DocumentPickerModule extends ReactContextBaseJavaModule {
private final ActivityEventListener activityEventListener = new BaseActivityEventListener() {
@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
if (promise == null) {
final Promise storedPromise = promise;
if (storedPromise == null) {
Log.e(NAME, "promise was null in onActivityResult");
return;
}
if (requestCode == READ_REQUEST_CODE) {
onShowActivityResult(resultCode, data, promise);
onShowActivityResult(resultCode, data, storedPromise);
} else if (requestCode == PICK_DIR_REQUEST_CODE) {
onPickDirectoryResult(resultCode, data, promise);
onPickDirectoryResult(resultCode, data);
}
}
};
Expand Down Expand Up @@ -112,7 +113,7 @@ public String getName() {
public void pick(ReadableMap args, Promise promise) {
Activity currentActivity = getCurrentActivity();
this.promise = promise;
this.copyTo = args.hasKey(OPTION_COPYTO) ? args.getString(OPTION_COPYTO) : null;
this.copyTo = args.hasKey(OPTION_COPY_TO) ? args.getString(OPTION_COPY_TO) : null;

if (currentActivity == null) {
sendError(E_ACTIVITY_DOES_NOT_EXIST, "Current activity does not exist");
Expand Down Expand Up @@ -165,7 +166,7 @@ public void pickDirectory(Promise promise) {
}
}

private void onPickDirectoryResult(int resultCode, Intent data, Promise promise) {
private void onPickDirectoryResult(int resultCode, Intent data) {
if (resultCode == Activity.RESULT_CANCELED) {
sendError(E_DOCUMENT_PICKER_CANCELED, "User canceled directory picker");
return;
Expand Down Expand Up @@ -348,8 +349,8 @@ private void sendError(String code, String message) {
}

private void sendError(String code, String message, Exception e) {
if (this.promise != null) {
Promise temp = this.promise;
Promise temp = this.promise;
if (temp != null) {
this.promise = null;
temp.reject(code, message, e);
}
Expand Down
Loading

0 comments on commit b949b47

Please sign in to comment.