MOBILE-3320 sites: Create sites factory

main
Noel De Martin 2021-05-05 17:49:21 +02:00
parent 9ad4fe5d7e
commit d9b55440df
3 changed files with 67 additions and 5 deletions

View File

@ -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,

View File

@ -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);

View File

@ -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<CoreSiteSchema[]>('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 ? <CoreSiteInfo> CoreTextUtils.parseJSON(entry.info) : undefined;
const config = entry.config ? <CoreSiteConfig> 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<CoreSitePublicConfigResponse> {
const temporarySite = new CoreSite(undefined, siteUrl);
const temporarySite = CoreSitesFactory.makeSite(undefined, siteUrl);
return temporarySite.getPublicConfig();
}