Skip to content

Commit

Permalink
Add support for multiple file uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
yksolanki9 committed Sep 26, 2022
1 parent 35689f8 commit 3d77c55
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.getcapacitor.Plugin;
import com.getcapacitor.annotation.CapacitorPlugin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -17,23 +18,42 @@ public class ShareWithPlugin extends Plugin {
@Override
protected void handleOnNewIntent(Intent intent) {
super.handleOnNewIntent(intent);

String action = intent.getAction();
Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);

if (!Intent.ACTION_SEND.equals(action) || imageUri == null) {
if (!Intent.ACTION_SEND.equals(action) && !Intent.ACTION_SEND_MULTIPLE.equals(action)) {
return;
}

Map<String, String> imageData = getImageDataFromUri(imageUri);
if(Intent.ACTION_SEND.equals(action)) {
Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if(imageUri == null) {
return;
}

JSObject ret = new JSObject();
Map<String, String> imageData = getImageDataFromUri(imageUri);
JSObject ret = new JSObject();
for (Map.Entry<String, String> entry : imageData.entrySet()) {
ret.put(entry.getKey(), entry.getValue());
}
notifyListeners("FILE_SINGLE", ret, true);
} else {
ArrayList<Uri> imageUriArray = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);

for (Map.Entry<String, String> entry : imageData.entrySet()) {
ret.put(entry.getKey(), entry.getValue());
}
if(imageUriArray == null) {
return;
}

notifyListeners("FILE_SINGLE", ret, true);
JSObject retArray = new JSObject();
for(int i = 0; i < imageUriArray.size(); i++) {
Map<String, String> imageData = getImageDataFromUri(imageUriArray.get(i));
JSObject ret = new JSObject();
for (Map.Entry<String, String> entry : imageData.entrySet()) {
ret.put(entry.getKey(), entry.getValue());
}
retArray.put(Integer.toString(i), ret);
}
notifyListeners("FILE_MULTIPLE", retArray, true);
}
}

public Map<String, String> getImageDataFromUri(Uri uri) {
Expand Down
2 changes: 1 addition & 1 deletion src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export interface ShareWithPlugin {
* @param eventName {string} - Name of the event to listen to
* @param {listenerFunc} listenerFunc - The callback that is executed when the event occurs
*/
addListener(eventName: 'FILE_SINGLE', listenerFunc: (event: any) => void): Promise<PluginListenerHandle> & PluginListenerHandle;
addListener(eventName: 'FILE_SINGLE' | 'FILE_MULTIPLE', listenerFunc: (event: any) => void): Promise<PluginListenerHandle> & PluginListenerHandle;
}

0 comments on commit 3d77c55

Please sign in to comment.