diff --git a/src/addons/addons.module.ts b/src/addons/addons.module.ts new file mode 100644 index 000000000..c73da06f0 --- /dev/null +++ b/src/addons/addons.module.ts @@ -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 { NgModule } from '@angular/core'; + +import { AddonPrivateFilesInitModule } from './privatefiles/privatefiles-init.module'; + +@NgModule({ + imports: [ + AddonPrivateFilesInitModule, + ], +}) +export class AddonsModule {} diff --git a/src/app/app.module.ts b/src/app/app.module.ts index df93cf574..0206d2dde 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -12,65 +12,22 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { NgModule, Injector } from '@angular/core'; +import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { RouteReuseStrategy } from '@angular/router'; -import { HttpClient, HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http'; - -import { IonicModule, IonicRouteStrategy, Platform } from '@ionic/angular'; - -import { AppComponent } from './app.component'; -import { AppRoutingModule } from './app-routing.module'; -import { CoreInterceptor } from '@classes/interceptor'; - -// Import core services. -import { CoreAppProvider } from '@services/app'; -import { CoreConfigProvider } from '@services/config'; -import { CoreCronDelegate } from '@services/cron'; -import { CoreDbProvider } from '@services/db'; -import { CoreFileHelperProvider } from '@services/file-helper'; -import { CoreFileSessionProvider } from '@services/file-session'; -import { CoreFileProvider, CoreFile } from '@services/file'; -import { CoreFilepoolProvider } from '@services/filepool'; -import { CoreGeolocationProvider } from '@services/geolocation'; -import { CoreGroupsProvider } from '@services/groups'; -import { CoreInitDelegate, CoreInit } from '@services/init'; -import { CoreLangProvider } from '@services/lang'; -import { CoreLocalNotificationsProvider } from '@services/local-notifications'; -import { CorePluginFileDelegate } from '@services/plugin-file.delegate'; -import { CoreSitesProvider, CoreSites } from '@services/sites'; -import { CoreSyncProvider } from '@services/sync'; -import { CoreUpdateManagerProvider, CoreUpdateManager } from '@services/update-manager'; -import { CoreWSProvider } from '@services/ws'; -import { CoreDomUtilsProvider } from '@services/utils/dom'; -import { CoreIframeUtilsProvider } from '@services/utils/iframe'; -import { CoreMimetypeUtilsProvider } from '@services/utils/mimetype'; -import { CoreTextUtilsProvider } from '@services/utils/text'; -import { CoreTimeUtilsProvider } from '@services/utils/time'; -import { CoreUrlUtilsProvider } from '@services/utils/url'; -import { CoreUtilsProvider } from '@services/utils/utils'; - -// Import init DB functions of core services. -import { initCoreFilepoolDB } from '@services/filepool.db'; -import { initCoreSitesDB } from '@services/sites.db'; -import { initCoreSyncDB } from '@services/sync.db'; -import { initCoreSearchHistoryDB } from '@features/search/services/search.history.db'; - -// Import core modules. -import { CoreEmulatorModule } from '@features/emulator/emulator.module'; -import { CoreLoginModule } from '@features/login/login.module'; -import { CoreCoursesModule } from '@features/courses/courses.module'; -import { CoreSettingsInitModule } from '@features/settings/settings-init.module'; -import { CoreFileUploaderInitModule } from '@features/fileuploader/fileuploader-init.module'; - -// Import addons init modules. -import { AddonPrivateFilesInitModule } from '@/addons/privatefiles/privatefiles-init.module'; - -import { setSingletonsInjector } from '@singletons/core.singletons'; +import { HttpClient, HttpClientModule } from '@angular/common/http'; import { TranslateModule, TranslateLoader } from '@ngx-translate/core'; import { TranslateHttpLoader } from '@ngx-translate/http-loader'; +import { IonicModule, IonicRouteStrategy } from '@ionic/angular'; + +import { CoreModule } from '@/core/core.module'; +import { AddonsModule } from '@/addons/addons.module'; + +import { AppComponent } from './app.component'; +import { AppRoutingModule } from './app-routing.module'; + // For translate loader. AoT requires an exported function for factories. export function createTranslateLoader(http: HttpClient): TranslateHttpLoader { return new TranslateHttpLoader(http, './assets/lang/', '.json'); @@ -91,93 +48,12 @@ export function createTranslateLoader(http: HttpClient): TranslateHttpLoader { }, }), AppRoutingModule, - CoreEmulatorModule, - CoreLoginModule, - CoreCoursesModule, - CoreSettingsInitModule, - CoreFileUploaderInitModule, - AddonPrivateFilesInitModule, + CoreModule, + AddonsModule, ], providers: [ { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }, - { provide: HTTP_INTERCEPTORS, useClass: CoreInterceptor, multi: true }, - CoreAppProvider, - CoreConfigProvider, - CoreCronDelegate, - CoreDbProvider, - CoreFileHelperProvider, - CoreFileSessionProvider, - CoreFileProvider, - CoreFilepoolProvider, - CoreGeolocationProvider, - CoreGroupsProvider, - CoreInitDelegate, - CoreLangProvider, - CoreLocalNotificationsProvider, - CorePluginFileDelegate, - CoreSitesProvider, - CoreSyncProvider, - CoreUpdateManagerProvider, - CoreWSProvider, - CoreDomUtilsProvider, - CoreIframeUtilsProvider, - CoreMimetypeUtilsProvider, - CoreTextUtilsProvider, - CoreTimeUtilsProvider, - CoreUrlUtilsProvider, - CoreUtilsProvider, ], bootstrap: [AppComponent], }) -export class AppModule { - - constructor(injector: Injector, platform: Platform) { - // Set the injector. - setSingletonsInjector(injector); - - this.initCoreServicesDB(); - - // Register a handler for platform ready. - CoreInit.instance.registerProcess({ - name: 'CorePlatformReady', - priority: CoreInitDelegate.MAX_RECOMMENDED_PRIORITY + 400, - blocking: true, - load: async () => { - await platform.ready(); - }, - }); - - // Register the update manager as an init process. - CoreInit.instance.registerProcess(CoreUpdateManager.instance); - - // Restore the user's session during the init process. - CoreInit.instance.registerProcess({ - name: 'CoreRestoreSession', - priority: CoreInitDelegate.MAX_RECOMMENDED_PRIORITY + 200, - blocking: false, - load: CoreSites.instance.restoreSession.bind(CoreSites.instance), - }); - - // Register clear app tmp folder. - CoreInit.instance.registerProcess({ - name: 'CoreClearTmpFolder', - priority: CoreInitDelegate.MAX_RECOMMENDED_PRIORITY + 150, - blocking: false, - load: CoreFile.instance.clearTmpFolder.bind(CoreFile.instance), - }); - - // Execute the init processes. - CoreInit.instance.executeInitProcesses(); - } - - /** - * Init the DB of core services. - */ - protected initCoreServicesDB(): void { - initCoreFilepoolDB(); - initCoreSitesDB(); - initCoreSyncDB(); - initCoreSearchHistoryDB(); - } - -} +export class AppModule {} diff --git a/src/core/core.module.ts b/src/core/core.module.ts new file mode 100644 index 000000000..839c48171 --- /dev/null +++ b/src/core/core.module.ts @@ -0,0 +1,87 @@ +// (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 { HTTP_INTERCEPTORS } from '@angular/common/http'; +import { Injector, NgModule } from '@angular/core'; + +import { Platform } from '@ionic/angular'; + +import { CoreFeaturesModule } from './features/features.module'; +import { CoreFile } from './services/file'; +import { CoreInit, CoreInitDelegate } from './services/init'; +import { CoreInterceptor } from './classes/interceptor'; +import { CoreSites, CORE_SITE_SCHEMAS } from './services/sites'; +import { CoreUpdateManager } from './services/update-manager'; +import { setSingletonsInjector } from './singletons/core.singletons'; +import { SITE_SCHEMA as FILEPOOL_SITE_SCHEMA } from './services/filepool-db'; +import { SITE_SCHEMA as SITES_SITE_SCHEMA } from './services/sites-db'; +import { SITE_SCHEMA as SYNC_SITE_SCHEMA } from './services/sync-db'; + +@NgModule({ + imports: [ + CoreFeaturesModule, + ], + providers: [ + { provide: HTTP_INTERCEPTORS, useClass: CoreInterceptor, multi: true }, + { + provide: CORE_SITE_SCHEMAS, + useValue: [ + FILEPOOL_SITE_SCHEMA, + SITES_SITE_SCHEMA, + SYNC_SITE_SCHEMA, + ], + multi: true, + }, + ], +}) +export class CoreModule { + + constructor(platform: Platform, injector: Injector) { + // Set the injector. + setSingletonsInjector(injector); + + // Register a handler for platform ready. + CoreInit.instance.registerProcess({ + name: 'CorePlatformReady', + priority: CoreInitDelegate.MAX_RECOMMENDED_PRIORITY + 400, + blocking: true, + load: async () => { + await platform.ready(); + }, + }); + + // Register the update manager as an init process. + CoreInit.instance.registerProcess(CoreUpdateManager.instance); + + // Restore the user's session during the init process. + CoreInit.instance.registerProcess({ + name: 'CoreRestoreSession', + priority: CoreInitDelegate.MAX_RECOMMENDED_PRIORITY + 200, + blocking: false, + load: CoreSites.instance.restoreSession.bind(CoreSites.instance), + }); + + // Register clear app tmp folder. + CoreInit.instance.registerProcess({ + name: 'CoreClearTmpFolder', + priority: CoreInitDelegate.MAX_RECOMMENDED_PRIORITY + 150, + blocking: false, + load: CoreFile.instance.clearTmpFolder.bind(CoreFile.instance), + }); + + // Execute the init processes. + CoreInit.instance.executeInitProcesses(); + } + +} diff --git a/src/core/features/features.module.ts b/src/core/features/features.module.ts new file mode 100644 index 000000000..ff3633baf --- /dev/null +++ b/src/core/features/features.module.ts @@ -0,0 +1,32 @@ +// (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 { NgModule } from '@angular/core'; + +import { CoreCoursesModule } from './courses/courses.module'; +import { CoreEmulatorModule } from './emulator/emulator.module'; +import { CoreFileUploaderInitModule } from './fileuploader/fileuploader-init.module'; +import { CoreLoginModule } from './login/login.module'; +import { CoreSettingsInitModule } from './settings/settings-init.module'; + +@NgModule({ + imports: [ + CoreEmulatorModule, + CoreLoginModule, + CoreCoursesModule, + CoreSettingsInitModule, + CoreFileUploaderInitModule, + ], +}) +export class CoreFeaturesModule {} diff --git a/src/core/features/search/components/search-box/search-box.ts b/src/core/features/search/components/search-box/search-box.ts index 23ab9a6ee..dd2048831 100644 --- a/src/core/features/search/components/search-box/search-box.ts +++ b/src/core/features/search/components/search-box/search-box.ts @@ -19,7 +19,7 @@ import { CoreDomUtils } from '@services/utils/dom'; import { CoreUtils } from '@services/utils/utils'; import { CoreSearchHistory } from '../../services/search-history'; import { Translate } from '@singletons/core.singletons'; -import { CoreSearchHistoryDBRecord } from '../../services/search.history.db'; +import { CoreSearchHistoryDBRecord } from '../../services/search-history-db'; /** * Component to display a "search box". diff --git a/src/core/features/search/search.module.ts b/src/core/features/search/search.module.ts index 4557cc77a..a59da3976 100644 --- a/src/core/features/search/search.module.ts +++ b/src/core/features/search/search.module.ts @@ -13,7 +13,11 @@ // limitations under the License. import { NgModule } from '@angular/core'; + +import { CORE_SITE_SCHEMAS } from '@services/sites'; + import { CoreSearchComponentsModule } from './components/components.module'; +import { SITE_SCHEMA } from './services/search-history-db'; @NgModule({ declarations: [ @@ -23,6 +27,7 @@ import { CoreSearchComponentsModule } from './components/components.module'; ], providers: [ CoreSearchComponentsModule, + { provide: CORE_SITE_SCHEMAS, useValue: [SITE_SCHEMA], multi: true }, ], }) export class CoreSearchModule {} diff --git a/src/core/features/search/services/search.history.db.ts b/src/core/features/search/services/search-history-db.ts similarity index 90% rename from src/core/features/search/services/search.history.db.ts rename to src/core/features/search/services/search-history-db.ts index abc79d8c9..19a1e8819 100644 --- a/src/core/features/search/services/search.history.db.ts +++ b/src/core/features/search/services/search-history-db.ts @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { CoreSiteSchema, registerSiteSchema } from '@services/sites'; +import { CoreSiteSchema } from '@services/sites'; /** * Database variables for CoreSearchHistory service. */ export const SEARCH_HISTORY_TABLE_NAME = 'seach_history'; -const SITE_SCHEMA: CoreSiteSchema = { +export const SITE_SCHEMA: CoreSiteSchema = { name: 'CoreSearchHistoryProvider', version: 1, tables: [ @@ -60,7 +60,3 @@ export type CoreSearchHistoryDBRecord = { searchedtext: string; // Text of the performed search. times: number; // Times search has been performed (if previously in history). }; - -export const initCoreSearchHistoryDB = (): void => { - registerSiteSchema(SITE_SCHEMA); -}; diff --git a/src/core/features/search/services/search-history.ts b/src/core/features/search/services/search-history.ts index 9ed2afaa4..7aa98df67 100644 --- a/src/core/features/search/services/search-history.ts +++ b/src/core/features/search/services/search-history.ts @@ -16,7 +16,7 @@ import { Injectable } from '@angular/core'; import { CoreSites } from '@services/sites'; import { SQLiteDB } from '@classes/sqlitedb'; -import { CoreSearchHistoryDBRecord, SEARCH_HISTORY_TABLE_NAME } from './search.history.db'; +import { CoreSearchHistoryDBRecord, SEARCH_HISTORY_TABLE_NAME } from './search-history-db'; import { makeSingleton } from '@singletons/core.singletons'; /** diff --git a/src/core/services/app.ts b/src/core/services/app.ts index 8ced91397..a09eea202 100644 --- a/src/core/services/app.ts +++ b/src/core/services/app.ts @@ -40,7 +40,7 @@ import { DBNAME, SCHEMA_VERSIONS_TABLE_NAME, SCHEMA_VERSIONS_TABLE_SCHEMA, Schem * } * ``` */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreAppProvider { protected db: SQLiteDB; diff --git a/src/core/services/config.ts b/src/core/services/config.ts index 9cdf15782..ed7d54f73 100644 --- a/src/core/services/config.ts +++ b/src/core/services/config.ts @@ -23,7 +23,7 @@ import { CONFIG_TABLE_NAME, APP_SCHEMA, ConfigDBEntry } from '@services/config.d * Factory to provide access to dynamic and permanent config and settings. * It should not be abused into a temporary storage. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreConfigProvider { protected appDB: SQLiteDB; diff --git a/src/core/services/cron.ts b/src/core/services/cron.ts index e7c6e2fc7..a96fec9cb 100644 --- a/src/core/services/cron.ts +++ b/src/core/services/cron.ts @@ -28,7 +28,7 @@ import { APP_SCHEMA, CRON_TABLE_NAME, CronDBEntry } from '@services/cron.db'; /* * Service to handle cron processes. The registered processes will be executed every certain time. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreCronDelegate { // Constants. diff --git a/src/core/services/db.ts b/src/core/services/db.ts index ce0979c67..c02b1a416 100644 --- a/src/core/services/db.ts +++ b/src/core/services/db.ts @@ -21,7 +21,7 @@ import { makeSingleton, SQLite, Platform } from '@singletons/core.singletons'; /** * This service allows interacting with the local database to store and retrieve data. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreDbProvider { protected dbInstances: {[name: string]: SQLiteDB} = {}; diff --git a/src/core/services/file-helper.ts b/src/core/services/file-helper.ts index da28831c8..e9b68c725 100644 --- a/src/core/services/file-helper.ts +++ b/src/core/services/file-helper.ts @@ -30,7 +30,7 @@ import { makeSingleton, Translate } from '@singletons/core.singletons'; /** * Provider to provide some helper functions regarding files and packages. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreFileHelperProvider { /** diff --git a/src/core/services/file-session.ts b/src/core/services/file-session.ts index 9cbdeadc9..c9e9fc661 100644 --- a/src/core/services/file-session.ts +++ b/src/core/services/file-session.ts @@ -26,7 +26,7 @@ import { makeSingleton } from '@singletons/core.singletons'; * Every component can provide a File area identifier to indentify every file list on the session. * This value can be the activity id or a mix of name and numbers. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreFileSessionProvider { protected files: {[siteId: string]: {[component: string]: {[id: string]: (CoreWSExternalFile | FileEntry)[]}}} = {}; diff --git a/src/core/services/file.ts b/src/core/services/file.ts index f760f2d2f..2e9704fa0 100644 --- a/src/core/services/file.ts +++ b/src/core/services/file.ts @@ -66,7 +66,7 @@ export const enum CoreFileFormat { /** * Factory to interact with the file system. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreFileProvider { // Formats to read a file. diff --git a/src/core/services/filepool.db.ts b/src/core/services/filepool-db.ts similarity index 98% rename from src/core/services/filepool.db.ts rename to src/core/services/filepool-db.ts index 42c8a15b0..710584800 100644 --- a/src/core/services/filepool.db.ts +++ b/src/core/services/filepool-db.ts @@ -13,7 +13,7 @@ // limitations under the License. import { CoreAppSchema } from '@services/app'; -import { CoreSiteSchema, registerSiteSchema } from '@services/sites'; +import { CoreSiteSchema } from '@services/sites'; /** * Database variables for CoreFilepool service. @@ -361,7 +361,3 @@ export type CoreFilepoolLinksRecord = { component: string; // Component name. componentId: number | string; // Component Id. }; - -export const initCoreFilepoolDB = (): void => { - registerSiteSchema(SITE_SCHEMA); -}; diff --git a/src/core/services/filepool.ts b/src/core/services/filepool.ts index 0318b7f28..279d14239 100644 --- a/src/core/services/filepool.ts +++ b/src/core/services/filepool.ts @@ -46,7 +46,7 @@ import { CoreFilepoolPackageEntry, CoreFilepoolQueueEntry, CoreFilepoolQueueDBEntry, -} from '@services/filepool.db'; +} from '@services/filepool-db'; /* * Factory for handling downloading files and retrieve downloaded files. @@ -57,7 +57,7 @@ import { * The two main goals of this is to keep the content available offline, and improve the user experience by caching * the content locally. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreFilepoolProvider { // Constants. diff --git a/src/core/services/geolocation.ts b/src/core/services/geolocation.ts index 110b368e8..05214b1f2 100644 --- a/src/core/services/geolocation.ts +++ b/src/core/services/geolocation.ts @@ -19,7 +19,7 @@ import { CoreApp } from '@services/app'; import { CoreError } from '@classes/errors/error'; import { Geolocation, Diagnostic, makeSingleton } from '@singletons/core.singletons'; -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreGeolocationProvider { /** diff --git a/src/core/services/groups.ts b/src/core/services/groups.ts index 3eb041db4..b82596a84 100644 --- a/src/core/services/groups.ts +++ b/src/core/services/groups.ts @@ -24,7 +24,7 @@ import { CoreCourseBase } from '@/types/global'; /* * Service to handle groups. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreGroupsProvider { // Group mode constants. diff --git a/src/core/services/init.ts b/src/core/services/init.ts index 5a8e042bd..feb39b7a3 100644 --- a/src/core/services/init.ts +++ b/src/core/services/init.ts @@ -48,7 +48,7 @@ export type CoreInitHandler = { /* * Provider for initialisation mechanisms. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreInitDelegate { static readonly DEFAULT_PRIORITY = 100; // Default priority for init processes. diff --git a/src/core/services/lang.ts b/src/core/services/lang.ts index 571d4df81..e1e0d6879 100644 --- a/src/core/services/lang.ts +++ b/src/core/services/lang.ts @@ -25,7 +25,7 @@ import * as moment from 'moment'; /* * Service to handle language features, like changing the current language. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreLangProvider { protected fallbackLanguage = 'en'; // Always use English as fallback language since it contains all strings. diff --git a/src/core/services/local-notifications.ts b/src/core/services/local-notifications.ts index c20ca3217..353affdee 100644 --- a/src/core/services/local-notifications.ts +++ b/src/core/services/local-notifications.ts @@ -39,7 +39,7 @@ import { /** * Service to handle local notifications. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreLocalNotificationsProvider { protected logger: CoreLogger; diff --git a/src/core/services/plugin-file.delegate.ts b/src/core/services/plugin-file.delegate.ts index 3c46e0ab6..cfd57613b 100644 --- a/src/core/services/plugin-file.delegate.ts +++ b/src/core/services/plugin-file.delegate.ts @@ -24,7 +24,7 @@ import { makeSingleton } from '@singletons/core.singletons'; /** * Delegate to register pluginfile information handlers. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CorePluginFileDelegate extends CoreDelegate { protected handlerNameProperty = 'component'; diff --git a/src/core/services/sites.db.ts b/src/core/services/sites-db.ts similarity index 97% rename from src/core/services/sites.db.ts rename to src/core/services/sites-db.ts index 83c6e8027..a6eea9464 100644 --- a/src/core/services/sites.db.ts +++ b/src/core/services/sites-db.ts @@ -13,7 +13,7 @@ // limitations under the License. import { CoreAppSchema } from '@services/app'; -import { CoreSiteSchema, registerSiteSchema } from '@services/sites'; +import { CoreSiteSchema } from '@services/sites'; import { SQLiteDB, SQLiteDBTableSchema } from '@classes/sqlitedb'; import { CoreSite } from '@classes/site'; @@ -227,7 +227,3 @@ export type SchemaVersionsDBEntry = { name: string; version: number; }; - -export const initCoreSitesDB = (): void => { - registerSiteSchema(SITE_SCHEMA); -}; diff --git a/src/core/services/sites.ts b/src/core/services/sites.ts index d60e0d026..3d8d52de2 100644 --- a/src/core/services/sites.ts +++ b/src/core/services/sites.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Injectable } from '@angular/core'; +import { Inject, Injectable, InjectionToken, Optional } from '@angular/core'; import { Md5 } from 'ts-md5/dist/md5'; import { timeout } from 'rxjs/operators'; @@ -47,25 +47,17 @@ import { SiteDBEntry, CurrentSiteDBEntry, SchemaVersionsDBEntry, -} from '@services/sites.db'; +} from '@services/sites-db'; +import { CoreArray } from '../singletons/array'; - -// Schemas for site tables. Other providers can add schemas in here using the registerSiteSchema function. -const siteSchemas: { [name: string]: CoreRegisteredSiteSchema } = {}; -export const registerSiteSchema = (schema: CoreSiteSchema): void => { - siteSchemas[schema.name] = schema; -}; +export const CORE_SITE_SCHEMAS = new InjectionToken('CORE_SITE_SCHEMAS'); /* * Service to manage and interact with sites. * It allows creating tables in the databases of all sites. Each service or component should be responsible of creating - * their own database tables. Example: - * - * import { registerSiteSchema } from '@services/sites'; - * - * registerSiteSchema(tableSchema); + * their own database tables calling the registerCoreSiteSchema method. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreSitesProvider { // Constants to validate a site version. @@ -84,11 +76,19 @@ export class CoreSitesProvider { protected appDB: SQLiteDB; protected dbReady: Promise; // Promise resolved when the app DB is initialized. protected siteSchemasMigration: { [siteId: string]: Promise } = {}; + protected siteSchemas: { [name: string]: CoreRegisteredSiteSchema } = {}; protected pluginsSiteSchemas: { [name: string]: CoreRegisteredSiteSchema } = {}; - constructor() { + constructor(@Optional() @Inject(CORE_SITE_SCHEMAS) siteSchemas: CoreSiteSchema[][] = []) { this.logger = CoreLogger.getInstance('CoreSitesProvider'); + this.siteSchemas = CoreArray.flatten(siteSchemas).reduce( + (siteSchemas, schema) => { + siteSchemas[schema.name] = schema; + return siteSchemas; + }, + this.siteSchemas, + ); this.appDB = CoreApp.instance.getDB(); this.dbReady = CoreApp.instance.createTablesFromSchema(APP_SCHEMA).catch(() => { // Ignore errors. @@ -1446,7 +1446,7 @@ export class CoreSitesProvider { /** * Register a site schema in current site. * This function is meant for site plugins to create DB tables in current site. Tables created from within the app - * whould use the registerSiteSchema function exported in this same file. + * should use the registerCoreSiteSchema method instead. * * @param schema The schema to register. * @return Promise resolved when done. @@ -1489,7 +1489,7 @@ export class CoreSitesProvider { // First create tables not registerd with name/version. const promise = site.getDb().createTableFromSchema(SCHEMA_VERSIONS_TABLE_SCHEMA) - .then(() => this.applySiteSchemas(site, siteSchemas)); + .then(() => this.applySiteSchemas(site, this.siteSchemas)); this.siteSchemasMigration[site.id] = promise; @@ -1602,7 +1602,7 @@ export class CoreSitesProvider { */ getSiteTableSchemasToClear(site: CoreSite): string[] { let reset: string[] = []; - const schemas = Object.values(siteSchemas).concat(Object.values(this.pluginsSiteSchemas)); + const schemas = Object.values(this.siteSchemas).concat(Object.values(this.pluginsSiteSchemas)); schemas.forEach((schema) => { if (schema.canBeCleared && (!schema.siteId || site.getId() == schema.siteId)) { diff --git a/src/core/services/sync.db.ts b/src/core/services/sync-db.ts similarity index 88% rename from src/core/services/sync.db.ts rename to src/core/services/sync-db.ts index 47d5dc900..c30db03c2 100644 --- a/src/core/services/sync.db.ts +++ b/src/core/services/sync-db.ts @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { CoreSiteSchema, registerSiteSchema } from '@services/sites'; +import { CoreSiteSchema } from '@services/sites'; /** * Database variables for CoreSync service. */ export const SYNC_TABLE_NAME = 'sync'; -const SITE_SCHEMA: CoreSiteSchema = { +export const SITE_SCHEMA: CoreSiteSchema = { name: 'CoreSyncProvider', version: 1, tables: [ @@ -55,7 +55,3 @@ export type CoreSyncRecord = { time: number; warnings: string; }; - -export const initCoreSyncDB = (): void => { - registerSiteSchema(SITE_SCHEMA); -}; diff --git a/src/core/services/sync.ts b/src/core/services/sync.ts index 16383a6f7..2b7f4a32e 100644 --- a/src/core/services/sync.ts +++ b/src/core/services/sync.ts @@ -16,12 +16,12 @@ import { Injectable } from '@angular/core'; import { CoreEvents } from '@singletons/events'; import { CoreSites } from '@services/sites'; import { makeSingleton } from '@singletons/core.singletons'; -import { SYNC_TABLE_NAME, CoreSyncRecord } from '@services/sync.db'; +import { SYNC_TABLE_NAME, CoreSyncRecord } from '@services/sync-db'; /* * Service that provides some features regarding synchronization. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreSyncProvider { // Store blocked sync objects. diff --git a/src/core/services/update-manager.ts b/src/core/services/update-manager.ts index 4c8eca8d9..78ee29cdd 100644 --- a/src/core/services/update-manager.ts +++ b/src/core/services/update-manager.ts @@ -27,7 +27,7 @@ const VERSION_APPLIED = 'version_applied'; * * This service handles processes that need to be run when updating the app, like migrate Ionic 1 database data to Ionic 3. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreUpdateManagerProvider implements CoreInitHandler { // Data for init delegate. diff --git a/src/core/services/utils/dom.ts b/src/core/services/utils/dom.ts index eb6ac5042..0e7473e1f 100644 --- a/src/core/services/utils/dom.ts +++ b/src/core/services/utils/dom.ts @@ -38,7 +38,7 @@ import { CoreLogger } from '@singletons/logger'; /* * "Utils" service with helper functions for UI, DOM elements and HTML code. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreDomUtilsProvider { protected readonly INSTANCE_ID_ATTR_NAME = 'core-instance-id'; diff --git a/src/core/services/utils/iframe.ts b/src/core/services/utils/iframe.ts index b6e09d003..3f0541a4e 100644 --- a/src/core/services/utils/iframe.ts +++ b/src/core/services/utils/iframe.ts @@ -42,7 +42,7 @@ type CoreFrameElement = (HTMLIFrameElement | HTMLFrameElement | HTMLObjectElemen /* * "Utils" service with helper functions for iframes, embed and similar. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreIframeUtilsProvider { static readonly FRAME_TAGS = ['iframe', 'frame', 'object', 'embed']; diff --git a/src/core/services/utils/mimetype.ts b/src/core/services/utils/mimetype.ts index bbf1219c4..4f400f8c5 100644 --- a/src/core/services/utils/mimetype.ts +++ b/src/core/services/utils/mimetype.ts @@ -44,7 +44,7 @@ const EXTENSION_REGEX = /^[a-z0-9]+$/; /* * "Utils" service with helper functions for mimetypes and extensions. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreMimetypeUtilsProvider { protected logger: CoreLogger; diff --git a/src/core/services/utils/text.ts b/src/core/services/utils/text.ts index dcaf3c00f..a5a81301d 100644 --- a/src/core/services/utils/text.ts +++ b/src/core/services/utils/text.ts @@ -37,7 +37,7 @@ export type CoreTextErrorObject = { /* * "Utils" service with helper functions for text. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreTextUtilsProvider { // List of regular expressions to convert the old nomenclature to new nomenclature for disabled features. diff --git a/src/core/services/utils/time.ts b/src/core/services/utils/time.ts index 0cdbfba45..345e10008 100644 --- a/src/core/services/utils/time.ts +++ b/src/core/services/utils/time.ts @@ -21,7 +21,7 @@ import { makeSingleton, Translate } from '@singletons/core.singletons'; /* * "Utils" service with helper functions for date and time. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreTimeUtilsProvider { protected static readonly FORMAT_REPLACEMENTS = { // To convert PHP strf format to Moment format. diff --git a/src/core/services/utils/url.ts b/src/core/services/utils/url.ts index 4f0917d0c..fb5489abe 100644 --- a/src/core/services/utils/url.ts +++ b/src/core/services/utils/url.ts @@ -23,7 +23,7 @@ import { CoreUrl } from '@singletons/url'; /* * "Utils" service with helper functions for URLs. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreUrlUtilsProvider { /** diff --git a/src/core/services/utils/utils.ts b/src/core/services/utils/utils.ts index 676f9855a..be7c87793 100644 --- a/src/core/services/utils/utils.ts +++ b/src/core/services/utils/utils.ts @@ -36,7 +36,7 @@ type TreeNode = T & { children: TreeNode[] }; /* * "Utils" service with helper functions. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreUtilsProvider { protected readonly DONT_CLONE = ['[object FileEntry]', '[object DirectoryEntry]', '[object DOMFileSystem]']; diff --git a/src/core/services/ws.ts b/src/core/services/ws.ts index 9e0d211eb..cd7b37868 100644 --- a/src/core/services/ws.ts +++ b/src/core/services/ws.ts @@ -40,7 +40,7 @@ import { CoreAjaxWSError } from '@classes/errors/ajaxwserror'; /** * This service allows performing WS calls and download/upload files. */ -@Injectable() +@Injectable({ providedIn: 'root' }) export class CoreWSProvider { protected logger: CoreLogger;