Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Mailer#canSend #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ public class MainApplication extends Application implements ReactApplication {
4. Whenever you want to use it within React code now you can: `var Mailer = require('NativeModules').RNMail;`


## Example
## Example 1
Show a mail dialog.

```javascript
var Mailer = require('NativeModules').RNMail;

Expand Down Expand Up @@ -139,5 +141,27 @@ var MailExampleApp = React.createClass({

On Android, the `callback` will only be called if an `error` occurs. The `event` argument is unused!

## Example 2
Check if the user has an email account configured.

```js
var Mailer = require('NativeModules').RNMail;

Mailer.canSend((error, canSend) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like error in the callback is always null. Seems like outcome canSend is always true or false. maybe keep the apis Mailer.canSend(canSend => {});?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, but I wanted to stick to the error-first callback convention. I think this is more valuable than saving an argument.

What do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any thoughts?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (error) {
//
}
else {
if (canSend) {
// User can send a mail
}
else {
// User cannot send a mail
}
}
});
}
```

## Here is how it looks:
![Demo gif](https://github.com/chirag04/react-native-mail/blob/master/screenshot.png)
5 changes: 5 additions & 0 deletions RNMail/RNMail.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ - (dispatch_queue_t)methodQueue

RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(canSend:(RCTResponseSenderBlock)callback)
{
callback(@[[NSNull null], @([MFMailComposeViewController canSendMail])]);
}

RCT_EXPORT_METHOD(mail:(NSDictionary *)options
callback: (RCTResponseSenderBlock)callback)
{
Expand Down
97 changes: 58 additions & 39 deletions android/src/main/java/com/chirag/RNMail/RNMailModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.support.annotation.Nullable;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
Expand Down Expand Up @@ -50,53 +51,71 @@ private String[] readableArrayToStringArray(ReadableArray r) {
return strArray;
}

@ReactMethod
public void mail(ReadableMap options, Callback callback) {
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:"));

if (options.hasKey("subject") && !options.isNull("subject")) {
i.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject"));
}
private Intent createIntent(@Nullable ReadableMap options) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));

if (options != null) {
if (options.hasKey("subject") && !options.isNull("subject")) {
intent.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject"));
}

if (options.hasKey("body") && !options.isNull("body")) {
intent.putExtra(Intent.EXTRA_TEXT, options.getString("body"));
}

if (options.hasKey("recipients") && !options.isNull("recipients")) {
ReadableArray recipients = options.getArray("recipients");
intent.putExtra(Intent.EXTRA_EMAIL, readableArrayToStringArray(recipients));
}

if (options.hasKey("ccRecipients") && !options.isNull("ccRecipients")) {
ReadableArray ccRecipients = options.getArray("ccRecipients");
intent.putExtra(Intent.EXTRA_CC, readableArrayToStringArray(ccRecipients));
}

if (options.hasKey("bccRecipients") && !options.isNull("bccRecipients")) {
ReadableArray bccRecipients = options.getArray("bccRecipients");
intent.putExtra(Intent.EXTRA_BCC, readableArrayToStringArray(bccRecipients));
}

if (options.hasKey("attachment") && !options.isNull("attachment")) {
ReadableMap attachment = options.getMap("attachment");
if (attachment.hasKey("path") && !attachment.isNull("path")) {
String path = attachment.getString("path");
File file = new File(path);
Uri p = Uri.fromFile(file);
intent.putExtra(Intent.EXTRA_STREAM, p);
}
}
}

if (options.hasKey("body") && !options.isNull("body")) {
i.putExtra(Intent.EXTRA_TEXT, options.getString("body"));
}
return intent;
}

if (options.hasKey("recipients") && !options.isNull("recipients")) {
ReadableArray recipients = options.getArray("recipients");
i.putExtra(Intent.EXTRA_EMAIL, readableArrayToStringArray(recipients));
}
private int countIntentActivities(Intent intent) {
PackageManager manager = reactContext.getPackageManager();
List<ResolveInfo> list = manager.queryIntentActivities(intent, 0);

if (options.hasKey("ccRecipients") && !options.isNull("ccRecipients")) {
ReadableArray ccRecipients = options.getArray("ccRecipients");
i.putExtra(Intent.EXTRA_CC, readableArrayToStringArray(ccRecipients));
}
return list.size();
}

if (options.hasKey("bccRecipients") && !options.isNull("bccRecipients")) {
ReadableArray bccRecipients = options.getArray("bccRecipients");
i.putExtra(Intent.EXTRA_BCC, readableArrayToStringArray(bccRecipients));
}
@ReactMethod
public void canSend(Callback callback) {
Intent intent = createIntent(null);
boolean canSendMail = countIntentActivities(intent) > 0;

if (options.hasKey("attachment") && !options.isNull("attachment")) {
ReadableMap attachment = options.getMap("attachment");
if (attachment.hasKey("path") && !attachment.isNull("path")) {
String path = attachment.getString("path");
File file = new File(path);
Uri p = Uri.fromFile(file);
i.putExtra(Intent.EXTRA_STREAM, p);
}
}
callback.invoke(null, canSendMail);
}

PackageManager manager = reactContext.getPackageManager();
List<ResolveInfo> list = manager.queryIntentActivities(i, 0);
@ReactMethod
public void mail(ReadableMap options, Callback callback) {
Intent i = createIntent(options);
int numberOfActivities = countIntentActivities(i);

if (list == null || list.size() == 0) {
if (numberOfActivities == 0) {
callback.invoke("not_available");
return;
}

if (list.size() == 1) {
} else if (numberOfActivities == 1) {
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
reactContext.startActivity(i);
Expand Down