From 9057dc2c934f5f8e804da84348d353a3234742a4 Mon Sep 17 00:00:00 2001 From: Daniel Sogl Date: Tue, 26 Nov 2019 17:05:56 +0100 Subject: [PATCH] fix(email-composer): Aligned function calls with Cordova plugin Co-authored-by: ChelloApps closes: #3220 --- .../plugins/email-composer/index.ts | 105 ++++++++++++++---- 1 file changed, 86 insertions(+), 19 deletions(-) diff --git a/src/@ionic-native/plugins/email-composer/index.ts b/src/@ionic-native/plugins/email-composer/index.ts index 353a4c44..5b3fe221 100644 --- a/src/@ionic-native/plugins/email-composer/index.ts +++ b/src/@ionic-native/plugins/email-composer/index.ts @@ -70,9 +70,27 @@ export interface EmailComposerOptions { * ... * * - * this.emailComposer.isAvailable().then((available: boolean) =>{ + * this.emailComposer.getClients().then((apps: []) => { + * // Returns an array of configured email clients for the device + * }); + * + * this.emailComposer.hasClient().then(app, (isValid: boolean) => { + * if (isValid) { + * // Now we know we have a valid email client configured + * // Not specifying an app will return true if at least one email client is configured + * } + * }); + * + * this.emailComposer.hasAccount().then((isValid: boolean) => { + * if (isValid) { + * // Now we know we have a valid email account configured + * } + * }); + * + * this.emailComposer.isAvailable().then(app, (available: boolean) => { * if(available) { - * //Now we know we can send + * // Now we know we can send an email, calls hasClient and hasAccount + * // Not specifying an app will return true if at least one email client is configured * } * }); * @@ -142,6 +160,68 @@ export class EmailComposer extends IonicNativePlugin { return; } + /** + * Verifies if an email account is configured on the device. + * + * @returns {Promise} Resolves if available, rejects if not available + */ + @CordovaCheck() + hasAccount(): Promise { + return getPromise((resolve) => { + EmailComposer.getPlugin().hasAccount((result: boolean) => { + if (result) { + resolve(true); + } else { + resolve(false); + } + }); + }); + } + + /** + * Verifies if a specific email client is installed on the device. + * + * @param {string} [app] App id or uri scheme. + * @returns {Promise} Resolves if available, rejects if not available + */ + + @CordovaCheck() + hasClient(app?: string): Promise { + return getPromise((resolve) => { + if (app) { + EmailComposer.getPlugin().hasClient(app, (result: boolean) => { + if (result) { + resolve(true); + } else { + resolve(false); + } + }); + } else { + EmailComposer.getPlugin().getClients((apps: string[]) => { + resolve(apps.length && apps.length > 0); + }); + } + }); + } + + /** + * Returns an array of email clients installed on the device. + * + * @returns {Promise} Resolves if available, rejects if not available + */ + @CordovaCheck() + @Cordova({ platforms: ['Android'] }) + getClients(): Promise { + return getPromise(resolve => { + EmailComposer.getPlugin().getClients((apps: any) => { + if (Object.prototype.toString.call(apps) === '[object String]') { + apps = [apps]; + } + resolve(apps); + }); + }); + } + /** * Verifies if sending emails is supported on the device. * @@ -150,24 +230,11 @@ export class EmailComposer extends IonicNativePlugin { */ @CordovaCheck() isAvailable(app?: string): Promise { - return getPromise((resolve, reject) => { - if (app) { - EmailComposer.getPlugin().isAvailable(app, (isAvailable: boolean) => { - if (isAvailable) { - resolve(); - } else { - reject(); - } + return getPromise((resolve) => { + Promise.all([this.hasAccount, this.hasClient(app)]) + .then((results) => { + return resolve(results.length === 2 && results[0] && results[1]); }); - } else { - EmailComposer.getPlugin().isAvailable((isAvailable: boolean) => { - if (isAvailable) { - resolve(); - } else { - reject(); - } - }); - } }); }