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

View File

@ -73,9 +73,9 @@ export class CoreSite {
protected wsProvider;
// Variables for the database.
protected WS_CACHE_STORE = 'wscache';
protected WS_CACHE_TABLE = 'wscache';
protected tableSchema = {
name: this.WS_CACHE_STORE,
name: this.WS_CACHE_TABLE,
columns: [
{
name: 'id',
@ -142,13 +142,13 @@ export class CoreSite {
* @param {Injector} injector Angular injector to prevent having to pass all the required services.
* @param {string} id Site ID.
* @param {string} siteUrl Site URL.
* @param {string} token Site's WS token.
* @param {any} info Site info.
* @param {string} [token] Site's WS token.
* @param {any} [info] Site info.
* @param {string} [privateToken] Private token.
* @param {any} [config] Site public config.
* @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) {
// Inject the required services.
let logger = injector.get(CoreLoggerProvider);
@ -627,10 +627,10 @@ export class CoreSite {
}
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) {
// 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) {
// More than one entry found. Search the one with same ID as this call.
for (let i = 0, len = entries.length; i < len; i++) {
@ -643,7 +643,7 @@ export class CoreSite {
return entries[0];
});
} 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) => {
@ -704,7 +704,7 @@ export class CoreSite {
if (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);
} else {
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 {
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);
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);
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);
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]);
}

View File

@ -24,6 +24,13 @@ export class CoreConstants {
public static downloadThreshold = 10485760; // 10MB.
public static dontShowError = 'CoreDontShowError';
public static settingsRichTextEditor = 'CoreSettingsRichTextEditor';
// WS constants.
public static wsTimeout = 30000;
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 { CoreLoggerProvider } from './logger';
import { SQLiteDB } from '../classes/sqlitedb';
/**
* Factory to provide some global functionalities, like access to the global app database.
@ -33,7 +34,7 @@ import { CoreLoggerProvider } from './logger';
@Injectable()
export class CoreAppProvider {
DBNAME = 'MoodleMobile';
db;
db: SQLiteDB;
logger;
ssoAuthenticationPromise : Promise<any>;
isKeyboardShown: boolean = false;
@ -81,9 +82,9 @@ export class CoreAppProvider {
/**
* Get the application global database.
*
* @return {any} App's DB.
* @return {SQLiteDB} App's DB.
*/
getDB() : any {
getDB() : SQLiteDB {
if (typeof this.db == 'undefined') {
this.db = this.dbProvider.getDB(this.DBNAME);
}

View File

@ -28,8 +28,8 @@ export class CoreSitesFactoryProvider {
*
* @param {string} id Site ID.
* @param {string} siteUrl Site URL.
* @param {string} token Site's WS token.
* @param {any} info Site info.
* @param {string} [token] Site's WS token.
* @param {any} [info] Site info.
* @param {string} [privateToken] Private token.
* @param {any} [config] Site public config.
* @param {boolean} [loggedOut] Whether user is logged out.
@ -37,7 +37,7 @@ export class CoreSitesFactoryProvider {
* @description
* 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 {
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) {
let siteId = siteIds[i];
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) {
enabledSites.push(siteId);
}
@ -953,7 +953,15 @@ export class CoreUtilsProvider {
*/
observableToPromise(obs: Observable<any>) : Promise<any> {
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);
});
});
}