forked from tschoffelen/react-native-email-link
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
144 lines (127 loc) · 3.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* React Native Email Link
*
* This file supports both iOS and Android.
*/
import { Platform, Alert, ActionSheetIOS, Linking } from 'react-native'
class EmailException {
constructor (message) {
this.message = message
this.name = 'EmailException'
}
}
const isIOS = Platform.OS === 'ios'
const prefixes = {
'apple-mail': 'message://',
'gmail': 'googlegmail://',
'inbox': 'inbox-gmail://',
'spark': 'readdle-spark://',
'airmail': 'airmail://',
'outlook': 'ms-outlook://'
}
const titles = {
'apple-mail': 'Mail',
'gmail': 'Gmail',
'inbox': 'Inbox',
'spark': 'Spark',
'airmail': 'Airmail',
'outlook' : 'Outlook'
}
/**
* Check if a given mail app is installed.
*
* @param {string} app
* @returns {Promise<boolean>}
*/
export function isAppInstalled (app) {
return new Promise((resolve) => {
if (!(app in prefixes)) {
return resolve(false)
}
Linking.canOpenURL(prefixes[app])
.then((isSupported) => {
resolve(isSupported)
})
.catch(() => resolve(false))
})
}
/**
* Ask the user to choose one of the available mail apps.
* @param title
* @param message
* @param cancelLabel
* @returns {Promise<String|null>}
*/
export function askAppChoice (
title = 'Open mail app',
message = 'Which app would you like to open?',
cancelLabel = 'Cancel',
) {
return new Promise(async (resolve) => {
let availableApps = []
for (let app in prefixes) {
let avail = await isAppInstalled(app)
if (avail) {
availableApps.push(app)
}
}
if (availableApps.length < 2) {
return resolve(availableApps[0] || null)
}
if (isIOS) {
let options = availableApps.map((app) => titles[app])
options.push(cancelLabel)
ActionSheetIOS.showActionSheetWithOptions({
title: title,
message: message,
options: options,
cancelButtonIndex: options.length - 1
}, (buttonIndex) => {
if (buttonIndex === options.length - 1) {
return resolve(null)
}
return resolve(availableApps[buttonIndex])
})
return
}
let options = availableApps.map((app) => ({text: titles[app], onPress: () => resolve(app)}))
options.push({text: cancelLabel, onPress: () => resolve(null), style: 'cancel'})
Alert.alert(
title,
message,
options,
{onDismiss: () => resolve(null)}
)
})
}
/**
* Open an email app, or let the user choose what app to open.
*
* @param {{
* app: string | undefined | null,
* title: string,
* message: string,
* cancelLabel: string,
* }} options
*/
export async function openInbox (options = {}) {
if (!options || typeof options !== 'object') {
throw new EmailException('First parameter of `openInbox` should contain object with options.')
}
if ('app' in options && options.app && Object.keys(prefixes).indexOf(options.app) < 0) {
throw new EmailException('Option `app` should be undefined, null, or one of the following: "' + Object.keys(prefixes).join('", "') + '".')
}
let { app = null } = options;
if (!app) {
const { title, message, cancelLabel } = options;
app = await askAppChoice(title, message, cancelLabel)
}
let url = null
switch (app) {
default:
url = prefixes[app]
}
if (url) {
return Linking.openURL(url)
}
}