From 6d1d4a34ce7a1000b1a3a9dad267d6ece01d737b Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Wed, 9 Dec 2020 13:39:18 +0100 Subject: [PATCH 1/5] MOBILE-3320 core: Standarize "initialize" usage --- src/core/initializers/initialize-databases.ts | 12 ++++++------ src/core/services/app.ts | 4 ++-- src/core/services/config.ts | 4 ++-- src/core/services/cron.ts | 4 ++-- src/core/services/filepool.ts | 4 ++-- src/core/services/local-notifications.ts | 4 ++-- src/core/services/sites.ts | 4 ++-- 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/core/initializers/initialize-databases.ts b/src/core/initializers/initialize-databases.ts index 9772871af..67b15aca9 100644 --- a/src/core/initializers/initialize-databases.ts +++ b/src/core/initializers/initialize-databases.ts @@ -21,11 +21,11 @@ import { CoreSites } from '@services/sites'; export default async function(): Promise { await Promise.all([ - CoreApp.instance.initialiseDatabase(), - CoreConfig.instance.initialiseDatabase(), - CoreCronDelegate.instance.initialiseDatabase(), - CoreFilepool.instance.initialiseDatabase(), - CoreLocalNotifications.instance.initialiseDatabase(), - CoreSites.instance.initialiseDatabase(), + CoreApp.instance.initializeDatabase(), + CoreConfig.instance.initializeDatabase(), + CoreCronDelegate.instance.initializeDatabase(), + CoreFilepool.instance.initializeDatabase(), + CoreLocalNotifications.instance.initializeDatabase(), + CoreSites.instance.initializeDatabase(), ]); } diff --git a/src/core/services/app.ts b/src/core/services/app.ts index 66fbc8e8d..d14d121a8 100644 --- a/src/core/services/app.ts +++ b/src/core/services/app.ts @@ -85,9 +85,9 @@ export class CoreAppProvider { } /** - * Initialise database. + * Initialize database. */ - async initialiseDatabase(): Promise { + async initializeDatabase(): Promise { await this.db.createTableFromSchema(SCHEMA_VERSIONS_TABLE_SCHEMA); this.resolveSchemaVersionsManager({ diff --git a/src/core/services/config.ts b/src/core/services/config.ts index a39e593ba..edc5e22d1 100644 --- a/src/core/services/config.ts +++ b/src/core/services/config.ts @@ -34,9 +34,9 @@ export class CoreConfigProvider { } /** - * Initialise database. + * Initialize database. */ - async initialiseDatabase(): Promise { + async initializeDatabase(): Promise { try { await CoreApp.instance.createTablesFromSchema(APP_SCHEMA); } catch (e) { diff --git a/src/core/services/cron.ts b/src/core/services/cron.ts index c30524cee..2ccc6e868 100644 --- a/src/core/services/cron.ts +++ b/src/core/services/cron.ts @@ -58,9 +58,9 @@ export class CoreCronDelegateService { } /** - * Initialise database. + * Initialize database. */ - async initialiseDatabase(): Promise { + async initializeDatabase(): Promise { try { await CoreApp.instance.createTablesFromSchema(APP_SCHEMA); } catch (e) { diff --git a/src/core/services/filepool.ts b/src/core/services/filepool.ts index a82ae02d0..974225eed 100644 --- a/src/core/services/filepool.ts +++ b/src/core/services/filepool.ts @@ -101,9 +101,9 @@ export class CoreFilepoolProvider { } /** - * Initialise database. + * Initialize database. */ - async initialiseDatabase(): Promise { + async initializeDatabase(): Promise { try { await CoreApp.instance.createTablesFromSchema(APP_SCHEMA); } catch (e) { diff --git a/src/core/services/local-notifications.ts b/src/core/services/local-notifications.ts index 7a60f3ded..5bd1c3ba2 100644 --- a/src/core/services/local-notifications.ts +++ b/src/core/services/local-notifications.ts @@ -69,9 +69,9 @@ export class CoreLocalNotificationsProvider { } /** - * Initialise database. + * Initialize database. */ - async initialiseDatabase(): Promise { + async initializeDatabase(): Promise { try { await CoreApp.instance.createTablesFromSchema(APP_SCHEMA); } catch (e) { diff --git a/src/core/services/sites.ts b/src/core/services/sites.ts index 7c6c6ac16..36aa71550 100644 --- a/src/core/services/sites.ts +++ b/src/core/services/sites.ts @@ -94,9 +94,9 @@ export class CoreSitesProvider { } /** - * Initialise database. + * Initialize database. */ - async initialiseDatabase(): Promise { + async initializeDatabase(): Promise { try { await CoreApp.instance.createTablesFromSchema(APP_SCHEMA); } catch (e) { From 012d211d1028e8bb334b36d106e40ab991220aec Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Wed, 9 Dec 2020 13:40:17 +0100 Subject: [PATCH 2/5] MOBILE-3320 core: Move animation helpers --- src/core/classes/animations.ts | 65 ----------------- src/core/components/animations.ts | 72 +++++++++++++++++++ .../download-refresh/download-refresh.ts | 4 +- src/core/components/loading/loading.ts | 4 +- 4 files changed, 76 insertions(+), 69 deletions(-) delete mode 100644 src/core/classes/animations.ts create mode 100644 src/core/components/animations.ts diff --git a/src/core/classes/animations.ts b/src/core/classes/animations.ts deleted file mode 100644 index 510ef6d88..000000000 --- a/src/core/classes/animations.ts +++ /dev/null @@ -1,65 +0,0 @@ -// (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 { trigger, style, transition, animate, keyframes } from '@angular/animations'; - -export const coreShowHideAnimation = trigger('coreShowHideAnimation', [ - transition(':enter', [ - style({ opacity: 0 }), - animate('500ms ease-in-out', style({ opacity: 1 })), - ]), - transition(':leave', [ - style({ opacity: 1 }), - animate('500ms ease-in-out', style({ opacity: 0 })), - ]), -]); - -export const coreSlideInOut = trigger('coreSlideInOut', [ - // Enter animation. - transition('void => fromLeft', [ - style({ transform: 'translateX(0)', opacity: 1 }), - animate(300, keyframes([ - style({ opacity: 0, transform: 'translateX(-100%)', offset: 0 }), - style({ opacity: 1, transform: 'translateX(5%)', offset: 0.7 }), - style({ opacity: 1, transform: 'translateX(0)', offset: 1.0 }), - ])), - ]), - // Leave animation. - transition('fromLeft => void', [ - style({ transform: 'translateX(-100%)', opacity: 0 }), - animate(300, keyframes([ - style({ opacity: 1, transform: 'translateX(0)', offset: 0 }), - style({ opacity: 1, transform: 'translateX(5%)', offset: 0.3 }), - style({ opacity: 0, transform: 'translateX(-100%)', offset: 1.0 }), - ])), - ]), - // Enter animation. - transition('void => fromRight', [ - style({ transform: 'translateX(0)', opacity: 1 }), - animate(300, keyframes([ - style({ opacity: 0, transform: 'translateX(100%)', offset: 0 }), - style({ opacity: 1, transform: 'translateX(-5%)', offset: 0.7 }), - style({ opacity: 1, transform: 'translateX(0)', offset: 1.0 }), - ])), - ]), - // Leave animation. - transition('fromRight => void', [ - style({ transform: 'translateX(-100%)', opacity: 0 }), - animate(300, keyframes([ - style({ opacity: 1, transform: 'translateX(0)', offset: 0 }), - style({ opacity: 1, transform: 'translateX(-5%)', offset: 0.3 }), - style({ opacity: 0, transform: 'translateX(100%)', offset: 1.0 }), - ])), - ]), -]); diff --git a/src/core/components/animations.ts b/src/core/components/animations.ts new file mode 100644 index 000000000..355386230 --- /dev/null +++ b/src/core/components/animations.ts @@ -0,0 +1,72 @@ +// (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 { trigger, style, transition, animate, keyframes } from '@angular/animations'; + +/** + * Defines core animations. + */ +export class CoreAnimations { + + static readonly SHOW_HIDE = trigger('coreShowHideAnimation', [ + transition(':enter', [ + style({ opacity: 0 }), + animate('500ms ease-in-out', style({ opacity: 1 })), + ]), + transition(':leave', [ + style({ opacity: 1 }), + animate('500ms ease-in-out', style({ opacity: 0 })), + ]), + ]); + + static readonly SLIDE_IN_OUT = trigger('coreSlideInOut', [ + // Enter animation. + transition('void => fromLeft', [ + style({ transform: 'translateX(0)', opacity: 1 }), + animate(300, keyframes([ + style({ opacity: 0, transform: 'translateX(-100%)', offset: 0 }), + style({ opacity: 1, transform: 'translateX(5%)', offset: 0.7 }), + style({ opacity: 1, transform: 'translateX(0)', offset: 1.0 }), + ])), + ]), + // Leave animation. + transition('fromLeft => void', [ + style({ transform: 'translateX(-100%)', opacity: 0 }), + animate(300, keyframes([ + style({ opacity: 1, transform: 'translateX(0)', offset: 0 }), + style({ opacity: 1, transform: 'translateX(5%)', offset: 0.3 }), + style({ opacity: 0, transform: 'translateX(-100%)', offset: 1.0 }), + ])), + ]), + // Enter animation. + transition('void => fromRight', [ + style({ transform: 'translateX(0)', opacity: 1 }), + animate(300, keyframes([ + style({ opacity: 0, transform: 'translateX(100%)', offset: 0 }), + style({ opacity: 1, transform: 'translateX(-5%)', offset: 0.7 }), + style({ opacity: 1, transform: 'translateX(0)', offset: 1.0 }), + ])), + ]), + // Leave animation. + transition('fromRight => void', [ + style({ transform: 'translateX(-100%)', opacity: 0 }), + animate(300, keyframes([ + style({ opacity: 1, transform: 'translateX(0)', offset: 0 }), + style({ opacity: 1, transform: 'translateX(-5%)', offset: 0.3 }), + style({ opacity: 0, transform: 'translateX(100%)', offset: 1.0 }), + ])), + ]), + ]); + +} diff --git a/src/core/components/download-refresh/download-refresh.ts b/src/core/components/download-refresh/download-refresh.ts index 820a4d93c..3aed6b4c6 100644 --- a/src/core/components/download-refresh/download-refresh.ts +++ b/src/core/components/download-refresh/download-refresh.ts @@ -14,7 +14,7 @@ import { Component, Input, Output, EventEmitter } from '@angular/core'; import { CoreConstants } from '@/core/constants'; -import { coreShowHideAnimation } from '@classes/animations'; +import { CoreAnimations } from '@components/animations'; /** * Component to show a download button with refresh option, the spinner and the status of it. @@ -26,7 +26,7 @@ import { coreShowHideAnimation } from '@classes/animations'; selector: 'core-download-refresh', templateUrl: 'core-download-refresh.html', styleUrls: ['download-refresh.scss'], - animations: [coreShowHideAnimation], + animations: [CoreAnimations.SHOW_HIDE], }) export class CoreDownloadRefreshComponent { diff --git a/src/core/components/loading/loading.ts b/src/core/components/loading/loading.ts index 51442cfbb..f40623dbf 100644 --- a/src/core/components/loading/loading.ts +++ b/src/core/components/loading/loading.ts @@ -16,7 +16,7 @@ import { Component, Input, OnInit, OnChanges, SimpleChange, ViewChild, ElementRe import { CoreEventLoadingChangedData, CoreEvents } from '@singletons/events'; import { CoreUtils } from '@services/utils/utils'; -import { coreShowHideAnimation } from '@classes/animations'; +import { CoreAnimations } from '@components/animations'; import { Translate } from '@singletons'; /** @@ -43,7 +43,7 @@ import { Translate } from '@singletons'; selector: 'core-loading', templateUrl: 'core-loading.html', styleUrls: ['loading.scss'], - animations: [coreShowHideAnimation], + animations: [CoreAnimations.SHOW_HIDE], }) export class CoreLoadingComponent implements OnInit, OnChanges, AfterViewInit { From 0f86372020d90e704f6932ccede383c0402f084c Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 10 Dec 2020 11:11:18 +0100 Subject: [PATCH 3/5] MOBILE-3320 DX: Add project snippets for vscode --- .vscode/moodle.code-snippets | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .vscode/moodle.code-snippets diff --git a/.vscode/moodle.code-snippets b/.vscode/moodle.code-snippets new file mode 100644 index 000000000..17126a06d --- /dev/null +++ b/.vscode/moodle.code-snippets @@ -0,0 +1,22 @@ +{ + "[Moodle] Service Singleton": { + "scope": "typescript", + "prefix": "massingleton", + "body": [ + + "import { Injectable } from '@angular/core';", + "import { makeSingleton } from '@singletons';", + "", + "@Injectable({ providedIn: 'root' })", + "export class ${1:${TM_FILENAME_BASE}}Service {", + "", + " $0", + "", + "}", + "", + "export class ${1:${TM_FILENAME_BASE}} extends makeSingleton(${1:${TM_FILENAME_BASE}}Service) {}", + "" + ], + "description": "[Moodle] Create a Service Singleton" + } +} From de38835518aea4193c8789da7548d9a409e8eb10 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Tue, 15 Dec 2020 09:44:17 +0100 Subject: [PATCH 4/5] MOBILE-3320 core: Implement shared module --- src/core/shared.module.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/core/shared.module.ts diff --git a/src/core/shared.module.ts b/src/core/shared.module.ts new file mode 100644 index 000000000..5f7987b34 --- /dev/null +++ b/src/core/shared.module.ts @@ -0,0 +1,27 @@ +// (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 { CoreComponentsModule } from '@components/components.module'; +import { CoreDirectivesModule } from '@directives/directives.module'; +import { CorePipesModule } from '@pipes/pipes.module'; + +@NgModule({ + imports: [ + CoreComponentsModule, + CoreDirectivesModule, + CorePipesModule, + ], +}) +export class CoreSharedModule {} From acefa8c35aea47548731aa3984a5280ba0e3cb0b Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Tue, 15 Dec 2020 09:47:15 +0100 Subject: [PATCH 5/5] MOBILE-3320 routes: Refactor module routes config --- src/addons/badges/badges.module.ts | 4 +--- src/app/app-routing.module.ts | 22 +++++++++++++++---- .../mainmenu/mainmenu-routing.module.ts | 4 ++-- .../mainmenu/mainmenu-tab-routing.module.ts | 4 ++-- .../pages/home/home-routing.module.ts | 4 ++-- src/core/features/settings/settings.module.ts | 2 +- src/core/features/user/user.module.ts | 2 +- 7 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/addons/badges/badges.module.ts b/src/addons/badges/badges.module.ts index b0b192970..42a91fa22 100644 --- a/src/addons/badges/badges.module.ts +++ b/src/addons/badges/badges.module.ts @@ -33,9 +33,7 @@ const mainMenuHomeSiblingRoutes: Routes = [ @NgModule({ imports: [ - CoreMainMenuTabRoutingModule.forChild({ - siblings: mainMenuHomeSiblingRoutes, - }), + CoreMainMenuTabRoutingModule.forChild(mainMenuHomeSiblingRoutes), ], providers: [ { diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 58f5f1650..256c558f4 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -22,13 +22,27 @@ function buildAppRoutes(injector: Injector): Routes { } export type ModuleRoutes = { children: Routes; siblings: Routes }; +export type ModuleRoutesConfig = Routes | Partial; -export function resolveModuleRoutes(injector: Injector, token: InjectionToken[]>): ModuleRoutes { - const routes = injector.get(token, []); +export function resolveModuleRoutes(injector: Injector, token: InjectionToken): ModuleRoutes { + const configs = injector.get(token, []); + const routes = configs.map(config => { + if (Array.isArray(config)) { + return { + children: [], + siblings: config, + }; + } + + return { + children: config.children || [], + siblings: config.siblings || [], + }; + }); return { - children: CoreArray.flatten(routes.map(r => r.children || [])), - siblings: CoreArray.flatten(routes.map(r => r.siblings || [])), + children: CoreArray.flatten(routes.map(r => r.children)), + siblings: CoreArray.flatten(routes.map(r => r.siblings)), }; } diff --git a/src/core/features/mainmenu/mainmenu-routing.module.ts b/src/core/features/mainmenu/mainmenu-routing.module.ts index c30b45479..5b9e25589 100644 --- a/src/core/features/mainmenu/mainmenu-routing.module.ts +++ b/src/core/features/mainmenu/mainmenu-routing.module.ts @@ -14,14 +14,14 @@ import { InjectionToken, ModuleWithProviders, NgModule } from '@angular/core'; -import { ModuleRoutes } from '@/app/app-routing.module'; +import { ModuleRoutesConfig } from '@/app/app-routing.module'; export const MAIN_MENU_ROUTES = new InjectionToken('MAIN_MENU_ROUTES'); @NgModule() export class CoreMainMenuRoutingModule { - static forChild(routes: Partial): ModuleWithProviders { + static forChild(routes: ModuleRoutesConfig): ModuleWithProviders { return { ngModule: CoreMainMenuRoutingModule, providers: [ diff --git a/src/core/features/mainmenu/mainmenu-tab-routing.module.ts b/src/core/features/mainmenu/mainmenu-tab-routing.module.ts index d673d2a34..05b1c1420 100644 --- a/src/core/features/mainmenu/mainmenu-tab-routing.module.ts +++ b/src/core/features/mainmenu/mainmenu-tab-routing.module.ts @@ -15,7 +15,7 @@ import { InjectionToken, Injector, ModuleWithProviders, NgModule } from '@angular/core'; import { Route, Routes } from '@angular/router'; -import { ModuleRoutes, resolveModuleRoutes } from '@/app/app-routing.module'; +import { ModuleRoutesConfig, resolveModuleRoutes } from '@/app/app-routing.module'; export const MAIN_MENU_TAB_ROUTES = new InjectionToken('MAIN_MENU_TAB_ROUTES'); @@ -35,7 +35,7 @@ export function buildTabMainRoutes(injector: Injector, mainRoute: Route): Routes @NgModule() export class CoreMainMenuTabRoutingModule { - static forChild(routes: Partial): ModuleWithProviders { + static forChild(routes: ModuleRoutesConfig): ModuleWithProviders { return { ngModule: CoreMainMenuTabRoutingModule, providers: [ diff --git a/src/core/features/mainmenu/pages/home/home-routing.module.ts b/src/core/features/mainmenu/pages/home/home-routing.module.ts index 73b3c4dc1..ab6a7634c 100644 --- a/src/core/features/mainmenu/pages/home/home-routing.module.ts +++ b/src/core/features/mainmenu/pages/home/home-routing.module.ts @@ -14,14 +14,14 @@ import { InjectionToken, ModuleWithProviders, NgModule } from '@angular/core'; -import { ModuleRoutes } from '@/app/app-routing.module'; +import { ModuleRoutesConfig } from '@/app/app-routing.module'; export const MAIN_MENU_HOME_ROUTES = new InjectionToken('MAIN_MENU_HOME_ROUTES'); @NgModule() export class CoreMainMenuHomeRoutingModule { - static forChild(routes: Partial): ModuleWithProviders { + static forChild(routes: ModuleRoutesConfig): ModuleWithProviders { return { ngModule: CoreMainMenuHomeRoutingModule, providers: [ diff --git a/src/core/features/settings/settings.module.ts b/src/core/features/settings/settings.module.ts index 2c7fe2a4d..c83228cc0 100644 --- a/src/core/features/settings/settings.module.ts +++ b/src/core/features/settings/settings.module.ts @@ -41,7 +41,7 @@ const mainMenuMoreRoutes: Routes = [ @NgModule({ imports: [ AppRoutingModule.forChild(appRoutes), - CoreMainMenuTabRoutingModule.forChild({ siblings: mainMenuMoreRoutes }), + CoreMainMenuTabRoutingModule.forChild(mainMenuMoreRoutes), ], providers: [ { diff --git a/src/core/features/user/user.module.ts b/src/core/features/user/user.module.ts index 669042ec1..8d0528ce9 100644 --- a/src/core/features/user/user.module.ts +++ b/src/core/features/user/user.module.ts @@ -37,7 +37,7 @@ const routes: Routes = [ @NgModule({ imports: [ - CoreMainMenuTabRoutingModule.forChild({ siblings: routes }), + CoreMainMenuTabRoutingModule.forChild(routes), CoreUserComponentsModule, ], providers: [