2022-06-10 22:38:50 +01:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-06-10 22:38:50 +01:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-06-10 22:38:50 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import SettingsHandler from "./SettingsHandler";
|
|
|
|
|
import PlatformPeg from "../../PlatformPeg";
|
2022-06-30 15:48:21 +01:00
|
|
|
import { SETTINGS } from "../Settings";
|
|
|
|
|
import { SettingLevel } from "../SettingLevel";
|
|
|
|
|
import defaultDispatcher from "../../dispatcher/dispatcher";
|
|
|
|
|
import { ActionPayload } from "../../dispatcher/payloads";
|
|
|
|
|
import { Action } from "../../dispatcher/actions";
|
2022-06-10 22:38:50 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets and sets settings at the "platform" level for the current device.
|
|
|
|
|
* This handler does not make use of the roomId parameter.
|
|
|
|
|
*/
|
|
|
|
|
export default class PlatformSettingsHandler extends SettingsHandler {
|
2022-06-30 15:48:21 +01:00
|
|
|
private store: { [settingName: string]: any } = {};
|
|
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public constructor() {
|
2022-06-30 15:48:21 +01:00
|
|
|
super();
|
|
|
|
|
|
|
|
|
|
defaultDispatcher.register(this.onAction);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private onAction = (payload: ActionPayload): void => {
|
2022-06-30 15:48:21 +01:00
|
|
|
if (payload.action === Action.PlatformSet) {
|
|
|
|
|
this.store = {};
|
|
|
|
|
// Load setting values as they are async and `getValue` must be synchronous
|
|
|
|
|
Object.entries(SETTINGS).forEach(([key, setting]) => {
|
2023-02-15 13:36:22 +00:00
|
|
|
if (setting.supportedLevels?.includes(SettingLevel.PLATFORM) && payload.platform.supportsSetting(key)) {
|
2023-02-13 11:39:16 +00:00
|
|
|
payload.platform.getSettingValue(key).then((value: any) => {
|
2022-06-30 15:48:21 +01:00
|
|
|
this.store[key] = value;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-10 22:38:50 +01:00
|
|
|
public canSetValue(settingName: string, roomId: string): boolean {
|
2023-02-15 13:36:22 +00:00
|
|
|
return PlatformPeg.get()?.supportsSetting(settingName) ?? false;
|
2022-06-10 22:38:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getValue(settingName: string, roomId: string): any {
|
2022-06-30 15:48:21 +01:00
|
|
|
return this.store[settingName];
|
2022-06-10 22:38:50 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-15 13:36:22 +00:00
|
|
|
public async setValue(settingName: string, roomId: string, newValue: any): Promise<void> {
|
2022-06-30 15:48:21 +01:00
|
|
|
this.store[settingName] = newValue; // keep cache up to date for synchronous access
|
2023-02-15 13:36:22 +00:00
|
|
|
await PlatformPeg.get()?.setSettingValue(settingName, newValue);
|
2022-06-10 22:38:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public isSupported(): boolean {
|
2023-02-15 13:36:22 +00:00
|
|
|
return PlatformPeg.get()?.supportsSetting() ?? false;
|
2022-06-10 22:38:50 +01:00
|
|
|
}
|
|
|
|
|
}
|