MOBILE-2261 sites: Implement sites provider

main
Dani Palou 2017-11-14 08:58:20 +01:00
parent c8935be6fe
commit fe6a24a0f2
7 changed files with 1074 additions and 22 deletions

View File

@ -1,3 +1,17 @@
// (C) Copyright 2015 Martin Dougiamas
//
// 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 { BrowserModule } from '@angular/platform-browser'; import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core'; import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule, Platform } from 'ionic-angular'; import { IonicApp, IonicErrorHandler, IonicModule, Platform } from 'ionic-angular';
@ -28,6 +42,7 @@ import { CoreFileProvider } from '../providers/file';
import { CoreWSProvider } from '../providers/ws'; import { CoreWSProvider } from '../providers/ws';
import { CoreEventsProvider } from '../providers/events'; import { CoreEventsProvider } from '../providers/events';
import { CoreSitesFactoryProvider } from '../providers/sites-factory'; import { CoreSitesFactoryProvider } from '../providers/sites-factory';
import { CoreSitesProvider } from '../providers/sites';
// For translate loader. AoT requires an exported function for factories. // For translate loader. AoT requires an exported function for factories.
export function createTranslateLoader(http: HttpClient) { export function createTranslateLoader(http: HttpClient) {
@ -77,6 +92,7 @@ export function createTranslateLoader(http: HttpClient) {
CoreWSProvider, CoreWSProvider,
CoreEventsProvider, CoreEventsProvider,
CoreSitesFactoryProvider, CoreSitesFactoryProvider,
CoreSitesProvider,
] ]
}) })
export class AppModule { export class AppModule {

View File

@ -73,9 +73,9 @@ export class CoreSite {
protected wsProvider; protected wsProvider;
// Variables for the database. // Variables for the database.
protected WS_CACHE_STORE = 'wscache'; protected WS_CACHE_TABLE = 'wscache';
protected tableSchema = { protected tableSchema = {
name: this.WS_CACHE_STORE, name: this.WS_CACHE_TABLE,
columns: [ columns: [
{ {
name: 'id', name: 'id',
@ -142,13 +142,13 @@ export class CoreSite {
* @param {Injector} injector Angular injector to prevent having to pass all the required services. * @param {Injector} injector Angular injector to prevent having to pass all the required services.
* @param {string} id Site ID. * @param {string} id Site ID.
* @param {string} siteUrl Site URL. * @param {string} siteUrl Site URL.
* @param {string} token Site's WS token. * @param {string} [token] Site's WS token.
* @param {any} info Site info. * @param {any} [info] Site info.
* @param {string} [privateToken] Private token. * @param {string} [privateToken] Private token.
* @param {any} [config] Site public config. * @param {any} [config] Site public config.
* @param {boolean} [loggedOut] Whether user is logged out. * @param {boolean} [loggedOut] Whether user is logged out.
*/ */
constructor(injector: Injector, public id: string, public siteUrl: string, public token: string, public infos: any, constructor(injector: Injector, public id: string, public siteUrl: string, public token?: string, public infos?: any,
public privateToken?: string, public config?: any, public loggedOut?: boolean) { public privateToken?: string, public config?: any, public loggedOut?: boolean) {
// Inject the required services. // Inject the required services.
let logger = injector.get(CoreLoggerProvider); let logger = injector.get(CoreLoggerProvider);
@ -627,10 +627,10 @@ export class CoreSite {
} }
if (preSets.getCacheUsingCacheKey || (emergency && preSets.getEmergencyCacheUsingCacheKey)) { if (preSets.getCacheUsingCacheKey || (emergency && preSets.getEmergencyCacheUsingCacheKey)) {
promise = this.db.getRecords(this.WS_CACHE_STORE, {key: preSets.cacheKey}).then((entries) => { promise = this.db.getRecords(this.WS_CACHE_TABLE, {key: preSets.cacheKey}).then((entries) => {
if (!entries.length) { if (!entries.length) {
// Cache key not found, get by params sent. // Cache key not found, get by params sent.
return this.db.getRecord(this.WS_CACHE_STORE, {id: id}); return this.db.getRecord(this.WS_CACHE_TABLE, {id: id});
} else if (entries.length > 1) { } else if (entries.length > 1) {
// More than one entry found. Search the one with same ID as this call. // More than one entry found. Search the one with same ID as this call.
for (let i = 0, len = entries.length; i < len; i++) { for (let i = 0, len = entries.length; i < len; i++) {
@ -643,7 +643,7 @@ export class CoreSite {
return entries[0]; return entries[0];
}); });
} else { } else {
promise = this.db.getRecord(this.WS_CACHE_STORE, {id: id}); promise = this.db.getRecord(this.WS_CACHE_TABLE, {id: id});
} }
return promise.then((entry) => { return promise.then((entry) => {
@ -704,7 +704,7 @@ export class CoreSite {
if (preSets.cacheKey) { if (preSets.cacheKey) {
entry.key = preSets.cacheKey; entry.key = preSets.cacheKey;
} }
return this.db.insertOrUpdateRecord(this.WS_CACHE_STORE, entry, {id: id}); return this.db.insertOrUpdateRecord(this.WS_CACHE_TABLE, entry, {id: id});
}); });
} }
} }
@ -725,9 +725,9 @@ export class CoreSite {
return Promise.reject(null); return Promise.reject(null);
} else { } else {
if (allCacheKey) { if (allCacheKey) {
return this.db.deleteRecords(this.WS_CACHE_STORE, {key: preSets.cacheKey}); return this.db.deleteRecords(this.WS_CACHE_TABLE, {key: preSets.cacheKey});
} else { } else {
return this.db.deleteRecords(this.WS_CACHE_STORE, {id: id}); return this.db.deleteRecords(this.WS_CACHE_TABLE, {id: id});
} }
} }
} }
@ -761,7 +761,7 @@ export class CoreSite {
} }
this.logger.debug('Invalidate all the cache for site: ' + this.id); this.logger.debug('Invalidate all the cache for site: ' + this.id);
return this.db.updateRecords(this.WS_CACHE_STORE, {expirationTime: 0}); return this.db.updateRecords(this.WS_CACHE_TABLE, {expirationTime: 0});
} }
/** /**
@ -779,7 +779,7 @@ export class CoreSite {
} }
this.logger.debug('Invalidate cache for key: ' + key); this.logger.debug('Invalidate cache for key: ' + key);
return this.db.updateRecords(this.WS_CACHE_STORE, {expirationTime: 0}, {key: key}); return this.db.updateRecords(this.WS_CACHE_TABLE, {expirationTime: 0}, {key: key});
} }
/** /**
@ -821,7 +821,7 @@ export class CoreSite {
} }
this.logger.debug('Invalidate cache for key starting with: ' + key); this.logger.debug('Invalidate cache for key starting with: ' + key);
let sql = 'UPDATE ' + this.WS_CACHE_STORE + ' SET expirationTime=0 WHERE key LIKE ?%'; let sql = 'UPDATE ' + this.WS_CACHE_TABLE + ' SET expirationTime=0 WHERE key LIKE ?%';
return this.db.execute(sql, [key]); return this.db.execute(sql, [key]);
} }

View File

@ -24,6 +24,13 @@ export class CoreConstants {
public static downloadThreshold = 10485760; // 10MB. public static downloadThreshold = 10485760; // 10MB.
public static dontShowError = 'CoreDontShowError'; public static dontShowError = 'CoreDontShowError';
public static settingsRichTextEditor = 'CoreSettingsRichTextEditor'; public static settingsRichTextEditor = 'CoreSettingsRichTextEditor';
// WS constants.
public static wsTimeout = 30000; public static wsTimeout = 30000;
public static wsPrefix = 'local_mobile_'; public static wsPrefix = 'local_mobile_';
// Login constants.
public static loginSSOCode = 2; // SSO in browser window is required.
public static loginSSOInAppCode = 3; // SSO in embedded browser is required.
public static loginLaunchData = 'mmLoginLaunchData';
} }

View File

@ -19,6 +19,7 @@ import { Network } from '@ionic-native/network';
import { CoreDbProvider } from './db'; import { CoreDbProvider } from './db';
import { CoreLoggerProvider } from './logger'; import { CoreLoggerProvider } from './logger';
import { SQLiteDB } from '../classes/sqlitedb';
/** /**
* Factory to provide some global functionalities, like access to the global app database. * Factory to provide some global functionalities, like access to the global app database.
@ -33,7 +34,7 @@ import { CoreLoggerProvider } from './logger';
@Injectable() @Injectable()
export class CoreAppProvider { export class CoreAppProvider {
DBNAME = 'MoodleMobile'; DBNAME = 'MoodleMobile';
db; db: SQLiteDB;
logger; logger;
ssoAuthenticationPromise : Promise<any>; ssoAuthenticationPromise : Promise<any>;
isKeyboardShown: boolean = false; isKeyboardShown: boolean = false;
@ -81,9 +82,9 @@ export class CoreAppProvider {
/** /**
* Get the application global database. * Get the application global database.
* *
* @return {any} App's DB. * @return {SQLiteDB} App's DB.
*/ */
getDB() : any { getDB() : SQLiteDB {
if (typeof this.db == 'undefined') { if (typeof this.db == 'undefined') {
this.db = this.dbProvider.getDB(this.DBNAME); this.db = this.dbProvider.getDB(this.DBNAME);
} }

View File

@ -28,8 +28,8 @@ export class CoreSitesFactoryProvider {
* *
* @param {string} id Site ID. * @param {string} id Site ID.
* @param {string} siteUrl Site URL. * @param {string} siteUrl Site URL.
* @param {string} token Site's WS token. * @param {string} [token] Site's WS token.
* @param {any} info Site info. * @param {any} [info] Site info.
* @param {string} [privateToken] Private token. * @param {string} [privateToken] Private token.
* @param {any} [config] Site public config. * @param {any} [config] Site public config.
* @param {boolean} [loggedOut] Whether user is logged out. * @param {boolean} [loggedOut] Whether user is logged out.
@ -37,7 +37,7 @@ export class CoreSitesFactoryProvider {
* @description * @description
* This returns a site object. * This returns a site object.
*/ */
makeSite = function(id: string, siteUrl: string, token: string, info: any, privateToken?: string, makeSite(id: string, siteUrl: string, token?: string, info?: any, privateToken?: string,
config?: any, loggedOut?: boolean) : CoreSite { config?: any, loggedOut?: boolean) : CoreSite {
return new CoreSite(this.injector, id, siteUrl, token, info, privateToken, config, loggedOut); return new CoreSite(this.injector, id, siteUrl, token, info, privateToken, config, loggedOut);
} }

File diff suppressed because it is too large Load Diff

View File

@ -414,7 +414,7 @@ export class CoreUtilsProvider {
for (let i in siteIds) { for (let i in siteIds) {
let siteId = siteIds[i]; let siteId = siteIds[i];
if (checkAll || !promises.length) { if (checkAll || !promises.length) {
promises.push(Promise.resolve(isEnabledFn.apply(isEnabledFn, [siteId].concat(...args))).then((enabled) => { promises.push(Promise.resolve(isEnabledFn.apply(isEnabledFn, [siteId].concat(args))).then((enabled) => {
if (enabled) { if (enabled) {
enabledSites.push(siteId); enabledSites.push(siteId);
} }
@ -953,7 +953,15 @@ export class CoreUtilsProvider {
*/ */
observableToPromise(obs: Observable<any>) : Promise<any> { observableToPromise(obs: Observable<any>) : Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
obs.subscribe(resolve, reject); let subscription = obs.subscribe((data) => {
// Data received, unsubscribe.
subscription.unsubscribe();
resolve(data);
}, (error) => {
// Data received, unsubscribe.
subscription.unsubscribe();
reject(error);
});
}); });
} }