Skip to content

Commit 0c1b244

Browse files
committed
MOBILE-4080 core: Improve devtools settings
1 parent d121fa2 commit 0c1b244

File tree

6 files changed

+88
-5
lines changed

6 files changed

+88
-5
lines changed

src/core/constants.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import envJson from '@/assets/env.json';
1616
import { EnvironmentConfig } from '@/types/config';
17+
import { CoreBrowser } from '@singletons/browser';
1718

1819
/**
1920
* Context levels enumeration.
@@ -154,7 +155,8 @@ export class CoreConstants {
154155
// @todo [4.0] This is not the proper way to check for development tools, we should rely only on the BUILD variable.
155156
return this.BUILD.isDevelopment
156157
|| this.BUILD.isTesting
157-
|| this.CONFIG.versionname.includes('-dev');
158+
|| this.CONFIG.versionname.includes('-dev')
159+
|| CoreBrowser.hasDevelopmentSetting('DevTools');
158160
}
159161

160162
}

src/core/initializers/prepare-devtools.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@ import { CoreApp, CoreAppProvider } from '@services/app';
1616
import { CoreConfig, CoreConfigProvider } from '@services/config';
1717
import { CoreDB, CoreDbProvider } from '@services/db';
1818
import { CoreCustomURLSchemes, CoreCustomURLSchemesProvider } from '@services/urlschemes';
19+
import { CoreBrowser } from '@singletons/browser';
1920
import { CoreConstants } from '../constants';
2021

2122
type DevelopmentWindow = Window & {
23+
browser?: typeof CoreBrowser;
2224
appProvider?: CoreAppProvider;
2325
configProvider?: CoreConfigProvider;
2426
dbProvider?: CoreDbProvider;
2527
urlSchemes?: CoreCustomURLSchemesProvider;
2628
};
2729

2830
function initializeDevelopmentWindow(window: DevelopmentWindow) {
31+
window.browser = CoreBrowser;
2932
window.appProvider = CoreApp.instance;
3033
window.configProvider = CoreConfig.instance;
3134
window.dbProvider = CoreDB.instance;

src/core/services/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,11 @@ export class CoreConfigProvider {
201201
* Load development config overrides.
202202
*/
203203
protected loadDevelopmentConfig(): void {
204-
if (!CoreConstants.enableDevTools() || !CoreBrowser.hasCookie('MoodleAppConfig')) {
204+
if (!CoreConstants.enableDevTools() || !CoreBrowser.hasDevelopmentSetting('Config')) {
205205
return;
206206
}
207207

208-
this.patchEnvironment(JSON.parse(CoreBrowser.getCookie('MoodleAppConfig') ?? '{}'), { patchDefault: true });
208+
this.patchEnvironment(JSON.parse(CoreBrowser.getDevelopmentSetting('Config') ?? '{}'), { patchDefault: true });
209209
}
210210

211211
/**

src/core/services/db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class CoreDbProvider {
3636
* @returns Whether queries should be logged.
3737
*/
3838
loggingEnabled(): boolean {
39-
return CoreBrowser.hasCookie('MoodleAppDBLoggingEnabled') || CoreAppProvider.isAutomated();
39+
return CoreBrowser.hasDevelopmentSetting('DBLoggingEnabled') || CoreAppProvider.isAutomated();
4040
}
4141

4242
/**

src/core/singletons/browser.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,28 @@ export class CoreBrowser {
2727
return new RegExp(`(\\s|;|^)${name}=`).test(document.cookie ?? '');
2828
}
2929

30+
/**
31+
* Check whether a development setting is set.
32+
*
33+
* @param name Setting name.
34+
* @returns Whether the development setting is set.
35+
*/
36+
static hasDevelopmentSetting(name: string): boolean {
37+
const setting = this.getDevelopmentSettingKey(name);
38+
39+
return this.hasCookie(setting) || this.hasLocalStorage(setting);
40+
}
41+
42+
/**
43+
* Check whether the given localStorage key is set.
44+
*
45+
* @param key localStorage key.
46+
* @returns Whether the key is set.
47+
*/
48+
static hasLocalStorage(key: string): boolean {
49+
return localStorage.getItem(key) !== null;
50+
}
51+
3052
/**
3153
* Read a cookie.
3254
*
@@ -45,4 +67,60 @@ export class CoreBrowser {
4567
return cookies[name] ?? null;
4668
}
4769

70+
/**
71+
* Read a localStorage key.
72+
*
73+
* @param key localStorage key.
74+
* @return localStorage value.
75+
*/
76+
static getLocalStorage(key: string): string | null {
77+
return localStorage.getItem(key);
78+
}
79+
80+
/**
81+
* Get development setting value.
82+
*
83+
* @param name Setting name.
84+
* @returns Development setting value.
85+
*/
86+
static getDevelopmentSetting(name: string): string | null {
87+
const setting = this.getDevelopmentSettingKey(name);
88+
89+
return this.getCookie(setting) ?? this.getLocalStorage(setting);
90+
}
91+
92+
/**
93+
* Set development setting.
94+
*
95+
* @param name Setting name.
96+
* @param value Setting value.
97+
*/
98+
static setDevelopmentSetting(name: string, value: string): void {
99+
const setting = this.getDevelopmentSettingKey(name);
100+
101+
document.cookie = `${setting}=${value};path=/`;
102+
localStorage.setItem(setting, value);
103+
}
104+
105+
/**
106+
* Unset development setting.
107+
*
108+
* @param name Setting name.
109+
*/
110+
static clearDevelopmentSetting(name: string): void {
111+
const setting = this.getDevelopmentSettingKey(name);
112+
113+
document.cookie = `${setting}=;path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT`;
114+
localStorage.removeItem(setting);
115+
}
116+
117+
/**
118+
* Get development setting key.
119+
*
120+
* @param name Development setting name.
121+
*/
122+
protected static getDevelopmentSettingKey(name: string): string {
123+
return `MoodleApp${name}`;
124+
}
125+
48126
}

src/core/singletons/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class CoreLogger {
6969
static getInstance(className: string): CoreLogger {
7070
// Disable log on production and testing.
7171
if (
72-
!CoreBrowser.hasCookie('MoodleAppLoggingEnabled') &&
72+
!CoreBrowser.hasDevelopmentSetting('LoggingEnabled') &&
7373
(CoreConstants.BUILD.isProduction || CoreConstants.BUILD.isTesting)
7474
) {
7575
if (CoreConstants.BUILD.isProduction) {

0 commit comments

Comments
 (0)