Merge pull request #3286 from NoelDeMartin/MOBILE-4080

MOBILE-4080: Improve devtools settings
main
Dani Palou 2022-05-19 11:47:03 +02:00 committed by GitHub
commit ba3ce03f62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 91 additions and 5 deletions

View File

@ -14,6 +14,7 @@
import envJson from '@/assets/env.json';
import { EnvironmentConfig } from '@/types/config';
import { CoreBrowser } from '@singletons/browser';
/**
* Context levels enumeration.
@ -154,7 +155,8 @@ export class CoreConstants {
// @todo [4.0] This is not the proper way to check for development tools, we should rely only on the BUILD variable.
return this.BUILD.isDevelopment
|| this.BUILD.isTesting
|| this.CONFIG.versionname.includes('-dev');
|| this.CONFIG.versionname.includes('-dev')
|| CoreBrowser.hasDevelopmentSetting('DevTools');
}
}

View File

@ -15,6 +15,7 @@
import { ApplicationRef, NgZone as NgZoneService } from '@angular/core';
import { CorePushNotifications, CorePushNotificationsProvider } from '@features/pushnotifications/services/pushnotifications';
import { CoreApp, CoreAppProvider } from '@services/app';
import { CoreConfig, CoreConfigProvider } from '@services/config';
import { CoreCronDelegate, CoreCronDelegateService } from '@services/cron';
import { CoreDB, CoreDbProvider } from '@services/db';
import { CoreCustomURLSchemes, CoreCustomURLSchemesProvider } from '@services/urlschemes';
@ -24,6 +25,7 @@ type AutomatedTestsWindow = Window & {
appRef?: ApplicationRef;
appProvider?: CoreAppProvider;
dbProvider?: CoreDbProvider;
configProvider?: CoreConfigProvider;
cronProvider?: CoreCronDelegateService;
ngZone?: NgZoneService;
pushNotifications?: CorePushNotificationsProvider;
@ -34,6 +36,7 @@ function initializeAutomatedTestsWindow(window: AutomatedTestsWindow) {
window.appRef = Application.instance;
window.appProvider = CoreApp.instance;
window.dbProvider = CoreDB.instance;
window.configProvider = CoreConfig.instance;
window.cronProvider = CoreCronDelegate.instance;
window.ngZone = NgZone.instance;
window.pushNotifications = CorePushNotifications.instance;

View File

@ -16,9 +16,11 @@ import { CoreApp, CoreAppProvider } from '@services/app';
import { CoreConfig, CoreConfigProvider } from '@services/config';
import { CoreDB, CoreDbProvider } from '@services/db';
import { CoreCustomURLSchemes, CoreCustomURLSchemesProvider } from '@services/urlschemes';
import { CoreBrowser } from '@singletons/browser';
import { CoreConstants } from '../constants';
type DevelopmentWindow = Window & {
browser?: typeof CoreBrowser;
appProvider?: CoreAppProvider;
configProvider?: CoreConfigProvider;
dbProvider?: CoreDbProvider;
@ -26,6 +28,7 @@ type DevelopmentWindow = Window & {
};
function initializeDevelopmentWindow(window: DevelopmentWindow) {
window.browser = CoreBrowser;
window.appProvider = CoreApp.instance;
window.configProvider = CoreConfig.instance;
window.dbProvider = CoreDB.instance;

View File

@ -201,11 +201,11 @@ export class CoreConfigProvider {
* Load development config overrides.
*/
protected loadDevelopmentConfig(): void {
if (!CoreConstants.enableDevTools() || !CoreBrowser.hasCookie('MoodleAppConfig')) {
if (!CoreConstants.enableDevTools() || !CoreBrowser.hasDevelopmentSetting('Config')) {
return;
}
this.patchEnvironment(JSON.parse(CoreBrowser.getCookie('MoodleAppConfig') ?? '{}'), { patchDefault: true });
this.patchEnvironment(JSON.parse(CoreBrowser.getDevelopmentSetting('Config') ?? '{}'), { patchDefault: true });
}
/**

View File

@ -36,7 +36,7 @@ export class CoreDbProvider {
* @returns Whether queries should be logged.
*/
loggingEnabled(): boolean {
return CoreBrowser.hasCookie('MoodleAppDBLoggingEnabled') || CoreAppProvider.isAutomated();
return CoreBrowser.hasDevelopmentSetting('DBLoggingEnabled') || CoreAppProvider.isAutomated();
}
/**

View File

@ -27,6 +27,28 @@ export class CoreBrowser {
return new RegExp(`(\\s|;|^)${name}=`).test(document.cookie ?? '');
}
/**
* Check whether a development setting is set.
*
* @param name Setting name.
* @returns Whether the development setting is set.
*/
static hasDevelopmentSetting(name: string): boolean {
const setting = this.getDevelopmentSettingKey(name);
return this.hasCookie(setting) || this.hasLocalStorage(setting);
}
/**
* Check whether the given localStorage key is set.
*
* @param key localStorage key.
* @returns Whether the key is set.
*/
static hasLocalStorage(key: string): boolean {
return localStorage.getItem(key) !== null;
}
/**
* Read a cookie.
*
@ -45,4 +67,60 @@ export class CoreBrowser {
return cookies[name] ?? null;
}
/**
* Read a localStorage key.
*
* @param key localStorage key.
* @return localStorage value.
*/
static getLocalStorage(key: string): string | null {
return localStorage.getItem(key);
}
/**
* Get development setting value.
*
* @param name Setting name.
* @returns Development setting value.
*/
static getDevelopmentSetting(name: string): string | null {
const setting = this.getDevelopmentSettingKey(name);
return this.getCookie(setting) ?? this.getLocalStorage(setting);
}
/**
* Set development setting.
*
* @param name Setting name.
* @param value Setting value.
*/
static setDevelopmentSetting(name: string, value: string): void {
const setting = this.getDevelopmentSettingKey(name);
document.cookie = `${setting}=${value};path=/`;
localStorage.setItem(setting, value);
}
/**
* Unset development setting.
*
* @param name Setting name.
*/
static clearDevelopmentSetting(name: string): void {
const setting = this.getDevelopmentSettingKey(name);
document.cookie = `${setting}=;path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT`;
localStorage.removeItem(setting);
}
/**
* Get development setting key.
*
* @param name Development setting name.
*/
protected static getDevelopmentSettingKey(name: string): string {
return `MoodleApp${name}`;
}
}

View File

@ -69,7 +69,7 @@ export class CoreLogger {
static getInstance(className: string): CoreLogger {
// Disable log on production and testing.
if (
!CoreBrowser.hasCookie('MoodleAppLoggingEnabled') &&
!CoreBrowser.hasDevelopmentSetting('LoggingEnabled') &&
(CoreConstants.BUILD.isProduction || CoreConstants.BUILD.isTesting)
) {
if (CoreConstants.BUILD.isProduction) {