From 49d283c22da721b49e172c87c7afa21510cc5d89 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 7 Jan 2019 13:19:02 +0100 Subject: [PATCH] MOBILE-2795 core: Allow setting several dismiss callbacks to alerts --- src/classes/site.ts | 4 +++- src/core/login/providers/helper.ts | 4 +++- src/providers/utils/dom.ts | 37 ++++++++++++++++++++++++++---- upgrade.txt | 1 + 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/classes/site.ts b/src/classes/site.ts index 18089cc1e..08b3512da 100644 --- a/src/classes/site.ts +++ b/src/classes/site.ts @@ -1273,7 +1273,9 @@ export class CoreSite { return this.domUtils.showAlert(this.translate.instant('core.notice'), alertMessage, undefined, 3000).then((alert) => { return new Promise((resolve, reject): void => { - alert.onDidDismiss(() => { + const subscription = alert.didDismiss.subscribe(() => { + subscription && subscription.unsubscribe(); + if (inApp) { resolve(this.utils.openInApp(url, options)); } else { diff --git a/src/core/login/providers/helper.ts b/src/core/login/providers/helper.ts index 2f0dc2d78..9cebad238 100644 --- a/src/core/login/providers/helper.ts +++ b/src/core/login/providers/helper.ts @@ -697,7 +697,9 @@ export class CoreLoginHelperProvider { */ openChangePassword(siteUrl: string, error: string): void { this.domUtils.showAlert(this.translate.instant('core.notice'), error, undefined, 3000).then((alert) => { - alert.onDidDismiss(() => { + const subscription = alert.didDismiss.subscribe(() => { + subscription && subscription.unsubscribe(); + this.utils.openInApp(siteUrl + '/login/change_password.php'); }); }); diff --git a/src/providers/utils/dom.ts b/src/providers/utils/dom.ts index faf948769..f4f2bbf62 100644 --- a/src/providers/utils/dom.ts +++ b/src/providers/utils/dom.ts @@ -25,6 +25,24 @@ import { CoreConfigProvider } from '../config'; import { CoreUrlUtilsProvider } from './url'; import { CoreConstants } from '@core/constants'; import { Md5 } from 'ts-md5/dist/md5'; +import { Subject } from 'rxjs'; + +/** + * Interface that defines an extension of the Ionic Alert class, to support multiple listeners. + */ +export interface CoreAlert extends Alert { + /** + * Observable that will notify when the alert is dismissed. + * @type {Subject<{data: any, role: string}>} + */ + didDismiss: Subject<{data: any, role: string}>; + + /** + * Observable that will notify when the alert will be dismissed. + * @type {Subject<{data: any, role: string}>} + */ + willDismiss: Subject<{data: any, role: string}>; +} /* * "Utils" service with helper functions for UI, DOM elements and HTML code. @@ -886,9 +904,9 @@ export class CoreDomUtilsProvider { * @param {string} message Message to show. * @param {string} [buttonText] Text of the button. * @param {number} [autocloseTime] Number of milliseconds to wait to close the modal. If not defined, modal won't be closed. - * @return {Promise} Promise resolved with the alert modal. + * @return {Promise} Promise resolved with the alert modal. */ - showAlert(title: string, message: string, buttonText?: string, autocloseTime?: number): Promise { + showAlert(title: string, message: string, buttonText?: string, autocloseTime?: number): Promise { const hasHTMLTags = this.textUtils.hasHTMLTags(message); let promise; @@ -907,7 +925,7 @@ export class CoreDomUtilsProvider { return this.displayedAlerts[alertId]; } - const alert = this.alertCtrl.create({ + const alert: CoreAlert = this.alertCtrl.create({ title: title, message: message, buttons: [buttonText || this.translate.instant('core.ok')] @@ -924,8 +942,19 @@ export class CoreDomUtilsProvider { // Store the alert and remove it when dismissed. this.displayedAlerts[alertId] = alert; - alert.onDidDismiss(() => { + // Define the observables to extend the Alert class. This will allow several callbacks instead of just one. + alert.didDismiss = new Subject(); + alert.willDismiss = new Subject(); + + // Set the callbacks to trigger an observable event. + alert.onDidDismiss((data: any, role: string) => { delete this.displayedAlerts[alertId]; + + alert.didDismiss.next({data: data, role: role}); + }); + + alert.onWillDismiss((data: any, role: string) => { + alert.willDismiss.next({data: data, role: role}); }); if (autocloseTime > 0) { diff --git a/upgrade.txt b/upgrade.txt index 1a0b363c3..86e4a9132 100644 --- a/upgrade.txt +++ b/upgrade.txt @@ -8,6 +8,7 @@ information provided here is intended especially for developers. - The value of the constant CoreCourseProvider.ALL_SECTIONS_ID has changed from -1 to -2. - Use of completionstatus on the module object has been deprecated, use completiondata instead. - The function CoreSitesProvider.loadSite has changed, now it will trigger SESSION_EXPIRED event if the site is logged out. Its params and return value have changed. +- When using CoreDomUtils.showAlert, please use alert.didDismiss.subscribe() instead of alert.onDidDismiss(). - The following strings have been deprecated: core.dfdaymonthyear. Please use core.strftimedatefullshort instead. core.dfdayweekmonth. Please use core.strftimedayshort instead.