Convert DeviceSettingsHandler to TS

This commit is contained in:
Travis Ralston 2020-07-28 21:53:07 -06:00
parent 8097810784
commit 4f6480c037

View file

@ -19,6 +19,7 @@ limitations under the License.
import SettingsHandler from "./SettingsHandler";
import {MatrixClientPeg} from "../../MatrixClientPeg";
import {SettingLevel} from "../SettingLevel";
import { CallbackFn, WatchManager } from "../WatchManager";
/**
* 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
* @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();
this._featureNames = featureNames;
this._watchers = watchManager;
}
getValue(settingName, roomId) {
if (this._featureNames.includes(settingName)) {
return this._readFeature(settingName);
public getValue(settingName: string, roomId: string): any {
if (this.featureNames.includes(settingName)) {
return this.readFeature(settingName);
}
// Special case notifications
@ -68,28 +67,28 @@ export default class DeviceSettingsHandler extends SettingsHandler {
return val['value'];
}
const settings = this._getSettings() || {};
const settings = this.getSettings() || {};
return settings[settingName];
}
setValue(settingName, roomId, newValue) {
if (this._featureNames.includes(settingName)) {
this._writeFeature(settingName, newValue);
public setValue(settingName: string, roomId: string, newValue: any): Promise<void> {
if (this.featureNames.includes(settingName)) {
this.writeFeature(settingName, newValue);
return Promise.resolve();
}
// Special case notifications
if (settingName === "notificationsEnabled") {
localStorage.setItem("notifications_enabled", newValue);
this._watchers.notifyUpdate(settingName, null, SettingLevel.DEVICE, newValue);
this.watchers.notifyUpdate(settingName, null, SettingLevel.DEVICE, newValue);
return Promise.resolve();
} else if (settingName === "notificationBodyEnabled") {
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();
} else if (settingName === "audioNotificationsEnabled") {
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();
}
@ -103,35 +102,35 @@ export default class DeviceSettingsHandler extends SettingsHandler {
"lastRightPanelPhaseForGroup",
].includes(settingName)) {
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();
}
const settings = this._getSettings() || {};
const settings = this.getSettings() || {};
settings[settingName] = newValue;
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();
}
canSetValue(settingName, roomId) {
public canSetValue(settingName: string, roomId: string): boolean {
return true; // It's their device, so they should be able to
}
isSupported() {
public isSupported(): boolean {
return localStorage !== undefined && localStorage !== null;
}
watchSetting(settingName, roomId, cb) {
this._watchers.watchSetting(settingName, roomId, cb);
public watchSetting(settingName: string, roomId: string, cb: CallbackFn) {
this.watchers.watchSetting(settingName, roomId, cb);
}
unwatchSetting(cb) {
this._watchers.unwatchSetting(cb);
public unwatchSetting(cb: CallbackFn) {
this.watchers.unwatchSetting(cb);
}
_getSettings() {
private getSettings(): any { // TODO: [TS] Type return
const value = localStorage.getItem("mx_local_settings");
if (!value) return null;
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
// and to be backwards compatible.
_readFeature(featureName) {
private readFeature(featureName: string): boolean | null {
if (MatrixClientPeg.get() && MatrixClientPeg.get().isGuest()) {
// Guests should not have any labs features enabled.
return false;
@ -153,8 +152,8 @@ export default class DeviceSettingsHandler extends SettingsHandler {
return null;
}
_writeFeature(featureName, enabled) {
localStorage.setItem("mx_labs_feature_" + featureName, enabled);
this._watchers.notifyUpdate(featureName, null, SettingLevel.DEVICE, enabled);
private writeFeature(featureName: string, enabled: boolean | null) {
localStorage.setItem("mx_labs_feature_" + featureName, `${enabled}`);
this.watchers.notifyUpdate(featureName, null, SettingLevel.DEVICE, enabled);
}
}