From d9b55440dfda680d4ecc45e05c12e38a8699f4cb Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Wed, 5 May 2021 17:49:21 +0200 Subject: [PATCH] MOBILE-3320 sites: Create sites factory --- .../services/pushnotifications.ts | 3 +- src/core/services/sites-factory.ts | 52 +++++++++++++++++++ src/core/services/sites.ts | 17 ++++-- 3 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 src/core/services/sites-factory.ts diff --git a/src/core/features/pushnotifications/services/pushnotifications.ts b/src/core/features/pushnotifications/services/pushnotifications.ts index 40090c253..5460402fa 100644 --- a/src/core/features/pushnotifications/services/pushnotifications.ts +++ b/src/core/features/pushnotifications/services/pushnotifications.ts @@ -40,6 +40,7 @@ import { } from './database/pushnotifications'; import { CoreError } from '@classes/errors/error'; import { CoreWSExternalWarning } from '@services/ws'; +import { CoreSitesFactory } from '@services/sites-factory'; /** * Service to handle push notifications. @@ -751,7 +752,7 @@ export class CorePushNotificationsProvider { await Promise.all(results.map(async (result) => { // Create a temporary site to unregister. - const tmpSite = new CoreSite( + const tmpSite = CoreSitesFactory.makeSite( result.siteid, result.siteurl, result.token, diff --git a/src/core/services/sites-factory.ts b/src/core/services/sites-factory.ts new file mode 100644 index 000000000..395494ea8 --- /dev/null +++ b/src/core/services/sites-factory.ts @@ -0,0 +1,52 @@ +// (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 { Injectable } from '@angular/core'; + +import { CoreSite, CoreSiteConfig, CoreSiteInfo } from '@classes/site'; +import { makeSingleton } from '@singletons'; + +/* + * Provider to create sites instances. +*/ +@Injectable({ providedIn: 'root' }) +export class CoreSitesFactoryService { + + /** + * Make a site object. + * + * @param id Site ID. + * @param siteUrl Site URL. + * @param token Site's WS token. + * @param info Site info. + * @param privateToken Private token. + * @param config Site public config. + * @param loggedOut Whether user is logged out. + * @return Site instance. + */ + makeSite( + id: string | undefined, + siteUrl: string, + token?: string, + info?: CoreSiteInfo, + privateToken?: string, + config?: CoreSiteConfig, + loggedOut?: boolean, + ): CoreSite { + return new CoreSite(id, siteUrl, token, info, privateToken, config, loggedOut); + } + +} + +export const CoreSitesFactory = makeSingleton(CoreSitesFactoryService); diff --git a/src/core/services/sites.ts b/src/core/services/sites.ts index f2385dd70..b85663783 100644 --- a/src/core/services/sites.ts +++ b/src/core/services/sites.ts @@ -51,6 +51,7 @@ import { import { CoreArray } from '../singletons/array'; import { CoreNetworkError } from '@classes/errors/network-error'; import { CoreNavigationOptions } from './navigator'; +import { CoreSitesFactory } from './sites-factory'; export const CORE_SITE_SCHEMAS = new InjectionToken('CORE_SITE_SCHEMAS'); @@ -220,7 +221,7 @@ export class CoreSitesProvider { } // Site exists. Create a temporary site to check if local_mobile is installed. - const temporarySite = new CoreSite(undefined, siteUrl); + const temporarySite = CoreSitesFactory.makeSite(undefined, siteUrl); let data: LocalMobileResponse; try { @@ -438,7 +439,7 @@ export class CoreSitesProvider { } // Create a "candidate" site to fetch the site info. - let candidateSite = new CoreSite(undefined, siteUrl, token, undefined, privateToken, undefined, undefined); + let candidateSite = CoreSitesFactory.makeSite(undefined, siteUrl, token, undefined, privateToken, undefined, undefined); let isNewSite = true; try { @@ -1004,7 +1005,15 @@ export class CoreSitesProvider { const info = entry.info ? CoreTextUtils.parseJSON(entry.info) : undefined; const config = entry.config ? CoreTextUtils.parseJSON(entry.config) : undefined; - const site = new CoreSite(entry.id, entry.siteUrl, entry.token, info, entry.privateToken, config, entry.loggedOut == 1); + const site = CoreSitesFactory.makeSite( + entry.id, + entry.siteUrl, + entry.token, + info, + entry.privateToken, + config, + entry.loggedOut == 1, + ); site.setOAuthId(entry.oauthId || undefined); return this.migrateSiteSchemas(site).then(() => { @@ -1430,7 +1439,7 @@ export class CoreSitesProvider { * @return Promise resolved with the public config. */ getSitePublicConfig(siteUrl: string): Promise { - const temporarySite = new CoreSite(undefined, siteUrl); + const temporarySite = CoreSitesFactory.makeSite(undefined, siteUrl); return temporarySite.getPublicConfig(); }