From 793e029cbd634e51a924253a1b3001090f4f7e87 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 17 Sep 2020 11:56:00 +0200 Subject: [PATCH] MOBILE-3523 login: Move login checks and modals to providers --- src/core/login/pages/site/site.ts | 2 +- src/core/login/providers/helper.ts | 45 ++------------------- src/providers/sites.ts | 9 +++++ src/providers/utils/dom.ts | 63 ++++++++++++++++++++++++------ 4 files changed, 65 insertions(+), 54 deletions(-) diff --git a/src/core/login/pages/site/site.ts b/src/core/login/pages/site/site.ts index 68b1c6142..18248c934 100644 --- a/src/core/login/pages/site/site.ts +++ b/src/core/login/pages/site/site.ts @@ -335,7 +335,7 @@ export class CoreLoginSitePage { * @return Promise resolved after logging in. */ protected async login(response: CoreSiteCheckResponse, foundSite?: CoreLoginSiteInfoExtended): Promise { - return this.sitesProvider.checkRequiredMinimumVersion(response.config).then(() => { + return this.sitesProvider.checkApplication(response).then(() => { this.domUtils.triggerFormSubmittedEvent(this.formElement, true); diff --git a/src/core/login/providers/helper.ts b/src/core/login/providers/helper.ts index 9e5ea3630..7d51cb745 100644 --- a/src/core/login/providers/helper.ts +++ b/src/core/login/providers/helper.ts @@ -14,7 +14,7 @@ import { Injectable } from '@angular/core'; import { Location } from '@angular/common'; -import { AlertController, NavController, NavOptions } from 'ionic-angular'; +import { NavController, NavOptions } from 'ionic-angular'; import { TranslateService } from '@ngx-translate/core'; import { CoreApp, CoreStoreConfig } from '@providers/app'; import { CoreConfigProvider } from '@providers/config'; @@ -101,7 +101,6 @@ export class CoreLoginHelperProvider { private initDelegate: CoreInitDelegate, private sitePluginsProvider: CoreSitePluginsProvider, private location: Location, - private alertCtrl: AlertController, private courseProvider: CoreCourseProvider ) { this.logger = logger.getInstance('CoreLoginHelper'); @@ -1235,7 +1234,7 @@ export class CoreLoginHelperProvider { protected showWorkplaceNoticeModal(message: string): void { const link = CoreApp.instance.getAppStoreUrl({android: 'com.moodle.workplace', ios: 'id1470929705' }); - this.showDownloadAppNoticeModal(message, link); + this.domUtils.showDownloadAppNoticeModal(message, link); } /** @@ -1251,45 +1250,7 @@ export class CoreLoginHelperProvider { const link = CoreApp.instance.getAppStoreUrl(storesConfig); - this.showDownloadAppNoticeModal(message, link); - } - - /** - * Show a modal warning the user that he should use a different app. - * - * @param message The warning message. - * @param link Link to the app to download if any. - */ - protected showDownloadAppNoticeModal(message: string, link?: string): void { - const buttons: any[] = [ - { - text: this.translate.instant('core.ok'), - role: 'cancel' - } - ]; - - if (link) { - buttons.push({ - text: this.translate.instant('core.download'), - handler: (): void => { - this.utils.openInBrowser(link); - } - }); - } - - const alert = this.alertCtrl.create({ - message: message, - buttons: buttons - }); - - alert.present().then(() => { - const isDevice = CoreApp.instance.isAndroid() || CoreApp.instance.isIOS(); - if (!isDevice) { - // Treat all anchors so they don't override the app. - const alertMessageEl: HTMLElement = alert.pageRef().nativeElement.querySelector('.alert-message'); - this.domUtils.treatAnchors(alertMessageEl); - } - }); + this.domUtils.showDownloadAppNoticeModal(message, link); } /** diff --git a/src/providers/sites.ts b/src/providers/sites.ts index 8609022d6..aed300a25 100644 --- a/src/providers/sites.ts +++ b/src/providers/sites.ts @@ -1023,6 +1023,15 @@ export class CoreSitesProvider { return this.appDB.insertRecord(CoreSitesProvider.SITES_TABLE, entry); } + /** + * Check the app for a site and show a download dialogs if necessary. + * + * @param response Data obtained during site check. + */ + async checkApplication(response: CoreSiteCheckResponse): Promise { + await this.checkRequiredMinimumVersion(response.config); + } + /** * Check the required minimum version of the app for a site and shows a download dialog. * diff --git a/src/providers/utils/dom.ts b/src/providers/utils/dom.ts index 5be93dddb..9297320de 100644 --- a/src/providers/utils/dom.ts +++ b/src/providers/utils/dom.ts @@ -1402,6 +1402,42 @@ export class CoreDomUtilsProvider { return loader; } + /** + * Show a modal warning the user that he should use a different app. + * + * @param message The warning message. + * @param link Link to the app to download if any. + */ + showDownloadAppNoticeModal(message: string, link?: string): void { + const buttons: any[] = [{ + text: this.translate.instant('core.ok'), + role: 'cancel' + }]; + + if (link) { + buttons.push({ + text: this.translate.instant('core.download'), + handler: (): void => { + this.openInBrowser(link); + } + }); + } + + const alert = this.alertCtrl.create({ + message: message, + buttons: buttons + }); + + alert.present().then(() => { + const isDevice = CoreApp.instance.isAndroid() || CoreApp.instance.isIOS(); + if (!isDevice) { + // Treat all anchors so they don't override the app. + const alertMessageEl: HTMLElement = alert.pageRef().nativeElement.querySelector('.alert-message'); + this.treatAnchors(alertMessageEl); + } + }); + } + /** * Show a prompt modal to input some data. * @@ -1559,17 +1595,7 @@ export class CoreDomUtilsProvider { event.preventDefault(); event.stopPropagation(); - // We cannot use CoreDomUtilsProvider.openInBrowser due to circular dependencies. - if (CoreApp.instance.isDesktop()) { - // It's a desktop app, use Electron shell library to open the browser. - const shell = require('electron').shell; - if (!shell.openExternal(href)) { - // Open browser failed, open a new window in the app. - window.open(href, '_system'); - } - } else { - window.open(href, '_system'); - } + this.openInBrowser(href); } }); }); @@ -1680,6 +1706,21 @@ export class CoreDomUtilsProvider { online: !!online, }, siteId); } + + // We cannot use CoreUtilsProvider.openInBrowser due to circular dependencies. + protected openInBrowser(url: string): void { + if (CoreApp.instance.isDesktop()) { + // It's a desktop app, use Electron shell library to open the browser. + const shell = require('electron').shell; + if (!shell.openExternal(url)) { + // Open browser failed, open a new window in the app. + window.open(url, '_system'); + } + } else { + window.open(url, '_system'); + } + } + } export class CoreDomUtils extends makeSingleton(CoreDomUtilsProvider) {}