MOBILE-3523 login: Move login checks and modals to providers
This commit is contained in:
		
							parent
							
								
									495ff96094
								
							
						
					
					
						commit
						793e029cbd
					
				@ -335,7 +335,7 @@ export class CoreLoginSitePage {
 | 
			
		||||
     * @return Promise resolved after logging in.
 | 
			
		||||
     */
 | 
			
		||||
    protected async login(response: CoreSiteCheckResponse, foundSite?: CoreLoginSiteInfoExtended): Promise<void> {
 | 
			
		||||
        return this.sitesProvider.checkRequiredMinimumVersion(response.config).then(() => {
 | 
			
		||||
        return this.sitesProvider.checkApplication(response).then(() => {
 | 
			
		||||
 | 
			
		||||
            this.domUtils.triggerFormSubmittedEvent(this.formElement, true);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 | 
			
		||||
@ -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<void> {
 | 
			
		||||
        await this.checkRequiredMinimumVersion(response.config);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Check the required minimum version of the app for a site and shows a download dialog.
 | 
			
		||||
     *
 | 
			
		||||
 | 
			
		||||
@ -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) {}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user