Convert DeviceSettingsHandler to TS
This commit is contained in:
parent
8097810784
commit
4f6480c037
1 changed files with 27 additions and 28 deletions
|
@ -19,6 +19,7 @@ limitations under the License.
|
||||||
import SettingsHandler from "./SettingsHandler";
|
import SettingsHandler from "./SettingsHandler";
|
||||||
import {MatrixClientPeg} from "../../MatrixClientPeg";
|
import {MatrixClientPeg} from "../../MatrixClientPeg";
|
||||||
import {SettingLevel} from "../SettingLevel";
|
import {SettingLevel} from "../SettingLevel";
|
||||||
|
import { CallbackFn, WatchManager } from "../WatchManager";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets and sets settings at the "device" level for the current device.
|
* Gets and sets settings at the "device" level for the current device.
|
||||||
|
@ -29,17 +30,15 @@ export default class DeviceSettingsHandler extends SettingsHandler {
|
||||||
/**
|
/**
|
||||||
* Creates a new device settings handler
|
* Creates a new device settings handler
|
||||||
* @param {string[]} featureNames The names of known features.
|
* @param {string[]} featureNames The names of known features.
|
||||||
* @param {WatchManager} watchManager The watch manager to notify updates to
|
* @param {WatchManager} watchers The watch manager to notify updates to
|
||||||
*/
|
*/
|
||||||
constructor(featureNames, watchManager) {
|
constructor(private featureNames: string[], private watchers: WatchManager) {
|
||||||
super();
|
super();
|
||||||
this._featureNames = featureNames;
|
|
||||||
this._watchers = watchManager;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getValue(settingName, roomId) {
|
public getValue(settingName: string, roomId: string): any {
|
||||||
if (this._featureNames.includes(settingName)) {
|
if (this.featureNames.includes(settingName)) {
|
||||||
return this._readFeature(settingName);
|
return this.readFeature(settingName);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Special case notifications
|
// Special case notifications
|
||||||
|
@ -68,28 +67,28 @@ export default class DeviceSettingsHandler extends SettingsHandler {
|
||||||
return val['value'];
|
return val['value'];
|
||||||
}
|
}
|
||||||
|
|
||||||
const settings = this._getSettings() || {};
|
const settings = this.getSettings() || {};
|
||||||
return settings[settingName];
|
return settings[settingName];
|
||||||
}
|
}
|
||||||
|
|
||||||
setValue(settingName, roomId, newValue) {
|
public setValue(settingName: string, roomId: string, newValue: any): Promise<void> {
|
||||||
if (this._featureNames.includes(settingName)) {
|
if (this.featureNames.includes(settingName)) {
|
||||||
this._writeFeature(settingName, newValue);
|
this.writeFeature(settingName, newValue);
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Special case notifications
|
// Special case notifications
|
||||||
if (settingName === "notificationsEnabled") {
|
if (settingName === "notificationsEnabled") {
|
||||||
localStorage.setItem("notifications_enabled", newValue);
|
localStorage.setItem("notifications_enabled", newValue);
|
||||||
this._watchers.notifyUpdate(settingName, null, SettingLevel.DEVICE, newValue);
|
this.watchers.notifyUpdate(settingName, null, SettingLevel.DEVICE, newValue);
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
} else if (settingName === "notificationBodyEnabled") {
|
} else if (settingName === "notificationBodyEnabled") {
|
||||||
localStorage.setItem("notifications_body_enabled", newValue);
|
localStorage.setItem("notifications_body_enabled", newValue);
|
||||||
this._watchers.notifyUpdate(settingName, null, SettingLevel.DEVICE, newValue);
|
this.watchers.notifyUpdate(settingName, null, SettingLevel.DEVICE, newValue);
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
} else if (settingName === "audioNotificationsEnabled") {
|
} else if (settingName === "audioNotificationsEnabled") {
|
||||||
localStorage.setItem("audio_notifications_enabled", newValue);
|
localStorage.setItem("audio_notifications_enabled", newValue);
|
||||||
this._watchers.notifyUpdate(settingName, null, SettingLevel.DEVICE, newValue);
|
this.watchers.notifyUpdate(settingName, null, SettingLevel.DEVICE, newValue);
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,35 +102,35 @@ export default class DeviceSettingsHandler extends SettingsHandler {
|
||||||
"lastRightPanelPhaseForGroup",
|
"lastRightPanelPhaseForGroup",
|
||||||
].includes(settingName)) {
|
].includes(settingName)) {
|
||||||
localStorage.setItem(`mx_${settingName}`, JSON.stringify({value: newValue}));
|
localStorage.setItem(`mx_${settingName}`, JSON.stringify({value: newValue}));
|
||||||
this._watchers.notifyUpdate(settingName, null, SettingLevel.DEVICE, newValue);
|
this.watchers.notifyUpdate(settingName, null, SettingLevel.DEVICE, newValue);
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
const settings = this._getSettings() || {};
|
const settings = this.getSettings() || {};
|
||||||
settings[settingName] = newValue;
|
settings[settingName] = newValue;
|
||||||
localStorage.setItem("mx_local_settings", JSON.stringify(settings));
|
localStorage.setItem("mx_local_settings", JSON.stringify(settings));
|
||||||
this._watchers.notifyUpdate(settingName, null, SettingLevel.DEVICE, newValue);
|
this.watchers.notifyUpdate(settingName, null, SettingLevel.DEVICE, newValue);
|
||||||
|
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
canSetValue(settingName, roomId) {
|
public canSetValue(settingName: string, roomId: string): boolean {
|
||||||
return true; // It's their device, so they should be able to
|
return true; // It's their device, so they should be able to
|
||||||
}
|
}
|
||||||
|
|
||||||
isSupported() {
|
public isSupported(): boolean {
|
||||||
return localStorage !== undefined && localStorage !== null;
|
return localStorage !== undefined && localStorage !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
watchSetting(settingName, roomId, cb) {
|
public watchSetting(settingName: string, roomId: string, cb: CallbackFn) {
|
||||||
this._watchers.watchSetting(settingName, roomId, cb);
|
this.watchers.watchSetting(settingName, roomId, cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
unwatchSetting(cb) {
|
public unwatchSetting(cb: CallbackFn) {
|
||||||
this._watchers.unwatchSetting(cb);
|
this.watchers.unwatchSetting(cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
_getSettings() {
|
private getSettings(): any { // TODO: [TS] Type return
|
||||||
const value = localStorage.getItem("mx_local_settings");
|
const value = localStorage.getItem("mx_local_settings");
|
||||||
if (!value) return null;
|
if (!value) return null;
|
||||||
return JSON.parse(value);
|
return JSON.parse(value);
|
||||||
|
@ -140,7 +139,7 @@ export default class DeviceSettingsHandler extends SettingsHandler {
|
||||||
// Note: features intentionally don't use the same key as settings to avoid conflicts
|
// Note: features intentionally don't use the same key as settings to avoid conflicts
|
||||||
// and to be backwards compatible.
|
// and to be backwards compatible.
|
||||||
|
|
||||||
_readFeature(featureName) {
|
private readFeature(featureName: string): boolean | null {
|
||||||
if (MatrixClientPeg.get() && MatrixClientPeg.get().isGuest()) {
|
if (MatrixClientPeg.get() && MatrixClientPeg.get().isGuest()) {
|
||||||
// Guests should not have any labs features enabled.
|
// Guests should not have any labs features enabled.
|
||||||
return false;
|
return false;
|
||||||
|
@ -153,8 +152,8 @@ export default class DeviceSettingsHandler extends SettingsHandler {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
_writeFeature(featureName, enabled) {
|
private writeFeature(featureName: string, enabled: boolean | null) {
|
||||||
localStorage.setItem("mx_labs_feature_" + featureName, enabled);
|
localStorage.setItem("mx_labs_feature_" + featureName, `${enabled}`);
|
||||||
this._watchers.notifyUpdate(featureName, null, SettingLevel.DEVICE, enabled);
|
this.watchers.notifyUpdate(featureName, null, SettingLevel.DEVICE, enabled);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue