Skip to content

Commit

Permalink
fix(email-composer): Aligned function calls with Cordova plugin
Browse files Browse the repository at this point in the history
Co-authored-by: ChelloApps<name@example.com>

closes: #3220
  • Loading branch information
danielsogl authored Nov 26, 2019
1 parent 5223dc7 commit 9057dc2
Showing 1 changed file with 82 additions and 15 deletions.
97 changes: 82 additions & 15 deletions src/@ionic-native/plugins/email-composer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
* }
* });
*
Expand Down Expand Up @@ -143,34 +161,83 @@ export class EmailComposer extends IonicNativePlugin {
}

/**
* Verifies if sending emails is supported on the device.
* Verifies if an email account is configured on the device.
*
* @returns {Promise<any>} Resolves if available, rejects if not available
*/
@CordovaCheck()
hasAccount(): Promise<any> {
return getPromise<boolean>((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<any>} Resolves if available, rejects if not available
*/

@CordovaCheck()
isAvailable(app?: string): Promise<any> {
return getPromise<boolean>((resolve, reject) => {
hasClient(app?: string): Promise<any> {
return getPromise<boolean>((resolve) => {
if (app) {
EmailComposer.getPlugin().isAvailable(app, (isAvailable: boolean) => {
if (isAvailable) {
resolve();
EmailComposer.getPlugin().hasClient(app, (result: boolean) => {
if (result) {
resolve(true);
} else {
reject();
resolve(false);
}
});
} else {
EmailComposer.getPlugin().isAvailable((isAvailable: boolean) => {
if (isAvailable) {
resolve();
} else {
reject();
}
EmailComposer.getPlugin().getClients((apps: string[]) => {
resolve(apps.length && apps.length > 0);
});
}
});
}

/**
* Returns an array of email clients installed on the device.
*
* @returns {Promise<string[]>} Resolves if available, rejects if not available
*/
@CordovaCheck()
@Cordova({ platforms: ['Android'] })
getClients(): Promise<string[]> {
return getPromise<string[]>(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.
*
* @param {string} [app] App id or uri scheme.
* @returns {Promise<any>} Resolves if available, rejects if not available
*/
@CordovaCheck()
isAvailable(app?: string): Promise<any> {
return getPromise<boolean>((resolve) => {
Promise.all([this.hasAccount, this.hasClient(app)])
.then((results) => {
return resolve(results.length === 2 && results[0] && results[1]);
});
});
}

/**
* Displays the email composer pre-filled with data.
*
Expand Down

0 comments on commit 9057dc2

Please sign in to comment.