MOBILE-3833 core: Extract constructor initializers

main
Noel De Martin 2021-08-12 13:26:46 +02:00
parent ae351a49b2
commit 8a5ccf1d71
12 changed files with 133 additions and 75 deletions

View File

@ -24,7 +24,7 @@ import { AddonModLtiListLinkHandler } from './services/handlers/list-link';
import { AddonModLtiModuleHandler, AddonModLtiModuleHandlerService } from './services/handlers/module'; import { AddonModLtiModuleHandler, AddonModLtiModuleHandlerService } from './services/handlers/module';
import { AddonModLtiPrefetchHandler } from './services/handlers/prefetch'; import { AddonModLtiPrefetchHandler } from './services/handlers/prefetch';
import { AddonModLtiProvider } from './services/lti'; import { AddonModLtiProvider } from './services/lti';
import { AddonModLtiHelperProvider } from './services/lti-helper'; import { AddonModLtiHelper, AddonModLtiHelperProvider } from './services/lti-helper';
export const ADDON_MOD_LTI_SERVICES: Type<unknown>[] = [ export const ADDON_MOD_LTI_SERVICES: Type<unknown>[] = [
AddonModLtiProvider, AddonModLtiProvider,
@ -44,6 +44,7 @@ const routes: Routes = [
AddonModLtiComponentsModule, AddonModLtiComponentsModule,
], ],
providers: [ providers: [
{ provide: APP_INITIALIZER, multi: true, useValue: () => AddonModLtiHelper.watchPendingCompletions() },
{ {
provide: APP_INITIALIZER, provide: APP_INITIALIZER,
multi: true, multi: true,

View File

@ -31,6 +31,13 @@ export class AddonModLtiHelperProvider {
protected pendingCheckCompletion: {[moduleId: string]: {courseId: number; module: CoreCourseModule}} = {}; protected pendingCheckCompletion: {[moduleId: string]: {courseId: number; module: CoreCourseModule}} = {};
constructor() { constructor() {
// Clear pending completion on logout.
CoreEvents.on(CoreEvents.LOGOUT, () => {
this.pendingCheckCompletion = {};
});
}
watchPendingCompletions(): void {
Platform.resume.subscribe(() => { Platform.resume.subscribe(() => {
// User went back to the app, check pending completions. // User went back to the app, check pending completions.
for (const moduleId in this.pendingCheckCompletion) { for (const moduleId in this.pendingCheckCompletion) {
@ -39,11 +46,6 @@ export class AddonModLtiHelperProvider {
CoreCourse.checkModuleCompletion(data.courseId, data.module.completiondata); CoreCourse.checkModuleCompletion(data.courseId, data.module.completiondata);
} }
}); });
// Clear pending completion on logout.
CoreEvents.on(CoreEvents.LOGOUT, () => {
this.pendingCheckCompletion = {};
});
} }
/** /**

View File

@ -65,7 +65,7 @@ export class CoreSettingsHelperProvider {
protected colorSchemes: CoreColorScheme[] = []; protected colorSchemes: CoreColorScheme[] = [];
protected currentColorScheme = CoreColorScheme.LIGHT; protected currentColorScheme = CoreColorScheme.LIGHT;
constructor() { async initialize(): Promise<void> {
this.prefersDark = window.matchMedia('(prefers-color-scheme: dark)'); this.prefersDark = window.matchMedia('(prefers-color-scheme: dark)');
if (!CoreConstants.CONFIG.forceColorScheme) { if (!CoreConstants.CONFIG.forceColorScheme) {
@ -94,6 +94,11 @@ export class CoreSettingsHelperProvider {
// Listen for changes to the prefers-color-scheme media query. // Listen for changes to the prefers-color-scheme media query.
this.prefersDark.addEventListener && this.prefersDark.addEventListener('change', this.toggleDarkModeListener.bind(this)); this.prefersDark.addEventListener && this.prefersDark.addEventListener('change', this.toggleDarkModeListener.bind(this));
// Init zoom level.
await this.upgradeZoomLevel();
this.initDomSettings();
} }
/** /**

View File

@ -17,7 +17,7 @@ import { Routes } from '@angular/router';
import { AppRoutingModule } from '@/app/app-routing.module'; import { AppRoutingModule } from '@/app/app-routing.module';
import { CoreMainMenuTabRoutingModule } from '@features/mainmenu/mainmenu-tab-routing.module'; import { CoreMainMenuTabRoutingModule } from '@features/mainmenu/mainmenu-tab-routing.module';
import { CoreSettingsHelperProvider } from './services/settings-helper'; import { CoreSettingsHelper, CoreSettingsHelperProvider } from './services/settings-helper';
import { CoreSettingsDelegateService } from './services/settings-delegate'; import { CoreSettingsDelegateService } from './services/settings-delegate';
export const CORE_SETTINGS_SERVICES: Type<unknown>[] = [ export const CORE_SETTINGS_SERVICES: Type<unknown>[] = [
@ -49,15 +49,7 @@ const mainMenuMoreRoutes: Routes = [
CoreMainMenuTabRoutingModule.forChild(mainMenuMoreRoutes), CoreMainMenuTabRoutingModule.forChild(mainMenuMoreRoutes),
], ],
providers: [ providers: [
{ { provide: APP_INITIALIZER, multi: true, useValue: () => CoreSettingsHelper.initialize() },
provide: APP_INITIALIZER,
multi: true,
deps: [CoreSettingsHelperProvider],
useFactory: (helper: CoreSettingsHelperProvider) => async () => {
await helper.upgradeZoomLevel();
helper.initDomSettings();
},
},
], ],
}) })
export class CoreSettingsModule {} export class CoreSettingsModule {}

View File

@ -0,0 +1,25 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { CoreFilepool } from '@services/filepool';
import { CoreLang } from '@services/lang';
import { CoreLocalNotifications } from '@services/local-notifications';
export default async function(): Promise<void> {
await Promise.all([
CoreFilepool.initialize(),
CoreLang.initialize(),
CoreLocalNotifications.initialize(),
]);
}

View File

@ -0,0 +1,26 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { CoreApp } from '@services/app';
import { CoreIframeUtils } from '@services/utils/iframe';
import { Platform } from '@singletons';
export default async function(): Promise<void> {
if (!CoreApp.isIOS() || !('WKUserScript' in window)) {
return;
}
await Platform.ready();
await CoreIframeUtils.injectiOSScripts(window);
}

View File

@ -0,0 +1,24 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { CoreCronDelegate } from '@services/cron';
import { Network, NgZone } from '@singletons';
export default function(): void {
// When the app is re-connected, start network handlers that were stopped.
Network.onConnect().subscribe(() => {
// Execute the callback in the Angular zone, so change detection doesn't stop working.
NgZone.run(() => CoreCronDelegate.startNetworkHandlers());
});
}

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
import { Injectable, NgZone } from '@angular/core'; import { Injectable } from '@angular/core';
import { CoreApp } from '@services/app'; import { CoreApp } from '@services/app';
import { CoreConfig } from '@services/config'; import { CoreConfig } from '@services/config';
@ -21,7 +21,7 @@ import { CoreConstants } from '@/core/constants';
import { SQLiteDB } from '@classes/sqlitedb'; import { SQLiteDB } from '@classes/sqlitedb';
import { CoreError } from '@classes/errors/error'; import { CoreError } from '@classes/errors/error';
import { makeSingleton, Network } from '@singletons'; import { makeSingleton } from '@singletons';
import { CoreLogger } from '@singletons/logger'; import { CoreLogger } from '@singletons/logger';
import { APP_SCHEMA, CRON_TABLE_NAME, CronDBEntry } from '@services/database/cron'; import { APP_SCHEMA, CRON_TABLE_NAME, CronDBEntry } from '@services/database/cron';
@ -44,17 +44,9 @@ export class CoreCronDelegateService {
protected appDB: Promise<SQLiteDB>; protected appDB: Promise<SQLiteDB>;
protected resolveAppDB!: (appDB: SQLiteDB) => void; protected resolveAppDB!: (appDB: SQLiteDB) => void;
constructor(zone: NgZone) { constructor() {
this.appDB = new Promise(resolve => this.resolveAppDB = resolve); this.appDB = new Promise(resolve => this.resolveAppDB = resolve);
this.logger = CoreLogger.getInstance('CoreCronDelegate'); this.logger = CoreLogger.getInstance('CoreCronDelegate');
// When the app is re-connected, start network handlers that were stopped.
Network.onConnect().subscribe(() => {
// Execute the callback in the Angular zone, so change detection doesn't stop working.
zone.run(() => {
this.startNetworkHandlers();
});
});
} }
/** /**

View File

@ -97,8 +97,24 @@ export class CoreFilepoolProvider {
constructor() { constructor() {
this.appDB = new Promise(resolve => this.resolveAppDB = resolve); this.appDB = new Promise(resolve => this.resolveAppDB = resolve);
this.logger = CoreLogger.getInstance('CoreFilepoolProvider'); this.logger = CoreLogger.getInstance('CoreFilepoolProvider');
}
this.init(); /**
* Initialize queue.
*/
async initialize(): Promise<void> {
// Waiting for the app to be ready to start processing the queue.
await ApplicationInit.donePromise;
this.checkQueueProcessing();
// Start queue when device goes online.
Network.onConnect().subscribe(() => {
// Execute the callback in the Angular zone, so change detection doesn't stop working.
NgZone.run(() => {
this.checkQueueProcessing();
});
});
} }
/** /**
@ -114,24 +130,6 @@ export class CoreFilepoolProvider {
this.resolveAppDB(CoreApp.getDB()); this.resolveAppDB(CoreApp.getDB());
} }
/**
* Init some properties.
*/
protected async init(): Promise<void> {
// Waiting for the app to be ready to start processing the queue.
await ApplicationInit.donePromise;
this.checkQueueProcessing();
// Start queue when device goes online.
Network.onConnect().subscribe(() => {
// Execute the callback in the Angular zone, so change detection doesn't stop working.
NgZone.run(() => {
this.checkQueueProcessing();
});
});
}
/** /**
* Link a file with a component. * Link a file with a component.
* *

View File

@ -37,13 +37,11 @@ export class CoreLangProvider {
protected customStringsRaw?: string; protected customStringsRaw?: string;
protected sitePluginsStrings: CoreLanguageObject = {}; // Strings defined by site plugins. protected sitePluginsStrings: CoreLanguageObject = {}; // Strings defined by site plugins.
constructor() { async initialize(): Promise<void> {
// Set fallback language and language to use until the app determines the right language to use. // Set fallback language and language to use until the app determines the right language to use.
Translate.setDefaultLang(this.fallbackLanguage); Translate.setDefaultLang(this.fallbackLanguage);
Translate.use(this.defaultLanguage); Translate.use(this.defaultLanguage);
this.initLanguage();
Translate.onLangChange.subscribe((event: LangChangeEvent) => { Translate.onLangChange.subscribe((event: LangChangeEvent) => {
document.documentElement.setAttribute('lang', event.lang); document.documentElement.setAttribute('lang', event.lang);
@ -51,12 +49,14 @@ export class CoreLangProvider {
dir = dir.indexOf('rtl') != -1 ? 'rtl' : 'ltr'; dir = dir.indexOf('rtl') != -1 ? 'rtl' : 'ltr';
document.documentElement.setAttribute('dir', dir); document.documentElement.setAttribute('dir', dir);
}); });
await this.initializeCurrentLanguage();
} }
/** /**
* Init language. * Init language.
*/ */
protected async initLanguage(): Promise<void> { protected async initializeCurrentLanguage(): Promise<void> {
await Platform.ready(); await Platform.ready();
let language: string; let language: string;
@ -68,7 +68,7 @@ export class CoreLangProvider {
language = await this.getCurrentLanguage(); language = await this.getCurrentLanguage();
} }
return this.changeCurrentLanguage(language); await this.changeCurrentLanguage(language);
} }
/** /**

View File

@ -62,27 +62,12 @@ export class CoreLocalNotificationsProvider {
this.appDB = new Promise(resolve => this.resolveAppDB = resolve); this.appDB = new Promise(resolve => this.resolveAppDB = resolve);
this.logger = CoreLogger.getInstance('CoreLocalNotificationsProvider'); this.logger = CoreLogger.getInstance('CoreLocalNotificationsProvider');
this.queueRunner = new CoreQueueRunner(10); this.queueRunner = new CoreQueueRunner(10);
this.init();
}
/**
* Initialize database.
*/
async initializeDatabase(): Promise<void> {
try {
await CoreApp.createTablesFromSchema(APP_SCHEMA);
} catch (e) {
// Ignore errors.
}
this.resolveAppDB(CoreApp.getDB());
} }
/** /**
* Init some properties. * Init some properties.
*/ */
protected async init(): Promise<void> { async initialize(): Promise<void> {
await Platform.ready(); await Platform.ready();
if (!this.isAvailable()) { if (!this.isAvailable()) {
@ -131,6 +116,19 @@ export class CoreLocalNotificationsProvider {
}); });
} }
/**
* Initialize database.
*/
async initializeDatabase(): Promise<void> {
try {
await CoreApp.createTablesFromSchema(APP_SCHEMA);
} catch (e) {
// Ignore errors.
}
this.resolveAppDB(CoreApp.getDB());
}
/** /**
* Cancel a local notification. * Cancel a local notification.
* *

View File

@ -25,7 +25,7 @@ import { CoreTextUtils } from '@services/utils/text';
import { CoreUrlUtils } from '@services/utils/url'; import { CoreUrlUtils } from '@services/utils/url';
import { CoreUtils, PromiseDefer } from '@services/utils/utils'; import { CoreUtils, PromiseDefer } from '@services/utils/utils';
import { makeSingleton, Network, Platform, NgZone, Translate, Diagnostic } from '@singletons'; import { makeSingleton, Network, NgZone, Translate, Diagnostic } from '@singletons';
import { CoreLogger } from '@singletons/logger'; import { CoreLogger } from '@singletons/logger';
import { CoreUrl } from '@singletons/url'; import { CoreUrl } from '@singletons/url';
import { CoreWindow } from '@singletons/window'; import { CoreWindow } from '@singletons/window';
@ -52,11 +52,6 @@ export class CoreIframeUtilsProvider {
constructor() { constructor() {
this.logger = CoreLogger.getInstance('CoreUtilsProvider'); this.logger = CoreLogger.getInstance('CoreUtilsProvider');
if (CoreApp.isIOS() && 'WKUserScript' in window) {
// eslint-disable-next-line promise/catch-or-return
Platform.ready().then(() => this.injectiOSScripts(window));
}
} }
/** /**
@ -558,7 +553,7 @@ export class CoreIframeUtilsProvider {
* *
* @param userScriptWindow Window. * @param userScriptWindow Window.
*/ */
private injectiOSScripts(userScriptWindow: WKUserScriptWindow) { injectiOSScripts(userScriptWindow: WKUserScriptWindow): void {
const wwwPath = CoreFile.getWWWAbsolutePath(); const wwwPath = CoreFile.getWWWAbsolutePath();
const linksPath = CoreTextUtils.concatenatePaths(wwwPath, 'assets/js/iframe-treat-links.js'); const linksPath = CoreTextUtils.concatenatePaths(wwwPath, 'assets/js/iframe-treat-links.js');